Skip to content

Commit c5d24a1

Browse files
committed
fix(perf): switch to string based operations.
* Remove regexp usage, and swith to `strings.ReplaceAll`. Signed-off-by: Thibault Normand <[email protected]>
1 parent e0cc838 commit c5d24a1

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

cjson/canonicaljson.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@ import (
66
"errors"
77
"fmt"
88
"reflect"
9-
"regexp"
109
"sort"
1110
"strings"
1211
)
1312

14-
var (
15-
stringEscapeRE = regexp.MustCompile(`([\"\\])`)
16-
)
17-
1813
/*
1914
encodeCanonicalString is a helper function to canonicalize the passed string
2015
according to the OLPC canonical JSON specification for strings (see
@@ -23,7 +18,12 @@ escaping backslashes ("\") and double quotes (") and wrapping the resulting
2318
string in double quotes (").
2419
*/
2520
func encodeCanonicalString(s string) string {
26-
return fmt.Sprintf("\"%s\"", stringEscapeRE.ReplaceAllString(s, "\\$1"))
21+
// Escape backslashes
22+
s = strings.ReplaceAll(s, "\\", "\\\\")
23+
// Escape double quotes
24+
s = strings.ReplaceAll(s, "\"", "\\\"")
25+
// Wrap with double quotes
26+
return fmt.Sprintf("\"%s\"", s)
2727
}
2828

2929
/*

0 commit comments

Comments
 (0)