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

View as plain text