...

Source file src/bytes/bytes.go

Documentation: bytes

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

View as plain text