github.com/Filosottile/go@v0.0.0-20170906193555-dbed9972d994/src/bytes/bytes.go (about)

     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  	"unicode"
    11  	"unicode/utf8"
    12  )
    13  
    14  func equalPortable(a, b []byte) bool {
    15  	if len(a) != len(b) {
    16  		return false
    17  	}
    18  	for i, c := range a {
    19  		if c != b[i] {
    20  			return false
    21  		}
    22  	}
    23  	return true
    24  }
    25  
    26  // explode splits s into a slice of UTF-8 sequences, one per Unicode code point (still slices of bytes),
    27  // up to a maximum of n byte slices. Invalid UTF-8 sequences are chopped into individual bytes.
    28  func explode(s []byte, n int) [][]byte {
    29  	if n <= 0 {
    30  		n = len(s)
    31  	}
    32  	a := make([][]byte, n)
    33  	var size int
    34  	na := 0
    35  	for len(s) > 0 {
    36  		if na+1 >= n {
    37  			a[na] = s
    38  			na++
    39  			break
    40  		}
    41  		_, size = utf8.DecodeRune(s)
    42  		a[na] = s[0:size]
    43  		s = s[size:]
    44  		na++
    45  	}
    46  	return a[0:na]
    47  }
    48  
    49  // countGeneric actually implements Count
    50  func countGeneric(s, sep []byte) int {
    51  	// special case
    52  	if len(sep) == 0 {
    53  		return utf8.RuneCount(s) + 1
    54  	}
    55  	n := 0
    56  	for {
    57  		i := Index(s, sep)
    58  		if i == -1 {
    59  			return n
    60  		}
    61  		n++
    62  		s = s[i+len(sep):]
    63  	}
    64  }
    65  
    66  // Contains reports whether subslice is within b.
    67  func Contains(b, subslice []byte) bool {
    68  	return Index(b, subslice) != -1
    69  }
    70  
    71  // ContainsAny reports whether any of the UTF-8-encoded Unicode code points in chars are within b.
    72  func ContainsAny(b []byte, chars string) bool {
    73  	return IndexAny(b, chars) >= 0
    74  }
    75  
    76  // ContainsRune reports whether the Unicode code point r is within b.
    77  func ContainsRune(b []byte, r rune) bool {
    78  	return IndexRune(b, r) >= 0
    79  }
    80  
    81  func indexBytePortable(s []byte, c byte) int {
    82  	for i, b := range s {
    83  		if b == c {
    84  			return i
    85  		}
    86  	}
    87  	return -1
    88  }
    89  
    90  // LastIndex returns the index of the last instance of sep in s, or -1 if sep is not present in s.
    91  func LastIndex(s, sep []byte) int {
    92  	n := len(sep)
    93  	if n == 0 {
    94  		return len(s)
    95  	}
    96  	c := sep[0]
    97  	for i := len(s) - n; i >= 0; i-- {
    98  		if s[i] == c && (n == 1 || Equal(s[i:i+n], sep)) {
    99  			return i
   100  		}
   101  	}
   102  	return -1
   103  }
   104  
   105  // LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s.
   106  func LastIndexByte(s []byte, c byte) int {
   107  	for i := len(s) - 1; i >= 0; i-- {
   108  		if s[i] == c {
   109  			return i
   110  		}
   111  	}
   112  	return -1
   113  }
   114  
   115  // IndexRune interprets s as a sequence of UTF-8-encoded Unicode code points.
   116  // It returns the byte index of the first occurrence in s of the given rune.
   117  // It returns -1 if rune is not present in s.
   118  // If r is utf8.RuneError, it returns the first instance of any
   119  // invalid UTF-8 byte sequence.
   120  func IndexRune(s []byte, r rune) int {
   121  	switch {
   122  	case 0 <= r && r < utf8.RuneSelf:
   123  		return IndexByte(s, byte(r))
   124  	case r == utf8.RuneError:
   125  		for i := 0; i < len(s); {
   126  			r1, n := utf8.DecodeRune(s[i:])
   127  			if r1 == utf8.RuneError {
   128  				return i
   129  			}
   130  			i += n
   131  		}
   132  		return -1
   133  	case !utf8.ValidRune(r):
   134  		return -1
   135  	default:
   136  		var b [utf8.UTFMax]byte
   137  		n := utf8.EncodeRune(b[:], r)
   138  		return Index(s, b[:n])
   139  	}
   140  }
   141  
   142  // IndexAny interprets s as a sequence of UTF-8-encoded Unicode code points.
   143  // It returns the byte index of the first occurrence in s of any of the Unicode
   144  // code points in chars. It returns -1 if chars is empty or if there is no code
   145  // point in common.
   146  func IndexAny(s []byte, chars string) int {
   147  	if len(chars) > 0 {
   148  		if len(s) > 8 {
   149  			if as, isASCII := makeASCIISet(chars); isASCII {
   150  				for i, c := range s {
   151  					if as.contains(c) {
   152  						return i
   153  					}
   154  				}
   155  				return -1
   156  			}
   157  		}
   158  		var width int
   159  		for i := 0; i < len(s); i += width {
   160  			r := rune(s[i])
   161  			if r < utf8.RuneSelf {
   162  				width = 1
   163  			} else {
   164  				r, width = utf8.DecodeRune(s[i:])
   165  			}
   166  			for _, ch := range chars {
   167  				if r == ch {
   168  					return i
   169  				}
   170  			}
   171  		}
   172  	}
   173  	return -1
   174  }
   175  
   176  // LastIndexAny interprets s as a sequence of UTF-8-encoded Unicode code
   177  // points. It returns the byte index of the last occurrence in s of any of
   178  // the Unicode code points in chars. It returns -1 if chars is empty or if
   179  // there is no code point in common.
   180  func LastIndexAny(s []byte, chars string) int {
   181  	if len(chars) > 0 {
   182  		if len(s) > 8 {
   183  			if as, isASCII := makeASCIISet(chars); isASCII {
   184  				for i := len(s) - 1; i >= 0; i-- {
   185  					if as.contains(s[i]) {
   186  						return i
   187  					}
   188  				}
   189  				return -1
   190  			}
   191  		}
   192  		for i := len(s); i > 0; {
   193  			r, size := utf8.DecodeLastRune(s[:i])
   194  			i -= size
   195  			for _, c := range chars {
   196  				if r == c {
   197  					return i
   198  				}
   199  			}
   200  		}
   201  	}
   202  	return -1
   203  }
   204  
   205  // Generic split: splits after each instance of sep,
   206  // including sepSave bytes of sep in the subslices.
   207  func genSplit(s, sep []byte, sepSave, n int) [][]byte {
   208  	if n == 0 {
   209  		return nil
   210  	}
   211  	if len(sep) == 0 {
   212  		return explode(s, n)
   213  	}
   214  	if n < 0 {
   215  		n = Count(s, sep) + 1
   216  	}
   217  
   218  	a := make([][]byte, n)
   219  	n--
   220  	i := 0
   221  	for i < n {
   222  		m := Index(s, sep)
   223  		if m < 0 {
   224  			break
   225  		}
   226  		a[i] = s[:m+sepSave]
   227  		s = s[m+len(sep):]
   228  		i++
   229  	}
   230  	a[i] = s
   231  	return a[:i+1]
   232  }
   233  
   234  // SplitN slices s into subslices separated by sep and returns a slice of
   235  // the subslices between those separators.
   236  // If sep is empty, SplitN splits after each UTF-8 sequence.
   237  // The count determines the number of subslices to return:
   238  //   n > 0: at most n subslices; the last subslice will be the unsplit remainder.
   239  //   n == 0: the result is nil (zero subslices)
   240  //   n < 0: all subslices
   241  func SplitN(s, sep []byte, n int) [][]byte { return genSplit(s, sep, 0, n) }
   242  
   243  // SplitAfterN slices s into subslices after each instance of sep and
   244  // returns a slice of those subslices.
   245  // If sep is empty, SplitAfterN splits after each UTF-8 sequence.
   246  // The count determines the number of subslices to return:
   247  //   n > 0: at most n subslices; the last subslice will be the unsplit remainder.
   248  //   n == 0: the result is nil (zero subslices)
   249  //   n < 0: all subslices
   250  func SplitAfterN(s, sep []byte, n int) [][]byte {
   251  	return genSplit(s, sep, len(sep), n)
   252  }
   253  
   254  // Split slices s into all subslices separated by sep and returns a slice of
   255  // the subslices between those separators.
   256  // If sep is empty, Split splits after each UTF-8 sequence.
   257  // It is equivalent to SplitN with a count of -1.
   258  func Split(s, sep []byte) [][]byte { return genSplit(s, sep, 0, -1) }
   259  
   260  // SplitAfter slices s into all subslices after each instance of sep and
   261  // returns a slice of those subslices.
   262  // If sep is empty, SplitAfter splits after each UTF-8 sequence.
   263  // It is equivalent to SplitAfterN with a count of -1.
   264  func SplitAfter(s, sep []byte) [][]byte {
   265  	return genSplit(s, sep, len(sep), -1)
   266  }
   267  
   268  var asciiSpace = [256]uint8{'\t': 1, '\n': 1, '\v': 1, '\f': 1, '\r': 1, ' ': 1}
   269  
   270  // Fields splits the slice s around each instance of one or more consecutive white space
   271  // characters, as defined by unicode.IsSpace, returning a slice of subslices of s or an
   272  // empty slice if s contains only white space.
   273  func Fields(s []byte) [][]byte {
   274  	// First count the fields.
   275  	// This is an exact count if s is ASCII, otherwise it is an approximation.
   276  	n := 0
   277  	wasSpace := 1
   278  	// setBits is used to track which bits are set in the bytes of s.
   279  	setBits := uint8(0)
   280  	for i := 0; i < len(s); i++ {
   281  		r := s[i]
   282  		setBits |= r
   283  		isSpace := int(asciiSpace[r])
   284  		n += wasSpace & ^isSpace
   285  		wasSpace = isSpace
   286  	}
   287  
   288  	if setBits >= utf8.RuneSelf {
   289  		// Some runes in the input slice are not ASCII.
   290  		return FieldsFunc(s, unicode.IsSpace)
   291  	}
   292  
   293  	// ASCII fast path
   294  	a := make([][]byte, n)
   295  	na := 0
   296  	fieldStart := 0
   297  	i := 0
   298  	// Skip spaces in the front of the input.
   299  	for i < len(s) && asciiSpace[s[i]] != 0 {
   300  		i++
   301  	}
   302  	fieldStart = i
   303  	for i < len(s) {
   304  		if asciiSpace[s[i]] == 0 {
   305  			i++
   306  			continue
   307  		}
   308  		a[na] = s[fieldStart:i]
   309  		na++
   310  		i++
   311  		// Skip spaces in between fields.
   312  		for i < len(s) && asciiSpace[s[i]] != 0 {
   313  			i++
   314  		}
   315  		fieldStart = i
   316  	}
   317  	if fieldStart < len(s) { // Last field might end at EOF.
   318  		a[na] = s[fieldStart:]
   319  	}
   320  	return a
   321  }
   322  
   323  // FieldsFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
   324  // It splits the slice s at each run of code points c satisfying f(c) and
   325  // returns a slice of subslices of s. If all code points in s satisfy f(c), or
   326  // len(s) == 0, an empty slice is returned.
   327  // FieldsFunc makes no guarantees about the order in which it calls f(c).
   328  // If f does not return consistent results for a given c, FieldsFunc may crash.
   329  func FieldsFunc(s []byte, f func(rune) bool) [][]byte {
   330  	// A span is used to record a slice of s of the form s[start:end].
   331  	// The start index is inclusive and the end index is exclusive.
   332  	type span struct {
   333  		start int
   334  		end   int
   335  	}
   336  	spans := make([]span, 0, 32)
   337  
   338  	// Find the field start and end indices.
   339  	wasField := false
   340  	fromIndex := 0
   341  	for i := 0; i < len(s); {
   342  		size := 1
   343  		r := rune(s[i])
   344  		if r >= utf8.RuneSelf {
   345  			r, size = utf8.DecodeRune(s[i:])
   346  		}
   347  		if f(r) {
   348  			if wasField {
   349  				spans = append(spans, span{start: fromIndex, end: i})
   350  				wasField = false
   351  			}
   352  		} else {
   353  			if !wasField {
   354  				fromIndex = i
   355  				wasField = true
   356  			}
   357  		}
   358  		i += size
   359  	}
   360  
   361  	// Last field might end at EOF.
   362  	if wasField {
   363  		spans = append(spans, span{fromIndex, len(s)})
   364  	}
   365  
   366  	// Create subslices from recorded field indices.
   367  	a := make([][]byte, len(spans))
   368  	for i, span := range spans {
   369  		a[i] = s[span.start:span.end]
   370  	}
   371  
   372  	return a
   373  }
   374  
   375  // Join concatenates the elements of s to create a new byte slice. The separator
   376  // sep is placed between elements in the resulting slice.
   377  func Join(s [][]byte, sep []byte) []byte {
   378  	if len(s) == 0 {
   379  		return []byte{}
   380  	}
   381  	if len(s) == 1 {
   382  		// Just return a copy.
   383  		return append([]byte(nil), s[0]...)
   384  	}
   385  	n := len(sep) * (len(s) - 1)
   386  	for _, v := range s {
   387  		n += len(v)
   388  	}
   389  
   390  	b := make([]byte, n)
   391  	bp := copy(b, s[0])
   392  	for _, v := range s[1:] {
   393  		bp += copy(b[bp:], sep)
   394  		bp += copy(b[bp:], v)
   395  	}
   396  	return b
   397  }
   398  
   399  // HasPrefix tests whether the byte slice s begins with prefix.
   400  func HasPrefix(s, prefix []byte) bool {
   401  	return len(s) >= len(prefix) && Equal(s[0:len(prefix)], prefix)
   402  }
   403  
   404  // HasSuffix tests whether the byte slice s ends with suffix.
   405  func HasSuffix(s, suffix []byte) bool {
   406  	return len(s) >= len(suffix) && Equal(s[len(s)-len(suffix):], suffix)
   407  }
   408  
   409  // Map returns a copy of the byte slice s with all its characters modified
   410  // according to the mapping function. If mapping returns a negative value, the character is
   411  // dropped from the string with no replacement. The characters in s and the
   412  // output are interpreted as UTF-8-encoded Unicode code points.
   413  func Map(mapping func(r rune) rune, s []byte) []byte {
   414  	// In the worst case, the slice can grow when mapped, making
   415  	// things unpleasant. But it's so rare we barge in assuming it's
   416  	// fine. It could also shrink but that falls out naturally.
   417  	maxbytes := len(s) // length of b
   418  	nbytes := 0        // number of bytes encoded in b
   419  	b := make([]byte, maxbytes)
   420  	for i := 0; i < len(s); {
   421  		wid := 1
   422  		r := rune(s[i])
   423  		if r >= utf8.RuneSelf {
   424  			r, wid = utf8.DecodeRune(s[i:])
   425  		}
   426  		r = mapping(r)
   427  		if r >= 0 {
   428  			rl := utf8.RuneLen(r)
   429  			if rl < 0 {
   430  				rl = len(string(utf8.RuneError))
   431  			}
   432  			if nbytes+rl > maxbytes {
   433  				// Grow the buffer.
   434  				maxbytes = maxbytes*2 + utf8.UTFMax
   435  				nb := make([]byte, maxbytes)
   436  				copy(nb, b[0:nbytes])
   437  				b = nb
   438  			}
   439  			nbytes += utf8.EncodeRune(b[nbytes:maxbytes], r)
   440  		}
   441  		i += wid
   442  	}
   443  	return b[0:nbytes]
   444  }
   445  
   446  // Repeat returns a new byte slice consisting of count copies of b.
   447  //
   448  // It panics if count is negative or if
   449  // the result of (len(b) * count) overflows.
   450  func Repeat(b []byte, count int) []byte {
   451  	// Since we cannot return an error on overflow,
   452  	// we should panic if the repeat will generate
   453  	// an overflow.
   454  	// See Issue golang.org/issue/16237.
   455  	if count < 0 {
   456  		panic("bytes: negative Repeat count")
   457  	} else if count > 0 && len(b)*count/count != len(b) {
   458  		panic("bytes: Repeat count causes overflow")
   459  	}
   460  
   461  	nb := make([]byte, len(b)*count)
   462  	bp := copy(nb, b)
   463  	for bp < len(nb) {
   464  		copy(nb[bp:], nb[:bp])
   465  		bp *= 2
   466  	}
   467  	return nb
   468  }
   469  
   470  // ToUpper returns a copy of the byte slice s with all Unicode letters mapped to their upper case.
   471  func ToUpper(s []byte) []byte { return Map(unicode.ToUpper, s) }
   472  
   473  // ToLower returns a copy of the byte slice s with all Unicode letters mapped to their lower case.
   474  func ToLower(s []byte) []byte { return Map(unicode.ToLower, s) }
   475  
   476  // ToTitle returns a copy of the byte slice s with all Unicode letters mapped to their title case.
   477  func ToTitle(s []byte) []byte { return Map(unicode.ToTitle, s) }
   478  
   479  // ToUpperSpecial returns a copy of the byte slice s with all Unicode letters mapped to their
   480  // upper case, giving priority to the special casing rules.
   481  func ToUpperSpecial(c unicode.SpecialCase, s []byte) []byte {
   482  	return Map(func(r rune) rune { return c.ToUpper(r) }, s)
   483  }
   484  
   485  // ToLowerSpecial returns a copy of the byte slice s with all Unicode letters mapped to their
   486  // lower case, giving priority to the special casing rules.
   487  func ToLowerSpecial(c unicode.SpecialCase, s []byte) []byte {
   488  	return Map(func(r rune) rune { return c.ToLower(r) }, s)
   489  }
   490  
   491  // ToTitleSpecial returns a copy of the byte slice s with all Unicode letters mapped to their
   492  // title case, giving priority to the special casing rules.
   493  func ToTitleSpecial(c unicode.SpecialCase, s []byte) []byte {
   494  	return Map(func(r rune) rune { return c.ToTitle(r) }, s)
   495  }
   496  
   497  // isSeparator reports whether the rune could mark a word boundary.
   498  // TODO: update when package unicode captures more of the properties.
   499  func isSeparator(r rune) bool {
   500  	// ASCII alphanumerics and underscore are not separators
   501  	if r <= 0x7F {
   502  		switch {
   503  		case '0' <= r && r <= '9':
   504  			return false
   505  		case 'a' <= r && r <= 'z':
   506  			return false
   507  		case 'A' <= r && r <= 'Z':
   508  			return false
   509  		case r == '_':
   510  			return false
   511  		}
   512  		return true
   513  	}
   514  	// Letters and digits are not separators
   515  	if unicode.IsLetter(r) || unicode.IsDigit(r) {
   516  		return false
   517  	}
   518  	// Otherwise, all we can do for now is treat spaces as separators.
   519  	return unicode.IsSpace(r)
   520  }
   521  
   522  // Title returns a copy of s with all Unicode letters that begin words
   523  // mapped to their title case.
   524  //
   525  // BUG(rsc): The rule Title uses for word boundaries does not handle Unicode punctuation properly.
   526  func Title(s []byte) []byte {
   527  	// Use a closure here to remember state.
   528  	// Hackish but effective. Depends on Map scanning in order and calling
   529  	// the closure once per rune.
   530  	prev := ' '
   531  	return Map(
   532  		func(r rune) rune {
   533  			if isSeparator(prev) {
   534  				prev = r
   535  				return unicode.ToTitle(r)
   536  			}
   537  			prev = r
   538  			return r
   539  		},
   540  		s)
   541  }
   542  
   543  // TrimLeftFunc returns a subslice of s by slicing off all leading UTF-8-encoded
   544  // Unicode code points c that satisfy f(c).
   545  func TrimLeftFunc(s []byte, f func(r rune) bool) []byte {
   546  	i := indexFunc(s, f, false)
   547  	if i == -1 {
   548  		return nil
   549  	}
   550  	return s[i:]
   551  }
   552  
   553  // TrimRightFunc returns a subslice of s by slicing off all trailing UTF-8
   554  // encoded Unicode code points c that satisfy f(c).
   555  func TrimRightFunc(s []byte, f func(r rune) bool) []byte {
   556  	i := lastIndexFunc(s, f, false)
   557  	if i >= 0 && s[i] >= utf8.RuneSelf {
   558  		_, wid := utf8.DecodeRune(s[i:])
   559  		i += wid
   560  	} else {
   561  		i++
   562  	}
   563  	return s[0:i]
   564  }
   565  
   566  // TrimFunc returns a subslice of s by slicing off all leading and trailing
   567  // UTF-8-encoded Unicode code points c that satisfy f(c).
   568  func TrimFunc(s []byte, f func(r rune) bool) []byte {
   569  	return TrimRightFunc(TrimLeftFunc(s, f), f)
   570  }
   571  
   572  // TrimPrefix returns s without the provided leading prefix string.
   573  // If s doesn't start with prefix, s is returned unchanged.
   574  func TrimPrefix(s, prefix []byte) []byte {
   575  	if HasPrefix(s, prefix) {
   576  		return s[len(prefix):]
   577  	}
   578  	return s
   579  }
   580  
   581  // TrimSuffix returns s without the provided trailing suffix string.
   582  // If s doesn't end with suffix, s is returned unchanged.
   583  func TrimSuffix(s, suffix []byte) []byte {
   584  	if HasSuffix(s, suffix) {
   585  		return s[:len(s)-len(suffix)]
   586  	}
   587  	return s
   588  }
   589  
   590  // IndexFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
   591  // It returns the byte index in s of the first Unicode
   592  // code point satisfying f(c), or -1 if none do.
   593  func IndexFunc(s []byte, f func(r rune) bool) int {
   594  	return indexFunc(s, f, true)
   595  }
   596  
   597  // LastIndexFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
   598  // It returns the byte index in s of the last Unicode
   599  // code point satisfying f(c), or -1 if none do.
   600  func LastIndexFunc(s []byte, f func(r rune) bool) int {
   601  	return lastIndexFunc(s, f, true)
   602  }
   603  
   604  // indexFunc is the same as IndexFunc except that if
   605  // truth==false, the sense of the predicate function is
   606  // inverted.
   607  func indexFunc(s []byte, f func(r rune) bool, truth bool) int {
   608  	start := 0
   609  	for start < len(s) {
   610  		wid := 1
   611  		r := rune(s[start])
   612  		if r >= utf8.RuneSelf {
   613  			r, wid = utf8.DecodeRune(s[start:])
   614  		}
   615  		if f(r) == truth {
   616  			return start
   617  		}
   618  		start += wid
   619  	}
   620  	return -1
   621  }
   622  
   623  // lastIndexFunc is the same as LastIndexFunc except that if
   624  // truth==false, the sense of the predicate function is
   625  // inverted.
   626  func lastIndexFunc(s []byte, f func(r rune) bool, truth bool) int {
   627  	for i := len(s); i > 0; {
   628  		r, size := rune(s[i-1]), 1
   629  		if r >= utf8.RuneSelf {
   630  			r, size = utf8.DecodeLastRune(s[0:i])
   631  		}
   632  		i -= size
   633  		if f(r) == truth {
   634  			return i
   635  		}
   636  	}
   637  	return -1
   638  }
   639  
   640  // asciiSet is a 32-byte value, where each bit represents the presence of a
   641  // given ASCII character in the set. The 128-bits of the lower 16 bytes,
   642  // starting with the least-significant bit of the lowest word to the
   643  // most-significant bit of the highest word, map to the full range of all
   644  // 128 ASCII characters. The 128-bits of the upper 16 bytes will be zeroed,
   645  // ensuring that any non-ASCII character will be reported as not in the set.
   646  type asciiSet [8]uint32
   647  
   648  // makeASCIISet creates a set of ASCII characters and reports whether all
   649  // characters in chars are ASCII.
   650  func makeASCIISet(chars string) (as asciiSet, ok bool) {
   651  	for i := 0; i < len(chars); i++ {
   652  		c := chars[i]
   653  		if c >= utf8.RuneSelf {
   654  			return as, false
   655  		}
   656  		as[c>>5] |= 1 << uint(c&31)
   657  	}
   658  	return as, true
   659  }
   660  
   661  // contains reports whether c is inside the set.
   662  func (as *asciiSet) contains(c byte) bool {
   663  	return (as[c>>5] & (1 << uint(c&31))) != 0
   664  }
   665  
   666  func makeCutsetFunc(cutset string) func(r rune) bool {
   667  	if len(cutset) == 1 && cutset[0] < utf8.RuneSelf {
   668  		return func(r rune) bool {
   669  			return r == rune(cutset[0])
   670  		}
   671  	}
   672  	if as, isASCII := makeASCIISet(cutset); isASCII {
   673  		return func(r rune) bool {
   674  			return r < utf8.RuneSelf && as.contains(byte(r))
   675  		}
   676  	}
   677  	return func(r rune) bool {
   678  		for _, c := range cutset {
   679  			if c == r {
   680  				return true
   681  			}
   682  		}
   683  		return false
   684  	}
   685  }
   686  
   687  // Trim returns a subslice of s by slicing off all leading and
   688  // trailing UTF-8-encoded Unicode code points contained in cutset.
   689  func Trim(s []byte, cutset string) []byte {
   690  	return TrimFunc(s, makeCutsetFunc(cutset))
   691  }
   692  
   693  // TrimLeft returns a subslice of s by slicing off all leading
   694  // UTF-8-encoded Unicode code points contained in cutset.
   695  func TrimLeft(s []byte, cutset string) []byte {
   696  	return TrimLeftFunc(s, makeCutsetFunc(cutset))
   697  }
   698  
   699  // TrimRight returns a subslice of s by slicing off all trailing
   700  // UTF-8-encoded Unicode code points that are contained in cutset.
   701  func TrimRight(s []byte, cutset string) []byte {
   702  	return TrimRightFunc(s, makeCutsetFunc(cutset))
   703  }
   704  
   705  // TrimSpace returns a subslice of s by slicing off all leading and
   706  // trailing white space, as defined by Unicode.
   707  func TrimSpace(s []byte) []byte {
   708  	return TrimFunc(s, unicode.IsSpace)
   709  }
   710  
   711  // Runes returns a slice of runes (Unicode code points) equivalent to s.
   712  func Runes(s []byte) []rune {
   713  	t := make([]rune, utf8.RuneCount(s))
   714  	i := 0
   715  	for len(s) > 0 {
   716  		r, l := utf8.DecodeRune(s)
   717  		t[i] = r
   718  		i++
   719  		s = s[l:]
   720  	}
   721  	return t
   722  }
   723  
   724  // Replace returns a copy of the slice s with the first n
   725  // non-overlapping instances of old replaced by new.
   726  // If old is empty, it matches at the beginning of the slice
   727  // and after each UTF-8 sequence, yielding up to k+1 replacements
   728  // for a k-rune slice.
   729  // If n < 0, there is no limit on the number of replacements.
   730  func Replace(s, old, new []byte, n int) []byte {
   731  	m := 0
   732  	if n != 0 {
   733  		// Compute number of replacements.
   734  		m = Count(s, old)
   735  	}
   736  	if m == 0 {
   737  		// Just return a copy.
   738  		return append([]byte(nil), s...)
   739  	}
   740  	if n < 0 || m < n {
   741  		n = m
   742  	}
   743  
   744  	// Apply replacements to buffer.
   745  	t := make([]byte, len(s)+n*(len(new)-len(old)))
   746  	w := 0
   747  	start := 0
   748  	for i := 0; i < n; i++ {
   749  		j := start
   750  		if len(old) == 0 {
   751  			if i > 0 {
   752  				_, wid := utf8.DecodeRune(s[start:])
   753  				j += wid
   754  			}
   755  		} else {
   756  			j += Index(s[start:], old)
   757  		}
   758  		w += copy(t[w:], s[start:j])
   759  		w += copy(t[w:], new)
   760  		start = j + len(old)
   761  	}
   762  	w += copy(t[w:], s[start:])
   763  	return t[0:w]
   764  }
   765  
   766  // EqualFold reports whether s and t, interpreted as UTF-8 strings,
   767  // are equal under Unicode case-folding.
   768  func EqualFold(s, t []byte) bool {
   769  	for len(s) != 0 && len(t) != 0 {
   770  		// Extract first rune from each.
   771  		var sr, tr rune
   772  		if s[0] < utf8.RuneSelf {
   773  			sr, s = rune(s[0]), s[1:]
   774  		} else {
   775  			r, size := utf8.DecodeRune(s)
   776  			sr, s = r, s[size:]
   777  		}
   778  		if t[0] < utf8.RuneSelf {
   779  			tr, t = rune(t[0]), t[1:]
   780  		} else {
   781  			r, size := utf8.DecodeRune(t)
   782  			tr, t = r, t[size:]
   783  		}
   784  
   785  		// If they match, keep going; if not, return false.
   786  
   787  		// Easy case.
   788  		if tr == sr {
   789  			continue
   790  		}
   791  
   792  		// Make sr < tr to simplify what follows.
   793  		if tr < sr {
   794  			tr, sr = sr, tr
   795  		}
   796  		// Fast check for ASCII.
   797  		if tr < utf8.RuneSelf && 'A' <= sr && sr <= 'Z' {
   798  			// ASCII, and sr is upper case.  tr must be lower case.
   799  			if tr == sr+'a'-'A' {
   800  				continue
   801  			}
   802  			return false
   803  		}
   804  
   805  		// General case. SimpleFold(x) returns the next equivalent rune > x
   806  		// or wraps around to smaller values.
   807  		r := unicode.SimpleFold(sr)
   808  		for r != sr && r < tr {
   809  			r = unicode.SimpleFold(r)
   810  		}
   811  		if r == tr {
   812  			continue
   813  		}
   814  		return false
   815  	}
   816  
   817  	// One string is empty. Are both?
   818  	return len(s) == len(t)
   819  }