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.
5 // Package bytes implements functions for the manipulation of byte slices.
6 // It is analogous to the facilities of the strings package.
14 func equalPortable(a
, b
[]byte) bool {
26 // explode splits s into a slice of UTF-8 sequences, one per Unicode character (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 {
32 a
:= make([][]byte, n
)
41 _
, size
= utf8
.DecodeRune(s
)
49 // Count counts the number of non-overlapping instances of sep in s.
50 func Count(s
, sep
[]byte) int {
53 return utf8
.RuneCount(s
) + 1
64 o
:= IndexByte(t
[i
:], c
)
70 if n
== 1 ||
Equal(s
[i
:i
+n
], sep
) {
80 // Contains reports whether subslice is within b.
81 func Contains(b
, subslice
[]byte) bool {
82 return Index(b
, subslice
) != -1
85 // Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
86 func Index(s
, sep
[]byte) int {
96 return IndexByte(s
, c
)
102 o
:= IndexByte(t
[i
:], c
)
108 if Equal(s
[i
:i
+n
], sep
) {
116 func indexBytePortable(s
[]byte, c
byte) int {
117 for i
, b
:= range s
{
125 // LastIndex returns the index of the last instance of sep in s, or -1 if sep is not present in s.
126 func LastIndex(s
, sep
[]byte) int {
132 for i
:= len(s
) - n
; i
>= 0; i
-- {
133 if s
[i
] == c
&& (n
== 1 ||
Equal(s
[i
:i
+n
], sep
)) {
140 // IndexRune interprets s as a sequence of UTF-8-encoded Unicode code points.
141 // It returns the byte index of the first occurrence in s of the given rune.
142 // It returns -1 if rune is not present in s.
143 func IndexRune(s
[]byte, r rune
) int {
144 for i
:= 0; i
< len(s
); {
145 r1
, size
:= utf8
.DecodeRune(s
[i
:])
154 // IndexAny interprets s as a sequence of UTF-8-encoded Unicode code points.
155 // It returns the byte index of the first occurrence in s of any of the Unicode
156 // code points in chars. It returns -1 if chars is empty or if there is no code
158 func IndexAny(s
[]byte, chars
string) int {
162 for i
:= 0; i
< len(s
); i
+= width
{
164 if r
< utf8
.RuneSelf
{
167 r
, width
= utf8
.DecodeRune(s
[i
:])
169 for _
, ch
:= range chars
{
179 // LastIndexAny interprets s as a sequence of UTF-8-encoded Unicode code
180 // points. It returns the byte index of the last occurrence in s of any of
181 // the Unicode code points in chars. It returns -1 if chars is empty or if
182 // there is no code point in common.
183 func LastIndexAny(s
[]byte, chars
string) int {
185 for i
:= len(s
); i
> 0; {
186 r
, size
:= utf8
.DecodeLastRune(s
[0:i
])
188 for _
, ch
:= range chars
{
198 // Generic split: splits after each instance of sep,
199 // including sepSave bytes of sep in the subslices.
200 func genSplit(s
, sep
[]byte, sepSave
, n
int) [][]byte {
208 n
= Count(s
, sep
) + 1
212 a
:= make([][]byte, n
)
214 for i
:= 0; i
+len(sep
) <= len(s
) && na
+1 < n
; i
++ {
215 if s
[i
] == c
&& (len(sep
) == 1 ||
Equal(s
[i
:i
+len(sep
)], sep
)) {
216 a
[na
] = s
[start
: i
+sepSave
]
226 // SplitN slices s into subslices separated by sep and returns a slice of
227 // the subslices between those separators.
228 // If sep is empty, SplitN splits after each UTF-8 sequence.
229 // The count determines the number of subslices to return:
230 // n > 0: at most n subslices; the last subslice will be the unsplit remainder.
231 // n == 0: the result is nil (zero subslices)
232 // n < 0: all subslices
233 func SplitN(s
, sep
[]byte, n
int) [][]byte { return genSplit(s
, sep
, 0, n
) }
235 // SplitAfterN slices s into subslices after each instance of sep and
236 // returns a slice of those subslices.
237 // If sep is empty, SplitAfterN splits after each UTF-8 sequence.
238 // The count determines the number of subslices to return:
239 // n > 0: at most n subslices; the last subslice will be the unsplit remainder.
240 // n == 0: the result is nil (zero subslices)
241 // n < 0: all subslices
242 func SplitAfterN(s
, sep
[]byte, n
int) [][]byte {
243 return genSplit(s
, sep
, len(sep
), n
)
246 // Split slices s into all subslices separated by sep and returns a slice of
247 // the subslices between those separators.
248 // If sep is empty, Split splits after each UTF-8 sequence.
249 // It is equivalent to SplitN with a count of -1.
250 func Split(s
, sep
[]byte) [][]byte { return genSplit(s
, sep
, 0, -1) }
252 // SplitAfter slices s into all subslices after each instance of sep and
253 // returns a slice of those subslices.
254 // If sep is empty, SplitAfter splits after each UTF-8 sequence.
255 // It is equivalent to SplitAfterN with a count of -1.
256 func SplitAfter(s
, sep
[]byte) [][]byte {
257 return genSplit(s
, sep
, len(sep
), -1)
260 // Fields splits the slice s around each instance of one or more consecutive white space
261 // characters, returning a slice of subslices of s or an empty list if s contains only white space.
262 func Fields(s
[]byte) [][]byte {
263 return FieldsFunc(s
, unicode
.IsSpace
)
266 // FieldsFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
267 // It splits the slice s at each run of code points c satisfying f(c) and
268 // returns a slice of subslices of s. If all code points in s satisfy f(c), or
269 // len(s) == 0, an empty slice is returned.
270 func FieldsFunc(s
[]byte, f
func(rune
) bool) [][]byte {
273 for i
:= 0; i
< len(s
); {
274 r
, size
:= utf8
.DecodeRune(s
[i
:])
275 wasInField
:= inField
277 if inField
&& !wasInField
{
283 a
:= make([][]byte, n
)
286 for i
:= 0; i
<= len(s
) && na
< n
; {
287 r
, size
:= utf8
.DecodeRune(s
[i
:])
288 if fieldStart
< 0 && size
> 0 && !f(r
) {
293 if fieldStart
>= 0 && (size
== 0 ||
f(r
)) {
294 a
[na
] = s
[fieldStart
:i
]
306 // Join concatenates the elements of s to create a new byte slice. The separator
307 // sep is placed between elements in the resulting slice.
308 func Join(s
[][]byte, sep
[]byte) []byte {
313 // Just return a copy.
314 return append([]byte(nil), s
[0]...)
316 n
:= len(sep
) * (len(s
) - 1)
317 for _
, v
:= range s
{
323 for _
, v
:= range s
[1:] {
324 bp
+= copy(b
[bp
:], sep
)
325 bp
+= copy(b
[bp
:], v
)
330 // HasPrefix tests whether the byte slice s begins with prefix.
331 func HasPrefix(s
, prefix
[]byte) bool {
332 return len(s
) >= len(prefix
) && Equal(s
[0:len(prefix
)], prefix
)
335 // HasSuffix tests whether the byte slice s ends with suffix.
336 func HasSuffix(s
, suffix
[]byte) bool {
337 return len(s
) >= len(suffix
) && Equal(s
[len(s
)-len(suffix
):], suffix
)
340 // Map returns a copy of the byte slice s with all its characters modified
341 // according to the mapping function. If mapping returns a negative value, the character is
342 // dropped from the string with no replacement. The characters in s and the
343 // output are interpreted as UTF-8-encoded Unicode code points.
344 func Map(mapping
func(r rune
) rune
, s
[]byte) []byte {
345 // In the worst case, the slice can grow when mapped, making
346 // things unpleasant. But it's so rare we barge in assuming it's
347 // fine. It could also shrink but that falls out naturally.
348 maxbytes
:= len(s
) // length of b
349 nbytes
:= 0 // number of bytes encoded in b
350 b
:= make([]byte, maxbytes
)
351 for i
:= 0; i
< len(s
); {
354 if r
>= utf8
.RuneSelf
{
355 r
, wid
= utf8
.DecodeRune(s
[i
:])
359 if nbytes
+utf8
.RuneLen(r
) > maxbytes
{
361 maxbytes
= maxbytes
*2 + utf8
.UTFMax
362 nb
:= make([]byte, maxbytes
)
363 copy(nb
, b
[0:nbytes
])
366 nbytes
+= utf8
.EncodeRune(b
[nbytes
:maxbytes
], r
)
373 // Repeat returns a new byte slice consisting of count copies of b.
374 func Repeat(b
[]byte, count
int) []byte {
375 nb
:= make([]byte, len(b
)*count
)
377 for i
:= 0; i
< count
; i
++ {
378 bp
+= copy(nb
[bp
:], b
)
383 // ToUpper returns a copy of the byte slice s with all Unicode letters mapped to their upper case.
384 func ToUpper(s
[]byte) []byte { return Map(unicode
.ToUpper
, s
) }
386 // ToLower returns a copy of the byte slice s with all Unicode letters mapped to their lower case.
387 func ToLower(s
[]byte) []byte { return Map(unicode
.ToLower
, s
) }
389 // ToTitle returns a copy of the byte slice s with all Unicode letters mapped to their title case.
390 func ToTitle(s
[]byte) []byte { return Map(unicode
.ToTitle
, s
) }
392 // ToUpperSpecial returns a copy of the byte slice s with all Unicode letters mapped to their
393 // upper case, giving priority to the special casing rules.
394 func ToUpperSpecial(_case unicode
.SpecialCase
, s
[]byte) []byte {
395 return Map(func(r rune
) rune
{ return _case
.ToUpper(r
) }, s
)
398 // ToLowerSpecial returns a copy of the byte slice s with all Unicode letters mapped to their
399 // lower case, giving priority to the special casing rules.
400 func ToLowerSpecial(_case unicode
.SpecialCase
, s
[]byte) []byte {
401 return Map(func(r rune
) rune
{ return _case
.ToLower(r
) }, s
)
404 // ToTitleSpecial returns a copy of the byte slice s with all Unicode letters mapped to their
405 // title case, giving priority to the special casing rules.
406 func ToTitleSpecial(_case unicode
.SpecialCase
, s
[]byte) []byte {
407 return Map(func(r rune
) rune
{ return _case
.ToTitle(r
) }, s
)
410 // isSeparator reports whether the rune could mark a word boundary.
411 // TODO: update when package unicode captures more of the properties.
412 func isSeparator(r rune
) bool {
413 // ASCII alphanumerics and underscore are not separators
416 case '0' <= r
&& r
<= '9':
418 case 'a' <= r
&& r
<= 'z':
420 case 'A' <= r
&& r
<= 'Z':
427 // Letters and digits are not separators
428 if unicode
.IsLetter(r
) || unicode
.IsDigit(r
) {
431 // Otherwise, all we can do for now is treat spaces as separators.
432 return unicode
.IsSpace(r
)
435 // Title returns a copy of s with all Unicode letters that begin words
436 // mapped to their title case.
438 // BUG: The rule Title uses for word boundaries does not handle Unicode punctuation properly.
439 func Title(s
[]byte) []byte {
440 // Use a closure here to remember state.
441 // Hackish but effective. Depends on Map scanning in order and calling
442 // the closure once per rune.
446 if isSeparator(prev
) {
448 return unicode
.ToTitle(r
)
456 // TrimLeftFunc returns a subslice of s by slicing off all leading UTF-8-encoded
457 // Unicode code points c that satisfy f(c).
458 func TrimLeftFunc(s
[]byte, f
func(r rune
) bool) []byte {
459 i
:= indexFunc(s
, f
, false)
466 // TrimRightFunc returns a subslice of s by slicing off all trailing UTF-8
467 // encoded Unicode code points c that satisfy f(c).
468 func TrimRightFunc(s
[]byte, f
func(r rune
) bool) []byte {
469 i
:= lastIndexFunc(s
, f
, false)
470 if i
>= 0 && s
[i
] >= utf8
.RuneSelf
{
471 _
, wid
:= utf8
.DecodeRune(s
[i
:])
479 // TrimFunc returns a subslice of s by slicing off all leading and trailing
480 // UTF-8-encoded Unicode code points c that satisfy f(c).
481 func TrimFunc(s
[]byte, f
func(r rune
) bool) []byte {
482 return TrimRightFunc(TrimLeftFunc(s
, f
), f
)
485 // TrimPrefix returns s without the provided leading prefix string.
486 // If s doesn't start with prefix, s is returned unchanged.
487 func TrimPrefix(s
, prefix
[]byte) []byte {
488 if HasPrefix(s
, prefix
) {
489 return s
[len(prefix
):]
494 // TrimSuffix returns s without the provided trailing suffix string.
495 // If s doesn't end with suffix, s is returned unchanged.
496 func TrimSuffix(s
, suffix
[]byte) []byte {
497 if HasSuffix(s
, suffix
) {
498 return s
[:len(s
)-len(suffix
)]
503 // IndexFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
504 // It returns the byte index in s of the first Unicode
505 // code point satisfying f(c), or -1 if none do.
506 func IndexFunc(s
[]byte, f
func(r rune
) bool) int {
507 return indexFunc(s
, f
, true)
510 // LastIndexFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
511 // It returns the byte index in s of the last Unicode
512 // code point satisfying f(c), or -1 if none do.
513 func LastIndexFunc(s
[]byte, f
func(r rune
) bool) int {
514 return lastIndexFunc(s
, f
, true)
517 // indexFunc is the same as IndexFunc except that if
518 // truth==false, the sense of the predicate function is
520 func indexFunc(s
[]byte, f
func(r rune
) bool, truth
bool) int {
525 if r
>= utf8
.RuneSelf
{
526 r
, wid
= utf8
.DecodeRune(s
[start
:])
536 // lastIndexFunc is the same as LastIndexFunc except that if
537 // truth==false, the sense of the predicate function is
539 func lastIndexFunc(s
[]byte, f
func(r rune
) bool, truth
bool) int {
540 for i
:= len(s
); i
> 0; {
541 r
, size
:= rune(s
[i
-1]), 1
542 if r
>= utf8
.RuneSelf
{
543 r
, size
= utf8
.DecodeLastRune(s
[0:i
])
553 func makeCutsetFunc(cutset
string) func(r rune
) bool {
554 return func(r rune
) bool {
555 for _
, c
:= range cutset
{
564 // Trim returns a subslice of s by slicing off all leading and
565 // trailing UTF-8-encoded Unicode code points contained in cutset.
566 func Trim(s
[]byte, cutset
string) []byte {
567 return TrimFunc(s
, makeCutsetFunc(cutset
))
570 // TrimLeft returns a subslice of s by slicing off all leading
571 // UTF-8-encoded Unicode code points contained in cutset.
572 func TrimLeft(s
[]byte, cutset
string) []byte {
573 return TrimLeftFunc(s
, makeCutsetFunc(cutset
))
576 // TrimRight returns a subslice of s by slicing off all trailing
577 // UTF-8-encoded Unicode code points that are contained in cutset.
578 func TrimRight(s
[]byte, cutset
string) []byte {
579 return TrimRightFunc(s
, makeCutsetFunc(cutset
))
582 // TrimSpace returns a subslice of s by slicing off all leading and
583 // trailing white space, as defined by Unicode.
584 func TrimSpace(s
[]byte) []byte {
585 return TrimFunc(s
, unicode
.IsSpace
)
588 // Runes returns a slice of runes (Unicode code points) equivalent to s.
589 func Runes(s
[]byte) []rune
{
590 t
:= make([]rune
, utf8
.RuneCount(s
))
593 r
, l
:= utf8
.DecodeRune(s
)
601 // Replace returns a copy of the slice s with the first n
602 // non-overlapping instances of old replaced by new.
603 // If n < 0, there is no limit on the number of replacements.
604 func Replace(s
, old
, new []byte, n
int) []byte {
607 // Compute number of replacements.
611 // Just return a copy.
612 return append([]byte(nil), s
...)
618 // Apply replacements to buffer.
619 t
:= make([]byte, len(s
)+n
*(len(new)-len(old
)))
622 for i
:= 0; i
< n
; i
++ {
626 _
, wid
:= utf8
.DecodeRune(s
[start
:])
630 j
+= Index(s
[start
:], old
)
632 w
+= copy(t
[w
:], s
[start
:j
])
633 w
+= copy(t
[w
:], new)
636 w
+= copy(t
[w
:], s
[start
:])
640 // EqualFold reports whether s and t, interpreted as UTF-8 strings,
641 // are equal under Unicode case-folding.
642 func EqualFold(s
, t
[]byte) bool {
643 for len(s
) != 0 && len(t
) != 0 {
644 // Extract first rune from each.
646 if s
[0] < utf8
.RuneSelf
{
647 sr
, s
= rune(s
[0]), s
[1:]
649 r
, size
:= utf8
.DecodeRune(s
)
652 if t
[0] < utf8
.RuneSelf
{
653 tr
, t
= rune(t
[0]), t
[1:]
655 r
, size
:= utf8
.DecodeRune(t
)
659 // If they match, keep going; if not, return false.
666 // Make sr < tr to simplify what follows.
670 // Fast check for ASCII.
671 if tr
< utf8
.RuneSelf
&& 'A' <= sr
&& sr
<= 'Z' {
672 // ASCII, and sr is upper case. tr must be lower case.
673 if tr
== sr
+'a'-'A' {
679 // General case. SimpleFold(x) returns the next equivalent rune > x
680 // or wraps around to smaller values.
681 r
:= unicode
.SimpleFold(sr
)
682 for r
!= sr
&& r
< tr
{
683 r
= unicode
.SimpleFold(r
)
691 // One string is empty. Are both?
692 return len(s
) == len(t
)