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

View as plain text