Skip to content

Commit 408b3d7

Browse files
martischandybons
authored andcommitted
[release-branch.go1.9] strings: fix encoding of \u0080 in map
Fix encoding of PAD (U+0080) which has the same value as utf8.RuneSelf being incorrectly encoded as \x80 in strings.Map due to using <= instead of a < comparison operator to check one byte encodings for utf8. Fixes #25573 Change-Id: Ib6c7d1f425a7ba81e431b6d64009e713d94ea3bc Reviewed-on: https://go-review.googlesource.com/111286 Run-TryBot: Martin Möhrmann <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> (cherry picked from commit 8c62fc0) Reviewed-on: https://go-review.googlesource.com/114636 Run-TryBot: Andrew Bonventre <[email protected]> Reviewed-by: Andrew Bonventre <[email protected]>
1 parent f31a132 commit 408b3d7

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/strings/strings.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ func Map(mapping func(rune) rune, s string) string {
523523
b = make([]byte, len(s)+utf8.UTFMax)
524524
nbytes = copy(b, s[:i])
525525
if r >= 0 {
526-
if r <= utf8.RuneSelf {
526+
if r < utf8.RuneSelf {
527527
b[nbytes] = byte(r)
528528
nbytes++
529529
} else {
@@ -553,7 +553,7 @@ func Map(mapping func(rune) rune, s string) string {
553553
r := mapping(c)
554554

555555
// common case
556-
if (0 <= r && r <= utf8.RuneSelf) && nbytes < len(b) {
556+
if (0 <= r && r < utf8.RuneSelf) && nbytes < len(b) {
557557
b[nbytes] = byte(r)
558558
nbytes++
559559
continue

src/strings/strings_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ var upperTests = []StringTest{
522522
{"AbC123", "ABC123"},
523523
{"azAZ09_", "AZAZ09_"},
524524
{"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
525+
{"a\u0080\U0010FFFF", "A\u0080\U0010FFFF"}, // test utf8.RuneSelf and utf8.MaxRune
525526
}
526527

527528
var lowerTests = []StringTest{
@@ -530,6 +531,7 @@ var lowerTests = []StringTest{
530531
{"AbC123", "abc123"},
531532
{"azAZ09_", "azaz09_"},
532533
{"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
534+
{"A\u0080\U0010FFFF", "a\u0080\U0010FFFF"}, // test utf8.RuneSelf and utf8.MaxRune
533535
}
534536

535537
const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
@@ -642,6 +644,27 @@ func TestMap(t *testing.T) {
642644
if m != expect {
643645
t.Errorf("replace invalid sequence: expected %q got %q", expect, m)
644646
}
647+
648+
// 8. Check utf8.RuneSelf and utf8.MaxRune encoding
649+
encode := func(r rune) rune {
650+
switch r {
651+
case utf8.RuneSelf:
652+
return unicode.MaxRune
653+
case unicode.MaxRune:
654+
return utf8.RuneSelf
655+
}
656+
return r
657+
}
658+
s := string(utf8.RuneSelf) + string(utf8.MaxRune)
659+
r := string(utf8.MaxRune) + string(utf8.RuneSelf) // reverse of s
660+
m = Map(encode, s)
661+
if m != r {
662+
t.Errorf("encoding not handled correctly: expected %q got %q", r, m)
663+
}
664+
m = Map(encode, r)
665+
if m != s {
666+
t.Errorf("encoding not handled correctly: expected %q got %q", s, m)
667+
}
645668
}
646669

647670
func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }

0 commit comments

Comments
 (0)