mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-03-25 16:43:37 +00:00
internal/encoding/text: escape Unicode control characters in strings
Escape not only ASCII control characters, but Unicode as well. Change-Id: I5f5791ae51fc5624599f66ce012ecef364e7ea97 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/262682 Trust: Damien Neil <dneil@google.com> Run-TryBot: Damien Neil <dneil@google.com> Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com> Reviewed-by: Joe Tsai <joetsai@google.com>
This commit is contained in:
parent
b1cd8e38bc
commit
711224230b
@ -141,7 +141,7 @@ func appendString(out []byte, in string, outputASCII bool) []byte {
|
|||||||
out = strconv.AppendUint(out, uint64(r), 16)
|
out = strconv.AppendUint(out, uint64(r), 16)
|
||||||
}
|
}
|
||||||
in = in[n:]
|
in = in[n:]
|
||||||
case outputASCII && r >= utf8.RuneSelf:
|
case r >= utf8.RuneSelf && (outputASCII || r <= 0x009f):
|
||||||
out = append(out, '\\')
|
out = append(out, '\\')
|
||||||
if r <= math.MaxUint16 {
|
if r <= math.MaxUint16 {
|
||||||
out = append(out, 'u')
|
out = append(out, 'u')
|
||||||
|
@ -394,17 +394,19 @@ func TestEncodeStrings(t *testing.T) {
|
|||||||
// String that has as few escaped characters as possible.
|
// String that has as few escaped characters as possible.
|
||||||
in: func() string {
|
in: func() string {
|
||||||
var b []byte
|
var b []byte
|
||||||
for i := 0; i < utf8.RuneSelf; i++ {
|
for i := rune(0); i <= 0x00a0; i++ {
|
||||||
switch i {
|
switch i {
|
||||||
case 0, '\\', '\n', '\'': // these must be escaped, so ignore them
|
case 0, '\\', '\n', '\'': // these must be escaped, so ignore them
|
||||||
default:
|
default:
|
||||||
b = append(b, byte(i))
|
var r [utf8.UTFMax]byte
|
||||||
|
n := utf8.EncodeRune(r[:], i)
|
||||||
|
b = append(b, r[:n]...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return string(b)
|
return string(b)
|
||||||
}(),
|
}(),
|
||||||
wantOut: `"\x01\x02\x03\x04\x05\x06\x07\x08\t\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_` + "`abcdefghijklmnopqrstuvwxyz{|}~\\x7f\"",
|
wantOut: `"\x01\x02\x03\x04\x05\x06\x07\x08\t\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_` + "`" + `abcdefghijklmnopqrstuvwxyz{|}~\x7f\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089\u008a\u008b\u008c\u008d\u008e\u008f\u0090\u0091\u0092\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009a\u009b\u009c\u009d\u009e\u009f` + "\u00a0" + `"`,
|
||||||
wantOutASCII: `"\x01\x02\x03\x04\x05\x06\x07\x08\t\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_` + "`abcdefghijklmnopqrstuvwxyz{|}~\\x7f\"",
|
wantOutASCII: `"\x01\x02\x03\x04\x05\x06\x07\x08\t\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_` + "`" + `abcdefghijklmnopqrstuvwxyz{|}~\x7f\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089\u008a\u008b\u008c\u008d\u008e\u008f\u0090\u0091\u0092\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009a\u009b\u009c\u009d\u009e\u009f\u00a0"`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// Valid UTF-8 wire encoding of the RuneError rune.
|
// Valid UTF-8 wire encoding of the RuneError rune.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user