Source file src/strings/strings.go

     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  	"internal/bytealg"
    12  	"internal/stringslite"
    13  	"math/bits"
    14  	"unicode"
    15  	"unicode/utf8"
    16  )
    17  
    18  const maxInt = int(^uint(0) >> 1)
    19  
    20  // explode splits s into a slice of UTF-8 strings,
    21  // one string per Unicode character up to a maximum of n (n < 0 means no limit).
    22  // Invalid UTF-8 bytes are sliced individually.
    23  func explode(s string, n int) []string {
    24  	l := utf8.RuneCountInString(s)
    25  	if n < 0 || n > l {
    26  		n = l
    27  	}
    28  	a := make([]string, n)
    29  	for i := 0; i < n-1; i++ {
    30  		_, size := utf8.DecodeRuneInString(s)
    31  		a[i] = s[:size]
    32  		s = s[size:]
    33  	}
    34  	if n > 0 {
    35  		a[n-1] = s
    36  	}
    37  	return a
    38  }
    39  
    40  // Count counts the number of non-overlapping instances of substr in s.
    41  // If substr is an empty string, Count returns 1 + the number of Unicode code points in s.
    42  func Count(s, substr string) int {
    43  	// special case
    44  	if len(substr) == 0 {
    45  		return utf8.RuneCountInString(s) + 1
    46  	}
    47  	if len(substr) == 1 {
    48  		return bytealg.CountString(s, substr[0])
    49  	}
    50  	n := 0
    51  	for {
    52  		i := Index(s, substr)
    53  		if i == -1 {
    54  			return n
    55  		}
    56  		n++
    57  		s = s[i+len(substr):]
    58  	}
    59  }
    60  
    61  // Contains reports whether substr is within s.
    62  func Contains(s, substr string) bool {
    63  	return Index(s, substr) >= 0
    64  }
    65  
    66  // ContainsAny reports whether any Unicode code points in chars are within s.
    67  func ContainsAny(s, chars string) bool {
    68  	return IndexAny(s, chars) >= 0
    69  }
    70  
    71  // ContainsRune reports whether the Unicode code point r is within s.
    72  func ContainsRune(s string, r rune) bool {
    73  	return IndexRune(s, r) >= 0
    74  }
    75  
    76  // ContainsFunc reports whether any Unicode code points r within s satisfy f(r).
    77  func ContainsFunc(s string, f func(rune) bool) bool {
    78  	return IndexFunc(s, f) >= 0
    79  }
    80  
    81  // LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.
    82  func LastIndex(s, substr string) int {
    83  	n := len(substr)
    84  	switch {
    85  	case n == 0:
    86  		return len(s)
    87  	case n == 1:
    88  		return bytealg.LastIndexByteString(s, substr[0])
    89  	case n == len(s):
    90  		if substr == s {
    91  			return 0
    92  		}
    93  		return -1
    94  	case n > len(s):
    95  		return -1
    96  	}
    97  	// Rabin-Karp search from the end of the string
    98  	hashss, pow := bytealg.HashStrRev(substr)
    99  	last := len(s) - n
   100  	var h uint32
   101  	for i := len(s) - 1; i >= last; i-- {
   102  		h = h*bytealg.PrimeRK + uint32(s[i])
   103  	}
   104  	if h == hashss && s[last:] == substr {
   105  		return last
   106  	}
   107  	for i := last - 1; i >= 0; i-- {
   108  		h *= bytealg.PrimeRK
   109  		h += uint32(s[i])
   110  		h -= pow * uint32(s[i+n])
   111  		if h == hashss && s[i:i+n] == substr {
   112  			return i
   113  		}
   114  	}
   115  	return -1
   116  }
   117  
   118  // IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.
   119  func IndexByte(s string, c byte) int {
   120  	return stringslite.IndexByte(s, c)
   121  }
   122  
   123  // IndexRune returns the index of the first instance of the Unicode code point
   124  // r, or -1 if rune is not present in s.
   125  // If r is [utf8.RuneError], it returns the first instance of any
   126  // invalid UTF-8 byte sequence.
   127  func IndexRune(s string, r rune) int {
   128  	const haveFastIndex = bytealg.MaxBruteForce > 0
   129  	switch {
   130  	case 0 <= r && r < utf8.RuneSelf:
   131  		return IndexByte(s, byte(r))
   132  	case r == utf8.RuneError:
   133  		for i, r := range s {
   134  			if r == utf8.RuneError {
   135  				return i
   136  			}
   137  		}
   138  		return -1
   139  	case !utf8.ValidRune(r):
   140  		return -1
   141  	default:
   142  		// Search for rune r using the last byte of its UTF-8 encoded form.
   143  		// The distribution of the last byte is more uniform compared to the
   144  		// first byte which has a 78% chance of being [240, 243, 244].
   145  		rs := string(r)
   146  		last := len(rs) - 1
   147  		i := last
   148  		fails := 0
   149  		for i < len(s) {
   150  			if s[i] != rs[last] {
   151  				o := IndexByte(s[i+1:], rs[last])
   152  				if o < 0 {
   153  					return -1
   154  				}
   155  				i += o + 1
   156  			}
   157  			// Step backwards comparing bytes.
   158  			for j := 1; j < len(rs); j++ {
   159  				if s[i-j] != rs[last-j] {
   160  					goto next
   161  				}
   162  			}
   163  			return i - last
   164  		next:
   165  			fails++
   166  			i++
   167  			if (haveFastIndex && fails > bytealg.Cutover(i)) && i < len(s) ||
   168  				(!haveFastIndex && fails >= 4+i>>4 && i < len(s)) {
   169  				goto fallback
   170  			}
   171  		}
   172  		return -1
   173  
   174  	fallback:
   175  		// see comment in ../bytes/bytes.go
   176  		if haveFastIndex {
   177  			if j := bytealg.IndexString(s[i-last:], string(r)); j >= 0 {
   178  				return i + j - last
   179  			}
   180  		} else {
   181  			c0 := rs[last]
   182  			c1 := rs[last-1]
   183  		loop:
   184  			for ; i < len(s); i++ {
   185  				if s[i] == c0 && s[i-1] == c1 {
   186  					for k := 2; k < len(rs); k++ {
   187  						if s[i-k] != rs[last-k] {
   188  							continue loop
   189  						}
   190  					}
   191  					return i - last
   192  				}
   193  			}
   194  		}
   195  		return -1
   196  	}
   197  }
   198  
   199  // IndexAny returns the index of the first instance of any Unicode code point
   200  // from chars in s, or -1 if no Unicode code point from chars is present in s.
   201  func IndexAny(s, chars string) int {
   202  	if chars == "" {
   203  		// Avoid scanning all of s.
   204  		return -1
   205  	}
   206  	if len(chars) == 1 {
   207  		// Avoid scanning all of s.
   208  		r := rune(chars[0])
   209  		if r >= utf8.RuneSelf {
   210  			r = utf8.RuneError
   211  		}
   212  		return IndexRune(s, r)
   213  	}
   214  	if len(s) > 8 {
   215  		if as, isASCII := makeASCIISet(chars); isASCII {
   216  			for i := 0; i < len(s); i++ {
   217  				if as.contains(s[i]) {
   218  					return i
   219  				}
   220  			}
   221  			return -1
   222  		}
   223  	}
   224  	for i, c := range s {
   225  		if IndexRune(chars, c) >= 0 {
   226  			return i
   227  		}
   228  	}
   229  	return -1
   230  }
   231  
   232  // LastIndexAny returns the index of the last instance of any Unicode code
   233  // point from chars in s, or -1 if no Unicode code point from chars is
   234  // present in s.
   235  func LastIndexAny(s, chars string) int {
   236  	if chars == "" {
   237  		// Avoid scanning all of s.
   238  		return -1
   239  	}
   240  	if len(s) == 1 {
   241  		rc := rune(s[0])
   242  		if rc >= utf8.RuneSelf {
   243  			rc = utf8.RuneError
   244  		}
   245  		if IndexRune(chars, rc) >= 0 {
   246  			return 0
   247  		}
   248  		return -1
   249  	}
   250  	if len(s) > 8 {
   251  		if as, isASCII := makeASCIISet(chars); isASCII {
   252  			for i := len(s) - 1; i >= 0; i-- {
   253  				if as.contains(s[i]) {
   254  					return i
   255  				}
   256  			}
   257  			return -1
   258  		}
   259  	}
   260  	if len(chars) == 1 {
   261  		rc := rune(chars[0])
   262  		if rc >= utf8.RuneSelf {
   263  			rc = utf8.RuneError
   264  		}
   265  		for i := len(s); i > 0; {
   266  			r, size := utf8.DecodeLastRuneInString(s[:i])
   267  			i -= size
   268  			if rc == r {
   269  				return i
   270  			}
   271  		}
   272  		return -1
   273  	}
   274  	for i := len(s); i > 0; {
   275  		r, size := utf8.DecodeLastRuneInString(s[:i])
   276  		i -= size
   277  		if IndexRune(chars, r) >= 0 {
   278  			return i
   279  		}
   280  	}
   281  	return -1
   282  }
   283  
   284  // LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s.
   285  func LastIndexByte(s string, c byte) int {
   286  	return bytealg.LastIndexByteString(s, c)
   287  }
   288  
   289  // Generic split: splits after each instance of sep,
   290  // including sepSave bytes of sep in the subarrays.
   291  func genSplit(s, sep string, sepSave, n int) []string {
   292  	if n == 0 {
   293  		return nil
   294  	}
   295  	if sep == "" {
   296  		return explode(s, n)
   297  	}
   298  	if n < 0 {
   299  		n = Count(s, sep) + 1
   300  	}
   301  
   302  	if n > len(s)+1 {
   303  		n = len(s) + 1
   304  	}
   305  	a := make([]string, n)
   306  	n--
   307  	i := 0
   308  	for i < n {
   309  		m := Index(s, sep)
   310  		if m < 0 {
   311  			break
   312  		}
   313  		a[i] = s[:m+sepSave]
   314  		s = s[m+len(sep):]
   315  		i++
   316  	}
   317  	a[i] = s
   318  	return a[:i+1]
   319  }
   320  
   321  // SplitN slices s into substrings separated by sep and returns a slice of
   322  // the substrings between those separators.
   323  //
   324  // The count determines the number of substrings to return:
   325  //   - n > 0: at most n substrings; the last substring will be the unsplit remainder;
   326  //   - n == 0: the result is nil (zero substrings);
   327  //   - n < 0: all substrings.
   328  //
   329  // Edge cases for s and sep (for example, empty strings) are handled
   330  // as described in the documentation for [Split].
   331  //
   332  // To split around the first instance of a separator, see [Cut].
   333  func SplitN(s, sep string, n int) []string { return genSplit(s, sep, 0, n) }
   334  
   335  // SplitAfterN slices s into substrings after each instance of sep and
   336  // returns a slice of those substrings.
   337  //
   338  // The count determines the number of substrings to return:
   339  //   - n > 0: at most n substrings; the last substring will be the unsplit remainder;
   340  //   - n == 0: the result is nil (zero substrings);
   341  //   - n < 0: all substrings.
   342  //
   343  // Edge cases for s and sep (for example, empty strings) are handled
   344  // as described in the documentation for [SplitAfter].
   345  func SplitAfterN(s, sep string, n int) []string {
   346  	return genSplit(s, sep, len(sep), n)
   347  }
   348  
   349  // Split slices s into all substrings separated by sep and returns a slice of
   350  // the substrings between those separators.
   351  //
   352  // If s does not contain sep and sep is not empty, Split returns a
   353  // slice of length 1 whose only element is s.
   354  //
   355  // If sep is empty, Split splits after each UTF-8 sequence. If both s
   356  // and sep are empty, Split returns an empty slice.
   357  //
   358  // It is equivalent to [SplitN] with a count of -1.
   359  //
   360  // To split around the first instance of a separator, see [Cut].
   361  func Split(s, sep string) []string { return genSplit(s, sep, 0, -1) }
   362  
   363  // SplitAfter slices s into all substrings after each instance of sep and
   364  // returns a slice of those substrings.
   365  //
   366  // If s does not contain sep and sep is not empty, SplitAfter returns
   367  // a slice of length 1 whose only element is s.
   368  //
   369  // If sep is empty, SplitAfter splits after each UTF-8 sequence. If
   370  // both s and sep are empty, SplitAfter returns an empty slice.
   371  //
   372  // It is equivalent to [SplitAfterN] with a count of -1.
   373  func SplitAfter(s, sep string) []string {
   374  	return genSplit(s, sep, len(sep), -1)
   375  }
   376  
   377  var asciiSpace = [256]uint8{'\t': 1, '\n': 1, '\v': 1, '\f': 1, '\r': 1, ' ': 1}
   378  
   379  // Fields splits the string s around each instance of one or more consecutive white space
   380  // characters, as defined by [unicode.IsSpace], returning a slice of substrings of s or an
   381  // empty slice if s contains only white space.
   382  func Fields(s string) []string {
   383  	// First count the fields.
   384  	// This is an exact count if s is ASCII, otherwise it is an approximation.
   385  	n := 0
   386  	wasSpace := 1
   387  	// setBits is used to track which bits are set in the bytes of s.
   388  	setBits := uint8(0)
   389  	for i := 0; i < len(s); i++ {
   390  		r := s[i]
   391  		setBits |= r
   392  		isSpace := int(asciiSpace[r])
   393  		n += wasSpace & ^isSpace
   394  		wasSpace = isSpace
   395  	}
   396  
   397  	if setBits >= utf8.RuneSelf {
   398  		// Some runes in the input string are not ASCII.
   399  		return FieldsFunc(s, unicode.IsSpace)
   400  	}
   401  	// ASCII fast path
   402  	a := make([]string, n)
   403  	na := 0
   404  	fieldStart := 0
   405  	i := 0
   406  	// Skip spaces in the front of the input.
   407  	for i < len(s) && asciiSpace[s[i]] != 0 {
   408  		i++
   409  	}
   410  	fieldStart = i
   411  	for i < len(s) {
   412  		if asciiSpace[s[i]] == 0 {
   413  			i++
   414  			continue
   415  		}
   416  		a[na] = s[fieldStart:i]
   417  		na++
   418  		i++
   419  		// Skip spaces in between fields.
   420  		for i < len(s) && asciiSpace[s[i]] != 0 {
   421  			i++
   422  		}
   423  		fieldStart = i
   424  	}
   425  	if fieldStart < len(s) { // Last field might end at EOF.
   426  		a[na] = s[fieldStart:]
   427  	}
   428  	return a
   429  }
   430  
   431  // FieldsFunc splits the string s at each run of Unicode code points c satisfying f(c)
   432  // and returns an array of slices of s. If all code points in s satisfy f(c) or the
   433  // string is empty, an empty slice is returned.
   434  //
   435  // FieldsFunc makes no guarantees about the order in which it calls f(c)
   436  // and assumes that f always returns the same value for a given c.
   437  func FieldsFunc(s string, f func(rune) bool) []string {
   438  	// A span is used to record a slice of s of the form s[start:end].
   439  	// The start index is inclusive and the end index is exclusive.
   440  	type span struct {
   441  		start int
   442  		end   int
   443  	}
   444  	spans := make([]span, 0, 32)
   445  
   446  	// Find the field start and end indices.
   447  	// Doing this in a separate pass (rather than slicing the string s
   448  	// and collecting the result substrings right away) is significantly
   449  	// more efficient, possibly due to cache effects.
   450  	start := -1 // valid span start if >= 0
   451  	for end, rune := range s {
   452  		if f(rune) {
   453  			if start >= 0 {
   454  				spans = append(spans, span{start, end})
   455  				// Set start to a negative value.
   456  				// Note: using -1 here consistently and reproducibly
   457  				// slows down this code by a several percent on amd64.
   458  				start = ^start
   459  			}
   460  		} else {
   461  			if start < 0 {
   462  				start = end
   463  			}
   464  		}
   465  	}
   466  
   467  	// Last field might end at EOF.
   468  	if start >= 0 {
   469  		spans = append(spans, span{start, len(s)})
   470  	}
   471  
   472  	// Create strings from recorded field indices.
   473  	a := make([]string, len(spans))
   474  	for i, span := range spans {
   475  		a[i] = s[span.start:span.end]
   476  	}
   477  
   478  	return a
   479  }
   480  
   481  // Join concatenates the elements of its first argument to create a single string. The separator
   482  // string sep is placed between elements in the resulting string.
   483  func Join(elems []string, sep string) string {
   484  	switch len(elems) {
   485  	case 0:
   486  		return ""
   487  	case 1:
   488  		return elems[0]
   489  	}
   490  
   491  	var n int
   492  	if len(sep) > 0 {
   493  		if len(sep) >= maxInt/(len(elems)-1) {
   494  			panic("strings: Join output length overflow")
   495  		}
   496  		n += len(sep) * (len(elems) - 1)
   497  	}
   498  	for _, elem := range elems {
   499  		if len(elem) > maxInt-n {
   500  			panic("strings: Join output length overflow")
   501  		}
   502  		n += len(elem)
   503  	}
   504  
   505  	var b Builder
   506  	b.Grow(n)
   507  	b.WriteString(elems[0])
   508  	for _, s := range elems[1:] {
   509  		b.WriteString(sep)
   510  		b.WriteString(s)
   511  	}
   512  	return b.String()
   513  }
   514  
   515  // HasPrefix reports whether the string s begins with prefix.
   516  func HasPrefix(s, prefix string) bool {
   517  	return stringslite.HasPrefix(s, prefix)
   518  }
   519  
   520  // HasSuffix reports whether the string s ends with suffix.
   521  func HasSuffix(s, suffix string) bool {
   522  	return stringslite.HasSuffix(s, suffix)
   523  }
   524  
   525  // Map returns a copy of the string s with all its characters modified
   526  // according to the mapping function. If mapping returns a negative value, the character is
   527  // dropped from the string with no replacement.
   528  func Map(mapping func(rune) rune, s string) string {
   529  	// In the worst case, the string can grow when mapped, making
   530  	// things unpleasant. But it's so rare we barge in assuming it's
   531  	// fine. It could also shrink but that falls out naturally.
   532  
   533  	// The output buffer b is initialized on demand, the first
   534  	// time a character differs.
   535  	var b Builder
   536  
   537  	for i, c := range s {
   538  		r := mapping(c)
   539  		if r == c && c != utf8.RuneError {
   540  			continue
   541  		}
   542  
   543  		var width int
   544  		if c == utf8.RuneError {
   545  			c, width = utf8.DecodeRuneInString(s[i:])
   546  			if width != 1 && r == c {
   547  				continue
   548  			}
   549  		} else {
   550  			width = utf8.RuneLen(c)
   551  		}
   552  
   553  		b.Grow(len(s) + utf8.UTFMax)
   554  		b.WriteString(s[:i])
   555  		if r >= 0 {
   556  			b.WriteRune(r)
   557  		}
   558  
   559  		s = s[i+width:]
   560  		break
   561  	}
   562  
   563  	// Fast path for unchanged input
   564  	if b.Cap() == 0 { // didn't call b.Grow above
   565  		return s
   566  	}
   567  
   568  	for _, c := range s {
   569  		r := mapping(c)
   570  
   571  		if r >= 0 {
   572  			// common case
   573  			// Due to inlining, it is more performant to determine if WriteByte should be
   574  			// invoked rather than always call WriteRune
   575  			if r < utf8.RuneSelf {
   576  				b.WriteByte(byte(r))
   577  			} else {
   578  				// r is not an ASCII rune.
   579  				b.WriteRune(r)
   580  			}
   581  		}
   582  	}
   583  
   584  	return b.String()
   585  }
   586  
   587  // According to static analysis, spaces, dashes, zeros, equals, and tabs
   588  // are the most commonly repeated string literal,
   589  // often used for display on fixed-width terminal windows.
   590  // Pre-declare constants for these for O(1) repetition in the common-case.
   591  const (
   592  	repeatedSpaces = "" +
   593  		"                                                                " +
   594  		"                                                                "
   595  	repeatedDashes = "" +
   596  		"----------------------------------------------------------------" +
   597  		"----------------------------------------------------------------"
   598  	repeatedZeroes = "" +
   599  		"0000000000000000000000000000000000000000000000000000000000000000"
   600  	repeatedEquals = "" +
   601  		"================================================================" +
   602  		"================================================================"
   603  	repeatedTabs = "" +
   604  		"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" +
   605  		"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
   606  )
   607  
   608  // Repeat returns a new string consisting of count copies of the string s.
   609  //
   610  // It panics if count is negative or if the result of (len(s) * count)
   611  // overflows.
   612  func Repeat(s string, count int) string {
   613  	switch count {
   614  	case 0:
   615  		return ""
   616  	case 1:
   617  		return s
   618  	}
   619  
   620  	// Since we cannot return an error on overflow,
   621  	// we should panic if the repeat will generate an overflow.
   622  	// See golang.org/issue/16237.
   623  	if count < 0 {
   624  		panic("strings: negative Repeat count")
   625  	}
   626  	hi, lo := bits.Mul(uint(len(s)), uint(count))
   627  	if hi > 0 || lo > uint(maxInt) {
   628  		panic("strings: Repeat output length overflow")
   629  	}
   630  	n := int(lo) // lo = len(s) * count
   631  
   632  	if len(s) == 0 {
   633  		return ""
   634  	}
   635  
   636  	// Optimize for commonly repeated strings of relatively short length.
   637  	switch s[0] {
   638  	case ' ', '-', '0', '=', '\t':
   639  		switch {
   640  		case n <= len(repeatedSpaces) && HasPrefix(repeatedSpaces, s):
   641  			return repeatedSpaces[:n]
   642  		case n <= len(repeatedDashes) && HasPrefix(repeatedDashes, s):
   643  			return repeatedDashes[:n]
   644  		case n <= len(repeatedZeroes) && HasPrefix(repeatedZeroes, s):
   645  			return repeatedZeroes[:n]
   646  		case n <= len(repeatedEquals) && HasPrefix(repeatedEquals, s):
   647  			return repeatedEquals[:n]
   648  		case n <= len(repeatedTabs) && HasPrefix(repeatedTabs, s):
   649  			return repeatedTabs[:n]
   650  		}
   651  	}
   652  
   653  	// Past a certain chunk size it is counterproductive to use
   654  	// larger chunks as the source of the write, as when the source
   655  	// is too large we are basically just thrashing the CPU D-cache.
   656  	// So if the result length is larger than an empirically-found
   657  	// limit (8KB), we stop growing the source string once the limit
   658  	// is reached and keep reusing the same source string - that
   659  	// should therefore be always resident in the L1 cache - until we
   660  	// have completed the construction of the result.
   661  	// This yields significant speedups (up to +100%) in cases where
   662  	// the result length is large (roughly, over L2 cache size).
   663  	const chunkLimit = 8 * 1024
   664  	chunkMax := n
   665  	if n > chunkLimit {
   666  		chunkMax = chunkLimit / len(s) * len(s)
   667  		if chunkMax == 0 {
   668  			chunkMax = len(s)
   669  		}
   670  	}
   671  
   672  	var b Builder
   673  	b.Grow(n)
   674  	b.WriteString(s)
   675  	for b.Len() < n {
   676  		chunk := min(n-b.Len(), b.Len(), chunkMax)
   677  		b.WriteString(b.String()[:chunk])
   678  	}
   679  	return b.String()
   680  }
   681  
   682  // ToUpper returns s with all Unicode letters mapped to their upper case.
   683  func ToUpper(s string) string {
   684  	isASCII, hasLower := true, false
   685  	for i := 0; i < len(s); i++ {
   686  		c := s[i]
   687  		if c >= utf8.RuneSelf {
   688  			isASCII = false
   689  			break
   690  		}
   691  		hasLower = hasLower || ('a' <= c && c <= 'z')
   692  	}
   693  
   694  	if isASCII { // optimize for ASCII-only strings.
   695  		if !hasLower {
   696  			return s
   697  		}
   698  		var (
   699  			b   Builder
   700  			pos int
   701  		)
   702  		b.Grow(len(s))
   703  		for i := 0; i < len(s); i++ {
   704  			c := s[i]
   705  			if 'a' <= c && c <= 'z' {
   706  				c -= 'a' - 'A'
   707  				if pos < i {
   708  					b.WriteString(s[pos:i])
   709  				}
   710  				b.WriteByte(c)
   711  				pos = i + 1
   712  			}
   713  		}
   714  		if pos < len(s) {
   715  			b.WriteString(s[pos:])
   716  		}
   717  		return b.String()
   718  	}
   719  	return Map(unicode.ToUpper, s)
   720  }
   721  
   722  // ToLower returns s with all Unicode letters mapped to their lower case.
   723  func ToLower(s string) string {
   724  	isASCII, hasUpper := true, false
   725  	for i := 0; i < len(s); i++ {
   726  		c := s[i]
   727  		if c >= utf8.RuneSelf {
   728  			isASCII = false
   729  			break
   730  		}
   731  		hasUpper = hasUpper || ('A' <= c && c <= 'Z')
   732  	}
   733  
   734  	if isASCII { // optimize for ASCII-only strings.
   735  		if !hasUpper {
   736  			return s
   737  		}
   738  		var (
   739  			b   Builder
   740  			pos int
   741  		)
   742  		b.Grow(len(s))
   743  		for i := 0; i < len(s); i++ {
   744  			c := s[i]
   745  			if 'A' <= c && c <= 'Z' {
   746  				c += 'a' - 'A'
   747  				if pos < i {
   748  					b.WriteString(s[pos:i])
   749  				}
   750  				b.WriteByte(c)
   751  				pos = i + 1
   752  			}
   753  		}
   754  		if pos < len(s) {
   755  			b.WriteString(s[pos:])
   756  		}
   757  		return b.String()
   758  	}
   759  	return Map(unicode.ToLower, s)
   760  }
   761  
   762  // ToTitle returns a copy of the string s with all Unicode letters mapped to
   763  // their Unicode title case.
   764  func ToTitle(s string) string { return Map(unicode.ToTitle, s) }
   765  
   766  // ToUpperSpecial returns a copy of the string s with all Unicode letters mapped to their
   767  // upper case using the case mapping specified by c.
   768  func ToUpperSpecial(c unicode.SpecialCase, s string) string {
   769  	return Map(c.ToUpper, s)
   770  }
   771  
   772  // ToLowerSpecial returns a copy of the string s with all Unicode letters mapped to their
   773  // lower case using the case mapping specified by c.
   774  func ToLowerSpecial(c unicode.SpecialCase, s string) string {
   775  	return Map(c.ToLower, s)
   776  }
   777  
   778  // ToTitleSpecial returns a copy of the string s with all Unicode letters mapped to their
   779  // Unicode title case, giving priority to the special casing rules.
   780  func ToTitleSpecial(c unicode.SpecialCase, s string) string {
   781  	return Map(c.ToTitle, s)
   782  }
   783  
   784  // ToValidUTF8 returns a copy of the string s with each run of invalid UTF-8 byte sequences
   785  // replaced by the replacement string, which may be empty.
   786  func ToValidUTF8(s, replacement string) string {
   787  	var b Builder
   788  
   789  	for i, c := range s {
   790  		if c != utf8.RuneError {
   791  			continue
   792  		}
   793  
   794  		_, wid := utf8.DecodeRuneInString(s[i:])
   795  		if wid == 1 {
   796  			b.Grow(len(s) + len(replacement))
   797  			b.WriteString(s[:i])
   798  			s = s[i:]
   799  			break
   800  		}
   801  	}
   802  
   803  	// Fast path for unchanged input
   804  	if b.Cap() == 0 { // didn't call b.Grow above
   805  		return s
   806  	}
   807  
   808  	invalid := false // previous byte was from an invalid UTF-8 sequence
   809  	for i := 0; i < len(s); {
   810  		c := s[i]
   811  		if c < utf8.RuneSelf {
   812  			i++
   813  			invalid = false
   814  			b.WriteByte(c)
   815  			continue
   816  		}
   817  		_, wid := utf8.DecodeRuneInString(s[i:])
   818  		if wid == 1 {
   819  			i++
   820  			if !invalid {
   821  				invalid = true
   822  				b.WriteString(replacement)
   823  			}
   824  			continue
   825  		}
   826  		invalid = false
   827  		b.WriteString(s[i : i+wid])
   828  		i += wid
   829  	}
   830  
   831  	return b.String()
   832  }
   833  
   834  // isSeparator reports whether the rune could mark a word boundary.
   835  // TODO: update when package unicode captures more of the properties.
   836  func isSeparator(r rune) bool {
   837  	// ASCII alphanumerics and underscore are not separators
   838  	if r <= 0x7F {
   839  		switch {
   840  		case '0' <= r && r <= '9':
   841  			return false
   842  		case 'a' <= r && r <= 'z':
   843  			return false
   844  		case 'A' <= r && r <= 'Z':
   845  			return false
   846  		case r == '_':
   847  			return false
   848  		}
   849  		return true
   850  	}
   851  	// Letters and digits are not separators
   852  	if unicode.IsLetter(r) || unicode.IsDigit(r) {
   853  		return false
   854  	}
   855  	// Otherwise, all we can do for now is treat spaces as separators.
   856  	return unicode.IsSpace(r)
   857  }
   858  
   859  // Title returns a copy of the string s with all Unicode letters that begin words
   860  // mapped to their Unicode title case.
   861  //
   862  // Deprecated: The rule Title uses for word boundaries does not handle Unicode
   863  // punctuation properly. Use golang.org/x/text/cases instead.
   864  func Title(s string) string {
   865  	// Use a closure here to remember state.
   866  	// Hackish but effective. Depends on Map scanning in order and calling
   867  	// the closure once per rune.
   868  	prev := ' '
   869  	return Map(
   870  		func(r rune) rune {
   871  			if isSeparator(prev) {
   872  				prev = r
   873  				return unicode.ToTitle(r)
   874  			}
   875  			prev = r
   876  			return r
   877  		},
   878  		s)
   879  }
   880  
   881  // TrimLeftFunc returns a slice of the string s with all leading
   882  // Unicode code points c satisfying f(c) removed.
   883  func TrimLeftFunc(s string, f func(rune) bool) string {
   884  	i := indexFunc(s, f, false)
   885  	if i == -1 {
   886  		return ""
   887  	}
   888  	return s[i:]
   889  }
   890  
   891  // TrimRightFunc returns a slice of the string s with all trailing
   892  // Unicode code points c satisfying f(c) removed.
   893  func TrimRightFunc(s string, f func(rune) bool) string {
   894  	i := lastIndexFunc(s, f, false)
   895  	if i >= 0 && s[i] >= utf8.RuneSelf {
   896  		_, wid := utf8.DecodeRuneInString(s[i:])
   897  		i += wid
   898  	} else {
   899  		i++
   900  	}
   901  	return s[0:i]
   902  }
   903  
   904  // TrimFunc returns a slice of the string s with all leading
   905  // and trailing Unicode code points c satisfying f(c) removed.
   906  func TrimFunc(s string, f func(rune) bool) string {
   907  	return TrimRightFunc(TrimLeftFunc(s, f), f)
   908  }
   909  
   910  // IndexFunc returns the index into s of the first Unicode
   911  // code point satisfying f(c), or -1 if none do.
   912  func IndexFunc(s string, f func(rune) bool) int {
   913  	return indexFunc(s, f, true)
   914  }
   915  
   916  // LastIndexFunc returns the index into s of the last
   917  // Unicode code point satisfying f(c), or -1 if none do.
   918  func LastIndexFunc(s string, f func(rune) bool) int {
   919  	return lastIndexFunc(s, f, true)
   920  }
   921  
   922  // indexFunc is the same as IndexFunc except that if
   923  // truth==false, the sense of the predicate function is
   924  // inverted.
   925  func indexFunc(s string, f func(rune) bool, truth bool) int {
   926  	for i, r := range s {
   927  		if f(r) == truth {
   928  			return i
   929  		}
   930  	}
   931  	return -1
   932  }
   933  
   934  // lastIndexFunc is the same as LastIndexFunc except that if
   935  // truth==false, the sense of the predicate function is
   936  // inverted.
   937  func lastIndexFunc(s string, f func(rune) bool, truth bool) int {
   938  	for i := len(s); i > 0; {
   939  		r, size := utf8.DecodeLastRuneInString(s[0:i])
   940  		i -= size
   941  		if f(r) == truth {
   942  			return i
   943  		}
   944  	}
   945  	return -1
   946  }
   947  
   948  // asciiSet is a 32-byte value, where each bit represents the presence of a
   949  // given ASCII character in the set. The 128-bits of the lower 16 bytes,
   950  // starting with the least-significant bit of the lowest word to the
   951  // most-significant bit of the highest word, map to the full range of all
   952  // 128 ASCII characters. The 128-bits of the upper 16 bytes will be zeroed,
   953  // ensuring that any non-ASCII character will be reported as not in the set.
   954  // This allocates a total of 32 bytes even though the upper half
   955  // is unused to avoid bounds checks in asciiSet.contains.
   956  type asciiSet [8]uint32
   957  
   958  // makeASCIISet creates a set of ASCII characters and reports whether all
   959  // characters in chars are ASCII.
   960  func makeASCIISet(chars string) (as asciiSet, ok bool) {
   961  	for i := 0; i < len(chars); i++ {
   962  		c := chars[i]
   963  		if c >= utf8.RuneSelf {
   964  			return as, false
   965  		}
   966  		as[c/32] |= 1 << (c % 32)
   967  	}
   968  	return as, true
   969  }
   970  
   971  // contains reports whether c is inside the set.
   972  func (as *asciiSet) contains(c byte) bool {
   973  	return (as[c/32] & (1 << (c % 32))) != 0
   974  }
   975  
   976  // Trim returns a slice of the string s with all leading and
   977  // trailing Unicode code points contained in cutset removed.
   978  func Trim(s, cutset string) string {
   979  	if s == "" || cutset == "" {
   980  		return s
   981  	}
   982  	if len(cutset) == 1 && cutset[0] < utf8.RuneSelf {
   983  		return trimLeftByte(trimRightByte(s, cutset[0]), cutset[0])
   984  	}
   985  	if as, ok := makeASCIISet(cutset); ok {
   986  		return trimLeftASCII(trimRightASCII(s, &as), &as)
   987  	}
   988  	return trimLeftUnicode(trimRightUnicode(s, cutset), cutset)
   989  }
   990  
   991  // TrimLeft returns a slice of the string s with all leading
   992  // Unicode code points contained in cutset removed.
   993  //
   994  // To remove a prefix, use [TrimPrefix] instead.
   995  func TrimLeft(s, cutset string) string {
   996  	if s == "" || cutset == "" {
   997  		return s
   998  	}
   999  	if len(cutset) == 1 && cutset[0] < utf8.RuneSelf {
  1000  		return trimLeftByte(s, cutset[0])
  1001  	}
  1002  	if as, ok := makeASCIISet(cutset); ok {
  1003  		return trimLeftASCII(s, &as)
  1004  	}
  1005  	return trimLeftUnicode(s, cutset)
  1006  }
  1007  
  1008  func trimLeftByte(s string, c byte) string {
  1009  	for len(s) > 0 && s[0] == c {
  1010  		s = s[1:]
  1011  	}
  1012  	return s
  1013  }
  1014  
  1015  func trimLeftASCII(s string, as *asciiSet) string {
  1016  	for len(s) > 0 {
  1017  		if !as.contains(s[0]) {
  1018  			break
  1019  		}
  1020  		s = s[1:]
  1021  	}
  1022  	return s
  1023  }
  1024  
  1025  func trimLeftUnicode(s, cutset string) string {
  1026  	for len(s) > 0 {
  1027  		r, n := rune(s[0]), 1
  1028  		if r >= utf8.RuneSelf {
  1029  			r, n = utf8.DecodeRuneInString(s)
  1030  		}
  1031  		if !ContainsRune(cutset, r) {
  1032  			break
  1033  		}
  1034  		s = s[n:]
  1035  	}
  1036  	return s
  1037  }
  1038  
  1039  // TrimRight returns a slice of the string s, with all trailing
  1040  // Unicode code points contained in cutset removed.
  1041  //
  1042  // To remove a suffix, use [TrimSuffix] instead.
  1043  func TrimRight(s, cutset string) string {
  1044  	if s == "" || cutset == "" {
  1045  		return s
  1046  	}
  1047  	if len(cutset) == 1 && cutset[0] < utf8.RuneSelf {
  1048  		return trimRightByte(s, cutset[0])
  1049  	}
  1050  	if as, ok := makeASCIISet(cutset); ok {
  1051  		return trimRightASCII(s, &as)
  1052  	}
  1053  	return trimRightUnicode(s, cutset)
  1054  }
  1055  
  1056  func trimRightByte(s string, c byte) string {
  1057  	for len(s) > 0 && s[len(s)-1] == c {
  1058  		s = s[:len(s)-1]
  1059  	}
  1060  	return s
  1061  }
  1062  
  1063  func trimRightASCII(s string, as *asciiSet) string {
  1064  	for len(s) > 0 {
  1065  		if !as.contains(s[len(s)-1]) {
  1066  			break
  1067  		}
  1068  		s = s[:len(s)-1]
  1069  	}
  1070  	return s
  1071  }
  1072  
  1073  func trimRightUnicode(s, cutset string) string {
  1074  	for len(s) > 0 {
  1075  		r, n := rune(s[len(s)-1]), 1
  1076  		if r >= utf8.RuneSelf {
  1077  			r, n = utf8.DecodeLastRuneInString(s)
  1078  		}
  1079  		if !ContainsRune(cutset, r) {
  1080  			break
  1081  		}
  1082  		s = s[:len(s)-n]
  1083  	}
  1084  	return s
  1085  }
  1086  
  1087  // TrimSpace returns a slice of the string s, with all leading
  1088  // and trailing white space removed, as defined by Unicode.
  1089  func TrimSpace(s string) string {
  1090  	// Fast path for ASCII: look for the first ASCII non-space byte
  1091  	start := 0
  1092  	for ; start < len(s); start++ {
  1093  		c := s[start]
  1094  		if c >= utf8.RuneSelf {
  1095  			// If we run into a non-ASCII byte, fall back to the
  1096  			// slower unicode-aware method on the remaining bytes
  1097  			return TrimFunc(s[start:], unicode.IsSpace)
  1098  		}
  1099  		if asciiSpace[c] == 0 {
  1100  			break
  1101  		}
  1102  	}
  1103  
  1104  	// Now look for the first ASCII non-space byte from the end
  1105  	stop := len(s)
  1106  	for ; stop > start; stop-- {
  1107  		c := s[stop-1]
  1108  		if c >= utf8.RuneSelf {
  1109  			// start has been already trimmed above, should trim end only
  1110  			return TrimRightFunc(s[start:stop], unicode.IsSpace)
  1111  		}
  1112  		if asciiSpace[c] == 0 {
  1113  			break
  1114  		}
  1115  	}
  1116  
  1117  	// At this point s[start:stop] starts and ends with an ASCII
  1118  	// non-space bytes, so we're done. Non-ASCII cases have already
  1119  	// been handled above.
  1120  	return s[start:stop]
  1121  }
  1122  
  1123  // TrimPrefix returns s without the provided leading prefix string.
  1124  // If s doesn't start with prefix, s is returned unchanged.
  1125  func TrimPrefix(s, prefix string) string {
  1126  	return stringslite.TrimPrefix(s, prefix)
  1127  }
  1128  
  1129  // TrimSuffix returns s without the provided trailing suffix string.
  1130  // If s doesn't end with suffix, s is returned unchanged.
  1131  func TrimSuffix(s, suffix string) string {
  1132  	return stringslite.TrimSuffix(s, suffix)
  1133  }
  1134  
  1135  // Replace returns a copy of the string s with the first n
  1136  // non-overlapping instances of old replaced by new.
  1137  // If old is empty, it matches at the beginning of the string
  1138  // and after each UTF-8 sequence, yielding up to k+1 replacements
  1139  // for a k-rune string.
  1140  // If n < 0, there is no limit on the number of replacements.
  1141  func Replace(s, old, new string, n int) string {
  1142  	if old == new || n == 0 {
  1143  		return s // avoid allocation
  1144  	}
  1145  
  1146  	// Compute number of replacements.
  1147  	if m := Count(s, old); m == 0 {
  1148  		return s // avoid allocation
  1149  	} else if n < 0 || m < n {
  1150  		n = m
  1151  	}
  1152  
  1153  	// Apply replacements to buffer.
  1154  	var b Builder
  1155  	b.Grow(len(s) + n*(len(new)-len(old)))
  1156  	start := 0
  1157  	for i := 0; i < n; i++ {
  1158  		j := start
  1159  		if len(old) == 0 {
  1160  			if i > 0 {
  1161  				_, wid := utf8.DecodeRuneInString(s[start:])
  1162  				j += wid
  1163  			}
  1164  		} else {
  1165  			j += Index(s[start:], old)
  1166  		}
  1167  		b.WriteString(s[start:j])
  1168  		b.WriteString(new)
  1169  		start = j + len(old)
  1170  	}
  1171  	b.WriteString(s[start:])
  1172  	return b.String()
  1173  }
  1174  
  1175  // ReplaceAll returns a copy of the string s with all
  1176  // non-overlapping instances of old replaced by new.
  1177  // If old is empty, it matches at the beginning of the string
  1178  // and after each UTF-8 sequence, yielding up to k+1 replacements
  1179  // for a k-rune string.
  1180  func ReplaceAll(s, old, new string) string {
  1181  	return Replace(s, old, new, -1)
  1182  }
  1183  
  1184  // EqualFold reports whether s and t, interpreted as UTF-8 strings,
  1185  // are equal under simple Unicode case-folding, which is a more general
  1186  // form of case-insensitivity.
  1187  func EqualFold(s, t string) bool {
  1188  	// ASCII fast path
  1189  	i := 0
  1190  	for ; i < len(s) && i < len(t); i++ {
  1191  		sr := s[i]
  1192  		tr := t[i]
  1193  		if sr|tr >= utf8.RuneSelf {
  1194  			goto hasUnicode
  1195  		}
  1196  
  1197  		// Easy case.
  1198  		if tr == sr {
  1199  			continue
  1200  		}
  1201  
  1202  		// Make sr < tr to simplify what follows.
  1203  		if tr < sr {
  1204  			tr, sr = sr, tr
  1205  		}
  1206  		// ASCII only, sr/tr must be upper/lower case
  1207  		if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' {
  1208  			continue
  1209  		}
  1210  		return false
  1211  	}
  1212  	// Check if we've exhausted both strings.
  1213  	return len(s) == len(t)
  1214  
  1215  hasUnicode:
  1216  	s = s[i:]
  1217  	t = t[i:]
  1218  	for _, sr := range s {
  1219  		// If t is exhausted the strings are not equal.
  1220  		if len(t) == 0 {
  1221  			return false
  1222  		}
  1223  
  1224  		// Extract first rune from second string.
  1225  		var tr rune
  1226  		if t[0] < utf8.RuneSelf {
  1227  			tr, t = rune(t[0]), t[1:]
  1228  		} else {
  1229  			r, size := utf8.DecodeRuneInString(t)
  1230  			tr, t = r, t[size:]
  1231  		}
  1232  
  1233  		// If they match, keep going; if not, return false.
  1234  
  1235  		// Easy case.
  1236  		if tr == sr {
  1237  			continue
  1238  		}
  1239  
  1240  		// Make sr < tr to simplify what follows.
  1241  		if tr < sr {
  1242  			tr, sr = sr, tr
  1243  		}
  1244  		// Fast check for ASCII.
  1245  		if tr < utf8.RuneSelf {
  1246  			// ASCII only, sr/tr must be upper/lower case
  1247  			if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' {
  1248  				continue
  1249  			}
  1250  			return false
  1251  		}
  1252  
  1253  		// General case. SimpleFold(x) returns the next equivalent rune > x
  1254  		// or wraps around to smaller values.
  1255  		r := unicode.SimpleFold(sr)
  1256  		for r != sr && r < tr {
  1257  			r = unicode.SimpleFold(r)
  1258  		}
  1259  		if r == tr {
  1260  			continue
  1261  		}
  1262  		return false
  1263  	}
  1264  
  1265  	// First string is empty, so check if the second one is also empty.
  1266  	return len(t) == 0
  1267  }
  1268  
  1269  // Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.
  1270  func Index(s, substr string) int {
  1271  	return stringslite.Index(s, substr)
  1272  }
  1273  
  1274  // Cut slices s around the first instance of sep,
  1275  // returning the text before and after sep.
  1276  // The found result reports whether sep appears in s.
  1277  // If sep does not appear in s, cut returns s, "", false.
  1278  func Cut(s, sep string) (before, after string, found bool) {
  1279  	return stringslite.Cut(s, sep)
  1280  }
  1281  
  1282  // CutPrefix returns s without the provided leading prefix string
  1283  // and reports whether it found the prefix.
  1284  // If s doesn't start with prefix, CutPrefix returns s, false.
  1285  // If prefix is the empty string, CutPrefix returns s, true.
  1286  func CutPrefix(s, prefix string) (after string, found bool) {
  1287  	return stringslite.CutPrefix(s, prefix)
  1288  }
  1289  
  1290  // CutSuffix returns s without the provided ending suffix string
  1291  // and reports whether it found the suffix.
  1292  // If s doesn't end with suffix, CutSuffix returns s, false.
  1293  // If suffix is the empty string, CutSuffix returns s, true.
  1294  func CutSuffix(s, suffix string) (before string, found bool) {
  1295  	return stringslite.CutSuffix(s, suffix)
  1296  }
  1297  

View as plain text