Skip to content

Commit

Permalink
fix(common): properly escape multi-byte Unicode characters
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota authored and leodido committed Sep 18, 2024
1 parent 42a2959 commit 12554a4
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions common/functions.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package common

import "strings"

// UnsafeUTF8DecimalCodePointsToInt converts a slice containing
// a series of UTF-8 decimal code points into their integer rapresentation.
//
Expand Down Expand Up @@ -29,16 +31,15 @@ func RemoveBytes(data []byte, positions []int, offset int) []byte {

// EscapeBytes adds a backslash to \, ], " characters.
func EscapeBytes(value string) string {
res := ""
for i, c := range value {
var sb strings.Builder
for _, c := range value {
// todo(leodido): generalize byte codes (the function should ideally accept a byte slice containing byte codes to escape)
if c == 92 || c == 93 || c == 34 {
res += `\`
if c == '\\' || c == ']' || c == '"' {
sb.WriteByte('\\')
}
res += string(value[i])
sb.WriteRune(c)
}

return res
return sb.String()
}

// InBetween tells whether value is into [min, max] range.
Expand Down

0 comments on commit 12554a4

Please sign in to comment.