Skip to content

Commit

Permalink
Fix binary string regex (qax-os#1415)
Browse files Browse the repository at this point in the history
  • Loading branch information
nesstord authored Dec 6, 2022
1 parent 5e0953d commit 61fda0b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
19 changes: 4 additions & 15 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,8 @@ func isNumeric(s string) (bool, int, float64) {
}

var (
bstrExp = regexp.MustCompile(`_x[a-zA-Z\d]{4}_`)
bstrEscapeExp = regexp.MustCompile(`x[a-zA-Z\d]{4}_`)
bstrExp = regexp.MustCompile(`_x[a-fA-F\d]{4}_`)
bstrEscapeExp = regexp.MustCompile(`x[a-fA-F\d]{4}_`)
)

// bstrUnmarshal parses the binary basic string, this will trim escaped string
Expand All @@ -729,16 +729,7 @@ func bstrUnmarshal(s string) (result string) {
}
if bstrExp.MatchString(subStr) {
cursor = match[1]
v, err := strconv.Unquote(`"\u` + s[match[0]+2:match[1]-1] + `"`)
if err != nil {
if l > match[1]+6 && bstrEscapeExp.MatchString(s[match[1]:match[1]+6]) {
result += subStr[:6]
cursor = match[1] + 6
continue
}
result += subStr
continue
}
v, _ := strconv.Unquote(`"\u` + s[match[0]+2:match[1]-1] + `"`)
result += v
}
}
Expand Down Expand Up @@ -769,12 +760,10 @@ func bstrMarshal(s string) (result string) {
}
if bstrExp.MatchString(subStr) {
cursor = match[1]
_, err := strconv.Unquote(`"\u` + s[match[0]+2:match[1]-1] + `"`)
if err == nil {
if _, err := strconv.Unquote(`"\u` + s[match[0]+2:match[1]-1] + `"`); err == nil {
result += "_x005F" + subStr
continue
}
result += subStr
}
}
if cursor < l {
Expand Down
7 changes: 4 additions & 3 deletions lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,18 +305,19 @@ func TestBstrUnmarshal(t *testing.T) {
"*_x0008_*": "*\b*",
"*_x4F60__x597D_": "*你好",
"*_xG000_": "*_xG000_",
"*_xG05F_x0001_*": "*_xG05F*",
"*_xG05F_x0001_*": "*_xG05F\x01*",
"*_x005F__x0008_*": "*_\b*",
"*_x005F_x0001_*": "*_x0001_*",
"*_x005f_x005F__x0008_*": "*_x005F_\b*",
"*_x005F_x005F_xG05F_x0006_*": "*_x005F_xG05F*",
"*_x005F_x005F_xG05F_x0006_*": "*_x005F_xG05F\x06*",
"*_x005F_x005F_x005F_x0006_*": "*_x005F_x0006_*",
"_x005F__x0008_******": "_\b******",
"******_x005F__x0008_": "******_\b",
"******_x005F__x0008_******": "******_\b******",
"_x000x_x005F_x000x_": "_x000x_x000x_",
}
for bstr, expected := range bstrs {
assert.Equal(t, expected, bstrUnmarshal(bstr))
assert.Equal(t, expected, bstrUnmarshal(bstr), bstr)
}
}

Expand Down

0 comments on commit 61fda0b

Please sign in to comment.