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

View as plain text