From 0ae2316523639787a8119b6ea6232fad707376da Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 5 May 2025 23:37:43 -0300 Subject: [PATCH] use IndexByte instead of IndexRune everywhere because it's faster. --- envelopes.go | 4 ++-- envelopes_default.go | 4 ++-- helpers.go | 8 ++++---- nip27/blocks.go | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/envelopes.go b/envelopes.go index 5c32def..28dab48 100644 --- a/envelopes.go +++ b/envelopes.go @@ -22,11 +22,11 @@ type MessageParser interface { // Deprecated: use NewMessageParser instead func ParseMessage(message string) Envelope { - firstQuote := strings.IndexRune(message, '"') + firstQuote := strings.IndexByte(message, '"') if firstQuote == -1 { return nil } - secondQuote := strings.IndexRune(message[firstQuote+1:], '"') + secondQuote := strings.IndexByte(message[firstQuote+1:], '"') if secondQuote == -1 { return nil } diff --git a/envelopes_default.go b/envelopes_default.go index 089c038..2119427 100644 --- a/envelopes_default.go +++ b/envelopes_default.go @@ -14,11 +14,11 @@ func NewMessageParser() MessageParser { type messageParser struct{} func (messageParser) ParseMessage(message string) (Envelope, error) { - firstQuote := strings.IndexRune(message, '"') + firstQuote := strings.IndexByte(message, '"') if firstQuote == -1 { return nil, errors.New("malformed json") } - secondQuote := strings.IndexRune(message[firstQuote+1:], '"') + secondQuote := strings.IndexByte(message[firstQuote+1:], '"') if secondQuote == -1 { return nil, errors.New("malformed json") } diff --git a/helpers.go b/helpers.go index 1538cbe..c0f8667 100644 --- a/helpers.go +++ b/helpers.go @@ -191,7 +191,7 @@ func extractEventID(jsonStr string) ID { } // move to the next quote - offset := strings.IndexRune(jsonStr[start+4:], '"') + offset := strings.IndexByte(jsonStr[start+4:], '"') start += 4 + offset + 1 // get 64 characters of the id @@ -208,7 +208,7 @@ func extractEventPubKey(jsonStr string) PubKey { } // move to the next quote - offset := strings.IndexRune(jsonStr[start+8:], '"') + offset := strings.IndexByte(jsonStr[start+8:], '"') start += 8 + offset + 1 // get 64 characters of the pubkey @@ -225,11 +225,11 @@ func extractDTag(jsonStr string) string { } // move to the next quote - offset := strings.IndexRune(jsonStr[start+4:], '"') + offset := strings.IndexByte(jsonStr[start+4:], '"') start += 4 + offset + 1 // find the ending quote - end := strings.IndexRune(jsonStr[start:], '"') + end := strings.IndexByte(jsonStr[start:], '"') if end == -1 { return "" } diff --git a/nip27/blocks.go b/nip27/blocks.go index 37b7eee..eb718fa 100644 --- a/nip27/blocks.go +++ b/nip27/blocks.go @@ -29,7 +29,7 @@ func Parse(content string) iter.Seq[Block] { prevIndex := 0 for index < max { - pu := strings.IndexRune(content[index:], ':') + pu := strings.IndexByte(content[index:], ':') if pu == -1 { // reached end break