mirror of
https://github.com/golang/go
synced 2024-11-19 23:44:43 -07:00
strconv: fix CanBackquote for invalid UTF-8
Make CanBackquote(invalid UTF-8) return false. Also add two test which show that CanBackquote reports true for strings containing a BOM. Fixes #7572. LGTM=r R=golang-codereviews, bradfitz, r CC=golang-codereviews https://golang.org/cl/111780045
This commit is contained in:
parent
deae10e038
commit
c0a824aad6
@ -143,9 +143,16 @@ func AppendQuoteRuneToASCII(dst []byte, r rune) []byte {
|
|||||||
// unchanged as a single-line backquoted string without control
|
// unchanged as a single-line backquoted string without control
|
||||||
// characters other than space and tab.
|
// characters other than space and tab.
|
||||||
func CanBackquote(s string) bool {
|
func CanBackquote(s string) bool {
|
||||||
for i := 0; i < len(s); i++ {
|
for len(s) > 0 {
|
||||||
c := s[i]
|
r, wid := utf8.DecodeRuneInString(s)
|
||||||
if (c < ' ' && c != '\t') || c == '`' || c == '\u007F' {
|
s = s[wid:]
|
||||||
|
if wid > 1 {
|
||||||
|
continue // All multibyte runes are correctly encoded and assumed printable.
|
||||||
|
}
|
||||||
|
if r == utf8.RuneError {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (r < ' ' && r != '\t') || r == '`' || r == '\u007F' {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -146,6 +146,10 @@ var canbackquotetests = []canBackquoteTest{
|
|||||||
{`ABCDEFGHIJKLMNOPQRSTUVWXYZ`, true},
|
{`ABCDEFGHIJKLMNOPQRSTUVWXYZ`, true},
|
||||||
{`abcdefghijklmnopqrstuvwxyz`, true},
|
{`abcdefghijklmnopqrstuvwxyz`, true},
|
||||||
{`☺`, true},
|
{`☺`, true},
|
||||||
|
{"\x80", false},
|
||||||
|
{"a\xe0\xa0z", false},
|
||||||
|
{"\ufeffabc", true},
|
||||||
|
{"a\ufeffz", true},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCanBackquote(t *testing.T) {
|
func TestCanBackquote(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user