search.go

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