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