1
0
mirror of https://github.com/golang/go synced 2024-11-23 02:10:03 -07:00

encoding/xml: rewrite func procInst

This CL tries to make function procInst more exact,
also adds test cases, however, including tricky ones.

Change-Id: If421299fc84d136e56a25dba7a4919c4424702c8
GitHub-Last-Rev: b9a3192718
GitHub-Pull-Request: golang/go#64336
Reviewed-on: https://go-review.googlesource.com/c/go/+/544475
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Jes Cok 2024-02-07 04:56:26 +00:00 committed by Gopher Robot
parent a56834922f
commit 414161ebfa
2 changed files with 26 additions and 8 deletions

View File

@ -2045,16 +2045,27 @@ func procInst(param, s string) string {
// TODO: this parsing is somewhat lame and not exact.
// It works for all actual cases, though.
param = param + "="
_, v, _ := strings.Cut(s, param)
if v == "" {
lenp := len(param)
i := 0
var sep byte
for i < len(s) {
sub := s[i:]
k := strings.Index(sub, param)
if k < 0 || lenp+k >= len(sub) {
return ""
}
i += lenp + k + 1
if c := sub[lenp+k]; c == '\'' || c == '"' {
sep = c
break
}
}
if sep == 0 {
return ""
}
if v[0] != '\'' && v[0] != '"' {
j := strings.IndexByte(s[i:], sep)
if j < 0 {
return ""
}
unquote, _, ok := strings.Cut(v[1:], v[:1])
if !ok {
return ""
}
return unquote
return s[i : i+j]
}

View File

@ -830,6 +830,13 @@ var procInstTests = []struct {
{`version="1.0" encoding='utf-8' `, [2]string{"1.0", "utf-8"}},
{`version="1.0" encoding=utf-8`, [2]string{"1.0", ""}},
{`encoding="FOO" `, [2]string{"", "FOO"}},
{`version=2.0 version="1.0" encoding=utf-7 encoding='utf-8'`, [2]string{"1.0", "utf-8"}},
{`version= encoding=`, [2]string{"", ""}},
{`encoding="version=1.0"`, [2]string{"", "version=1.0"}},
{``, [2]string{"", ""}},
// TODO: what's the right approach to handle these nested cases?
{`encoding="version='1.0'"`, [2]string{"1.0", "version='1.0'"}},
{`version="encoding='utf-8'"`, [2]string{"encoding='utf-8'", "utf-8"}},
}
func TestProcInstEncoding(t *testing.T) {