github.com/flyinox/gosm@v0.0.0-20171117061539-16768cb62077/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  // Fields splits the slice s around each instance of one or more consecutive white space
   269  // characters, returning a slice of subslices of s or an empty list if s contains only white space.
   270  func Fields(s []byte) [][]byte {
   271  	return FieldsFunc(s, unicode.IsSpace)
   272  }
   273  
   274  // FieldsFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
   275  // It splits the slice s at each run of code points c satisfying f(c) and
   276  // returns a slice of subslices of s. If all code points in s satisfy f(c), or
   277  // len(s) == 0, an empty slice is returned.
   278  // FieldsFunc makes no guarantees about the order in which it calls f(c).
   279  // If f does not return consistent results for a given c, FieldsFunc may crash.
   280  func FieldsFunc(s []byte, f func(rune) bool) [][]byte {
   281  	n := 0
   282  	inField := false
   283  	for i := 0; i < len(s); {
   284  		r, size := utf8.DecodeRune(s[i:])
   285  		wasInField := inField
   286  		inField = !f(r)
   287  		if inField && !wasInField {
   288  			n++
   289  		}
   290  		i += size
   291  	}
   292  
   293  	a := make([][]byte, n)
   294  	na := 0
   295  	fieldStart := -1
   296  	for i := 0; i <= len(s) && na < n; {
   297  		r, size := utf8.DecodeRune(s[i:])
   298  		if fieldStart < 0 && size > 0 && !f(r) {
   299  			fieldStart = i
   300  			i += size
   301  			continue
   302  		}
   303  		if fieldStart >= 0 && (size == 0 || f(r)) {
   304  			a[na] = s[fieldStart:i]
   305  			na++
   306  			fieldStart = -1
   307  		}
   308  		if size == 0 {
   309  			break
   310  		}
   311  		i += size
   312  	}
   313  	return a[0:na]
   314  }
   315  
   316  // Join concatenates the elements of s to create a new byte slice. The separator
   317  // sep is placed between elements in the resulting slice.
   318  func Join(s [][]byte, sep []byte) []byte {
   319  	if len(s) == 0 {
   320  		return []byte{}
   321  	}
   322  	if len(s) == 1 {
   323  		// Just return a copy.
   324  		return append([]byte(nil), s[0]...)
   325  	}
   326  	n := len(sep) * (len(s) - 1)
   327  	for _, v := range s {
   328  		n += len(v)
   329  	}
   330  
   331  	b := make([]byte, n)
   332  	bp := copy(b, s[0])
   333  	for _, v := range s[1:] {
   334  		bp += copy(b[bp:], sep)
   335  		bp += copy(b[bp:], v)
   336  	}
   337  	return b
   338  }
   339  
   340  // HasPrefix tests whether the byte slice s begins with prefix.
   341  func HasPrefix(s, prefix []byte) bool {
   342  	return len(s) >= len(prefix) && Equal(s[0:len(prefix)], prefix)
   343  }
   344  
   345  // HasSuffix tests whether the byte slice s ends with suffix.
   346  func HasSuffix(s, suffix []byte) bool {
   347  	return len(s) >= len(suffix) && Equal(s[len(s)-len(suffix):], suffix)
   348  }
   349  
   350  // Map returns a copy of the byte slice s with all its characters modified
   351  // according to the mapping function. If mapping returns a negative value, the character is
   352  // dropped from the string with no replacement. The characters in s and the
   353  // output are interpreted as UTF-8-encoded Unicode code points.
   354  func Map(mapping func(r rune) rune, s []byte) []byte {
   355  	// In the worst case, the slice can grow when mapped, making
   356  	// things unpleasant. But it's so rare we barge in assuming it's
   357  	// fine. It could also shrink but that falls out naturally.
   358  	maxbytes := len(s) // length of b
   359  	nbytes := 0        // number of bytes encoded in b
   360  	b := make([]byte, maxbytes)
   361  	for i := 0; i < len(s); {
   362  		wid := 1
   363  		r := rune(s[i])
   364  		if r >= utf8.RuneSelf {
   365  			r, wid = utf8.DecodeRune(s[i:])
   366  		}
   367  		r = mapping(r)
   368  		if r >= 0 {
   369  			rl := utf8.RuneLen(r)
   370  			if rl < 0 {
   371  				rl = len(string(utf8.RuneError))
   372  			}
   373  			if nbytes+rl > maxbytes {
   374  				// Grow the buffer.
   375  				maxbytes = maxbytes*2 + utf8.UTFMax
   376  				nb := make([]byte, maxbytes)
   377  				copy(nb, b[0:nbytes])
   378  				b = nb
   379  			}
   380  			nbytes += utf8.EncodeRune(b[nbytes:maxbytes], r)
   381  		}
   382  		i += wid
   383  	}
   384  	return b[0:nbytes]
   385  }
   386  
   387  // Repeat returns a new byte slice consisting of count copies of b.
   388  //
   389  // It panics if count is negative or if
   390  // the result of (len(b) * count) overflows.
   391  func Repeat(b []byte, count int) []byte {
   392  	// Since we cannot return an error on overflow,
   393  	// we should panic if the repeat will generate
   394  	// an overflow.
   395  	// See Issue golang.org/issue/16237.
   396  	if count < 0 {
   397  		panic("bytes: negative Repeat count")
   398  	} else if count > 0 && len(b)*count/count != len(b) {
   399  		panic("bytes: Repeat count causes overflow")
   400  	}
   401  
   402  	nb := make([]byte, len(b)*count)
   403  	bp := copy(nb, b)
   404  	for bp < len(nb) {
   405  		copy(nb[bp:], nb[:bp])
   406  		bp *= 2
   407  	}
   408  	return nb
   409  }
   410  
   411  // ToUpper returns a copy of the byte slice s with all Unicode letters mapped to their upper case.
   412  func ToUpper(s []byte) []byte { return Map(unicode.ToUpper, s) }
   413  
   414  // ToLower returns a copy of the byte slice s with all Unicode letters mapped to their lower case.
   415  func ToLower(s []byte) []byte { return Map(unicode.ToLower, s) }
   416  
   417  // ToTitle returns a copy of the byte slice s with all Unicode letters mapped to their title case.
   418  func ToTitle(s []byte) []byte { return Map(unicode.ToTitle, s) }
   419  
   420  // ToUpperSpecial returns a copy of the byte slice s with all Unicode letters mapped to their
   421  // upper case, giving priority to the special casing rules.
   422  func ToUpperSpecial(c unicode.SpecialCase, s []byte) []byte {
   423  	return Map(func(r rune) rune { return c.ToUpper(r) }, s)
   424  }
   425  
   426  // ToLowerSpecial returns a copy of the byte slice s with all Unicode letters mapped to their
   427  // lower case, giving priority to the special casing rules.
   428  func ToLowerSpecial(c unicode.SpecialCase, s []byte) []byte {
   429  	return Map(func(r rune) rune { return c.ToLower(r) }, s)
   430  }
   431  
   432  // ToTitleSpecial returns a copy of the byte slice s with all Unicode letters mapped to their
   433  // title case, giving priority to the special casing rules.
   434  func ToTitleSpecial(c unicode.SpecialCase, s []byte) []byte {
   435  	return Map(func(r rune) rune { return c.ToTitle(r) }, s)
   436  }
   437  
   438  // isSeparator reports whether the rune could mark a word boundary.
   439  // TODO: update when package unicode captures more of the properties.
   440  func isSeparator(r rune) bool {
   441  	// ASCII alphanumerics and underscore are not separators
   442  	if r <= 0x7F {
   443  		switch {
   444  		case '0' <= r && r <= '9':
   445  			return false
   446  		case 'a' <= r && r <= 'z':
   447  			return false
   448  		case 'A' <= r && r <= 'Z':
   449  			return false
   450  		case r == '_':
   451  			return false
   452  		}
   453  		return true
   454  	}
   455  	// Letters and digits are not separators
   456  	if unicode.IsLetter(r) || unicode.IsDigit(r) {
   457  		return false
   458  	}
   459  	// Otherwise, all we can do for now is treat spaces as separators.
   460  	return unicode.IsSpace(r)
   461  }
   462  
   463  // Title returns a copy of s with all Unicode letters that begin words
   464  // mapped to their title case.
   465  //
   466  // BUG(rsc): The rule Title uses for word boundaries does not handle Unicode punctuation properly.
   467  func Title(s []byte) []byte {
   468  	// Use a closure here to remember state.
   469  	// Hackish but effective. Depends on Map scanning in order and calling
   470  	// the closure once per rune.
   471  	prev := ' '
   472  	return Map(
   473  		func(r rune) rune {
   474  			if isSeparator(prev) {
   475  				prev = r
   476  				return unicode.ToTitle(r)
   477  			}
   478  			prev = r
   479  			return r
   480  		},
   481  		s)
   482  }
   483  
   484  // TrimLeftFunc returns a subslice of s by slicing off all leading UTF-8-encoded
   485  // Unicode code points c that satisfy f(c).
   486  func TrimLeftFunc(s []byte, f func(r rune) bool) []byte {
   487  	i := indexFunc(s, f, false)
   488  	if i == -1 {
   489  		return nil
   490  	}
   491  	return s[i:]
   492  }
   493  
   494  // TrimRightFunc returns a subslice of s by slicing off all trailing UTF-8
   495  // encoded Unicode code points c that satisfy f(c).
   496  func TrimRightFunc(s []byte, f func(r rune) bool) []byte {
   497  	i := lastIndexFunc(s, f, false)
   498  	if i >= 0 && s[i] >= utf8.RuneSelf {
   499  		_, wid := utf8.DecodeRune(s[i:])
   500  		i += wid
   501  	} else {
   502  		i++
   503  	}
   504  	return s[0:i]
   505  }
   506  
   507  // TrimFunc returns a subslice of s by slicing off all leading and trailing
   508  // UTF-8-encoded Unicode code points c that satisfy f(c).
   509  func TrimFunc(s []byte, f func(r rune) bool) []byte {
   510  	return TrimRightFunc(TrimLeftFunc(s, f), f)
   511  }
   512  
   513  // TrimPrefix returns s without the provided leading prefix string.
   514  // If s doesn't start with prefix, s is returned unchanged.
   515  func TrimPrefix(s, prefix []byte) []byte {
   516  	if HasPrefix(s, prefix) {
   517  		return s[len(prefix):]
   518  	}
   519  	return s
   520  }
   521  
   522  // TrimSuffix returns s without the provided trailing suffix string.
   523  // If s doesn't end with suffix, s is returned unchanged.
   524  func TrimSuffix(s, suffix []byte) []byte {
   525  	if HasSuffix(s, suffix) {
   526  		return s[:len(s)-len(suffix)]
   527  	}
   528  	return s
   529  }
   530  
   531  // IndexFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
   532  // It returns the byte index in s of the first Unicode
   533  // code point satisfying f(c), or -1 if none do.
   534  func IndexFunc(s []byte, f func(r rune) bool) int {
   535  	return indexFunc(s, f, true)
   536  }
   537  
   538  // LastIndexFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
   539  // It returns the byte index in s of the last Unicode
   540  // code point satisfying f(c), or -1 if none do.
   541  func LastIndexFunc(s []byte, f func(r rune) bool) int {
   542  	return lastIndexFunc(s, f, true)
   543  }
   544  
   545  // indexFunc is the same as IndexFunc except that if
   546  // truth==false, the sense of the predicate function is
   547  // inverted.
   548  func indexFunc(s []byte, f func(r rune) bool, truth bool) int {
   549  	start := 0
   550  	for start < len(s) {
   551  		wid := 1
   552  		r := rune(s[start])
   553  		if r >= utf8.RuneSelf {
   554  			r, wid = utf8.DecodeRune(s[start:])
   555  		}
   556  		if f(r) == truth {
   557  			return start
   558  		}
   559  		start += wid
   560  	}
   561  	return -1
   562  }
   563  
   564  // lastIndexFunc is the same as LastIndexFunc except that if
   565  // truth==false, the sense of the predicate function is
   566  // inverted.
   567  func lastIndexFunc(s []byte, f func(r rune) bool, truth bool) int {
   568  	for i := len(s); i > 0; {
   569  		r, size := rune(s[i-1]), 1
   570  		if r >= utf8.RuneSelf {
   571  			r, size = utf8.DecodeLastRune(s[0:i])
   572  		}
   573  		i -= size
   574  		if f(r) == truth {
   575  			return i
   576  		}
   577  	}
   578  	return -1
   579  }
   580  
   581  // asciiSet is a 32-byte value, where each bit represents the presence of a
   582  // given ASCII character in the set. The 128-bits of the lower 16 bytes,
   583  // starting with the least-significant bit of the lowest word to the
   584  // most-significant bit of the highest word, map to the full range of all
   585  // 128 ASCII characters. The 128-bits of the upper 16 bytes will be zeroed,
   586  // ensuring that any non-ASCII character will be reported as not in the set.
   587  type asciiSet [8]uint32
   588  
   589  // makeASCIISet creates a set of ASCII characters and reports whether all
   590  // characters in chars are ASCII.
   591  func makeASCIISet(chars string) (as asciiSet, ok bool) {
   592  	for i := 0; i < len(chars); i++ {
   593  		c := chars[i]
   594  		if c >= utf8.RuneSelf {
   595  			return as, false
   596  		}
   597  		as[c>>5] |= 1 << uint(c&31)
   598  	}
   599  	return as, true
   600  }
   601  
   602  // contains reports whether c is inside the set.
   603  func (as *asciiSet) contains(c byte) bool {
   604  	return (as[c>>5] & (1 << uint(c&31))) != 0
   605  }
   606  
   607  func makeCutsetFunc(cutset string) func(r rune) bool {
   608  	if len(cutset) == 1 && cutset[0] < utf8.RuneSelf {
   609  		return func(r rune) bool {
   610  			return r == rune(cutset[0])
   611  		}
   612  	}
   613  	if as, isASCII := makeASCIISet(cutset); isASCII {
   614  		return func(r rune) bool {
   615  			return r < utf8.RuneSelf && as.contains(byte(r))
   616  		}
   617  	}
   618  	return func(r rune) bool {
   619  		for _, c := range cutset {
   620  			if c == r {
   621  				return true
   622  			}
   623  		}
   624  		return false
   625  	}
   626  }
   627  
   628  // Trim returns a subslice of s by slicing off all leading and
   629  // trailing UTF-8-encoded Unicode code points contained in cutset.
   630  func Trim(s []byte, cutset string) []byte {
   631  	return TrimFunc(s, makeCutsetFunc(cutset))
   632  }
   633  
   634  // TrimLeft returns a subslice of s by slicing off all leading
   635  // UTF-8-encoded Unicode code points contained in cutset.
   636  func TrimLeft(s []byte, cutset string) []byte {
   637  	return TrimLeftFunc(s, makeCutsetFunc(cutset))
   638  }
   639  
   640  // TrimRight returns a subslice of s by slicing off all trailing
   641  // UTF-8-encoded Unicode code points that are contained in cutset.
   642  func TrimRight(s []byte, cutset string) []byte {
   643  	return TrimRightFunc(s, makeCutsetFunc(cutset))
   644  }
   645  
   646  // TrimSpace returns a subslice of s by slicing off all leading and
   647  // trailing white space, as defined by Unicode.
   648  func TrimSpace(s []byte) []byte {
   649  	return TrimFunc(s, unicode.IsSpace)
   650  }
   651  
   652  // Runes returns a slice of runes (Unicode code points) equivalent to s.
   653  func Runes(s []byte) []rune {
   654  	t := make([]rune, utf8.RuneCount(s))
   655  	i := 0
   656  	for len(s) > 0 {
   657  		r, l := utf8.DecodeRune(s)
   658  		t[i] = r
   659  		i++
   660  		s = s[l:]
   661  	}
   662  	return t
   663  }
   664  
   665  // Replace returns a copy of the slice s with the first n
   666  // non-overlapping instances of old replaced by new.
   667  // If old is empty, it matches at the beginning of the slice
   668  // and after each UTF-8 sequence, yielding up to k+1 replacements
   669  // for a k-rune slice.
   670  // If n < 0, there is no limit on the number of replacements.
   671  func Replace(s, old, new []byte, n int) []byte {
   672  	m := 0
   673  	if n != 0 {
   674  		// Compute number of replacements.
   675  		m = Count(s, old)
   676  	}
   677  	if m == 0 {
   678  		// Just return a copy.
   679  		return append([]byte(nil), s...)
   680  	}
   681  	if n < 0 || m < n {
   682  		n = m
   683  	}
   684  
   685  	// Apply replacements to buffer.
   686  	t := make([]byte, len(s)+n*(len(new)-len(old)))
   687  	w := 0
   688  	start := 0
   689  	for i := 0; i < n; i++ {
   690  		j := start
   691  		if len(old) == 0 {
   692  			if i > 0 {
   693  				_, wid := utf8.DecodeRune(s[start:])
   694  				j += wid
   695  			}
   696  		} else {
   697  			j += Index(s[start:], old)
   698  		}
   699  		w += copy(t[w:], s[start:j])
   700  		w += copy(t[w:], new)
   701  		start = j + len(old)
   702  	}
   703  	w += copy(t[w:], s[start:])
   704  	return t[0:w]
   705  }
   706  
   707  // EqualFold reports whether s and t, interpreted as UTF-8 strings,
   708  // are equal under Unicode case-folding.
   709  func EqualFold(s, t []byte) bool {
   710  	for len(s) != 0 && len(t) != 0 {
   711  		// Extract first rune from each.
   712  		var sr, tr rune
   713  		if s[0] < utf8.RuneSelf {
   714  			sr, s = rune(s[0]), s[1:]
   715  		} else {
   716  			r, size := utf8.DecodeRune(s)
   717  			sr, s = r, s[size:]
   718  		}
   719  		if t[0] < utf8.RuneSelf {
   720  			tr, t = rune(t[0]), t[1:]
   721  		} else {
   722  			r, size := utf8.DecodeRune(t)
   723  			tr, t = r, t[size:]
   724  		}
   725  
   726  		// If they match, keep going; if not, return false.
   727  
   728  		// Easy case.
   729  		if tr == sr {
   730  			continue
   731  		}
   732  
   733  		// Make sr < tr to simplify what follows.
   734  		if tr < sr {
   735  			tr, sr = sr, tr
   736  		}
   737  		// Fast check for ASCII.
   738  		if tr < utf8.RuneSelf && 'A' <= sr && sr <= 'Z' {
   739  			// ASCII, and sr is upper case.  tr must be lower case.
   740  			if tr == sr+'a'-'A' {
   741  				continue
   742  			}
   743  			return false
   744  		}
   745  
   746  		// General case. SimpleFold(x) returns the next equivalent rune > x
   747  		// or wraps around to smaller values.
   748  		r := unicode.SimpleFold(sr)
   749  		for r != sr && r < tr {
   750  			r = unicode.SimpleFold(r)
   751  		}
   752  		if r == tr {
   753  			continue
   754  		}
   755  		return false
   756  	}
   757  
   758  	// One string is empty. Are both?
   759  	return len(s) == len(t)
   760  }