mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
bytes, strings: use rune
Various rune-based APIs change. R=golang-dev, r CC=golang-dev https://golang.org/cl/5306044
This commit is contained in:
parent
0e513317b1
commit
8f5718176f
8 changed files with 189 additions and 187 deletions
|
|
@ -122,7 +122,7 @@ func TestLastIndexAny(t *testing.T) { runIndexTests(t, LastIndexAny, "LastIndexA
|
|||
|
||||
var indexRuneTests = []struct {
|
||||
s string
|
||||
rune int
|
||||
rune rune
|
||||
out int
|
||||
}{
|
||||
{"a A x", 'A', 2},
|
||||
|
|
@ -312,7 +312,7 @@ var FieldsFuncTests = []FieldsTest{
|
|||
}
|
||||
|
||||
func TestFieldsFunc(t *testing.T) {
|
||||
pred := func(c int) bool { return c == 'X' }
|
||||
pred := func(c rune) bool { return c == 'X' }
|
||||
for _, tt := range FieldsFuncTests {
|
||||
a := FieldsFunc(tt.s, pred)
|
||||
if !eq(a, tt.a) {
|
||||
|
|
@ -374,31 +374,31 @@ var trimSpaceTests = []StringTest{
|
|||
{"x ☺ ", "x ☺"},
|
||||
}
|
||||
|
||||
func tenRunes(rune int) string {
|
||||
r := make([]int, 10)
|
||||
func tenRunes(ch rune) string {
|
||||
r := make([]rune, 10)
|
||||
for i := range r {
|
||||
r[i] = rune
|
||||
r[i] = ch
|
||||
}
|
||||
return string(r)
|
||||
}
|
||||
|
||||
// User-defined self-inverse mapping function
|
||||
func rot13(rune int) int {
|
||||
step := 13
|
||||
if rune >= 'a' && rune <= 'z' {
|
||||
return ((rune - 'a' + step) % 26) + 'a'
|
||||
func rot13(r rune) rune {
|
||||
step := rune(13)
|
||||
if r >= 'a' && r <= 'z' {
|
||||
return ((r - 'a' + step) % 26) + 'a'
|
||||
}
|
||||
if rune >= 'A' && rune <= 'Z' {
|
||||
return ((rune - 'A' + step) % 26) + 'A'
|
||||
if r >= 'A' && r <= 'Z' {
|
||||
return ((r - 'A' + step) % 26) + 'A'
|
||||
}
|
||||
return rune
|
||||
return r
|
||||
}
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
// Run a couple of awful growth/shrinkage tests
|
||||
a := tenRunes('a')
|
||||
// 1. Grow. This triggers two reallocations in Map.
|
||||
maxRune := func(rune int) int { return unicode.MaxRune }
|
||||
maxRune := func(rune) rune { return unicode.MaxRune }
|
||||
m := Map(maxRune, a)
|
||||
expect := tenRunes(unicode.MaxRune)
|
||||
if m != expect {
|
||||
|
|
@ -406,7 +406,7 @@ func TestMap(t *testing.T) {
|
|||
}
|
||||
|
||||
// 2. Shrink
|
||||
minRune := func(rune int) int { return 'a' }
|
||||
minRune := func(rune) rune { return 'a' }
|
||||
m = Map(minRune, tenRunes(unicode.MaxRune))
|
||||
expect = a
|
||||
if m != expect {
|
||||
|
|
@ -428,9 +428,9 @@ func TestMap(t *testing.T) {
|
|||
}
|
||||
|
||||
// 5. Drop
|
||||
dropNotLatin := func(rune int) int {
|
||||
if unicode.Is(unicode.Latin, rune) {
|
||||
return rune
|
||||
dropNotLatin := func(r rune) rune {
|
||||
if unicode.Is(unicode.Latin, r) {
|
||||
return r
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
|
@ -441,8 +441,8 @@ func TestMap(t *testing.T) {
|
|||
}
|
||||
|
||||
// 6. Identity
|
||||
identity := func(rune int) int {
|
||||
return rune
|
||||
identity := func(r rune) rune {
|
||||
return r
|
||||
}
|
||||
orig := "Input string that we expect not to be copied."
|
||||
m = Map(identity, orig)
|
||||
|
|
@ -457,8 +457,8 @@ func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTest
|
|||
func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTests) }
|
||||
|
||||
func BenchmarkMapNoChanges(b *testing.B) {
|
||||
identity := func(rune int) int {
|
||||
return rune
|
||||
identity := func(r rune) rune {
|
||||
return r
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
Map(identity, "Some string that won't be modified.")
|
||||
|
|
@ -536,7 +536,7 @@ func TestTrim(t *testing.T) {
|
|||
}
|
||||
|
||||
type predicate struct {
|
||||
f func(r int) bool
|
||||
f func(rune) bool
|
||||
name string
|
||||
}
|
||||
|
||||
|
|
@ -544,7 +544,7 @@ var isSpace = predicate{unicode.IsSpace, "IsSpace"}
|
|||
var isDigit = predicate{unicode.IsDigit, "IsDigit"}
|
||||
var isUpper = predicate{unicode.IsUpper, "IsUpper"}
|
||||
var isValidRune = predicate{
|
||||
func(r int) bool {
|
||||
func(r rune) bool {
|
||||
return r != utf8.RuneError
|
||||
},
|
||||
"IsValidRune",
|
||||
|
|
@ -552,7 +552,7 @@ var isValidRune = predicate{
|
|||
|
||||
func not(p predicate) predicate {
|
||||
return predicate{
|
||||
func(r int) bool {
|
||||
func(r rune) bool {
|
||||
return !p.f(r)
|
||||
},
|
||||
"not " + p.name,
|
||||
|
|
@ -645,9 +645,9 @@ func TestCaseConsistency(t *testing.T) {
|
|||
if testing.Short() {
|
||||
numRunes = 1000
|
||||
}
|
||||
a := make([]int, numRunes)
|
||||
a := make([]rune, numRunes)
|
||||
for i := range a {
|
||||
a[i] = i
|
||||
a[i] = rune(i)
|
||||
}
|
||||
s := string(a)
|
||||
// convert the cases.
|
||||
|
|
@ -706,7 +706,7 @@ func TestRepeat(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func runesEqual(a, b []int) bool {
|
||||
func runesEqual(a, b []rune) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
|
|
@ -720,30 +720,30 @@ func runesEqual(a, b []int) bool {
|
|||
|
||||
var RunesTests = []struct {
|
||||
in string
|
||||
out []int
|
||||
out []rune
|
||||
lossy bool
|
||||
}{
|
||||
{"", []int{}, false},
|
||||
{" ", []int{32}, false},
|
||||
{"ABC", []int{65, 66, 67}, false},
|
||||
{"abc", []int{97, 98, 99}, false},
|
||||
{"\u65e5\u672c\u8a9e", []int{26085, 26412, 35486}, false},
|
||||
{"ab\x80c", []int{97, 98, 0xFFFD, 99}, true},
|
||||
{"ab\xc0c", []int{97, 98, 0xFFFD, 99}, true},
|
||||
{"", []rune{}, false},
|
||||
{" ", []rune{32}, false},
|
||||
{"ABC", []rune{65, 66, 67}, false},
|
||||
{"abc", []rune{97, 98, 99}, false},
|
||||
{"\u65e5\u672c\u8a9e", []rune{26085, 26412, 35486}, false},
|
||||
{"ab\x80c", []rune{97, 98, 0xFFFD, 99}, true},
|
||||
{"ab\xc0c", []rune{97, 98, 0xFFFD, 99}, true},
|
||||
}
|
||||
|
||||
func TestRunes(t *testing.T) {
|
||||
for _, tt := range RunesTests {
|
||||
a := []int(tt.in)
|
||||
a := []rune(tt.in)
|
||||
if !runesEqual(a, tt.out) {
|
||||
t.Errorf("[]int(%q) = %v; want %v", tt.in, a, tt.out)
|
||||
t.Errorf("[]rune(%q) = %v; want %v", tt.in, a, tt.out)
|
||||
continue
|
||||
}
|
||||
if !tt.lossy {
|
||||
// can only test reassembly if we didn't lose information
|
||||
s := string(a)
|
||||
if s != tt.in {
|
||||
t.Errorf("string([]int(%q)) = %x; want %x", tt.in, s, tt.in)
|
||||
t.Errorf("string([]rune(%q)) = %x; want %x", tt.in, s, tt.in)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue