Preserve XML control character in bstrUnmarshal result

This commit is contained in:
xuri 2021-08-28 09:23:44 +08:00
parent 7d9b9275bd
commit c3d1d7dddd
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
2 changed files with 10 additions and 23 deletions

15
lib.go
View File

@ -21,7 +21,6 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"unicode"
) )
// ReadZipReader can be used to read the spreadsheet in memory without touching the // ReadZipReader can be used to read the spreadsheet in memory without touching the
@ -588,10 +587,6 @@ func bstrUnmarshal(s string) (result string) {
subStr := s[match[0]:match[1]] subStr := s[match[0]:match[1]]
if subStr == "_x005F_" { if subStr == "_x005F_" {
cursor = match[1] cursor = match[1]
if l > match[1]+6 && !bstrEscapeExp.MatchString(s[match[1]:match[1]+6]) {
result += subStr
continue
}
result += "_" result += "_"
continue continue
} }
@ -607,15 +602,7 @@ func bstrUnmarshal(s string) (result string) {
result += subStr result += subStr
continue continue
} }
hasRune := false result += v
for _, c := range v {
if unicode.IsControl(c) {
hasRune = true
}
}
if !hasRune {
result += v
}
} }
} }
if cursor < l { if cursor < l {

View File

@ -263,21 +263,21 @@ 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_": "*\x00",
"*_x0008_": "*", "*_x0008_": "*\b",
"_x0008_*": "*", "_x0008_*": "\b*",
"*_x0008_*": "**", "*_x0008_*": "*\b*",
"*_x4F60__x597D_": "*你好", "*_x4F60__x597D_": "*你好",
"*_xG000_": "*_xG000_", "*_xG000_": "*_xG000_",
"*_xG05F_x0001_*": "*_xG05F*", "*_xG05F_x0001_*": "*_xG05F*",
"*_x005F__x0008_*": "*_x005F_*", "*_x005F__x0008_*": "*_\b*",
"*_x005F_x0001_*": "*_x0001_*", "*_x005F_x0001_*": "*_x0001_*",
"*_x005f_x005F__x0008_*": "*_x005F_*", "*_x005f_x005F__x0008_*": "*_x005F_\b*",
"*_x005F_x005F_xG05F_x0006_*": "*_x005F_xG05F*", "*_x005F_x005F_xG05F_x0006_*": "*_x005F_xG05F*",
"*_x005F_x005F_x005F_x0006_*": "*_x005F_x0006_*", "*_x005F_x005F_x005F_x0006_*": "*_x005F_x0006_*",
"_x005F__x0008_******": "_x005F_******", "_x005F__x0008_******": "_\b******",
"******_x005F__x0008_": "******_x005F_", "******_x005F__x0008_": "******_\b",
"******_x005F__x0008_******": "******_x005F_******", "******_x005F__x0008_******": "******_\b******",
} }
for bstr, expected := range bstrs { for bstr, expected := range bstrs {
assert.Equal(t, expected, bstrUnmarshal(bstr)) assert.Equal(t, expected, bstrUnmarshal(bstr))