WithCheckDuplicateReplaceable() and helper functions for efficient replaceable event matching.

This commit is contained in:
fiatjaf
2025-03-12 00:19:41 -03:00
parent d2ceac48f6
commit 441f94563f
4 changed files with 88 additions and 4 deletions

View File

@@ -154,3 +154,49 @@ func extractEventID(jsonStr string) string {
// get 64 characters of the id
return jsonStr[start : start+64]
}
func extractDTag(jsonStr string) string {
// look for ["d", pattern
start := strings.Index(jsonStr, `["d"`)
if start == -1 {
return ""
}
// move to the next quote
offset := strings.IndexRune(jsonStr[start+4:], '"')
start += 4 + offset + 1
// find the ending quote
end := strings.IndexRune(jsonStr[start:], '"')
if end == -1 {
return ""
}
// get the contents
return jsonStr[start : start+end]
}
func extractTimestamp(jsonStr string) Timestamp {
// look for "created_at": pattern
start := strings.Index(jsonStr, `"created_at"`)
if start == -1 {
return 0
}
// move to the next number
offset := strings.IndexAny(jsonStr[start+12:], "9876543210")
if offset == -1 {
return 0
}
start += 12 + offset
// find the end
end := strings.IndexAny(jsonStr[start:], ",} ")
if end == -1 {
return 0
}
// get the contents
ts, _ := strconv.ParseInt(jsonStr[start:start+end], 10, 64)
return Timestamp(ts)
}