| 1 |
package test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"math/rand" |
| 6 |
"regexp" |
| 7 |
"runtime" |
| 8 |
"strconv" |
| 9 |
"strings" |
| 10 |
"sync" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
"unicode" |
| 14 |
"unicode/utf8" |
| 15 |
|
| 16 |
"github.com/charlievieth/strcase/internal/tables" |
| 17 |
) |
| 18 |
|
| 19 |
type IndexFunc func(s, substr string) int |
| 20 |
|
| 21 |
func ByteIndexFunc(fn func(s, sep []byte) int) IndexFunc { |
| 22 |
return func(s, sep string) int { |
| 23 |
return fn([]byte(s), []byte(sep)) |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func WrapRabinKarp(rabinKarp IndexFunc) IndexFunc { |
| 28 |
return func(s, substr string) int { |
| 29 |
if len(substr) == 0 { |
| 30 |
// Can't use Rabin-Karp for this test case |
| 31 |
return 0 |
| 32 |
} |
| 33 |
return rabinKarp(s, substr) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
type ContainsFunc func(s, substr string) bool |
| 38 |
|
| 39 |
func ByteContainsFunc(fn func(s, sep []byte) bool) ContainsFunc { |
| 40 |
return func(s, sep string) bool { |
| 41 |
return fn([]byte(s), []byte(sep)) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
type IndexRuneFunc func(s string, r rune) int |
| 46 |
|
| 47 |
func ByteIndexRuneFunc(fn func(s []byte, r rune) int) IndexRuneFunc { |
| 48 |
return func(s string, r rune) int { |
| 49 |
return fn([]byte(s), r) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
type IndexByteFunc func(s string, c byte) int |
| 54 |
|
| 55 |
func ByteIndexByte(fn func(s []byte, c byte) int) IndexByteFunc { |
| 56 |
return func(s string, c byte) int { |
| 57 |
return fn([]byte(s), c) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
type PrefixFunc func(s, prefix string) (bool, bool) |
| 62 |
|
| 63 |
func BytePrefixFunc(fn func(s, prefix []byte) (bool, bool)) PrefixFunc { |
| 64 |
return func(s, prefix string) (bool, bool) { |
| 65 |
return fn([]byte(s), []byte(prefix)) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
type TrimFunc func(s1, s2 string) string |
| 70 |
|
| 71 |
func ByteTrimFunc(fn func(s1, s2 []byte) []byte) TrimFunc { |
| 72 |
return func(s1, s2 string) string { |
| 73 |
return string(fn([]byte(s1), []byte(s2))) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func UnicodeVersion(t *testing.T, version string) { |
| 78 |
if version != unicode.Version { |
| 79 |
t.Fatalf("unicode.Version (%s) != UnicodeVersion (%s):\n"+ |
| 80 |
"The version of Unicode included in the version of Go (%s) running this test\n"+ |
| 81 |
"does not match the Unicode version the strcase tables were generated with.\n"+ |
| 82 |
"\n"+ |
| 83 |
"This is likely due to the Unicode version being updated in a newer Go release.\n"+ |
| 84 |
"To regenerate the Unicode tables run: `go generate` and check in the changes to\n"+ |
| 85 |
"\"tables.go\" and \".tables.json\".\n"+ |
| 86 |
"\n"+ |
| 87 |
"NOTE: re-generating the Unicode tables can take a few minutes.", |
| 88 |
unicode.Version, version, runtime.Version()) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
type compareTest struct { |
| 93 |
s, t string |
| 94 |
out int |
| 95 |
} |
| 96 |
|
| 97 |
var compareTests = []compareTest{ |
| 98 |
{"", "", 0}, |
| 99 |
{"a", "a", 0}, |
| 100 |
{"a", "ab", -1}, |
| 101 |
{"ab", "a", 1}, |
| 102 |
{"ABC", "abd", -1}, |
| 103 |
{"abc", "ABD", -1}, |
| 104 |
{"abd", "ABC", 1}, |
| 105 |
{"123abc", "123ABC", 0}, |
| 106 |
{"αβδ", "ΑΒΔ", 0}, |
| 107 |
{"ΑΒΔ", "αβδ", 0}, |
| 108 |
{"αβδa", "ΑΒΔ", 1}, |
| 109 |
{"αβδ", "ΑΒΔa", -1}, |
| 110 |
{"αβa", "ΑΒΔ", -1}, |
| 111 |
{"ΑΒΔ", "αβa", 1}, |
| 112 |
{"αβδ", "ΑΒa", 1}, |
| 113 |
{"αabc", "αABD", -1}, |
| 114 |
{"αabd", "αABC", 1}, |
| 115 |
{strings.Repeat("\u212a", 8), strings.Repeat("k", 8), 0}, |
| 116 |
|
| 117 |
// Invalid UTF-8 should be considered equal (mapped to RuneError) |
| 118 |
{"a" + string(utf8.RuneError), "a" + string(unicode.MaxRune+1), 0}, |
| 119 |
{"a" + string(utf8.RuneError), "a\xFF", 0}, |
| 120 |
{"\xed\xa0\x80", "\xed\xa0\x81", 0}, |
| 121 |
{"\xF4\x7F\xBF\xBF", "\xF2\x7F\xBF\xBF", 0}, |
| 122 |
} |
| 123 |
|
| 124 |
func Compare(t *testing.T, fn IndexFunc) { |
| 125 |
// Test the tests (NOTE: we may want to remove this at some point since |
| 126 |
// strings.ToLower is not always correct). |
| 127 |
for i, test := range compareTests { |
| 128 |
got := strings.Compare(strings.ToLower(test.s), strings.ToLower(test.t)) |
| 129 |
if got != test.out { |
| 130 |
t.Errorf("%d: strings.Compare(%q, %q) = %d; want: %d", |
| 131 |
i, strings.ToLower(test.s), strings.ToLower(test.t), got, test.out) |
| 132 |
} |
| 133 |
} |
| 134 |
if t.Failed() { |
| 135 |
t.Fatal("invalid Compare tests") |
| 136 |
return |
| 137 |
} |
| 138 |
|
| 139 |
for i, test := range compareTests { |
| 140 |
got := fn(test.s, test.t) |
| 141 |
if got != test.out { |
| 142 |
t.Errorf("%d: Compare(%q, %q) = %d; want: %d", i, test.s, test.t, got, test.out) |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func EqualFold(t *testing.T, fn func(s1, s2 string) bool) { |
| 148 |
// Ensure that strings.EqualFold does not match 'İ' (U+0130) |
| 149 |
// and ASCII 'i' or 'I'. This is mostly a sanity check. |
| 150 |
tests := append(compareTests, |
| 151 |
compareTest{"İ", "i", 1}, |
| 152 |
compareTest{"İ", "I", 1}, |
| 153 |
) |
| 154 |
for _, test := range tests { |
| 155 |
want := test.out == 0 |
| 156 |
got := strings.EqualFold(test.s, test.t) |
| 157 |
if got != want { |
| 158 |
t.Errorf("strings.EqualFold(%q, %q) = %t; want: %t", test.s, test.t, got, want) |
| 159 |
} |
| 160 |
} |
| 161 |
if t.Failed() { |
| 162 |
t.Fatal("invalid EqualFold tests") |
| 163 |
return |
| 164 |
} |
| 165 |
|
| 166 |
for _, test := range tests { |
| 167 |
want := test.out == 0 |
| 168 |
got := fn(test.s, test.t) |
| 169 |
if got != want { |
| 170 |
t.Errorf("EqualFold(%q, %q) = %t; want: %t", test.s, test.t, got, want) |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
type indexTest struct { |
| 176 |
s string |
| 177 |
sep string |
| 178 |
out int |
| 179 |
} |
| 180 |
|
| 181 |
var indexTests = []indexTest{ |
| 182 |
{"", "", 0}, |
| 183 |
{"", "a", -1}, |
| 184 |
{"", "foo", -1}, |
| 185 |
{"fo", "foo", -1}, |
| 186 |
{"foo", "foo", 0}, |
| 187 |
{"oofofoofooo", "f", 2}, |
| 188 |
{"oofofoofooo", "foo", 4}, |
| 189 |
{"barfoobarfoo", "foo", 3}, |
| 190 |
{"foo", "", 0}, |
| 191 |
{"foo", "o", 1}, |
| 192 |
{"abcABCabc", "A", 0}, |
| 193 |
{"abcVBCabc", "V", 3}, |
| 194 |
// cases with one byte strings - test special case in Index() |
| 195 |
{"", "a", -1}, |
| 196 |
{"x", "a", -1}, |
| 197 |
{"x", "x", 0}, |
| 198 |
{"abc", "a", 0}, |
| 199 |
{"abc", "b", 1}, |
| 200 |
{"abc", "c", 2}, |
| 201 |
{"ABC", "BC", 1}, |
| 202 |
{"abc", "x", -1}, |
| 203 |
// test special cases in Index() for short strings |
| 204 |
{"", "ab", -1}, |
| 205 |
{"bc", "ab", -1}, |
| 206 |
{"ab", "ab", 0}, |
| 207 |
{"xab", "ab", 1}, |
| 208 |
{"xab"[:2], "ab", -1}, |
| 209 |
{"", "abc", -1}, |
| 210 |
{"xbc", "abc", -1}, |
| 211 |
{"abc", "abc", 0}, |
| 212 |
{"xabc", "abc", 1}, |
| 213 |
{"xabc"[:3], "abc", -1}, |
| 214 |
{"xabxc", "abc", -1}, |
| 215 |
{"", "abcd", -1}, |
| 216 |
{"xbcd", "abcd", -1}, |
| 217 |
{"abcd", "abcd", 0}, |
| 218 |
{"xabcd", "abcd", 1}, |
| 219 |
{"xyabcd"[:5], "abcd", -1}, |
| 220 |
{"xbcqq", "abcqq", -1}, |
| 221 |
{"abcqq", "abcqq", 0}, |
| 222 |
{"xabcqq", "abcqq", 1}, |
| 223 |
{"xyabcqq"[:6], "abcqq", -1}, |
| 224 |
{"xabxcqq", "abcqq", -1}, |
| 225 |
{"xabcqxq", "abcqq", -1}, |
| 226 |
{"", "01234567", -1}, |
| 227 |
{"32145678", "01234567", -1}, |
| 228 |
{"01234567", "01234567", 0}, |
| 229 |
{"x01234567", "01234567", 1}, |
| 230 |
{"x0123456x01234567", "01234567", 9}, |
| 231 |
{"xx01234567"[:9], "01234567", -1}, |
| 232 |
{"", "0123456789", -1}, |
| 233 |
{"3214567844", "0123456789", -1}, |
| 234 |
{"0123456789", "0123456789", 0}, |
| 235 |
{"x0123456789", "0123456789", 1}, |
| 236 |
{"x012345678x0123456789", "0123456789", 11}, |
| 237 |
{"xyz0123456789"[:12], "0123456789", -1}, |
| 238 |
{"x01234567x89", "0123456789", -1}, |
| 239 |
{"", "0123456789012345", -1}, |
| 240 |
{"3214567889012345", "0123456789012345", -1}, |
| 241 |
{"0123456789012345", "0123456789012345", 0}, |
| 242 |
{"x0123456789012345", "0123456789012345", 1}, |
| 243 |
{"x012345678901234x0123456789012345", "0123456789012345", 17}, |
| 244 |
{"", "01234567890123456789", -1}, |
| 245 |
{"32145678890123456789", "01234567890123456789", -1}, |
| 246 |
{"01234567890123456789", "01234567890123456789", 0}, |
| 247 |
{"x01234567890123456789", "01234567890123456789", 1}, |
| 248 |
{"x0123456789012345678x01234567890123456789", "01234567890123456789", 21}, |
| 249 |
{"xyz01234567890123456789"[:22], "01234567890123456789", -1}, |
| 250 |
{"", "0123456789012345678901234567890", -1}, |
| 251 |
{"321456788901234567890123456789012345678911", "0123456789012345678901234567890", -1}, |
| 252 |
{"0123456789012345678901234567890", "0123456789012345678901234567890", 0}, |
| 253 |
{"x0123456789012345678901234567890", "0123456789012345678901234567890", 1}, |
| 254 |
{"x012345678901234567890123456789x0123456789012345678901234567890", "0123456789012345678901234567890", 32}, |
| 255 |
{"xyz0123456789012345678901234567890"[:33], "0123456789012345678901234567890", -1}, |
| 256 |
{"", "01234567890123456789012345678901", -1}, |
| 257 |
{"32145678890123456789012345678901234567890211", "01234567890123456789012345678901", -1}, |
| 258 |
{"01234567890123456789012345678901", "01234567890123456789012345678901", 0}, |
| 259 |
{"x01234567890123456789012345678901", "01234567890123456789012345678901", 1}, |
| 260 |
{"x0123456789012345678901234567890x01234567890123456789012345678901", "01234567890123456789012345678901", 33}, |
| 261 |
{"xyz01234567890123456789012345678901"[:34], "01234567890123456789012345678901", -1}, |
| 262 |
{"xxxxxx012345678901234567890123456789012345678901234567890123456789012", "012345678901234567890123456789012345678901234567890123456789012", 6}, |
| 263 |
{"", "0123456789012345678901234567890123456789", -1}, |
| 264 |
{"xx012345678901234567890123456789012345678901234567890123456789012", "0123456789012345678901234567890123456789", 2}, |
| 265 |
{"xx012345678901234567890123456789012345678901234567890123456789012"[:41], "0123456789012345678901234567890123456789", -1}, |
| 266 |
{"xx012345678901234567890123456789012345678901234567890123456789012", "0123456789012345678901234567890123456xxx", -1}, |
| 267 |
{"xx0123456789012345678901234567890123456789012345678901234567890120123456789012345678901234567890123456xxx", "0123456789012345678901234567890123456xxx", 65}, |
| 268 |
|
| 269 |
// Invalid UTF8 |
| 270 |
{"abc" + string(rune(utf8.RuneError)) + "123", string(rune(utf8.RuneError)), 3}, |
| 271 |
{"abc", string(rune(utf8.RuneError)), -1}, |
| 272 |
{"abc", string(rune(utf8.MaxRune)), -1}, |
| 273 |
|
| 274 |
// test fallback to Rabin-Karp. |
| 275 |
{"oxoxoxoxoxoxoxoxoxoxoxoy", "oy", 22}, |
| 276 |
{"oxoxoxoxoxoxoxoxoxoxoxox", "oy", -1}, |
| 277 |
|
| 278 |
// Actually test fallback to Rabin-Karp (the above tests don't trigger it). |
| 279 |
{strings.Repeat("ox", 64) + "yox", "oα" + strings.Repeat("ox", 32/len("ox")), -1}, |
| 280 |
{strings.Repeat("ox", 64) + "oα" + strings.Repeat("ox", 32/2), "oα" + strings.Repeat("ox", 32/2), 128}, |
| 281 |
|
| 282 |
// Sep longer (in bytes) than s |
| 283 |
{"aa", "aaa", -1}, |
| 284 |
{"aa", "aaaa", -1}, |
| 285 |
{"aa", "aaaaa", -1}, |
| 286 |
|
| 287 |
// Unicode strings |
| 288 |
{"oxoxoxoxoxoxoxoxoxoxoxoyoα", "oα", 24}, |
| 289 |
{"oxoxoxoxoxoxoxoxoxoxoxα", "α", 22}, |
| 290 |
|
| 291 |
// test fallback to Rabin-Karp (unicode). |
| 292 |
{"xx0123456789012345678901234567890123456789012345678901234567890120123456789012345678901234567890123456xxx☻", "0123456789012345678901234567890123456xxx☻", 65}, |
| 293 |
|
| 294 |
{"abc☻", "abc☻", 0}, |
| 295 |
{"abc☻", "ABC☻", 0}, |
| 296 |
{"123abc☻", "ABC☻", 3}, |
| 297 |
} |
| 298 |
|
| 299 |
// TODO: combine with indexTests |
| 300 |
var unicodeIndexTests = []indexTest{ |
| 301 |
// Map Kelvin 'K' (U+212A) to lowercase latin 'k'. |
| 302 |
{"abcK@", "k@", 3}, |
| 303 |
|
| 304 |
// Map the long 'S' 'ſ' to 'S' and 's'. |
| 305 |
{"abcſ@", "s@", 3}, |
| 306 |
{"abcS@", "ſ@", 3}, |
| 307 |
|
| 308 |
// Test with a unicode prefix in the substr to make sure the unicode |
| 309 |
// implementation is correct. |
| 310 |
{"abc☻K@", "☻k@", 3}, |
| 311 |
{"abc☻S@", "☻ſ@", 3}, |
| 312 |
|
| 313 |
// Sep longer (in bytes) |
| 314 |
{"\u212a", "a", -1}, |
| 315 |
{"\u212a", "k", 0}, |
| 316 |
{"a\u212a", "a\u212a", 0}, |
| 317 |
{"a\u212a", "a\u212a\u212a", -1}, |
| 318 |
|
| 319 |
// Test that İ does not fold to [Ii] |
| 320 |
{"İ", "İ", 0}, |
| 321 |
{"İ", "i", -1}, |
| 322 |
{"İ", "I", -1}, |
| 323 |
{"İİ", "İİ", 0}, |
| 324 |
{"İİİİ", "İİ", 0}, |
| 325 |
{"İİİİİİ", "İİ", 0}, |
| 326 |
{"0123456789İİ", "İİ", 10}, |
| 327 |
{"01234567890123456789İİ", "İİ", 20}, |
| 328 |
{"İİ" + strings.Repeat("a", 64), "İİ" + strings.Repeat("a", 64), 0}, |
| 329 |
|
| 330 |
// "İ" does not fold to "i" |
| 331 |
{"İ", "i", -1}, |
| 332 |
{"aİ", "ai", -1}, |
| 333 |
// "İ" does not fold to "ı" |
| 334 |
{"İ", "ı", -1}, |
| 335 |
{"aİ", "aı", -1}, |
| 336 |
|
| 337 |
// Special Unicode points that are not equal to either their |
| 338 |
// uppercase or lowercase form. |
| 339 |
{"aLjLJljb", "ljlj", 1}, |
| 340 |
{"aDzDZdzb", "dzDZDz", 1}, |
| 341 |
{"DzDZdzDzDZdz", "dzDZDza", -1}, |
| 342 |
|
| 343 |
// Test the cutover to to bytealg.IndexString when it is triggered in |
| 344 |
// the middle of rune that contains consecutive runs of equal bytes. |
| 345 |
{"aaaaaKKKK\U000bc104a", "\U000bc104a", 17}, // cutover: (n + 16) / 8 |
| 346 |
{"aaaaaKKKK鄄a", "鄄a", 17}, |
| 347 |
{"aaKKKKKa\U000bc104a", "\U000bc104a", 18}, // cutover: 4 + n>>4 |
| 348 |
{"aaKKKKKa鄄a", "鄄a", 18}, |
| 349 |
|
| 350 |
// Test cases found by fuzzing |
| 351 |
{"\x00iK", "iK", 1}, |
| 352 |
{"İKKKK\x00iK", "iK", 15}, |
| 353 |
{"İKKKKiK", "iK", 14}, |
| 354 |
{"4=K ", "=\u212a", 1}, |
| 355 |
{"I", "\u0131", -1}, |
| 356 |
|
| 357 |
// Evil strings discovered fuzzing. |
| 358 |
{ |
| 359 |
s: "32ⱭⱭⱭⱭⱭ45678890ⱭⱭⱭⱭⱭ234567890ⱭⱭⱭⱭⱭ234567890ⱭⱭⱭⱭⱭ23456789ⱭⱭⱭⱭⱭⱭⱭⱭⱭⱭ", |
| 360 |
sep: "0ⱭⱭⱭⱭⱭ234567890ⱭⱭⱭⱭⱭ234567890ⱭⱭⱭⱭⱭ234567890", |
| 361 |
out: -1, |
| 362 |
}, |
| 363 |
{ |
| 364 |
s: "<<\ua7ac\x02\ub680\U0010f410\U0002ac40\n\x15\u2126\ufa12\x14", |
| 365 |
sep: "<\ua7ac\x02\ub680\U0010f410\U0002ac40\n\x15\u03c9\ufa12", |
| 366 |
out: 1, |
| 367 |
}, |
| 368 |
{ |
| 369 |
s: "\U00024b8a\u2c65I\u7c12\u313a/A\u027d\u017f=\x05", |
| 370 |
sep: "\U00024b8a\u2c65I\u7c12\u313a/a\u2c64\u017f=", |
| 371 |
out: 0, |
| 372 |
}, |
| 373 |
{ |
| 374 |
s: "\U0002a433\u3577\U000230d6\U001024b4\u73f1\u56f0\U0002d7db\U0010e3ac\U000204ca\u2575~\u8825\U0002ba82\U0002c0e4\u743aK]", |
| 375 |
sep: "\u743a\u212a", |
| 376 |
out: 48, |
| 377 |
}, |
| 378 |
{ |
| 379 |
s: "z0\U0010640b\U0001f326k-k\U00100621\U000240ff\U000e013fl", |
| 380 |
sep: "\u212a-\u212a", |
| 381 |
out: 10, |
| 382 |
}, |
| 383 |
{ |
| 384 |
s: "\U0007279d\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd", |
| 385 |
sep: "\ufffd\ufffd\ufffd\ufffd\ufffd", |
| 386 |
out: 4, |
| 387 |
}, |
| 388 |
{ |
| 389 |
s: ">Ā\U000c4c1bKKKK\x00YUв\U000bc104q9", |
| 390 |
sep: "\U000bc104q9", |
| 391 |
out: 24, |
| 392 |
}, |
| 393 |
{ |
| 394 |
s: "\U000bc104q9", |
| 395 |
sep: "\U000bc104q9", |
| 396 |
out: 0, |
| 397 |
}, |
| 398 |
{ |
| 399 |
s: "\ufffd\uff42", |
| 400 |
sep: "\ufffd", |
| 401 |
out: 0, |
| 402 |
}, |
| 403 |
{ |
| 404 |
s: "abc\ufffd\uff42", |
| 405 |
sep: "\ufffd", |
| 406 |
out: 3, |
| 407 |
}, |
| 408 |
// Make sure we don't fold 'ı' (LATIN SMALL LETTER DOTLESS I) |
| 409 |
{ |
| 410 |
|
| 411 |
s: "\u0224Ie\U00010cd4", |
| 412 |
sep: "\u0224\u0131e\U00010cd4", |
| 413 |
out: -1, |
| 414 |
}, |
| 415 |
} |
| 416 |
|
| 417 |
func init() { |
| 418 |
// Append some test cases that include Kelvin K and ASCII K. Since Kelvin |
| 419 |
// K is 3x the width of ASCII [Kk] we want to test the logic for handling |
| 420 |
// that. |
| 421 |
p0 := strings.Repeat("\u212a", 64) // Kelvin K |
| 422 |
p1 := strings.Repeat("K", 64) |
| 423 |
n := utf8.RuneLen('\u212a') |
| 424 |
for i := 2; i <= 64; i *= 2 { |
| 425 |
s0 := p0[:i*n] |
| 426 |
s1 := p1[:i] |
| 427 |
unicodeIndexTests = append(unicodeIndexTests, indexTest{s0, s1, 0}, indexTest{s1, s0, 0}) |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
var lastIndexTests = []indexTest{ |
| 432 |
{"", "", 0}, |
| 433 |
{"", "a", -1}, |
| 434 |
{"", "foo", -1}, |
| 435 |
{"fo", "foo", -1}, |
| 436 |
{"foo", "foo", 0}, |
| 437 |
{"foo", "f", 0}, |
| 438 |
{"oofofoofooo", "f", 7}, |
| 439 |
{"oofofoofooo", "foo", 7}, |
| 440 |
{"barfoobarfoo", "foo", 9}, |
| 441 |
{"foo", "", 3}, |
| 442 |
{"foo", "o", 2}, |
| 443 |
{"abcABCabc", "A", 6}, |
| 444 |
{"abcABCabc", "a", 6}, |
| 445 |
|
| 446 |
// Invalid UTF8 |
| 447 |
{"abc" + string(rune(utf8.RuneError)) + "123", string(rune(utf8.RuneError)), 3}, |
| 448 |
{"abc", string(rune(utf8.RuneError)), -1}, |
| 449 |
{"abc", string(rune(utf8.MaxRune)), -1}, |
| 450 |
{ |
| 451 |
string(rune(unicode.MaxRune)), |
| 452 |
string(rune(unicode.MaxRune)), |
| 453 |
strings.LastIndex(string(rune(unicode.MaxRune)), string(rune(unicode.MaxRune))), |
| 454 |
}, |
| 455 |
{ |
| 456 |
"a" + string(rune(unicode.MaxRune)), |
| 457 |
string(rune(unicode.MaxRune)), |
| 458 |
strings.LastIndex("a"+string(rune(unicode.MaxRune)), string(rune(unicode.MaxRune))), |
| 459 |
}, |
| 460 |
{ |
| 461 |
string(rune(unicode.MaxRune + 1)), |
| 462 |
string(rune(unicode.MaxRune + 1)), |
| 463 |
strings.LastIndex(string(rune(unicode.MaxRune+1)), string(rune(unicode.MaxRune+1))), |
| 464 |
}, |
| 465 |
{ |
| 466 |
"a" + string(rune(unicode.MaxRune+1)), |
| 467 |
string(rune(unicode.MaxRune + 1)), |
| 468 |
strings.LastIndex("a"+string(rune(unicode.MaxRune+1)), string(rune(unicode.MaxRune+1))), |
| 469 |
}, |
| 470 |
|
| 471 |
// Unicode |
| 472 |
|
| 473 |
{"fooΑΒΔbar", "αβδ", 3}, |
| 474 |
|
| 475 |
// Map Kelvin 'K' (U+212A) to lowercase latin 'k'. |
| 476 |
{"abcK@", "k@", 3}, |
| 477 |
|
| 478 |
// Map the long 'S' 'ſ' to 'S' and 's'. |
| 479 |
{"abcſ@", "s@", 3}, |
| 480 |
{"abcS@", "ſ@", 3}, |
| 481 |
|
| 482 |
// Test with a unicode prefix in the substr to make sure the unicode |
| 483 |
// implementation is correct. |
| 484 |
{"abc☻K@", "☻k@", 3}, |
| 485 |
{"abc☻S@", "☻ſ@", 3}, |
| 486 |
|
| 487 |
// Sep longer (in bytes) than s |
| 488 |
{"aa", "aaa", -1}, |
| 489 |
{"aa", "aaaa", -1}, |
| 490 |
{"aa", "aaaaa", -1}, |
| 491 |
{"a\u212a", "a\u212a", 0}, |
| 492 |
{"a\u212a", "a\u212a\u212a", -1}, |
| 493 |
{"a\u212a", "a\u212a\u212a\u212a", -1}, |
| 494 |
{"a\u212aa", "ka", 1}, |
| 495 |
|
| 496 |
// Tests discovered with fuzzing |
| 497 |
{"4=K ", "=\u212a", 1}, |
| 498 |
{"I", "\u0131", -1}, |
| 499 |
{"aßẛ", "ß", 1}, |
| 500 |
{"aßẛ", "a", 0}, |
| 501 |
{"OFf", "İF", -1}, |
| 502 |
{"``Ɽ", "\U000823eb`", -1}, |
| 503 |
|
| 504 |
{"\u0250\u0250\u0250\u0250\u0250 a", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F A", 0}, // grows one byte per char |
| 505 |
{"a\u0250\u0250\u0250\u0250\u0250", "A\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F", 0}, // |
| 506 |
{"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D a", "\u0251\u0251\u0251\u0251\u0251 A", 0}, // shrinks one byte per char |
| 507 |
{"a\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "A\u0251\u0251\u0251\u0251\u0251", 0}, // shrinks one byte per char |
| 508 |
{"abc\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251", 3}, |
| 509 |
{"ΑΒΔ\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251", len("ΑΒΔ")}, |
| 510 |
} |
| 511 |
|
| 512 |
// Execute f on each test case. funcName should be the name of f; it's used |
| 513 |
// in failure reports. |
| 514 |
func runIndexTests(t testing.TB, f IndexFunc, funcName string, testCases []indexTest, noError bool) { |
| 515 |
t.Helper() |
| 516 |
fails := 0 |
| 517 |
for _, test := range testCases { |
| 518 |
actual := f(test.s, test.sep) |
| 519 |
if actual != test.out { |
| 520 |
fails++ |
| 521 |
errorf := t.Errorf |
| 522 |
if noError { |
| 523 |
errorf = t.Logf |
| 524 |
} |
| 525 |
var foldable bool |
| 526 |
for _, r := range test.sep { |
| 527 |
foldable = tables.FoldMap(r) != nil |
| 528 |
if foldable { |
| 529 |
break |
| 530 |
} |
| 531 |
} |
| 532 |
errorf("%s\n"+ |
| 533 |
"S: %q\n"+ |
| 534 |
"Sep: %q\n"+ |
| 535 |
"Got: %d\n"+ |
| 536 |
"Want: %d\n"+ |
| 537 |
"Fold: %t\n"+ |
| 538 |
"\n"+ |
| 539 |
"S: %s\n"+ |
| 540 |
"Sep: %s\n"+ |
| 541 |
"Lower:\n"+ |
| 542 |
"S: %s\n"+ |
| 543 |
"Sep: %s\n"+ |
| 544 |
"\n", |
| 545 |
funcName, |
| 546 |
test.s, test.sep, actual, test.out, |
| 547 |
foldable, |
| 548 |
strconv.QuoteToASCII(test.s), |
| 549 |
strconv.QuoteToASCII(test.sep), |
| 550 |
strconv.QuoteToASCII(strings.ToLower(test.s)), |
| 551 |
strconv.QuoteToASCII(strings.ToLower(test.sep)), |
| 552 |
) |
| 553 |
} |
| 554 |
} |
| 555 |
if t.Failed() && testing.Verbose() { |
| 556 |
t.Logf("%s: failed %d out of %d tests", funcName, fails, len(testCases)) |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
func Index(t *testing.T, fn IndexFunc) { |
| 561 |
if t.Failed() { |
| 562 |
t.Fatal("Reference Index function failed: tests are invalid") |
| 563 |
} |
| 564 |
runIndexTests(t, fn, "Index", indexTests, false) |
| 565 |
} |
| 566 |
|
| 567 |
// Test Index with invalid UTF-8 |
| 568 |
func IndexInvalid(t *testing.T, fn IndexFunc) { |
| 569 |
t.Run("Prefix", func(t *testing.T) { |
| 570 |
rr := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 571 |
basePrefix := strings.Repeat("=☻", 32) |
| 572 |
var tests []indexTest |
| 573 |
for i := 0; i < 4; i++ { |
| 574 |
iterateInvalidSequenceTests(func(s0, s1 string) { |
| 575 |
for i := 0; i < 8; i++ { |
| 576 |
prefix := basePrefix[:rr.Intn(len(basePrefix))] |
| 577 |
if len(prefix) > 0 && i&1 != 0 { |
| 578 |
// Make the first rune Unicode |
| 579 |
prefix = strings.TrimPrefix(prefix, "=") |
| 580 |
} |
| 581 |
for len(prefix) > 0 { |
| 582 |
// Make sure the prefix is valid otherwise we may match the |
| 583 |
// trailing invalid btytes (since s1 is mostly invalid). |
| 584 |
if r, _ := utf8.DecodeLastRuneInString(prefix); r != utf8.RuneError { |
| 585 |
break |
| 586 |
} |
| 587 |
prefix = prefix[:len(prefix)-1] |
| 588 |
} |
| 589 |
out := -1 |
| 590 |
if strings.EqualFold(s0, s1) { |
| 591 |
out = len(prefix) |
| 592 |
} |
| 593 |
tests = append(tests, indexTest{ |
| 594 |
s: prefix + s0, |
| 595 |
sep: s1, |
| 596 |
out: out, |
| 597 |
}) |
| 598 |
} |
| 599 |
}) |
| 600 |
} |
| 601 |
runIndexTests(&testWrapper{T: t}, fn, "Index", tests, false) |
| 602 |
}) |
| 603 |
|
| 604 |
t.Run("NeedleLongerThanHaystack", func(t *testing.T) { |
| 605 |
rr := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 606 |
suffix := strings.Repeat("=☻", 8) |
| 607 |
var tests []indexTest |
| 608 |
iterateInvalidSequenceTests(func(s0, s1 string) { |
| 609 |
n := rr.Intn(len(suffix)-1) + 1 |
| 610 |
tests = append(tests, indexTest{ |
| 611 |
s: s0, |
| 612 |
sep: s1 + suffix[:n], |
| 613 |
out: -1, |
| 614 |
}) |
| 615 |
}) |
| 616 |
runIndexTests(&testWrapper{T: t}, fn, "Index", tests, false) |
| 617 |
}) |
| 618 |
|
| 619 |
// Test that when the inputs are invalid UTF-8 encoded strings our results |
| 620 |
// match what strings.EqualFold would give. |
| 621 |
t.Run("EqualFold", func(t *testing.T) { |
| 622 |
// We cannot use IndexRunesReference here because when invalid UTF-8 |
| 623 |
// is converted to a rune slice each invalid sequence is converted to |
| 624 |
// RuneError which changes the encoded length and thus the expected |
| 625 |
// index is incorrect. |
| 626 |
simpleIndex := func(s, sep string) int { |
| 627 |
n := len(sep) |
| 628 |
for i := n; i <= len(s); i++ { |
| 629 |
if strings.EqualFold(s[i-n:i], sep) { |
| 630 |
return i - n |
| 631 |
} |
| 632 |
} |
| 633 |
return -1 |
| 634 |
} |
| 635 |
split := func(s string) (a []string) { |
| 636 |
for _, b := range bytes.Split([]byte(s), nil) { |
| 637 |
a = append(a, string(b)) |
| 638 |
} |
| 639 |
return a |
| 640 |
} |
| 641 |
var tests []indexTest |
| 642 |
iterateInvalidSequenceTests(func(s0, s1 string) { |
| 643 |
for i := 1; i < len(s1); i++ { |
| 644 |
tests = append(tests, indexTest{ |
| 645 |
s: s0, |
| 646 |
sep: s1[:i], |
| 647 |
out: simpleIndex(s0, s1[:i]), |
| 648 |
}) |
| 649 |
} |
| 650 |
s := strings.Join(split(s0), "αβa") |
| 651 |
sep := strings.Join(split(s1), "ΑΒA") |
| 652 |
out := -1 |
| 653 |
if strings.EqualFold(s0, s1) { |
| 654 |
out = 0 |
| 655 |
} |
| 656 |
tests = append(tests, indexTest{ |
| 657 |
s: s, |
| 658 |
sep: sep, |
| 659 |
out: out, |
| 660 |
}) |
| 661 |
}) |
| 662 |
runIndexTests(&testWrapper{T: t}, fn, "Index", tests, false) |
| 663 |
}) |
| 664 |
} |
| 665 |
|
| 666 |
func IndexRuneIndexParity(t *testing.T, index IndexFunc, indexRune IndexRuneFunc) { |
| 667 |
iterateInvalidSequenceTests(func(s0, s1 string) { |
| 668 |
for _, c := range []byte(s1) { |
| 669 |
r := rune(c) |
| 670 |
i := index(s0, string(r)) |
| 671 |
j := indexRune(s0, r) |
| 672 |
if i != j { |
| 673 |
t.Fatalf("Index(%+q, %+q) = %d; want: %d:", s0, r, i, j) |
| 674 |
} |
| 675 |
} |
| 676 |
}) |
| 677 |
} |
| 678 |
|
| 679 |
type TestFunc struct { |
| 680 |
Name string |
| 681 |
Index IndexFunc |
| 682 |
Contains ContainsFunc |
| 683 |
} |
| 684 |
|
| 685 |
// Test against all assigned Unicode code points to make sure nothing changes |
| 686 |
// in new versions. In particular this is to identify code points that require |
| 687 |
// special handling (i.e. 'İ' and 'ı'). |
| 688 |
func IndexAllAssigned(t *testing.T, fns ...TestFunc) { |
| 689 |
if testing.Short() { |
| 690 |
t.Skip("short test") |
| 691 |
} |
| 692 |
dupe := func(rs []rune) []rune { |
| 693 |
a := make([]rune, len(rs)) |
| 694 |
copy(a, rs) |
| 695 |
return a |
| 696 |
} |
| 697 |
|
| 698 |
all := make([]rune, len(assignedRunes)) |
| 699 |
copy(all, assignedRunes) |
| 700 |
rr := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 701 |
rr.Shuffle(len(all), func(i, j int) { |
| 702 |
all[i], all[j] = all[j], all[i] |
| 703 |
}) |
| 704 |
|
| 705 |
var ( |
| 706 |
upperStr string |
| 707 |
lowerStr string |
| 708 |
foldStr string |
| 709 |
allStr = string(all) |
| 710 |
upper = dupe(all) |
| 711 |
lower = dupe(all) |
| 712 |
fold = dupe(all) |
| 713 |
) |
| 714 |
|
| 715 |
conv := func(rs []rune, fn func(rune) rune) string { |
| 716 |
for i, r := range rs { |
| 717 |
if r != 'İ' && r != 'ı' { |
| 718 |
r = fn(r) |
| 719 |
} |
| 720 |
rs[i] = r |
| 721 |
} |
| 722 |
return string(rs) |
| 723 |
} |
| 724 |
|
| 725 |
// This is slow so do it in parallel |
| 726 |
var wg sync.WaitGroup |
| 727 |
wg.Add(3) |
| 728 |
go func() { |
| 729 |
defer wg.Done() |
| 730 |
upperStr = conv(upper, unicode.ToUpper) |
| 731 |
}() |
| 732 |
go func() { |
| 733 |
defer wg.Done() |
| 734 |
lowerStr = conv(lower, unicode.ToLower) |
| 735 |
}() |
| 736 |
go func() { |
| 737 |
defer wg.Done() |
| 738 |
for i, r := range fold { |
| 739 |
fold[i] = unicode.SimpleFold(r) |
| 740 |
} |
| 741 |
foldStr = string(fold) |
| 742 |
}() |
| 743 |
wg.Wait() |
| 744 |
|
| 745 |
names := map[string]string{ |
| 746 |
upperStr: "Upper", |
| 747 |
lowerStr: "Lower", |
| 748 |
foldStr: "Fold", |
| 749 |
allStr: "All", |
| 750 |
} |
| 751 |
tname := func(s1, s2 string) string { |
| 752 |
n1 := names[s1] |
| 753 |
n2 := names[s2] |
| 754 |
if n1 == "" { |
| 755 |
t.Fatalf("missing name for test string: %q", s1[:8]) |
| 756 |
} |
| 757 |
if n2 == "" { |
| 758 |
t.Fatalf("missing name for test string: %q", s2[:8]) |
| 759 |
} |
| 760 |
return n1 + "_" + n2 |
| 761 |
} |
| 762 |
|
| 763 |
tests := []indexTest{ |
| 764 |
{allStr, allStr, 0}, |
| 765 |
{upperStr, allStr, 0}, |
| 766 |
{upperStr, upperStr, 0}, |
| 767 |
{upperStr, lowerStr, 0}, |
| 768 |
{lowerStr, allStr, 0}, |
| 769 |
{lowerStr, lowerStr, 0}, |
| 770 |
{foldStr, allStr, 0}, |
| 771 |
} |
| 772 |
for _, d := range fns { |
| 773 |
d := d |
| 774 |
t.Run(d.Name, func(t *testing.T) { |
| 775 |
t.Parallel() |
| 776 |
for i := range tests { |
| 777 |
i := i |
| 778 |
tt := tests[i] |
| 779 |
t.Run(tname(tt.s, tt.sep), func(t *testing.T) { |
| 780 |
t.Parallel() |
| 781 |
switch { |
| 782 |
case d.Index != nil: |
| 783 |
got := d.Index(tt.s, tt.sep) |
| 784 |
if got != tt.out { |
| 785 |
t.Errorf("%d got: %d want: %d", i, got, tt.out) |
| 786 |
} |
| 787 |
case d.Contains != nil: |
| 788 |
got := d.Contains(tt.s, tt.sep) |
| 789 |
want := (tt.out == 0) |
| 790 |
if got != want { |
| 791 |
t.Errorf("%d got: %t want: %t", i, got, want) |
| 792 |
} |
| 793 |
default: |
| 794 |
t.Fatalf("No function defined for test: %+v", d) |
| 795 |
} |
| 796 |
}) |
| 797 |
} |
| 798 |
}) |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
func IndexUnicode(t *testing.T, fn IndexFunc) { |
| 803 |
type Replacement struct { |
| 804 |
old, new string |
| 805 |
} |
| 806 |
replacements := [][]Replacement{ |
| 807 |
{{"a", "α"}, {"A", "Α"}, {"1", "Δ"}}, |
| 808 |
{{"a", "α"}, {"A", "Α"}, {"1", "日a本b語ç日ð本Ê語þ日¥本¼語i日©"}}, |
| 809 |
{{"1", "\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D"}}, // shrinks one byte per char |
| 810 |
{{"1", "\u0250\u0250\u0250\u0250\u0250"}}, // grows one byte per char |
| 811 |
} |
| 812 |
for _, reps := range replacements { |
| 813 |
t.Run("", func(t *testing.T) { |
| 814 |
replace := func(s string) string { |
| 815 |
for _, rr := range reps { |
| 816 |
o := strings.ReplaceAll(s, rr.old, rr.new) |
| 817 |
if !utf8.ValidString(o) { |
| 818 |
t.Fatalf("Invalid transformation %q => %q", s, o) |
| 819 |
} |
| 820 |
s = o |
| 821 |
} |
| 822 |
return s |
| 823 |
} |
| 824 |
|
| 825 |
tests := append(indexTests, unicodeIndexTests...) |
| 826 |
for _, test := range tests { |
| 827 |
out := test.out |
| 828 |
if out > 0 { |
| 829 |
out = len(replace(test.s[:test.out])) |
| 830 |
} |
| 831 |
tests = append(tests, indexTest{ |
| 832 |
s: replace(test.s), |
| 833 |
sep: replace(test.sep), |
| 834 |
out: out, |
| 835 |
}) |
| 836 |
} |
| 837 |
|
| 838 |
runIndexTests(t, fn, "Index", tests, false) |
| 839 |
}) |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
// Test our use of bytealg.IndexString |
| 844 |
func IndexNumeric(t *testing.T, fn IndexFunc) { |
| 845 |
ns := strings.Repeat("1234", 128/4) |
| 846 |
hs := strings.Repeat(" ", 256) |
| 847 |
tests := make([]indexTest, 0, 1024) |
| 848 |
// Test the boundaries around the bytealg.MaxBruteForce cutover |
| 849 |
for _, i := range []int{1, 4, 8, 15, 16, 17, 31, 32, 33, 63, 64, 65, 128} { |
| 850 |
for j := 0; j <= len(hs); j += 3 { |
| 851 |
sep := ns[:i] |
| 852 |
tests = append(tests, indexTest{ |
| 853 |
s: hs[:j] + sep, |
| 854 |
sep: sep, |
| 855 |
out: j, |
| 856 |
}) |
| 857 |
if len(sep) > 1 { |
| 858 |
tests = append(tests, indexTest{ |
| 859 |
s: hs[:j] + sep[:len(sep)-1] + " ", |
| 860 |
sep: sep, |
| 861 |
out: -1, |
| 862 |
}) |
| 863 |
} |
| 864 |
} |
| 865 |
} |
| 866 |
runIndexTests(t, fn, "Index", tests, false) |
| 867 |
} |
| 868 |
|
| 869 |
// Extensively test the handling of Kelvin K since it is three times the size |
| 870 |
// of ASCII [Kk] it requires special handling. |
| 871 |
func IndexKelvin(t *testing.T, fn IndexFunc) { |
| 872 |
const K = "\u212A" // Kelvin |
| 873 |
|
| 874 |
test := func(t *testing.T, s, substr string, want int) { |
| 875 |
t.Helper() |
| 876 |
if got := fn(s, substr); got != want { |
| 877 |
t.Errorf("Index(%q, %q) = %d; want: %d", s, substr, got, want) |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
t.Run("Match0", func(t *testing.T) { |
| 882 |
for i := 1; i < 128; i++ { |
| 883 |
s := strings.Repeat("k", i) |
| 884 |
substr := strings.Repeat(K, i) |
| 885 |
test(t, s, substr, 0) |
| 886 |
test(t, K+s[:len(s)-1], substr, 0) |
| 887 |
test(t, s[:len(s)-1]+K, substr, 0) |
| 888 |
} |
| 889 |
}) |
| 890 |
|
| 891 |
r := strings.Repeat |
| 892 |
t.Run("Match1", func(t *testing.T) { |
| 893 |
for i := 1; i < 128; i++ { |
| 894 |
test(t, "a"+r("k", i), r(K, i), 1) |
| 895 |
} |
| 896 |
}) |
| 897 |
t.Run("NoMatchPrefix", func(t *testing.T) { |
| 898 |
for i := 1; i < 128; i++ { |
| 899 |
test(t, "a"+r("k", i-1), r(K, i), -1) |
| 900 |
} |
| 901 |
}) |
| 902 |
t.Run("NoMatchSuffix", func(t *testing.T) { |
| 903 |
for i := 1; i < 128; i++ { |
| 904 |
test(t, r("k", i-1)+"a", r(K, i), -1) |
| 905 |
} |
| 906 |
}) |
| 907 |
} |
| 908 |
|
| 909 |
var invalidSequenceTests = []string{ |
| 910 |
"\xed\xa0\x80\x80", // surrogate min |
| 911 |
"\xed\xbf\xbf\x80", // surrogate max |
| 912 |
|
| 913 |
// xx |
| 914 |
"\x91\x80\x80\x80", |
| 915 |
|
| 916 |
// s1 |
| 917 |
"\xC2\x7F\x80\x80", |
| 918 |
"\xC2\xC0\x80\x80", |
| 919 |
"\xDF\x7F\x80\x80", |
| 920 |
"\xDF\xC0\x80\x80", |
| 921 |
|
| 922 |
// s2 |
| 923 |
"\xE0\x9F\xBF\x80", |
| 924 |
"\xE0\xA0\x7F\x80", |
| 925 |
"\xE0\xBF\xC0\x80", |
| 926 |
"\xE0\xC0\x80\x80", |
| 927 |
|
| 928 |
// s3 |
| 929 |
"\xE1\x7F\xBF\x80", |
| 930 |
"\xE1\x80\x7F\x80", |
| 931 |
"\xE1\xBF\xC0\x80", |
| 932 |
"\xE1\xC0\x80\x80", |
| 933 |
|
| 934 |
//s4 |
| 935 |
"\xED\x7F\xBF\x80", |
| 936 |
"\xED\x80\x7F\x80", |
| 937 |
"\xED\x9F\xC0\x80", |
| 938 |
"\xED\xA0\x80\x80", |
| 939 |
|
| 940 |
// s5 |
| 941 |
"\xF0\x8F\xBF\xBF", |
| 942 |
"\xF0\x90\x7F\xBF", |
| 943 |
"\xF0\x90\x80\x7F", |
| 944 |
"\xF0\xBF\xBF\xC0", |
| 945 |
"\xF0\xBF\xC0\x80", |
| 946 |
"\xF0\xC0\x80\x80", |
| 947 |
|
| 948 |
// s6 |
| 949 |
"\xF1\x7F\xBF\xBF", |
| 950 |
"\xF1\x80\x7F\xBF", |
| 951 |
"\xF1\x80\x80\x7F", |
| 952 |
"\xF1\xBF\xBF\xC0", |
| 953 |
"\xF1\xBF\xC0\x80", |
| 954 |
"\xF1\xC0\x80\x80", |
| 955 |
|
| 956 |
// s7 |
| 957 |
"\xF4\x7F\xBF\xBF", |
| 958 |
"\xF4\x80\x7F\xBF", |
| 959 |
"\xF4\x80\x80\x7F", |
| 960 |
"\xF4\x8F\xBF\xC0", |
| 961 |
"\xF4\x8F\xC0\x80", |
| 962 |
"\xF4\x90\x80\x80", |
| 963 |
} |
| 964 |
|
| 965 |
func iterateInvalidSequenceTests(fn func(s0, s1 string)) { |
| 966 |
// Test against self |
| 967 |
for _, s := range invalidSequenceTests { |
| 968 |
fn(s, s) |
| 969 |
} |
| 970 |
|
| 971 |
// Test against next string |
| 972 |
for i := 0; i < len(invalidSequenceTests)-1; i++ { |
| 973 |
fn(invalidSequenceTests[i], invalidSequenceTests[i+1]) |
| 974 |
} |
| 975 |
|
| 976 |
// Test all possible combinations |
| 977 |
if *exhaustiveFuzz { |
| 978 |
for i := 0; i < len(invalidSequenceTests); i++ { |
| 979 |
for j := 0; j < len(invalidSequenceTests); j++ { |
| 980 |
if j == i { |
| 981 |
continue |
| 982 |
} |
| 983 |
fn(invalidSequenceTests[i], invalidSequenceTests[j]) |
| 984 |
} |
| 985 |
} |
| 986 |
return |
| 987 |
} |
| 988 |
|
| 989 |
// All the invalid sequences should be equal so test |
| 990 |
// random pairs. |
| 991 |
rr := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 992 |
for i := 0; i < 4; i++ { |
| 993 |
for i, s := range invalidSequenceTests { |
| 994 |
j := rr.Intn(len(invalidSequenceTests)) |
| 995 |
for j == i { |
| 996 |
j = rr.Intn(len(invalidSequenceTests)) |
| 997 |
} |
| 998 |
fn(s, invalidSequenceTests[j]) |
| 999 |
} |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
func Contains(t *testing.T, fn ContainsFunc) { |
| 1004 |
tt := &testWrapper{T: t} |
| 1005 |
for _, test := range indexTests { |
| 1006 |
got := fn(test.s, test.sep) |
| 1007 |
want := test.out >= 0 |
| 1008 |
if got != want { |
| 1009 |
tt.Errorf("Contains(%q, %q) = %t; want: %t", test.s, test.sep, got, want) |
| 1010 |
} |
| 1011 |
} |
| 1012 |
// Invalid: equal length |
| 1013 |
iterateInvalidSequenceTests(func(s0, s1 string) { |
| 1014 |
got := fn(s0, s1) |
| 1015 |
want := strings.EqualFold(s0, s1) |
| 1016 |
if got != want { |
| 1017 |
tt.Errorf("Contains(%q, %q) = %t; want: %t", s0, s1, got, want) |
| 1018 |
} |
| 1019 |
}) |
| 1020 |
// Invalid: short |
| 1021 |
iterateInvalidSequenceTests(func(s0, s1 string) { |
| 1022 |
want := strings.EqualFold(s0, s1) |
| 1023 |
s0 = "a" + s0 |
| 1024 |
got := fn(s0, s1) |
| 1025 |
if got != want { |
| 1026 |
tt.Errorf("Contains(%q, %q) = %t; want: %t", s0, s1, got, want) |
| 1027 |
} |
| 1028 |
}) |
| 1029 |
// Invalid long |
| 1030 |
prefix := strings.Repeat("=", 64) |
| 1031 |
iterateInvalidSequenceTests(func(s0, s1 string) { |
| 1032 |
want := strings.EqualFold(s0, s1) |
| 1033 |
s0 = prefix + s0 |
| 1034 |
got := fn(s0, s1) |
| 1035 |
// want := strings.Contains(s0, s1) |
| 1036 |
if got != want { |
| 1037 |
tt.Errorf("Contains(%q, %q) = %t; want: %t", s0, s1, got, want) |
| 1038 |
} |
| 1039 |
}) |
| 1040 |
} |
| 1041 |
|
| 1042 |
const ( |
| 1043 |
a32 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" // "a" repeated 32 times |
| 1044 |
dots = "1....2....3....4" |
| 1045 |
) |
| 1046 |
|
| 1047 |
var ContainsAnyTests = []struct { |
| 1048 |
str, substr string |
| 1049 |
expected bool |
| 1050 |
}{ |
| 1051 |
{"", "", false}, |
| 1052 |
{"", "a", false}, |
| 1053 |
{"", "abc", false}, |
| 1054 |
{"a", "", false}, |
| 1055 |
{"a", "a", true}, |
| 1056 |
{"aaa", "a", true}, |
| 1057 |
{"abc", "xyz", false}, |
| 1058 |
{"abc", "xcz", true}, |
| 1059 |
{"bas", "SsKs", true}, |
| 1060 |
{"bak", "SsKs", true}, |
| 1061 |
{a32 + "\u212a", "k", true}, |
| 1062 |
{a32 + "\u212a", "K", true}, |
| 1063 |
{"a☺b☻c☹d", "uvw☻xyz", true}, |
| 1064 |
{"aRegExp*", ".(|)*+?^$[]", true}, |
| 1065 |
{dots + dots + dots, " ", false}, |
| 1066 |
|
| 1067 |
// Case-insensitive |
| 1068 |
{"a", "A", true}, |
| 1069 |
{"aaa", "A", true}, |
| 1070 |
{"αβa", "ΑΒΔ", true}, |
| 1071 |
|
| 1072 |
// Use asciiSet only if str is all ASCII |
| 1073 |
{a32, "sS", false}, |
| 1074 |
{a32, "kK", false}, |
| 1075 |
// Cannot use asciiSet fallback to Unicode aware algorithm |
| 1076 |
{a32 + "\u212a", "sS", false}, |
| 1077 |
{a32 + "\u212a", "kK", true}, |
| 1078 |
{a32, "kK" + "\u212a", false}, |
| 1079 |
} |
| 1080 |
|
| 1081 |
func ContainsAny(t *testing.T, fn ContainsFunc) { |
| 1082 |
for _, ct := range ContainsAnyTests { |
| 1083 |
if fn(ct.str, ct.substr) != ct.expected { |
| 1084 |
t.Errorf("ContainsAny(%s, %s) = %v, want %v", |
| 1085 |
ct.str, ct.substr, !ct.expected, ct.expected) |
| 1086 |
} |
| 1087 |
} |
| 1088 |
|
| 1089 |
// Invalid |
| 1090 |
iterateInvalidSequenceTests(func(s0, s1 string) { |
| 1091 |
for _, s := range []string{s1, string(utf8.RuneError)} { |
| 1092 |
got := fn(s0, s) |
| 1093 |
if !got { |
| 1094 |
t.Errorf("ContainsAny(%s, %s) = %v, want %v", |
| 1095 |
s0, s1, got, true) |
| 1096 |
} |
| 1097 |
} |
| 1098 |
}) |
| 1099 |
} |
| 1100 |
|
| 1101 |
func LastIndex(t *testing.T, fn IndexFunc) { |
| 1102 |
reference := func(s, sep string) int { |
| 1103 |
return LastIndexRunesReference([]rune(s), []rune(sep)) |
| 1104 |
} |
| 1105 |
runIndexTests(t, reference, "LastIndexReference", lastIndexTests, false) |
| 1106 |
|
| 1107 |
runIndexTests(t, fn, "LastIndex", lastIndexTests, false) |
| 1108 |
} |
| 1109 |
|
| 1110 |
func LastIndexInvalid(t *testing.T, fn IndexFunc) { |
| 1111 |
var tests []indexTest |
| 1112 |
iterateInvalidSequenceTests(func(s0, s1 string) { |
| 1113 |
out := -1 |
| 1114 |
if strings.EqualFold(s0, s1) { |
| 1115 |
out = 0 |
| 1116 |
} |
| 1117 |
tests = append(tests, indexTest{ |
| 1118 |
s: s0, |
| 1119 |
sep: s1, |
| 1120 |
out: out, |
| 1121 |
}) |
| 1122 |
}) |
| 1123 |
|
| 1124 |
tt := &testWrapper{T: t} // Limit number of failures |
| 1125 |
|
| 1126 |
reference := func(s, sep string) int { |
| 1127 |
return LastIndexRunesReference([]rune(s), []rune(sep)) |
| 1128 |
} |
| 1129 |
runIndexTests(tt, reference, "LastIndexReference", tests, false) |
| 1130 |
if t.Failed() { |
| 1131 |
t.Fatal("invalid test cases") |
| 1132 |
} |
| 1133 |
|
| 1134 |
runIndexTests(tt, fn, "LastIndex", tests, false) |
| 1135 |
} |
| 1136 |
|
| 1137 |
type indexRuneTest struct { |
| 1138 |
in string |
| 1139 |
rune rune |
| 1140 |
want int |
| 1141 |
} |
| 1142 |
|
| 1143 |
var indexRuneTests = []indexRuneTest{ |
| 1144 |
{"", 'a', -1}, |
| 1145 |
{"", '☺', -1}, |
| 1146 |
{"foo", '☹', -1}, |
| 1147 |
{"foo", 'o', 1}, |
| 1148 |
{"foo☺bar", '☺', 3}, |
| 1149 |
{"foo☺☻☹bar", '☹', 9}, |
| 1150 |
{"a A x", 'A', 0}, |
| 1151 |
{"some_text=some_value", '=', 9}, |
| 1152 |
{"☺a", 'a', 3}, |
| 1153 |
{"a☻☺b", '☺', 4}, |
| 1154 |
{"abc𐀀", '𐀀', 3}, |
| 1155 |
|
| 1156 |
// RuneError should match any invalid UTF-8 byte sequence. |
| 1157 |
{"�", '�', 0}, |
| 1158 |
{"\xff", '�', 0}, |
| 1159 |
{"☻x�", '�', len("☻x")}, |
| 1160 |
{"☻x\xe2\x98", '�', len("☻x")}, |
| 1161 |
{"☻x\xe2\x98�", '�', len("☻x")}, |
| 1162 |
{"☻x\xe2\x98x", '�', len("☻x")}, |
| 1163 |
|
| 1164 |
// Invalid rune values should never match. |
| 1165 |
{"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", -1, -1}, |
| 1166 |
{"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", 0xD800, -1}, // Surrogate pair |
| 1167 |
{"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", utf8.MaxRune + 1, -1}, |
| 1168 |
|
| 1169 |
// Case-folding |
| 1170 |
{"Αβδ", 'α', 0}, // "ΑΒΔ" |
| 1171 |
{"αβδ", 'Α', 0}, // "" |
| 1172 |
{"αβδ", 'Δ', 4}, // "ΑΒΔ" |
| 1173 |
|
| 1174 |
// Case-folding with ASCII |
| 1175 |
{"K", 'K', 0}, // U+212A |
| 1176 |
{"S", 'ſ', 0}, // U+017F |
| 1177 |
{"K", 'k', 0}, // U+006B |
| 1178 |
{"ſ", 's', 0}, // U+0073 |
| 1179 |
{"İ", 'İ', 0}, // U+0130 |
| 1180 |
{"i", 'İ', -1}, // U+0130 |
| 1181 |
{"ſS*ք", 'S', 0}, |
| 1182 |
|
| 1183 |
// Test cutover when strings.IndexByte does not advance far |
| 1184 |
// enough. All the runes here have the same last byte when |
| 1185 |
// encoded as UTF-8. |
| 1186 |
{strings.Repeat("ā", 128) + "Á", 'Á', len("ā") * 128}, // 2 bytes per-rune |
| 1187 |
{strings.Repeat("ā", 128), 'Á', -1}, |
| 1188 |
{strings.Repeat("ᲅ", 128) + "ꙅ", 'ꙅ', len("ᲅ") * 128}, // 3 bytes per-rune |
| 1189 |
{strings.Repeat("ᲅ", 128), 'ꙅ', -1}, |
| 1190 |
{strings.Repeat("𥺻", 128) + "𥻻", '𥻻', len("𥺻") * 128}, // 4 bytes per-rune |
| 1191 |
{strings.Repeat("𥺻", 128), '𥻻', -1}, |
| 1192 |
|
| 1193 |
// Test the cutover to to bytealg.IndexString when it is triggered in |
| 1194 |
// the middle of rune that contains consecutive runs of equal bytes. |
| 1195 |
{"aaaaaKKKK\U000bc104", '\U000bc104', 17}, // cutover: (n + 16) / 8 |
| 1196 |
{"aaaaaKKKK鄄", '鄄', 17}, |
| 1197 |
{"aaKKKKKa\U000bc104", '\U000bc104', 18}, // cutover: 4 + n>>4 |
| 1198 |
{"aaKKKKKa鄄", '鄄', 18}, |
| 1199 |
|
| 1200 |
// Invalid rune |
| 1201 |
{"abc", utf8.RuneError, -1}, |
| 1202 |
} |
| 1203 |
|
| 1204 |
func IndexRune(t *testing.T, fn IndexRuneFunc) { |
| 1205 |
for _, tt := range indexRuneTests { |
| 1206 |
if got := fn(tt.in, tt.rune); got != tt.want { |
| 1207 |
t.Errorf("IndexRune(%q, %q) = %v; want %v", tt.in, tt.rune, got, tt.want) |
| 1208 |
} |
| 1209 |
} |
| 1210 |
} |
| 1211 |
|
| 1212 |
func IndexRuneCase(t *testing.T, fn IndexRuneFunc) { |
| 1213 |
tests := []indexRuneTest{ |
| 1214 |
{"", 'a', -1}, |
| 1215 |
{"", '☺', -1}, |
| 1216 |
{"foo", '☹', -1}, |
| 1217 |
{"foo", 'o', 1}, |
| 1218 |
{"foo☺bar", '☺', 3}, |
| 1219 |
{"foo☺☻☹bar", '☹', 9}, |
| 1220 |
{"a A x", 'A', 2}, |
| 1221 |
{"some_text=some_value", '=', 9}, |
| 1222 |
{"☺a", 'a', 3}, |
| 1223 |
{"a☻☺b", '☺', 4}, |
| 1224 |
|
| 1225 |
// RuneError should match any invalid UTF-8 byte sequence. |
| 1226 |
{"a", utf8.RuneError, -1}, |
| 1227 |
{"�", '�', 0}, |
| 1228 |
{"\xff", '�', 0}, |
| 1229 |
{"☻x�", '�', len("☻x")}, |
| 1230 |
{"☻x\xe2\x98", '�', len("☻x")}, |
| 1231 |
{"☻x\xe2\x98�", '�', len("☻x")}, |
| 1232 |
{"☻x\xe2\x98x", '�', len("☻x")}, |
| 1233 |
|
| 1234 |
// Invalid rune values should never match. |
| 1235 |
{"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", -1, -1}, |
| 1236 |
{"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", 0xD800, -1}, // Surrogate pair |
| 1237 |
{"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", utf8.MaxRune + 1, -1}, |
| 1238 |
|
| 1239 |
// Make sure IndexRune does not panic when the byte being searched |
| 1240 |
// for occurs at the end of the string. |
| 1241 |
{"abcÀ"[:len("abcÀ")-1], 'À', -1}, |
| 1242 |
{"abc本"[:len("abc本")-1], '本', -1}, |
| 1243 |
{"abc本"[:len("abc本")-2], '本', -1}, |
| 1244 |
{"abc𐀀"[:len("abc𐀀")-1], '𐀀', -1}, |
| 1245 |
{"abc𐀀"[:len("abc𐀀")-2], '𐀀', -1}, |
| 1246 |
{"abc𐀀"[:len("abc𐀀")-3], '𐀀', -1}, |
| 1247 |
} |
| 1248 |
for _, tt := range tests { |
| 1249 |
if got := fn(tt.in, tt.rune); got != tt.want { |
| 1250 |
t.Errorf("indexRuneCase(%q, %d) = %v; want %v", tt.in, tt.rune, got, tt.want) |
| 1251 |
} |
| 1252 |
} |
| 1253 |
} |
| 1254 |
|
| 1255 |
func ContainsRune(t *testing.T, fn func(s string, r rune) bool) { |
| 1256 |
for _, test := range indexRuneTests { |
| 1257 |
got := fn(test.in, test.rune) |
| 1258 |
want := test.want >= 0 |
| 1259 |
if got != want { |
| 1260 |
t.Errorf("ContainsRune(%q, 0x%04X) = %t; want: %t", test.in, test.rune, got, want) |
| 1261 |
} |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
func LastIndexRune(t *testing.T, fn IndexRuneFunc) { |
| 1266 |
tests := []struct { |
| 1267 |
in string |
| 1268 |
rune rune |
| 1269 |
want int |
| 1270 |
}{ |
| 1271 |
{"", 'a', -1}, |
| 1272 |
{"", '☺', -1}, |
| 1273 |
{"foo", '☹', -1}, |
| 1274 |
{"foo", 'o', 2}, |
| 1275 |
{"foo☺bar", '☺', 3}, |
| 1276 |
{"foo☺☻☹bar", '☹', 9}, |
| 1277 |
{"a A x", 'A', 2}, |
| 1278 |
{"some_text=some_value", '=', 9}, |
| 1279 |
{"☺a", 'a', 3}, |
| 1280 |
{"a☻☺b", '☺', 4}, |
| 1281 |
|
| 1282 |
// RuneError should match any invalid UTF-8 byte sequence. |
| 1283 |
{"�", '�', 0}, |
| 1284 |
{"\xff", '�', 0}, |
| 1285 |
{"☻x�", '�', len("☻x")}, |
| 1286 |
|
| 1287 |
// Invalid rune values should never match. |
| 1288 |
{"foo" + string(rune(utf8.RuneError)), utf8.RuneError, 3}, |
| 1289 |
{"foo" + string(rune(unicode.MaxRune+1)), unicode.MaxRune + 1, -1}, |
| 1290 |
{"foo" + string(utf8.RuneError), utf8.RuneError, 3}, |
| 1291 |
{"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", -1, -1}, |
| 1292 |
{"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", 0xD800, -1}, // Surrogate pair |
| 1293 |
{"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", utf8.MaxRune + 1, -1}, |
| 1294 |
|
| 1295 |
// Case-folding |
| 1296 |
{"Αβδ", 'α', 0}, // "ΑΒΔ" |
| 1297 |
{"αβδ", 'Α', 0}, // "ΑΒΔ" |
| 1298 |
{"αβδ", 'Δ', 4}, // "ΑΒΔ" |
| 1299 |
{"αβδ", 'Δ', 4}, // "ΑΒΔ" |
| 1300 |
{"abcßẞ", 'ß', len("abcß")}, |
| 1301 |
{"aΩωΩ", 'ω', len("aΩω")}, |
| 1302 |
{"Θθϑϴabc", 0x03D1, len("Θθϑ")}, |
| 1303 |
|
| 1304 |
// Case-folding with ASCII |
| 1305 |
{"K", 'K', 0}, // U+212A |
| 1306 |
{"S", 'ſ', 0}, // U+017F |
| 1307 |
{"K", 'k', 0}, // U+006B |
| 1308 |
{"ſ", 's', 0}, // U+0073 |
| 1309 |
{"İ", 'İ', 0}, // U+0130 |
| 1310 |
{"i", 'İ', -1}, // U+0130 |
| 1311 |
} |
| 1312 |
for _, tt := range tests { |
| 1313 |
if got := fn(tt.in, tt.rune); got != tt.want { |
| 1314 |
t.Errorf("lastIndexRune(%q, %q) = %v; want %v", tt.in, tt.rune, got, tt.want) |
| 1315 |
} |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
func IndexByte(t *testing.T, fn IndexByteFunc) { |
| 1320 |
tests := []struct { |
| 1321 |
in string |
| 1322 |
char byte |
| 1323 |
want int |
| 1324 |
}{ |
| 1325 |
// Case-folding with ASCII |
| 1326 |
{"", 0, -1}, |
| 1327 |
{"K", 'k', 0}, |
| 1328 |
{"K", 'K', 0}, |
| 1329 |
{"ſ", 's', 0}, |
| 1330 |
{"ſ", 'S', 0}, |
| 1331 |
{"sſ", 'S', 0}, |
| 1332 |
{"aKkK", 'k', 1}, |
| 1333 |
{"aſSs", 's', 1}, |
| 1334 |
} |
| 1335 |
for _, tt := range tests { |
| 1336 |
if got := fn(tt.in, tt.char); got != tt.want { |
| 1337 |
t.Errorf("IndexByte(%q, %q) = %v; want %v", tt.in, tt.char, got, tt.want) |
| 1338 |
} |
| 1339 |
} |
| 1340 |
} |
| 1341 |
|
| 1342 |
func LastIndexByte(t *testing.T, fn IndexByteFunc) { |
| 1343 |
tests := []struct { |
| 1344 |
in string |
| 1345 |
char byte |
| 1346 |
want int |
| 1347 |
}{ |
| 1348 |
{"", 'a', -1}, |
| 1349 |
{"1", '2', -1}, |
| 1350 |
{"abc", 'A', 0}, |
| 1351 |
{"abc", 'B', 1}, |
| 1352 |
{"abc", 'c', 2}, |
| 1353 |
{"abc", 'x', -1}, |
| 1354 |
|
| 1355 |
// Case-folding with ASCII |
| 1356 |
{"K", 'k', 0}, |
| 1357 |
{"K", 'K', 0}, |
| 1358 |
{"ſ", 's', 0}, |
| 1359 |
{"ſ", 'S', 0}, |
| 1360 |
{"x", 'S', -1}, |
| 1361 |
{"akK", 'k', len("ak")}, |
| 1362 |
{"aſSx", 's', len("aſ")}, |
| 1363 |
} |
| 1364 |
for _, tt := range tests { |
| 1365 |
if got := fn(tt.in, tt.char); got != tt.want { |
| 1366 |
t.Errorf("LastIndexByte(%q, %q) = %v; want %v", tt.in, tt.char, got, tt.want) |
| 1367 |
} |
| 1368 |
} |
| 1369 |
} |
| 1370 |
|
| 1371 |
func IndexNonASCII(t *testing.T, fn func(s string) int) { |
| 1372 |
index := func(s string) int { |
| 1373 |
for i, r := range s { |
| 1374 |
if r >= utf8.RuneSelf { |
| 1375 |
return i |
| 1376 |
} |
| 1377 |
} |
| 1378 |
return -1 |
| 1379 |
} |
| 1380 |
|
| 1381 |
t.Run("IndexTests", func(t *testing.T) { |
| 1382 |
tests := append([]indexTest(nil), indexTests...) |
| 1383 |
tests = append(tests, unicodeIndexTests...) |
| 1384 |
for _, test := range tests { |
| 1385 |
want := index(test.s) |
| 1386 |
got := fn(test.s) |
| 1387 |
if got != want { |
| 1388 |
t.Errorf("IndexNonASCII(%q) = %d; want: %d", test.s, got, want) |
| 1389 |
} |
| 1390 |
} |
| 1391 |
}) |
| 1392 |
|
| 1393 |
t.Run("LongString", func(t *testing.T) { |
| 1394 |
long := strings.Repeat("a", 4096) + "βaβa" |
| 1395 |
idx := index(long) |
| 1396 |
for i := 0; i < len(long); i++ { |
| 1397 |
s := long[i:] |
| 1398 |
want := idx - i |
| 1399 |
if want < 0 { |
| 1400 |
want = index(s) |
| 1401 |
} |
| 1402 |
got := fn(s) |
| 1403 |
if got != want { |
| 1404 |
t.Errorf("IndexNonASCII(long[%d:]) = %d; want: %d", i, got, want) |
| 1405 |
} |
| 1406 |
} |
| 1407 |
}) |
| 1408 |
} |
| 1409 |
|
| 1410 |
func ContainsNonASCII(t *testing.T, fn func(s string) bool) { |
| 1411 |
contains := func(s string) bool { |
| 1412 |
for i := 0; i < len(s); i++ { |
| 1413 |
if s[i] >= utf8.RuneSelf { |
| 1414 |
return true |
| 1415 |
} |
| 1416 |
} |
| 1417 |
return false |
| 1418 |
} |
| 1419 |
|
| 1420 |
tests := append([]indexTest(nil), indexTests...) |
| 1421 |
tests = append(tests, unicodeIndexTests...) |
| 1422 |
for _, test := range tests { |
| 1423 |
want := contains(test.s) |
| 1424 |
got := fn(test.s) |
| 1425 |
if got != want { |
| 1426 |
t.Errorf("ContainsNonASCII(%q) = %t; want: %t", test.s, got, want) |
| 1427 |
} |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
type prefixTest struct { |
| 1432 |
s, prefix string |
| 1433 |
out, exhausted bool |
| 1434 |
} |
| 1435 |
|
| 1436 |
var prefixTests = []prefixTest{ |
| 1437 |
{"", "", true, true}, |
| 1438 |
{"1", "", true, false}, |
| 1439 |
{"1", "2", false, true}, |
| 1440 |
{"foo", "f", true, false}, |
| 1441 |
{"αβδ", "ΑΒΔ", true, true}, |
| 1442 |
{"αβδΑΒΔ", "ΑΒΔ", true, false}, |
| 1443 |
{"abc", "xyz", false, false}, |
| 1444 |
{"abc", "XYZ", false, false}, |
| 1445 |
{"abc", "abc", true, true}, |
| 1446 |
{"abc", "abd", false, true}, |
| 1447 |
{"abcdefghijk", "abcdefghijX", false, true}, |
| 1448 |
{"abcdefghijk", "abcdefghij\u212A", true, true}, |
| 1449 |
{"abcdefghijk", "abcdefghij\u212Axyz", false, true}, |
| 1450 |
{"abcdefghijk☺", "abcdefghij\u212A", true, false}, |
| 1451 |
{"abcdefghijkz", "abcdefghij\u212Ay", false, true}, |
| 1452 |
{"abcdefghijKz", "abcdefghij\u212Ay", false, true}, |
| 1453 |
{"☺aβ", "☺aΔ", false, true}, |
| 1454 |
{"☺aβc", "☺aΔ", false, false}, |
| 1455 |
{"\u0250\u0250\u0250\u0250\u0250 a", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F A", true, true}, // grows one byte per char |
| 1456 |
{"a\u0250\u0250\u0250\u0250\u0250", "A\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F", true, true}, // |
| 1457 |
{"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D a", "\u0251\u0251\u0251\u0251\u0251 A", true, true}, // shrinks one byte per char |
| 1458 |
{"a\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "A\u0251\u0251\u0251\u0251\u0251", true, true}, // shrinks one byte per char |
| 1459 |
|
| 1460 |
// Handle large differences in encoded size ([kK]: 1 vs. 'K' (U+212A): 3 bytes). |
| 1461 |
{strings.Repeat("\u212a", 8), strings.Repeat("k", 8), true, true}, |
| 1462 |
{strings.Repeat("k", 8), strings.Repeat("\u212a", 8), true, true}, |
| 1463 |
{"k-k", "\u212a-\u212a", true, true}, |
| 1464 |
|
| 1465 |
{"a", "bbb", false, true}, |
| 1466 |
{"\u212a", strings.Repeat("a", len("\u212a")*2), false, true}, |
| 1467 |
{"\u212a", strings.Repeat("a", len("\u212a")*3), false, true}, |
| 1468 |
{"\u212a", strings.Repeat("a", len("\u212a")*4), false, true}, |
| 1469 |
} |
| 1470 |
|
| 1471 |
func HasPrefix(t *testing.T, fn PrefixFunc) { |
| 1472 |
// Make sure the tests cases are valid |
| 1473 |
for i, test := range prefixTests { |
| 1474 |
s := []rune(test.s) |
| 1475 |
prefix := []rune(test.prefix) |
| 1476 |
out, exhausted := HasPrefixRunes(s, prefix) |
| 1477 |
if out != test.out || exhausted != test.exhausted { |
| 1478 |
t.Errorf("invalid test: %d: %+v", i, test) |
| 1479 |
} |
| 1480 |
if n := len(prefix); len(s) >= n { |
| 1481 |
if out := strings.EqualFold(string(s[:n]), string(prefix)); out != test.out { |
| 1482 |
t.Errorf("strings.EqualFold(%q, %q) = %t; want: %t", |
| 1483 |
test.s, test.prefix, out, test.out) |
| 1484 |
} |
| 1485 |
} |
| 1486 |
} |
| 1487 |
if t.Failed() { |
| 1488 |
t.Fatal("Invalid tests cases") |
| 1489 |
} |
| 1490 |
|
| 1491 |
for _, test := range prefixTests { |
| 1492 |
out, exhausted := fn(test.s, test.prefix) |
| 1493 |
if out != test.out || exhausted != test.exhausted { |
| 1494 |
t.Errorf("hasPrefixUnicode(%q, %q) = %t, %t; want: %t, %t", test.s, test.prefix, |
| 1495 |
out, exhausted, test.out, test.exhausted) |
| 1496 |
t.Error("s: ", len(test.s), utf8.RuneCountInString(test.s)) |
| 1497 |
t.Error("prefix:", len(test.prefix), utf8.RuneCountInString(test.prefix)) |
| 1498 |
} |
| 1499 |
} |
| 1500 |
|
| 1501 |
// Invalid |
| 1502 |
iterateInvalidSequenceTests(func(s, prefix string) { |
| 1503 |
got, _ := fn(s, prefix) |
| 1504 |
want := strings.EqualFold(s, prefix) |
| 1505 |
if got != want { |
| 1506 |
t.Errorf("HasPrefix(%q, %q) = %t; want: %t", s, prefix, got, want) |
| 1507 |
} |
| 1508 |
}) |
| 1509 |
} |
| 1510 |
|
| 1511 |
func TrimPrefix(t *testing.T, fn TrimFunc) { |
| 1512 |
for i, test := range prefixTests { |
| 1513 |
want := test.s |
| 1514 |
if test.out { |
| 1515 |
s := []rune(test.s) |
| 1516 |
prefix := []rune(test.prefix) |
| 1517 |
if len(prefix) <= len(s) { |
| 1518 |
want = string(s[len(prefix):]) |
| 1519 |
} |
| 1520 |
} |
| 1521 |
got := fn(test.s, test.prefix) |
| 1522 |
if got != want { |
| 1523 |
t.Errorf("%d: TrimPrefix(%q, %q) = %q; want: %q", |
| 1524 |
i, test.s, test.prefix, got, want) |
| 1525 |
} |
| 1526 |
} |
| 1527 |
} |
| 1528 |
|
| 1529 |
type suffixTest struct { |
| 1530 |
s, suffix string |
| 1531 |
out bool |
| 1532 |
} |
| 1533 |
|
| 1534 |
var suffixTests = []suffixTest{ |
| 1535 |
{"", "", true}, |
| 1536 |
{"a", "", true}, |
| 1537 |
{"", "a", false}, |
| 1538 |
{"1", "2", false}, |
| 1539 |
{"αβδ", "ΑΒΔ", true}, |
| 1540 |
{"αβδΑΒΔ", "ΑΒΔ", true}, |
| 1541 |
{"abc", "xyz", false}, |
| 1542 |
{"abc", "XYZ", false}, |
| 1543 |
{"abc", "abc", true}, |
| 1544 |
{"abc", "abd", false}, |
| 1545 |
{"aaβ", "☺aβ", false}, |
| 1546 |
{"☺aβc", "☺aΔ", false}, |
| 1547 |
|
| 1548 |
{"\u0250\u0250\u0250\u0250\u0250 a", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F A", true}, // grows one byte per char |
| 1549 |
{"a\u0250\u0250\u0250\u0250\u0250", "A\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F", true}, // |
| 1550 |
{"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D a", "\u0251\u0251\u0251\u0251\u0251 A", true}, // shrinks one byte per char |
| 1551 |
{"a\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "A\u0251\u0251\u0251\u0251\u0251", true}, // shrinks one byte per char |
| 1552 |
|
| 1553 |
// Handle large differences in encoded size ([kK]: 1 vs. 'K' (U+212A): 3 bytes). |
| 1554 |
{strings.Repeat("\u212a", 8), strings.Repeat("k", 8), true}, |
| 1555 |
{strings.Repeat("k", 8), strings.Repeat("\u212a", 8), true}, |
| 1556 |
{"k-k", "\u212a-\u212a", true}, |
| 1557 |
|
| 1558 |
{"g^Y3i", "I", true}, |
| 1559 |
{"G|S&>;C", "&>;C", true}, |
| 1560 |
} |
| 1561 |
|
| 1562 |
func HasSuffix(t *testing.T, fn func(s, suffix string) bool) { |
| 1563 |
// Make sure the tests cases are valid |
| 1564 |
for _, test := range suffixTests { |
| 1565 |
out := regexp.MustCompile(`(?i)` + regexp.QuoteMeta(test.suffix) + "$").MatchString(test.s) |
| 1566 |
if out != test.out { |
| 1567 |
t.Errorf("Invalid test s: %q, suffix: %q got: %t want: %t", test.s, test.suffix, out, test.out) |
| 1568 |
} |
| 1569 |
} |
| 1570 |
if t.Failed() { |
| 1571 |
t.Fatal("Invalid tests cases") |
| 1572 |
} |
| 1573 |
|
| 1574 |
for _, test := range suffixTests { |
| 1575 |
out := fn(test.s, test.suffix) |
| 1576 |
if out != test.out { |
| 1577 |
t.Errorf("HasSuffix(%q, %q) = %t; want: %t", test.s, test.suffix, out, test.out) |
| 1578 |
} |
| 1579 |
} |
| 1580 |
|
| 1581 |
// Invalid |
| 1582 |
iterateInvalidSequenceTests(func(s, suffix string) { |
| 1583 |
got := fn(s, suffix) |
| 1584 |
want := strings.EqualFold(s, suffix) |
| 1585 |
if got != want { |
| 1586 |
t.Errorf("HasSuffix(%q, %q) = %t; want: %t", s, suffix, got, want) |
| 1587 |
} |
| 1588 |
}) |
| 1589 |
} |
| 1590 |
|
| 1591 |
func TrimSuffix(t *testing.T, fn TrimFunc) { |
| 1592 |
for i, test := range suffixTests { |
| 1593 |
hasSuffix := test.out && test.suffix != "" |
| 1594 |
want := test.s |
| 1595 |
if hasSuffix { |
| 1596 |
s := []rune(test.s) |
| 1597 |
suffix := []rune(test.suffix) |
| 1598 |
if len(s) >= len(suffix) { |
| 1599 |
want = string(s[:len(s)-len(suffix)]) |
| 1600 |
} |
| 1601 |
} |
| 1602 |
got := fn(test.s, test.suffix) |
| 1603 |
if got != want { |
| 1604 |
t.Errorf("%d: TrimSuffix(%q, %q) = %q; want: %q", |
| 1605 |
i, test.s, test.suffix, got, want) |
| 1606 |
} |
| 1607 |
} |
| 1608 |
} |
| 1609 |
|
| 1610 |
var countTests = []struct { |
| 1611 |
s, sep string |
| 1612 |
num int |
| 1613 |
}{ |
| 1614 |
{"", "", 1}, |
| 1615 |
{"", "notempty", 0}, |
| 1616 |
{"notempty", "", 9}, |
| 1617 |
{"smaller", "not smaller", 0}, |
| 1618 |
{"12345678987654321", "6", 2}, |
| 1619 |
{"611161116", "6", 3}, |
| 1620 |
{"notequal", "NotEqual", 1}, |
| 1621 |
{"equal", "equal", 1}, |
| 1622 |
{"abc1231231123q", "123", 3}, |
| 1623 |
{"11111", "11", 2}, |
| 1624 |
{"aAaAa", "a", 5}, |
| 1625 |
{"a\u212akKa", "K", 3}, |
| 1626 |
{"a\u212akKa", "S", 0}, |
| 1627 |
{"a\u212a", "a\u212a", 1}, |
| 1628 |
{"a\u212aa\u212a", "a\u212a", 2}, |
| 1629 |
{"sſS", "s", 3}, |
| 1630 |
{strings.Repeat("\u212a", 8), "kk", 4}, |
| 1631 |
{strings.Repeat("k", 8), "\u212a\u212a", 4}, |
| 1632 |
{strings.Repeat("\u212a", 32), "kk", 16}, |
| 1633 |
{strings.Repeat("k", 32), "\u212a\u212a", 16}, |
| 1634 |
} |
| 1635 |
|
| 1636 |
func Count(t *testing.T, fn IndexFunc) { |
| 1637 |
for _, tt := range countTests { |
| 1638 |
if num := fn(tt.s, tt.sep); num != tt.num { |
| 1639 |
t.Errorf("Count(%q, %q) = %d, want %d", tt.s, tt.sep, num, tt.num) |
| 1640 |
} |
| 1641 |
} |
| 1642 |
} |
| 1643 |
|
| 1644 |
var indexAnyTests = []indexTest{ |
| 1645 |
{"", "", -1}, |
| 1646 |
{"", "a", -1}, |
| 1647 |
{"", "abc", -1}, |
| 1648 |
{"a", "", -1}, |
| 1649 |
{"a", "a", 0}, |
| 1650 |
{"\x80", "\xffb", 0}, |
| 1651 |
{"aaa", "a", 0}, |
| 1652 |
{"abc", "xyz", -1}, |
| 1653 |
{"abc", "xcz", 2}, |
| 1654 |
{"abc", "XCZ", 2}, |
| 1655 |
{"abcdefghijklmnop", "xyz", -1}, |
| 1656 |
{"ab☺c", "x☺yz", 2}, |
| 1657 |
{"a☺b☻c☹d", "cx", len("a☺b☻")}, |
| 1658 |
{"a☺b☻c☹d", "uvw☻xyz", len("a☺b")}, |
| 1659 |
{"aRegExp*", ".(|)*+?^$[]", 7}, |
| 1660 |
{dots + dots + dots, " ", -1}, |
| 1661 |
{dots + dots + dots + "a", "A", len(dots + dots + dots)}, |
| 1662 |
{dots + dots + dots + "\u212a", "k", len(dots + dots + dots)}, |
| 1663 |
{dots + dots + dots + "a", "Z", -1}, |
| 1664 |
{"012abcba210", "\xffb", 4}, |
| 1665 |
{"012\x80bcb\x80210", "\xffb", 3}, |
| 1666 |
{"0123456\xcf\x80abc", "\xcfb\x80", 10}, |
| 1667 |
{"a☺b☻c☹d", "☺"[:1], -1}, |
| 1668 |
|
| 1669 |
// ASCII chars that are equal to multi-byte runes |
| 1670 |
{"\u212A" + strings.Repeat("x", 16), "k", 0}, |
| 1671 |
{strings.Repeat("k", 16), "\u212A", 0}, |
| 1672 |
{"\u017F" + strings.Repeat("x", 16), "s", 0}, |
| 1673 |
{strings.Repeat("s", 16), "\u017F", 0}, |
| 1674 |
} |
| 1675 |
|
| 1676 |
var lastIndexAnyTests = []indexTest{ |
| 1677 |
{"", "", -1}, |
| 1678 |
{"", "a", -1}, |
| 1679 |
{"", "abc", -1}, |
| 1680 |
{"a", "", -1}, |
| 1681 |
{"a", "b", -1}, |
| 1682 |
{"a", "a", 0}, |
| 1683 |
{"\x80", "\xffb", 0}, |
| 1684 |
{"aaa", "a", 2}, |
| 1685 |
{"abc", "xyz", -1}, |
| 1686 |
{"abc", "ab", 1}, |
| 1687 |
{"ab☺c", "x☺yz", 2}, |
| 1688 |
{"a☺b☻c☹d", "cx", len("a☺b☻")}, |
| 1689 |
{"a☺b☻c☹d", "uvw☻xyz", len("a☺b")}, |
| 1690 |
{"a.RegExp*", ".(|)*+?^$[]", 8}, |
| 1691 |
{dots + dots + dots, " ", -1}, |
| 1692 |
{"012abcba210", "\xffb", 6}, |
| 1693 |
{"012\x80bcb\x80210", "\xffb", 7}, |
| 1694 |
{"0123456\xcf\x80abc", "\xcfb\x80", 10}, |
| 1695 |
|
| 1696 |
// Case-insensitive |
| 1697 |
{"a", "A", 0}, |
| 1698 |
{"a☺b☻c☹d", "CX", len("a☺b☻")}, |
| 1699 |
{"a☺b☻c☹d", "UVW☻XYZ", len("a☺b")}, |
| 1700 |
{"kkk", "\u212a", 2}, |
| 1701 |
{"☹", "☹"[:1], -1}, |
| 1702 |
{"abc" + "☹"[:1], "☹"[:1], len("abc")}, |
| 1703 |
|
| 1704 |
// ASCII chars that are equal to multi-byte runes |
| 1705 |
{"\u212A" + strings.Repeat("x", 16), "k", 0}, |
| 1706 |
{strings.Repeat("k", 16), "\u212A", 15}, |
| 1707 |
{"\u017F" + strings.Repeat("x", 16), "s", 0}, |
| 1708 |
{strings.Repeat("s", 16), "\u017F", 15}, |
| 1709 |
} |
| 1710 |
|
| 1711 |
func IndexAny(t *testing.T, fn IndexFunc) { |
| 1712 |
runIndexTests(t, fn, "IndexAny", indexAnyTests, false) |
| 1713 |
} |
| 1714 |
|
| 1715 |
func LastIndexAny(t *testing.T, fn IndexFunc) { |
| 1716 |
runIndexTests(t, fn, "LastIndexAny", lastIndexAnyTests, false) |
| 1717 |
} |
| 1718 |
|
| 1719 |
var cutTests = []struct { |
| 1720 |
s, sep string |
| 1721 |
before, after string |
| 1722 |
found bool |
| 1723 |
}{ |
| 1724 |
{"abc", "b", "a", "c", true}, |
| 1725 |
{"abc", "a", "", "bc", true}, |
| 1726 |
{"abc", "c", "ab", "", true}, |
| 1727 |
{"abc", "abc", "", "", true}, |
| 1728 |
{"abc", "", "", "abc", true}, |
| 1729 |
{"abc", "d", "abc", "", false}, |
| 1730 |
{"", "d", "", "", false}, |
| 1731 |
{"", "", "", "", true}, |
| 1732 |
|
| 1733 |
// Unicode |
| 1734 |
{"αβδ", "ΑΒΔ", "", "", true}, |
| 1735 |
{"αβδΑΒΔ", "ΑΒΔ", "", "ΑΒΔ", true}, |
| 1736 |
{"123αβδ456", "ΑΒΔ", "123", "456", true}, |
| 1737 |
{"\u212aZZZ\u212aABC", "ZKA", "\u212aZZ", "BC", true}, |
| 1738 |
|
| 1739 |
// TODO: test invalid UTF-8 sequences |
| 1740 |
// |
| 1741 |
// {"\xed\xa0\x80", string(utf8.RuneError), "", "", true}, |
| 1742 |
// {"\xed\xa0\x80", string(utf8.RuneError), string(utf8.RuneError), "\xa0\x80", true}, |
| 1743 |
// {string(utf8.RuneError), "\xed\xa0\x80", "", "", true}, |
| 1744 |
} |
| 1745 |
|
| 1746 |
func Cut(t *testing.T, fn func(s, sep string) (before, after string, found bool)) { |
| 1747 |
for _, tt := range cutTests { |
| 1748 |
before, after, found := fn(tt.s, tt.sep) |
| 1749 |
if before != tt.before || after != tt.after || found != tt.found { |
| 1750 |
t.Errorf("Cut(%q, %q) = %q, %q, %v; want: %q, %q, %v", |
| 1751 |
tt.s, tt.sep, before, after, found, tt.before, tt.after, tt.found) |
| 1752 |
} |
| 1753 |
} |
| 1754 |
} |
| 1755 |
|
| 1756 |
var cutPrefixTests = []struct { |
| 1757 |
s, sep string |
| 1758 |
after string |
| 1759 |
found bool |
| 1760 |
}{ |
| 1761 |
{"abc", "a", "bc", true}, |
| 1762 |
{"abc", "abc", "", true}, |
| 1763 |
{"abc", "", "abc", true}, |
| 1764 |
{"abc", "d", "abc", false}, |
| 1765 |
{"", "d", "", false}, |
| 1766 |
{"", "", "", true}, |
| 1767 |
|
| 1768 |
// Unicode |
| 1769 |
{"αβδ", "ΑΒΔ", "", true}, |
| 1770 |
{"αβδΑΒΔ", "ΑΒΔ", "ΑΒΔ", true}, |
| 1771 |
{"123αβδ456", "ΑΒΔ", "123αβδ456", false}, |
| 1772 |
{"kk123", "\u212a\u212a123", "", true}, |
| 1773 |
{"kk123xyz", "\u212a\u212a123", "xyz", true}, |
| 1774 |
{"\u212a\u212a123xyz", "kK123", "xyz", true}, |
| 1775 |
} |
| 1776 |
|
| 1777 |
func CutPrefix(t *testing.T, fn func(s, prefix string) (after string, found bool)) { |
| 1778 |
for _, tt := range cutPrefixTests { |
| 1779 |
after, found := fn(tt.s, tt.sep) |
| 1780 |
if after != tt.after || found != tt.found { |
| 1781 |
t.Errorf("CutPrefix(%q, %q) = %q, %v, want %q, %v", |
| 1782 |
tt.s, tt.sep, after, found, tt.after, tt.found) |
| 1783 |
} |
| 1784 |
} |
| 1785 |
} |
| 1786 |
|
| 1787 |
var cutSuffixTests = []struct { |
| 1788 |
s, sep string |
| 1789 |
after string |
| 1790 |
found bool |
| 1791 |
}{ |
| 1792 |
{"abc", "bc", "a", true}, |
| 1793 |
{"abc", "abc", "", true}, |
| 1794 |
{"abc", "", "abc", true}, |
| 1795 |
{"abc", "d", "abc", false}, |
| 1796 |
{"", "d", "", false}, |
| 1797 |
{"", "", "", true}, |
| 1798 |
|
| 1799 |
// Unicode |
| 1800 |
{"αβδ", "ΑΒΔ", "", true}, |
| 1801 |
{"αβδΑΒΔ", "ΑΒΔ", "αβδ", true}, |
| 1802 |
{"123αβδ456", "ΑΒΔ", "123αβδ456", false}, |
| 1803 |
{"kk123", "\u212a\u212a123", "", true}, |
| 1804 |
{"xyzkK123", "\u212a\u212a123", "xyz", true}, |
| 1805 |
} |
| 1806 |
|
| 1807 |
func CutSuffix(t *testing.T, fn func(s, prefix string) (before string, found bool)) { |
| 1808 |
for _, tt := range cutSuffixTests { |
| 1809 |
after, found := fn(tt.s, tt.sep) |
| 1810 |
if after != tt.after || found != tt.found { |
| 1811 |
t.Errorf("CutSuffix(%q, %q) = %q, %v, want %q, %v", |
| 1812 |
tt.s, tt.sep, after, found, tt.after, tt.found) |
| 1813 |
} |
| 1814 |
} |
| 1815 |
} |
| 1816 |
|
| 1817 |
// Ensure that strings.EqualFold does not match 'İ' (U+0130) and ASCII 'i' or 'I'. |
| 1818 |
// This is mostly a sanity check. |
| 1819 |
func LatinCapitalLetterIWithDotAbove(t *testing.T, fn IndexFunc) { |
| 1820 |
if strings.EqualFold("İ", "i") { |
| 1821 |
t.Errorf("strings.EqualFold(%q, %q) = true; want: false", "İ", "i") |
| 1822 |
} |
| 1823 |
if strings.EqualFold("İ", "I") { |
| 1824 |
t.Errorf("strings.EqualFold(%q, %q) = true; want: false", "İ", "I") |
| 1825 |
} |
| 1826 |
if fn("İ", "i") == 0 { |
| 1827 |
t.Errorf("Compare(%q, %q) = true; want: false", "İ", "i") |
| 1828 |
} |
| 1829 |
if fn("İ", "I") == 0 { |
| 1830 |
t.Errorf("Compare(%q, %q) = true; want: false", "İ", "I") |
| 1831 |
} |
| 1832 |
} |
| 1833 |
|
| 1834 |
func NonLetterASCII(t *testing.T, fn func(s string) bool) { |
| 1835 |
tests := []struct { |
| 1836 |
s string |
| 1837 |
want bool |
| 1838 |
}{ |
| 1839 |
{"", true}, |
| 1840 |
{"1234", true}, |
| 1841 |
{"1a", false}, |
| 1842 |
{"1A", false}, |
| 1843 |
} |
| 1844 |
for _, test := range tests { |
| 1845 |
got := fn(test.s) |
| 1846 |
if got != test.want { |
| 1847 |
t.Errorf("nonLetterASCII(%q) = %t; want: %t", test.s, got, test.want) |
| 1848 |
} |
| 1849 |
} |
| 1850 |
} |
| 1851 |
|
| 1852 |
//////////////////////////////////////////////////////////// |
| 1853 |
// Helper functions |
| 1854 |
|
| 1855 |
// EqualRune returns if runes sr and tr are equivalent under |
| 1856 |
// Unicode-defined simple case folding. |
| 1857 |
func EqualRune(sr, tr rune) bool { |
| 1858 |
if sr == tr { |
| 1859 |
return true |
| 1860 |
} |
| 1861 |
// Invalid runes are encoded as RuneError |
| 1862 |
if (sr == utf8.RuneError || !utf8.ValidRune(sr)) && |
| 1863 |
(tr == utf8.RuneError || !utf8.ValidRune(tr)) { |
| 1864 |
return true |
| 1865 |
} |
| 1866 |
if tr < sr { |
| 1867 |
tr, sr = sr, tr |
| 1868 |
} |
| 1869 |
if tr < utf8.RuneSelf { |
| 1870 |
return 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' |
| 1871 |
} |
| 1872 |
return tables.CaseFold(sr) == tables.CaseFold(tr) |
| 1873 |
} |
| 1874 |
|
| 1875 |
// EqualRuneSlice returns if rune slices s1 and s2 are equivalent under |
| 1876 |
// Unicode-defined simple case folding. |
| 1877 |
func EqualRuneSlice(s1, s2 []rune) bool { |
| 1878 |
if len(s1) != len(s2) { |
| 1879 |
return false |
| 1880 |
} |
| 1881 |
for i, r := range s1 { |
| 1882 |
if r != s2[i] && !EqualRune(r, s2[i]) { |
| 1883 |
return false |
| 1884 |
} |
| 1885 |
} |
| 1886 |
return true |
| 1887 |
} |
| 1888 |
|
| 1889 |
// IndexRunesReference is a slow, but accurate case-insensitive version |
| 1890 |
// of strings.Index. |
| 1891 |
func IndexRunesReference(s, sep []rune) int { |
| 1892 |
n := len(sep) |
| 1893 |
for i := n; i <= len(s); i++ { |
| 1894 |
if EqualRuneSlice(s[i-n:i], sep) { |
| 1895 |
return EncodedLen(s[:i-n]) |
| 1896 |
} |
| 1897 |
} |
| 1898 |
return -1 |
| 1899 |
} |
| 1900 |
|
| 1901 |
// TODO: consider renaming |
| 1902 |
func hasPrefixRunes(s, prefix []rune) (bool, bool) { |
| 1903 |
if len(s) < len(prefix) { |
| 1904 |
return false, true |
| 1905 |
} |
| 1906 |
var i int |
| 1907 |
for i = 0; i < len(prefix); i++ { |
| 1908 |
if !EqualRune(s[i], prefix[i]) { |
| 1909 |
return false, i == len(s)-1 |
| 1910 |
} |
| 1911 |
} |
| 1912 |
return i == len(prefix), i == len(s) |
| 1913 |
} |
| 1914 |
|
| 1915 |
// Very slow, but accurate. |
| 1916 |
func indexRegex(s, sep string) int { |
| 1917 |
a := regexp.MustCompile(`(?i)` + regexp.QuoteMeta(sep)).FindStringIndex(s) |
| 1918 |
if len(a) == 2 { |
| 1919 |
return a[0] |
| 1920 |
} |
| 1921 |
return -1 |
| 1922 |
} |
| 1923 |
|
| 1924 |
// Very slow, but accurate. |
| 1925 |
func lastIndexRegex(s, sep string) int { |
| 1926 |
a := regexp.MustCompile(`(?i)`+regexp.QuoteMeta(sep)).FindAllStringIndex(s, -1) |
| 1927 |
if len(a) > 0 { |
| 1928 |
return a[len(a)-1][0] |
| 1929 |
} |
| 1930 |
return -1 |
| 1931 |
} |
| 1932 |
|
| 1933 |
func hasSuffixRegex(s, suffix string) bool { |
| 1934 |
return regexp.MustCompile(`(?i)(` + regexp.QuoteMeta(suffix) + `)$`).MatchString(s) |
| 1935 |
} |
| 1936 |
|
| 1937 |
// EncodedLen returns the UTF-8 encoded length of rune slice rs. |
| 1938 |
func EncodedLen(rs []rune) int { |
| 1939 |
i := 0 |
| 1940 |
for _, r := range rs { |
| 1941 |
n := utf8.RuneLen(r) |
| 1942 |
if n < 0 { |
| 1943 |
n = 3 // Invalid rune (add 3 to encode RuneError) |
| 1944 |
} |
| 1945 |
i += n |
| 1946 |
} |
| 1947 |
return i |
| 1948 |
} |
| 1949 |
|
| 1950 |
// LastIndexRunesReference is a slow, but accurate case-insensitive |
| 1951 |
// version of strings.Index |
| 1952 |
func LastIndexRunesReference(s, sep []rune) int { |
| 1953 |
n := len(sep) |
| 1954 |
for i := len(s) - n; i >= 0; i-- { |
| 1955 |
if EqualRuneSlice(s[i:i+n], sep) { |
| 1956 |
return EncodedLen(s[:i]) |
| 1957 |
} |
| 1958 |
} |
| 1959 |
return -1 |
| 1960 |
} |
| 1961 |
|
| 1962 |
func HasPrefixRunes(s, prefix []rune) (bool, bool) { |
| 1963 |
if len(s) < len(prefix) { |
| 1964 |
return false, true |
| 1965 |
} |
| 1966 |
var i int |
| 1967 |
for i = 0; i < len(prefix); i++ { |
| 1968 |
sr := tables.CaseFold(s[i]) |
| 1969 |
pr := tables.CaseFold(prefix[i]) |
| 1970 |
if !utf8.ValidRune(sr) { |
| 1971 |
sr = utf8.RuneError |
| 1972 |
} |
| 1973 |
if !utf8.ValidRune(pr) { |
| 1974 |
pr = utf8.RuneError |
| 1975 |
} |
| 1976 |
if sr == pr { |
| 1977 |
continue |
| 1978 |
} |
| 1979 |
return false, i == len(s)-1 |
| 1980 |
} |
| 1981 |
return i == len(prefix), i == len(s) |
| 1982 |
} |