string pattern match context check instead of regex lookahead assertion

v2
xuri 4 years ago
parent 5faa36430c
commit e354db69b0
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7

@ -475,34 +475,35 @@ func bstrUnmarshal(s string) (result string) {
result += s[cursor:match[0]] result += s[cursor:match[0]]
subStr := s[match[0]:match[1]] subStr := s[match[0]:match[1]]
if subStr == "_x005F_" { if subStr == "_x005F_" {
cursor = match[1]
if l > match[1]+6 && !escapeExp.MatchString(s[match[1]:match[1]+6]) { if l > match[1]+6 && !escapeExp.MatchString(s[match[1]:match[1]+6]) {
result += subStr result += subStr
cursor = match[1]
continue
}
if l > match[1]+5 && s[match[1]:match[1]+5] == "x005F" {
result += "_"
cursor = match[1]
continue
}
if escapeExp.MatchString(subStr) {
result += "_"
cursor = match[1]
continue continue
} }
result += "_"
continue
} }
if bstrExp.MatchString(subStr) { if bstrExp.MatchString(subStr) {
x, _ := strconv.Unquote(`"\u` + s[match[0]+2:match[1]-1] + `"`) cursor = match[1]
v, err := strconv.Unquote(`"\u` + s[match[0]+2:match[1]-1] + `"`)
if err != nil {
if l > match[1]+6 && escapeExp.MatchString(s[match[1]:match[1]+6]) {
result += subStr[:6]
cursor = match[1] + 6
continue
}
result += subStr
continue
}
hasRune := false hasRune := false
for _, c := range string(x) { for _, c := range v {
if unicode.IsControl(c) { if unicode.IsControl(c) {
hasRune = true hasRune = true
} }
} }
if !hasRune { if !hasRune {
result += string(x) result += v
} }
cursor = match[1]
} }
} }
if cursor < l { if cursor < l {

@ -237,19 +237,22 @@ func TestGenXMLNamespace(t *testing.T) {
func TestBstrUnmarshal(t *testing.T) { func TestBstrUnmarshal(t *testing.T) {
bstrs := map[string]string{ bstrs := map[string]string{
"*_x0000_": "*",
"*": "*", "*": "*",
"*_x0000_": "*",
"*_x0008_": "*", "*_x0008_": "*",
"_x0008_*": "*", "_x0008_*": "*",
"*_x0008_*": "**", "*_x0008_*": "**",
"*_x4F60__x597D_": "*你好", "*_x4F60__x597D_": "*你好",
"*_xG000_": "*_xG000_",
"*_xG05F_x0001_*": "*_xG05F*",
"*_x005F__x0008_*": "*_x005F_*", "*_x005F__x0008_*": "*_x005F_*",
"*_x005F_x0001_*": "*_x0001_*", "*_x005F_x0001_*": "*_x0001_*",
"*_x005f_x005F__x0008_*": "*_x005F_*",
"*_x005F_x005F_xG05F_x0006_*": "*_x005F_xG05F*",
"*_x005F_x005F_x005F_x0006_*": "*_x005F_x0006_*", "*_x005F_x005F_x005F_x0006_*": "*_x005F_x0006_*",
"_x005F__x0008_******": "_x005F_******", "_x005F__x0008_******": "_x005F_******",
"******_x005F__x0008_": "******_x005F_", "******_x005F__x0008_": "******_x005F_",
"******_x005F__x0008_******": "******_x005F_******", "******_x005F__x0008_******": "******_x005F_******",
"*_x005F_x005F__x0008_*": "*_x005F_*",
} }
for bstr, expected := range bstrs { for bstr, expected := range bstrs {
assert.Equal(t, expected, bstrUnmarshal(bstr)) assert.Equal(t, expected, bstrUnmarshal(bstr))

Loading…
Cancel
Save