github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/src/strings/strings.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package strings implements simple functions to manipulate UTF-8 encoded strings. 6 // 7 // For information about UTF-8 strings in Go, see https://blog.golang.org/strings. 8 package strings 9 10 import ( 11 "unicode" 12 "unicode/utf8" 13 ) 14 15 // explode splits s into a slice of UTF-8 strings, 16 // one string per Unicode character up to a maximum of n (n < 0 means no limit). 17 // Invalid UTF-8 sequences become correct encodings of U+FFFD. 18 func explode(s string, n int) []string { 19 l := utf8.RuneCountInString(s) 20 if n < 0 || n > l { 21 n = l 22 } 23 a := make([]string, n) 24 for i := 0; i < n-1; i++ { 25 ch, size := utf8.DecodeRuneInString(s) 26 a[i] = s[:size] 27 s = s[size:] 28 if ch == utf8.RuneError { 29 a[i] = string(utf8.RuneError) 30 } 31 } 32 if n > 0 { 33 a[n-1] = s 34 } 35 return a 36 } 37 38 // primeRK is the prime base used in Rabin-Karp algorithm. 39 const primeRK = 16777619 40 41 // hashStr returns the hash and the appropriate multiplicative 42 // factor for use in Rabin-Karp algorithm. 43 func hashStr(sep string) (uint32, uint32) { 44 hash := uint32(0) 45 for i := 0; i < len(sep); i++ { 46 hash = hash*primeRK + uint32(sep[i]) 47 } 48 var pow, sq uint32 = 1, primeRK 49 for i := len(sep); i > 0; i >>= 1 { 50 if i&1 != 0 { 51 pow *= sq 52 } 53 sq *= sq 54 } 55 return hash, pow 56 } 57 58 // hashStrRev returns the hash of the reverse of sep and the 59 // appropriate multiplicative factor for use in Rabin-Karp algorithm. 60 func hashStrRev(sep string) (uint32, uint32) { 61 hash := uint32(0) 62 for i := len(sep) - 1; i >= 0; i-- { 63 hash = hash*primeRK + uint32(sep[i]) 64 } 65 var pow, sq uint32 = 1, primeRK 66 for i := len(sep); i > 0; i >>= 1 { 67 if i&1 != 0 { 68 pow *= sq 69 } 70 sq *= sq 71 } 72 return hash, pow 73 } 74 75 // countGeneric implements Count. 76 func countGeneric(s, substr string) int { 77 // special case 78 if len(substr) == 0 { 79 return utf8.RuneCountInString(s) + 1 80 } 81 n := 0 82 for { 83 i := Index(s, substr) 84 if i == -1 { 85 return n 86 } 87 n++ 88 s = s[i+len(substr):] 89 } 90 } 91 92 // Contains reports whether substr is within s. 93 func Contains(s, substr string) bool { 94 return Index(s, substr) >= 0 95 } 96 97 // ContainsAny reports whether any Unicode code points in chars are within s. 98 func ContainsAny(s, chars string) bool { 99 return IndexAny(s, chars) >= 0 100 } 101 102 // ContainsRune reports whether the Unicode code point r is within s. 103 func ContainsRune(s string, r rune) bool { 104 return IndexRune(s, r) >= 0 105 } 106 107 // LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s. 108 func LastIndex(s, substr string) int { 109 n := len(substr) 110 switch { 111 case n == 0: 112 return len(s) 113 case n == 1: 114 return LastIndexByte(s, substr[0]) 115 case n == len(s): 116 if substr == s { 117 return 0 118 } 119 return -1 120 case n > len(s): 121 return -1 122 } 123 // Rabin-Karp search from the end of the string 124 hashss, pow := hashStrRev(substr) 125 last := len(s) - n 126 var h uint32 127 for i := len(s) - 1; i >= last; i-- { 128 h = h*primeRK + uint32(s[i]) 129 } 130 if h == hashss && s[last:] == substr { 131 return last 132 } 133 for i := last - 1; i >= 0; i-- { 134 h *= primeRK 135 h += uint32(s[i]) 136 h -= pow * uint32(s[i+n]) 137 if h == hashss && s[i:i+n] == substr { 138 return i 139 } 140 } 141 return -1 142 } 143 144 // IndexRune returns the index of the first instance of the Unicode code point 145 // r, or -1 if rune is not present in s. 146 // If r is utf8.RuneError, it returns the first instance of any 147 // invalid UTF-8 byte sequence. 148 func IndexRune(s string, r rune) int { 149 switch { 150 case 0 <= r && r < utf8.RuneSelf: 151 return IndexByte(s, byte(r)) 152 case r == utf8.RuneError: 153 for i, r := range s { 154 if r == utf8.RuneError { 155 return i 156 } 157 } 158 return -1 159 case !utf8.ValidRune(r): 160 return -1 161 default: 162 return Index(s, string(r)) 163 } 164 } 165 166 // IndexAny returns the index of the first instance of any Unicode code point 167 // from chars in s, or -1 if no Unicode code point from chars is present in s. 168 func IndexAny(s, chars string) int { 169 if len(chars) > 0 { 170 if len(s) > 8 { 171 if as, isASCII := makeASCIISet(chars); isASCII { 172 for i := 0; i < len(s); i++ { 173 if as.contains(s[i]) { 174 return i 175 } 176 } 177 return -1 178 } 179 } 180 for i, c := range s { 181 for _, m := range chars { 182 if c == m { 183 return i 184 } 185 } 186 } 187 } 188 return -1 189 } 190 191 // LastIndexAny returns the index of the last instance of any Unicode code 192 // point from chars in s, or -1 if no Unicode code point from chars is 193 // present in s. 194 func LastIndexAny(s, chars string) int { 195 if len(chars) > 0 { 196 if len(s) > 8 { 197 if as, isASCII := makeASCIISet(chars); isASCII { 198 for i := len(s) - 1; i >= 0; i-- { 199 if as.contains(s[i]) { 200 return i 201 } 202 } 203 return -1 204 } 205 } 206 for i := len(s); i > 0; { 207 r, size := utf8.DecodeLastRuneInString(s[:i]) 208 i -= size 209 for _, c := range chars { 210 if r == c { 211 return i 212 } 213 } 214 } 215 } 216 return -1 217 } 218 219 // LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s. 220 func LastIndexByte(s string, c byte) int { 221 for i := len(s) - 1; i >= 0; i-- { 222 if s[i] == c { 223 return i 224 } 225 } 226 return -1 227 } 228 229 // Generic split: splits after each instance of sep, 230 // including sepSave bytes of sep in the subarrays. 231 func genSplit(s, sep string, sepSave, n int) []string { 232 if n == 0 { 233 return nil 234 } 235 if sep == "" { 236 return explode(s, n) 237 } 238 if n < 0 { 239 n = Count(s, sep) + 1 240 } 241 242 a := make([]string, n) 243 n-- 244 i := 0 245 for i < n { 246 m := Index(s, sep) 247 if m < 0 { 248 break 249 } 250 a[i] = s[:m+sepSave] 251 s = s[m+len(sep):] 252 i++ 253 } 254 a[i] = s 255 return a[:i+1] 256 } 257 258 // SplitN slices s into substrings separated by sep and returns a slice of 259 // the substrings between those separators. 260 // 261 // The count determines the number of substrings to return: 262 // n > 0: at most n substrings; the last substring will be the unsplit remainder. 263 // n == 0: the result is nil (zero substrings) 264 // n < 0: all substrings 265 // 266 // Edge cases for s and sep (for example, empty strings) are handled 267 // as described in the documentation for Split. 268 func SplitN(s, sep string, n int) []string { return genSplit(s, sep, 0, n) } 269 270 // SplitAfterN slices s into substrings after each instance of sep and 271 // returns a slice of those substrings. 272 // 273 // The count determines the number of substrings to return: 274 // n > 0: at most n substrings; the last substring will be the unsplit remainder. 275 // n == 0: the result is nil (zero substrings) 276 // n < 0: all substrings 277 // 278 // Edge cases for s and sep (for example, empty strings) are handled 279 // as described in the documentation for SplitAfter. 280 func SplitAfterN(s, sep string, n int) []string { 281 return genSplit(s, sep, len(sep), n) 282 } 283 284 // Split slices s into all substrings separated by sep and returns a slice of 285 // the substrings between those separators. 286 // 287 // If s does not contain sep and sep is not empty, Split returns a 288 // slice of length 1 whose only element is s. 289 // 290 // If sep is empty, Split splits after each UTF-8 sequence. If both s 291 // and sep are empty, Split returns an empty slice. 292 // 293 // It is equivalent to SplitN with a count of -1. 294 func Split(s, sep string) []string { return genSplit(s, sep, 0, -1) } 295 296 // SplitAfter slices s into all substrings after each instance of sep and 297 // returns a slice of those substrings. 298 // 299 // If s does not contain sep and sep is not empty, SplitAfter returns 300 // a slice of length 1 whose only element is s. 301 // 302 // If sep is empty, SplitAfter splits after each UTF-8 sequence. If 303 // both s and sep are empty, SplitAfter returns an empty slice. 304 // 305 // It is equivalent to SplitAfterN with a count of -1. 306 func SplitAfter(s, sep string) []string { 307 return genSplit(s, sep, len(sep), -1) 308 } 309 310 var asciiSpace = [256]uint8{'\t': 1, '\n': 1, '\v': 1, '\f': 1, '\r': 1, ' ': 1} 311 312 // Fields splits the string s around each instance of one or more consecutive white space 313 // characters, as defined by unicode.IsSpace, returning an array of substrings of s or an 314 // empty list if s contains only white space. 315 func Fields(s string) []string { 316 // First count the fields. 317 // This is an exact count if s is ASCII, otherwise it is an approximation. 318 n := 0 319 wasSpace := 1 320 // setBits is used to track which bits are set in the bytes of s. 321 setBits := uint8(0) 322 for i := 0; i < len(s); i++ { 323 r := s[i] 324 setBits |= r 325 isSpace := int(asciiSpace[r]) 326 n += wasSpace & ^isSpace 327 wasSpace = isSpace 328 } 329 330 if setBits < utf8.RuneSelf { // ASCII fast path 331 a := make([]string, n) 332 na := 0 333 fieldStart := 0 334 i := 0 335 // Skip spaces in the front of the input. 336 for i < len(s) && asciiSpace[s[i]] != 0 { 337 i++ 338 } 339 fieldStart = i 340 for i < len(s) { 341 if asciiSpace[s[i]] == 0 { 342 i++ 343 continue 344 } 345 a[na] = s[fieldStart:i] 346 na++ 347 i++ 348 // Skip spaces in between fields. 349 for i < len(s) && asciiSpace[s[i]] != 0 { 350 i++ 351 } 352 fieldStart = i 353 } 354 if fieldStart < len(s) { // Last field might end at EOF. 355 a[na] = s[fieldStart:] 356 } 357 return a 358 } 359 360 // Some runes in the input string are not ASCII. 361 // Same general approach as in the ASCII path but 362 // uses DecodeRuneInString and unicode.IsSpace if 363 // a non-ASCII rune needs to be decoded and checked 364 // if it corresponds to a space. 365 a := make([]string, 0, n) 366 fieldStart := 0 367 i := 0 368 // Skip spaces in the front of the input. 369 for i < len(s) { 370 if c := s[i]; c < utf8.RuneSelf { 371 if asciiSpace[c] == 0 { 372 break 373 } 374 i++ 375 } else { 376 r, w := utf8.DecodeRuneInString(s[i:]) 377 if !unicode.IsSpace(r) { 378 break 379 } 380 i += w 381 } 382 } 383 fieldStart = i 384 for i < len(s) { 385 if c := s[i]; c < utf8.RuneSelf { 386 if asciiSpace[c] == 0 { 387 i++ 388 continue 389 } 390 a = append(a, s[fieldStart:i]) 391 i++ 392 } else { 393 r, w := utf8.DecodeRuneInString(s[i:]) 394 if !unicode.IsSpace(r) { 395 i += w 396 continue 397 } 398 a = append(a, s[fieldStart:i]) 399 i += w 400 } 401 // Skip spaces in between fields. 402 for i < len(s) { 403 if c := s[i]; c < utf8.RuneSelf { 404 if asciiSpace[c] == 0 { 405 break 406 } 407 i++ 408 } else { 409 r, w := utf8.DecodeRuneInString(s[i:]) 410 if !unicode.IsSpace(r) { 411 break 412 } 413 i += w 414 } 415 } 416 fieldStart = i 417 } 418 if fieldStart < len(s) { // Last field might end at EOF. 419 a = append(a, s[fieldStart:]) 420 } 421 return a 422 } 423 424 // FieldsFunc splits the string s at each run of Unicode code points c satisfying f(c) 425 // and returns an array of slices of s. If all code points in s satisfy f(c) or the 426 // string is empty, an empty slice is returned. 427 // FieldsFunc makes no guarantees about the order in which it calls f(c). 428 // If f does not return consistent results for a given c, FieldsFunc may crash. 429 func FieldsFunc(s string, f func(rune) bool) []string { 430 // First count the fields. 431 n := 0 432 inField := false 433 for _, rune := range s { 434 wasInField := inField 435 inField = !f(rune) 436 if inField && !wasInField { 437 n++ 438 } 439 } 440 441 // Now create them. 442 a := make([]string, n) 443 na := 0 444 fieldStart := -1 // Set to -1 when looking for start of field. 445 for i, rune := range s { 446 if f(rune) { 447 if fieldStart >= 0 { 448 a[na] = s[fieldStart:i] 449 na++ 450 fieldStart = -1 451 } 452 } else if fieldStart == -1 { 453 fieldStart = i 454 } 455 } 456 if fieldStart >= 0 { // Last field might end at EOF. 457 a[na] = s[fieldStart:] 458 } 459 return a 460 } 461 462 // Join concatenates the elements of a to create a single string. The separator string 463 // sep is placed between elements in the resulting string. 464 func Join(a []string, sep string) string { 465 switch len(a) { 466 case 0: 467 return "" 468 case 1: 469 return a[0] 470 case 2: 471 // Special case for common small values. 472 // Remove if golang.org/issue/6714 is fixed 473 return a[0] + sep + a[1] 474 case 3: 475 // Special case for common small values. 476 // Remove if golang.org/issue/6714 is fixed 477 return a[0] + sep + a[1] + sep + a[2] 478 } 479 n := len(sep) * (len(a) - 1) 480 for i := 0; i < len(a); i++ { 481 n += len(a[i]) 482 } 483 484 b := make([]byte, n) 485 bp := copy(b, a[0]) 486 for _, s := range a[1:] { 487 bp += copy(b[bp:], sep) 488 bp += copy(b[bp:], s) 489 } 490 return string(b) 491 } 492 493 // HasPrefix tests whether the string s begins with prefix. 494 func HasPrefix(s, prefix string) bool { 495 return len(s) >= len(prefix) && s[0:len(prefix)] == prefix 496 } 497 498 // HasSuffix tests whether the string s ends with suffix. 499 func HasSuffix(s, suffix string) bool { 500 return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix 501 } 502 503 // Map returns a copy of the string s with all its characters modified 504 // according to the mapping function. If mapping returns a negative value, the character is 505 // dropped from the string with no replacement. 506 func Map(mapping func(rune) rune, s string) string { 507 // In the worst case, the string can grow when mapped, making 508 // things unpleasant. But it's so rare we barge in assuming it's 509 // fine. It could also shrink but that falls out naturally. 510 511 // The output buffer b is initialized on demand, the first 512 // time a character differs. 513 var b []byte 514 // nbytes is the number of bytes encoded in b. 515 var nbytes int 516 517 for i, c := range s { 518 r := mapping(c) 519 if r == c { 520 continue 521 } 522 523 b = make([]byte, len(s)+utf8.UTFMax) 524 nbytes = copy(b, s[:i]) 525 if r >= 0 { 526 if r < utf8.RuneSelf { 527 b[nbytes] = byte(r) 528 nbytes++ 529 } else { 530 nbytes += utf8.EncodeRune(b[nbytes:], r) 531 } 532 } 533 534 if c == utf8.RuneError { 535 // RuneError is the result of either decoding 536 // an invalid sequence or '\uFFFD'. Determine 537 // the correct number of bytes we need to advance. 538 _, w := utf8.DecodeRuneInString(s[i:]) 539 i += w 540 } else { 541 i += utf8.RuneLen(c) 542 } 543 544 s = s[i:] 545 break 546 } 547 548 if b == nil { 549 return s 550 } 551 552 for _, c := range s { 553 r := mapping(c) 554 555 // common case 556 if (0 <= r && r < utf8.RuneSelf) && nbytes < len(b) { 557 b[nbytes] = byte(r) 558 nbytes++ 559 continue 560 } 561 562 // b is not big enough or r is not a ASCII rune. 563 if r >= 0 { 564 if nbytes+utf8.UTFMax >= len(b) { 565 // Grow the buffer. 566 nb := make([]byte, 2*len(b)) 567 copy(nb, b[:nbytes]) 568 b = nb 569 } 570 nbytes += utf8.EncodeRune(b[nbytes:], r) 571 } 572 } 573 574 return string(b[:nbytes]) 575 } 576 577 // Repeat returns a new string consisting of count copies of the string s. 578 // 579 // It panics if count is negative or if 580 // the result of (len(s) * count) overflows. 581 func Repeat(s string, count int) string { 582 // Since we cannot return an error on overflow, 583 // we should panic if the repeat will generate 584 // an overflow. 585 // See Issue golang.org/issue/16237 586 if count < 0 { 587 panic("strings: negative Repeat count") 588 } else if count > 0 && len(s)*count/count != len(s) { 589 panic("strings: Repeat count causes overflow") 590 } 591 592 b := make([]byte, len(s)*count) 593 bp := copy(b, s) 594 for bp < len(b) { 595 copy(b[bp:], b[:bp]) 596 bp *= 2 597 } 598 return string(b) 599 } 600 601 // ToUpper returns a copy of the string s with all Unicode letters mapped to their upper case. 602 func ToUpper(s string) string { return Map(unicode.ToUpper, s) } 603 604 // ToLower returns a copy of the string s with all Unicode letters mapped to their lower case. 605 func ToLower(s string) string { return Map(unicode.ToLower, s) } 606 607 // ToTitle returns a copy of the string s with all Unicode letters mapped to their title case. 608 func ToTitle(s string) string { return Map(unicode.ToTitle, s) } 609 610 // ToUpperSpecial returns a copy of the string s with all Unicode letters mapped to their 611 // upper case, giving priority to the special casing rules. 612 func ToUpperSpecial(c unicode.SpecialCase, s string) string { 613 return Map(func(r rune) rune { return c.ToUpper(r) }, s) 614 } 615 616 // ToLowerSpecial returns a copy of the string s with all Unicode letters mapped to their 617 // lower case, giving priority to the special casing rules. 618 func ToLowerSpecial(c unicode.SpecialCase, s string) string { 619 return Map(func(r rune) rune { return c.ToLower(r) }, s) 620 } 621 622 // ToTitleSpecial returns a copy of the string s with all Unicode letters mapped to their 623 // title case, giving priority to the special casing rules. 624 func ToTitleSpecial(c unicode.SpecialCase, s string) string { 625 return Map(func(r rune) rune { return c.ToTitle(r) }, s) 626 } 627 628 // isSeparator reports whether the rune could mark a word boundary. 629 // TODO: update when package unicode captures more of the properties. 630 func isSeparator(r rune) bool { 631 // ASCII alphanumerics and underscore are not separators 632 if r <= 0x7F { 633 switch { 634 case '0' <= r && r <= '9': 635 return false 636 case 'a' <= r && r <= 'z': 637 return false 638 case 'A' <= r && r <= 'Z': 639 return false 640 case r == '_': 641 return false 642 } 643 return true 644 } 645 // Letters and digits are not separators 646 if unicode.IsLetter(r) || unicode.IsDigit(r) { 647 return false 648 } 649 // Otherwise, all we can do for now is treat spaces as separators. 650 return unicode.IsSpace(r) 651 } 652 653 // Title returns a copy of the string s with all Unicode letters that begin words 654 // mapped to their title case. 655 // 656 // BUG(rsc): The rule Title uses for word boundaries does not handle Unicode punctuation properly. 657 func Title(s string) string { 658 // Use a closure here to remember state. 659 // Hackish but effective. Depends on Map scanning in order and calling 660 // the closure once per rune. 661 prev := ' ' 662 return Map( 663 func(r rune) rune { 664 if isSeparator(prev) { 665 prev = r 666 return unicode.ToTitle(r) 667 } 668 prev = r 669 return r 670 }, 671 s) 672 } 673 674 // TrimLeftFunc returns a slice of the string s with all leading 675 // Unicode code points c satisfying f(c) removed. 676 func TrimLeftFunc(s string, f func(rune) bool) string { 677 i := indexFunc(s, f, false) 678 if i == -1 { 679 return "" 680 } 681 return s[i:] 682 } 683 684 // TrimRightFunc returns a slice of the string s with all trailing 685 // Unicode code points c satisfying f(c) removed. 686 func TrimRightFunc(s string, f func(rune) bool) string { 687 i := lastIndexFunc(s, f, false) 688 if i >= 0 && s[i] >= utf8.RuneSelf { 689 _, wid := utf8.DecodeRuneInString(s[i:]) 690 i += wid 691 } else { 692 i++ 693 } 694 return s[0:i] 695 } 696 697 // TrimFunc returns a slice of the string s with all leading 698 // and trailing Unicode code points c satisfying f(c) removed. 699 func TrimFunc(s string, f func(rune) bool) string { 700 return TrimRightFunc(TrimLeftFunc(s, f), f) 701 } 702 703 // IndexFunc returns the index into s of the first Unicode 704 // code point satisfying f(c), or -1 if none do. 705 func IndexFunc(s string, f func(rune) bool) int { 706 return indexFunc(s, f, true) 707 } 708 709 // LastIndexFunc returns the index into s of the last 710 // Unicode code point satisfying f(c), or -1 if none do. 711 func LastIndexFunc(s string, f func(rune) bool) int { 712 return lastIndexFunc(s, f, true) 713 } 714 715 // indexFunc is the same as IndexFunc except that if 716 // truth==false, the sense of the predicate function is 717 // inverted. 718 func indexFunc(s string, f func(rune) bool, truth bool) int { 719 for i, r := range s { 720 if f(r) == truth { 721 return i 722 } 723 } 724 return -1 725 } 726 727 // lastIndexFunc is the same as LastIndexFunc except that if 728 // truth==false, the sense of the predicate function is 729 // inverted. 730 func lastIndexFunc(s string, f func(rune) bool, truth bool) int { 731 for i := len(s); i > 0; { 732 r, size := utf8.DecodeLastRuneInString(s[0:i]) 733 i -= size 734 if f(r) == truth { 735 return i 736 } 737 } 738 return -1 739 } 740 741 // asciiSet is a 32-byte value, where each bit represents the presence of a 742 // given ASCII character in the set. The 128-bits of the lower 16 bytes, 743 // starting with the least-significant bit of the lowest word to the 744 // most-significant bit of the highest word, map to the full range of all 745 // 128 ASCII characters. The 128-bits of the upper 16 bytes will be zeroed, 746 // ensuring that any non-ASCII character will be reported as not in the set. 747 type asciiSet [8]uint32 748 749 // makeASCIISet creates a set of ASCII characters and reports whether all 750 // characters in chars are ASCII. 751 func makeASCIISet(chars string) (as asciiSet, ok bool) { 752 for i := 0; i < len(chars); i++ { 753 c := chars[i] 754 if c >= utf8.RuneSelf { 755 return as, false 756 } 757 as[c>>5] |= 1 << uint(c&31) 758 } 759 return as, true 760 } 761 762 // contains reports whether c is inside the set. 763 func (as *asciiSet) contains(c byte) bool { 764 return (as[c>>5] & (1 << uint(c&31))) != 0 765 } 766 767 func makeCutsetFunc(cutset string) func(rune) bool { 768 if len(cutset) == 1 && cutset[0] < utf8.RuneSelf { 769 return func(r rune) bool { 770 return r == rune(cutset[0]) 771 } 772 } 773 if as, isASCII := makeASCIISet(cutset); isASCII { 774 return func(r rune) bool { 775 return r < utf8.RuneSelf && as.contains(byte(r)) 776 } 777 } 778 return func(r rune) bool { return IndexRune(cutset, r) >= 0 } 779 } 780 781 // Trim returns a slice of the string s with all leading and 782 // trailing Unicode code points contained in cutset removed. 783 func Trim(s string, cutset string) string { 784 if s == "" || cutset == "" { 785 return s 786 } 787 return TrimFunc(s, makeCutsetFunc(cutset)) 788 } 789 790 // TrimLeft returns a slice of the string s with all leading 791 // Unicode code points contained in cutset removed. 792 func TrimLeft(s string, cutset string) string { 793 if s == "" || cutset == "" { 794 return s 795 } 796 return TrimLeftFunc(s, makeCutsetFunc(cutset)) 797 } 798 799 // TrimRight returns a slice of the string s, with all trailing 800 // Unicode code points contained in cutset removed. 801 func TrimRight(s string, cutset string) string { 802 if s == "" || cutset == "" { 803 return s 804 } 805 return TrimRightFunc(s, makeCutsetFunc(cutset)) 806 } 807 808 // TrimSpace returns a slice of the string s, with all leading 809 // and trailing white space removed, as defined by Unicode. 810 func TrimSpace(s string) string { 811 return TrimFunc(s, unicode.IsSpace) 812 } 813 814 // TrimPrefix returns s without the provided leading prefix string. 815 // If s doesn't start with prefix, s is returned unchanged. 816 func TrimPrefix(s, prefix string) string { 817 if HasPrefix(s, prefix) { 818 return s[len(prefix):] 819 } 820 return s 821 } 822 823 // TrimSuffix returns s without the provided trailing suffix string. 824 // If s doesn't end with suffix, s is returned unchanged. 825 func TrimSuffix(s, suffix string) string { 826 if HasSuffix(s, suffix) { 827 return s[:len(s)-len(suffix)] 828 } 829 return s 830 } 831 832 // Replace returns a copy of the string s with the first n 833 // non-overlapping instances of old replaced by new. 834 // If old is empty, it matches at the beginning of the string 835 // and after each UTF-8 sequence, yielding up to k+1 replacements 836 // for a k-rune string. 837 // If n < 0, there is no limit on the number of replacements. 838 func Replace(s, old, new string, n int) string { 839 if old == new || n == 0 { 840 return s // avoid allocation 841 } 842 843 // Compute number of replacements. 844 if m := Count(s, old); m == 0 { 845 return s // avoid allocation 846 } else if n < 0 || m < n { 847 n = m 848 } 849 850 // Apply replacements to buffer. 851 t := make([]byte, len(s)+n*(len(new)-len(old))) 852 w := 0 853 start := 0 854 for i := 0; i < n; i++ { 855 j := start 856 if len(old) == 0 { 857 if i > 0 { 858 _, wid := utf8.DecodeRuneInString(s[start:]) 859 j += wid 860 } 861 } else { 862 j += Index(s[start:], old) 863 } 864 w += copy(t[w:], s[start:j]) 865 w += copy(t[w:], new) 866 start = j + len(old) 867 } 868 w += copy(t[w:], s[start:]) 869 return string(t[0:w]) 870 } 871 872 // EqualFold reports whether s and t, interpreted as UTF-8 strings, 873 // are equal under Unicode case-folding. 874 func EqualFold(s, t string) bool { 875 for s != "" && t != "" { 876 // Extract first rune from each string. 877 var sr, tr rune 878 if s[0] < utf8.RuneSelf { 879 sr, s = rune(s[0]), s[1:] 880 } else { 881 r, size := utf8.DecodeRuneInString(s) 882 sr, s = r, s[size:] 883 } 884 if t[0] < utf8.RuneSelf { 885 tr, t = rune(t[0]), t[1:] 886 } else { 887 r, size := utf8.DecodeRuneInString(t) 888 tr, t = r, t[size:] 889 } 890 891 // If they match, keep going; if not, return false. 892 893 // Easy case. 894 if tr == sr { 895 continue 896 } 897 898 // Make sr < tr to simplify what follows. 899 if tr < sr { 900 tr, sr = sr, tr 901 } 902 // Fast check for ASCII. 903 if tr < utf8.RuneSelf && 'A' <= sr && sr <= 'Z' { 904 // ASCII, and sr is upper case. tr must be lower case. 905 if tr == sr+'a'-'A' { 906 continue 907 } 908 return false 909 } 910 911 // General case. SimpleFold(x) returns the next equivalent rune > x 912 // or wraps around to smaller values. 913 r := unicode.SimpleFold(sr) 914 for r != sr && r < tr { 915 r = unicode.SimpleFold(r) 916 } 917 if r == tr { 918 continue 919 } 920 return false 921 } 922 923 // One string is empty. Are both? 924 return s == t 925 }