Skip to content

Commit e354db6

Browse files
committed
string pattern match context check instead of regex lookahead assertion
1 parent 5faa364 commit e354db6

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

lib.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -475,34 +475,35 @@ func bstrUnmarshal(s string) (result string) {
475475
result += s[cursor:match[0]]
476476
subStr := s[match[0]:match[1]]
477477
if subStr == "_x005F_" {
478+
cursor = match[1]
478479
if l > match[1]+6 && !escapeExp.MatchString(s[match[1]:match[1]+6]) {
479480
result += subStr
480-
cursor = match[1]
481-
continue
482-
}
483-
if l > match[1]+5 && s[match[1]:match[1]+5] == "x005F" {
484-
result += "_"
485-
cursor = match[1]
486-
continue
487-
}
488-
if escapeExp.MatchString(subStr) {
489-
result += "_"
490-
cursor = match[1]
491481
continue
492482
}
483+
result += "_"
484+
continue
493485
}
494486
if bstrExp.MatchString(subStr) {
495-
x, _ := strconv.Unquote(`"\u` + s[match[0]+2:match[1]-1] + `"`)
487+
cursor = match[1]
488+
v, err := strconv.Unquote(`"\u` + s[match[0]+2:match[1]-1] + `"`)
489+
if err != nil {
490+
if l > match[1]+6 && escapeExp.MatchString(s[match[1]:match[1]+6]) {
491+
result += subStr[:6]
492+
cursor = match[1] + 6
493+
continue
494+
}
495+
result += subStr
496+
continue
497+
}
496498
hasRune := false
497-
for _, c := range string(x) {
499+
for _, c := range v {
498500
if unicode.IsControl(c) {
499501
hasRune = true
500502
}
501503
}
502504
if !hasRune {
503-
result += string(x)
505+
result += v
504506
}
505-
cursor = match[1]
506507
}
507508
}
508509
if cursor < l {

lib_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -237,19 +237,22 @@ func TestGenXMLNamespace(t *testing.T) {
237237

238238
func TestBstrUnmarshal(t *testing.T) {
239239
bstrs := map[string]string{
240-
"*_x0000_": "*",
241240
"*": "*",
241+
"*_x0000_": "*",
242242
"*_x0008_": "*",
243243
"_x0008_*": "*",
244244
"*_x0008_*": "**",
245245
"*_x4F60__x597D_": "*你好",
246+
"*_xG000_": "*_xG000_",
247+
"*_xG05F_x0001_*": "*_xG05F*",
246248
"*_x005F__x0008_*": "*_x005F_*",
247249
"*_x005F_x0001_*": "*_x0001_*",
250+
"*_x005f_x005F__x0008_*": "*_x005F_*",
251+
"*_x005F_x005F_xG05F_x0006_*": "*_x005F_xG05F*",
248252
"*_x005F_x005F_x005F_x0006_*": "*_x005F_x0006_*",
249253
"_x005F__x0008_******": "_x005F_******",
250254
"******_x005F__x0008_": "******_x005F_",
251255
"******_x005F__x0008_******": "******_x005F_******",
252-
"*_x005F_x005F__x0008_*": "*_x005F_*",
253256
}
254257
for bstr, expected := range bstrs {
255258
assert.Equal(t, expected, bstrUnmarshal(bstr))

0 commit comments

Comments
 (0)