...
Run Format

Source file src/strings/strings.go

Documentation: strings

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

View as plain text