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

View as plain text