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 strings implements simple functions to manipulate UTF-8 encoded strings.
7 // For information about UTF-8 strings in Go, see https://blog.golang.org/strings.
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.
19 func explode(s
string, n
int) []string {
20 l
:= utf8
.RuneCountInString(s
)
24 a
:= make([]string, n
)
25 for i
:= 0; i
< n
-1; i
++ {
26 ch
, size
:= utf8
.DecodeRuneInString(s
)
29 if ch
== utf8
.RuneError
{
30 a
[i
] = string(utf8
.RuneError
)
39 // primeRK is the prime base used in Rabin-Karp algorithm.
40 const primeRK
= 16777619
42 // hashStr returns the hash and the appropriate multiplicative
43 // factor for use in Rabin-Karp algorithm.
44 func hashStr(sep
string) (uint32, uint32) {
46 for i
:= 0; i
< len(sep
); i
++ {
47 hash
= hash
*primeRK
+ uint32(sep
[i
])
49 var pow
, sq
uint32 = 1, primeRK
50 for i
:= len(sep
); i
> 0; i
>>= 1 {
59 // hashStrRev returns the hash of the reverse of sep and the
60 // appropriate multiplicative factor for use in Rabin-Karp algorithm.
61 func hashStrRev(sep
string) (uint32, uint32) {
63 for i
:= len(sep
) - 1; i
>= 0; i
-- {
64 hash
= hash
*primeRK
+ uint32(sep
[i
])
66 var pow
, sq
uint32 = 1, primeRK
67 for i
:= len(sep
); i
> 0; i
>>= 1 {
76 // Count counts the number of non-overlapping instances of substr in s.
77 // If substr is an empty string, Count returns 1 + the number of Unicode code points in s.
78 func Count(s
, substr
string) int {
81 return utf8
.RuneCountInString(s
) + 1
84 return bytealg
.CountString(s
, substr
[0])
97 // Contains reports whether substr is within s.
98 func Contains(s
, substr
string) bool {
99 return Index(s
, substr
) >= 0
102 // ContainsAny reports whether any Unicode code points in chars are within s.
103 func ContainsAny(s
, chars
string) bool {
104 return IndexAny(s
, chars
) >= 0
107 // ContainsRune reports whether the Unicode code point r is within s.
108 func ContainsRune(s
string, r rune
) bool {
109 return IndexRune(s
, r
) >= 0
112 // LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.
113 func LastIndex(s
, substr
string) int {
119 return LastIndexByte(s
, substr
[0])
128 // Rabin-Karp search from the end of the string
129 hashss
, pow
:= hashStrRev(substr
)
132 for i
:= len(s
) - 1; i
>= last
; i
-- {
133 h
= h
*primeRK
+ uint32(s
[i
])
135 if h
== hashss
&& s
[last
:] == substr
{
138 for i
:= last
- 1; i
>= 0; i
-- {
141 h
-= pow
* uint32(s
[i
+n
])
142 if h
== hashss
&& s
[i
:i
+n
] == substr
{
149 // IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.
150 func IndexByte(s
string, c
byte) int {
151 return bytealg
.IndexByteString(s
, c
)
154 // IndexRune returns the index of the first instance of the Unicode code point
155 // r, or -1 if rune is not present in s.
156 // If r is utf8.RuneError, it returns the first instance of any
157 // invalid UTF-8 byte sequence.
158 func IndexRune(s
string, r rune
) int {
160 case 0 <= r
&& r
< utf8
.RuneSelf
:
161 return IndexByte(s
, byte(r
))
162 case r
== utf8
.RuneError
:
163 for i
, r
:= range s
{
164 if r
== utf8
.RuneError
{
169 case !utf8
.ValidRune(r
):
172 return Index(s
, string(r
))
176 // IndexAny returns the index of the first instance of any Unicode code point
177 // from chars in s, or -1 if no Unicode code point from chars is present in s.
178 func IndexAny(s
, chars
string) int {
180 // Avoid scanning all of s.
184 if as
, isASCII
:= makeASCIISet(chars
); isASCII
{
185 for i
:= 0; i
< len(s
); i
++ {
186 if as
.contains(s
[i
]) {
193 for i
, c
:= range s
{
194 for _
, m
:= range chars
{
203 // LastIndexAny returns the index of the last instance of any Unicode code
204 // point from chars in s, or -1 if no Unicode code point from chars is
206 func LastIndexAny(s
, chars
string) int {
208 // Avoid scanning all of s.
212 if as
, isASCII
:= makeASCIISet(chars
); isASCII
{
213 for i
:= len(s
) - 1; i
>= 0; i
-- {
214 if as
.contains(s
[i
]) {
221 for i
:= len(s
); i
> 0; {
222 r
, size
:= utf8
.DecodeLastRuneInString(s
[:i
])
224 for _
, c
:= range chars
{
233 // LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s.
234 func LastIndexByte(s
string, c
byte) int {
235 for i
:= len(s
) - 1; i
>= 0; i
-- {
243 // Generic split: splits after each instance of sep,
244 // including sepSave bytes of sep in the subarrays.
245 func genSplit(s
, sep
string, sepSave
, n
int) []string {
253 n
= Count(s
, sep
) + 1
256 a
:= make([]string, n
)
272 // SplitN slices s into substrings separated by sep and returns a slice of
273 // the substrings between those separators.
275 // The count determines the number of substrings to return:
276 // n > 0: at most n substrings; the last substring will be the unsplit remainder.
277 // n == 0: the result is nil (zero substrings)
278 // n < 0: all substrings
280 // Edge cases for s and sep (for example, empty strings) are handled
281 // as described in the documentation for Split.
282 func SplitN(s
, sep
string, n
int) []string { return genSplit(s
, sep
, 0, n
) }
284 // SplitAfterN slices s into substrings after each instance of sep and
285 // returns a slice of those substrings.
287 // The count determines the number of substrings to return:
288 // n > 0: at most n substrings; the last substring will be the unsplit remainder.
289 // n == 0: the result is nil (zero substrings)
290 // n < 0: all substrings
292 // Edge cases for s and sep (for example, empty strings) are handled
293 // as described in the documentation for SplitAfter.
294 func SplitAfterN(s
, sep
string, n
int) []string {
295 return genSplit(s
, sep
, len(sep
), n
)
298 // Split slices s into all substrings separated by sep and returns a slice of
299 // the substrings between those separators.
301 // If s does not contain sep and sep is not empty, Split returns a
302 // slice of length 1 whose only element is s.
304 // If sep is empty, Split splits after each UTF-8 sequence. If both s
305 // and sep are empty, Split returns an empty slice.
307 // It is equivalent to SplitN with a count of -1.
308 func Split(s
, sep
string) []string { return genSplit(s
, sep
, 0, -1) }
310 // SplitAfter slices s into all substrings after each instance of sep and
311 // returns a slice of those substrings.
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.
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.
319 // It is equivalent to SplitAfterN with a count of -1.
320 func SplitAfter(s
, sep
string) []string {
321 return genSplit(s
, sep
, len(sep
), -1)
324 var asciiSpace
= [256]uint8{'\t': 1, '\n': 1, '\v': 1, '\f': 1, '\r': 1, ' ': 1}
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.
329 func Fields(s
string) []string {
330 // First count the fields.
331 // This is an exact count if s is ASCII, otherwise it is an approximation.
334 // setBits is used to track which bits are set in the bytes of s.
336 for i
:= 0; i
< len(s
); i
++ {
339 isSpace
:= int(asciiSpace
[r
])
340 n
+= wasSpace
& ^isSpace
344 if setBits
< utf8
.RuneSelf
{ // ASCII fast path
345 a
:= make([]string, n
)
349 // Skip spaces in the front of the input.
350 for i
< len(s
) && asciiSpace
[s
[i
]] != 0 {
355 if asciiSpace
[s
[i
]] == 0 {
359 a
[na
] = s
[fieldStart
:i
]
362 // Skip spaces in between fields.
363 for i
< len(s
) && asciiSpace
[s
[i
]] != 0 {
368 if fieldStart
< len(s
) { // Last field might end at EOF.
369 a
[na
] = s
[fieldStart
:]
374 // Some runes in the input string are not ASCII.
375 return FieldsFunc(s
, unicode
.IsSpace
)
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 // FieldsFunc makes no guarantees about the order in which it calls f(c).
382 // If f does not return consistent results for a given c, FieldsFunc may crash.
383 func FieldsFunc(s
string, f
func(rune
) bool) []string {
384 // A span is used to record a slice of s of the form s[start:end].
385 // The start index is inclusive and the end index is exclusive.
390 spans
:= make([]span
, 0, 32)
392 // Find the field start and end indices.
395 for i
, rune
:= range s
{
398 spans
= append(spans
, span
{start
: fromIndex
, end
: i
})
409 // Last field might end at EOF.
411 spans
= append(spans
, span
{fromIndex
, len(s
)})
414 // Create strings from recorded field indices.
415 a
:= make([]string, len(spans
))
416 for i
, span
:= range spans
{
417 a
[i
] = s
[span
.start
:span
.end
]
423 // Join concatenates the elements of a to create a single string. The separator string
424 // sep is placed between elements in the resulting string.
425 func Join(a
[]string, sep
string) string {
432 n
:= len(sep
) * (len(a
) - 1)
433 for i
:= 0; i
< len(a
); i
++ {
440 for _
, s
:= range a
[1:] {
447 // HasPrefix tests whether the string s begins with prefix.
448 func HasPrefix(s
, prefix
string) bool {
449 return len(s
) >= len(prefix
) && s
[0:len(prefix
)] == prefix
452 // HasSuffix tests whether the string s ends with suffix.
453 func HasSuffix(s
, suffix
string) bool {
454 return len(s
) >= len(suffix
) && s
[len(s
)-len(suffix
):] == suffix
457 // Map returns a copy of the string s with all its characters modified
458 // according to the mapping function. If mapping returns a negative value, the character is
459 // dropped from the string with no replacement.
460 func Map(mapping
func(rune
) rune
, s
string) string {
461 // In the worst case, the string can grow when mapped, making
462 // things unpleasant. But it's so rare we barge in assuming it's
463 // fine. It could also shrink but that falls out naturally.
465 // The output buffer b is initialized on demand, the first
466 // time a character differs.
469 for i
, c
:= range s
{
471 if r
== c
&& c
!= utf8
.RuneError
{
476 if c
== utf8
.RuneError
{
477 c
, width
= utf8
.DecodeRuneInString(s
[i
:])
478 if width
!= 1 && r
== c
{
482 width
= utf8
.RuneLen(c
)
485 b
.Grow(len(s
) + utf8
.UTFMax
)
495 // Fast path for unchanged input
496 if b
.Cap() == 0 { // didn't call b.Grow above
500 for _
, c
:= range s
{
505 // Due to inlining, it is more performant to determine if WriteByte should be
506 // invoked rather than always call WriteRune
507 if r
< utf8
.RuneSelf
{
510 // r is not a ASCII rune.
519 // Repeat returns a new string consisting of count copies of the string s.
521 // It panics if count is negative or if
522 // the result of (len(s) * count) overflows.
523 func Repeat(s
string, count
int) string {
528 // Since we cannot return an error on overflow,
529 // we should panic if the repeat will generate
531 // See Issue golang.org/issue/16237
533 panic("strings: negative Repeat count")
534 } else if len(s
)*count
/count
!= len(s
) {
535 panic("strings: Repeat count causes overflow")
544 b
.WriteString(b
.String())
546 b
.WriteString(b
.String()[:n
-b
.Len()])
553 // ToUpper returns a copy of the string s with all Unicode letters mapped to their upper case.
554 func ToUpper(s
string) string {
555 isASCII
, hasLower
:= true, false
556 for i
:= 0; i
< len(s
); i
++ {
558 if c
>= utf8
.RuneSelf
{
562 hasLower
= hasLower ||
(c
>= 'a' && c
<= 'z')
565 if isASCII
{ // optimize for ASCII-only strings.
571 for i
:= 0; i
< len(s
); i
++ {
573 if c
>= 'a' && c
<= 'z' {
580 return Map(unicode
.ToUpper
, s
)
583 // ToLower returns a copy of the string s with all Unicode letters mapped to their lower case.
584 func ToLower(s
string) string {
585 isASCII
, hasUpper
:= true, false
586 for i
:= 0; i
< len(s
); i
++ {
588 if c
>= utf8
.RuneSelf
{
592 hasUpper
= hasUpper ||
(c
>= 'A' && c
<= 'Z')
595 if isASCII
{ // optimize for ASCII-only strings.
601 for i
:= 0; i
< len(s
); i
++ {
603 if c
>= 'A' && c
<= 'Z' {
610 return Map(unicode
.ToLower
, s
)
613 // ToTitle returns a copy of the string s with all Unicode letters mapped to their title case.
614 func ToTitle(s
string) string { return Map(unicode
.ToTitle
, s
) }
616 // ToUpperSpecial returns a copy of the string s with all Unicode letters mapped to their
617 // upper case using the case mapping specified by c.
618 func ToUpperSpecial(c unicode
.SpecialCase
, s
string) string {
619 return Map(c
.ToUpper
, s
)
622 // ToLowerSpecial returns a copy of the string s with all Unicode letters mapped to their
623 // lower case using the case mapping specified by c.
624 func ToLowerSpecial(c unicode
.SpecialCase
, s
string) string {
625 return Map(c
.ToLower
, s
)
628 // ToTitleSpecial returns a copy of the string s with all Unicode letters mapped to their
629 // title case, giving priority to the special casing rules.
630 func ToTitleSpecial(c unicode
.SpecialCase
, s
string) string {
631 return Map(c
.ToTitle
, s
)
634 // isSeparator reports whether the rune could mark a word boundary.
635 // TODO: update when package unicode captures more of the properties.
636 func isSeparator(r rune
) bool {
637 // ASCII alphanumerics and underscore are not separators
640 case '0' <= r
&& r
<= '9':
642 case 'a' <= r
&& r
<= 'z':
644 case 'A' <= r
&& r
<= 'Z':
651 // Letters and digits are not separators
652 if unicode
.IsLetter(r
) || unicode
.IsDigit(r
) {
655 // Otherwise, all we can do for now is treat spaces as separators.
656 return unicode
.IsSpace(r
)
659 // Title returns a copy of the string s with all Unicode letters that begin words
660 // mapped to their title case.
662 // BUG(rsc): The rule Title uses for word boundaries does not handle Unicode punctuation properly.
663 func Title(s
string) string {
664 // Use a closure here to remember state.
665 // Hackish but effective. Depends on Map scanning in order and calling
666 // the closure once per rune.
670 if isSeparator(prev
) {
672 return unicode
.ToTitle(r
)
680 // TrimLeftFunc returns a slice of the string s with all leading
681 // Unicode code points c satisfying f(c) removed.
682 func TrimLeftFunc(s
string, f
func(rune
) bool) string {
683 i
:= indexFunc(s
, f
, false)
690 // TrimRightFunc returns a slice of the string s with all trailing
691 // Unicode code points c satisfying f(c) removed.
692 func TrimRightFunc(s
string, f
func(rune
) bool) string {
693 i
:= lastIndexFunc(s
, f
, false)
694 if i
>= 0 && s
[i
] >= utf8
.RuneSelf
{
695 _
, wid
:= utf8
.DecodeRuneInString(s
[i
:])
703 // TrimFunc returns a slice of the string s with all leading
704 // and trailing Unicode code points c satisfying f(c) removed.
705 func TrimFunc(s
string, f
func(rune
) bool) string {
706 return TrimRightFunc(TrimLeftFunc(s
, f
), f
)
709 // IndexFunc returns the index into s of the first Unicode
710 // code point satisfying f(c), or -1 if none do.
711 func IndexFunc(s
string, f
func(rune
) bool) int {
712 return indexFunc(s
, f
, true)
715 // LastIndexFunc returns the index into s of the last
716 // Unicode code point satisfying f(c), or -1 if none do.
717 func LastIndexFunc(s
string, f
func(rune
) bool) int {
718 return lastIndexFunc(s
, f
, true)
721 // indexFunc is the same as IndexFunc except that if
722 // truth==false, the sense of the predicate function is
724 func indexFunc(s
string, f
func(rune
) bool, truth
bool) int {
725 for i
, r
:= range s
{
733 // lastIndexFunc is the same as LastIndexFunc except that if
734 // truth==false, the sense of the predicate function is
736 func lastIndexFunc(s
string, f
func(rune
) bool, truth
bool) int {
737 for i
:= len(s
); i
> 0; {
738 r
, size
:= utf8
.DecodeLastRuneInString(s
[0:i
])
747 // asciiSet is a 32-byte value, where each bit represents the presence of a
748 // given ASCII character in the set. The 128-bits of the lower 16 bytes,
749 // starting with the least-significant bit of the lowest word to the
750 // most-significant bit of the highest word, map to the full range of all
751 // 128 ASCII characters. The 128-bits of the upper 16 bytes will be zeroed,
752 // ensuring that any non-ASCII character will be reported as not in the set.
753 type asciiSet
[8]uint32
755 // makeASCIISet creates a set of ASCII characters and reports whether all
756 // characters in chars are ASCII.
757 func makeASCIISet(chars
string) (as asciiSet
, ok
bool) {
758 for i
:= 0; i
< len(chars
); i
++ {
760 if c
>= utf8
.RuneSelf
{
763 as
[c
>>5] |
= 1 << uint(c
&31)
768 // contains reports whether c is inside the set.
769 func (as
*asciiSet
) contains(c
byte) bool {
770 return (as
[c
>>5] & (1 << uint(c
&31))) != 0
773 func makeCutsetFunc(cutset
string) func(rune
) bool {
774 if len(cutset
) == 1 && cutset
[0] < utf8
.RuneSelf
{
775 return func(r rune
) bool {
776 return r
== rune(cutset
[0])
779 if as
, isASCII
:= makeASCIISet(cutset
); isASCII
{
780 return func(r rune
) bool {
781 return r
< utf8
.RuneSelf
&& as
.contains(byte(r
))
784 return func(r rune
) bool { return IndexRune(cutset
, r
) >= 0 }
787 // Trim returns a slice of the string s with all leading and
788 // trailing Unicode code points contained in cutset removed.
789 func Trim(s
string, cutset
string) string {
790 if s
== "" || cutset
== "" {
793 return TrimFunc(s
, makeCutsetFunc(cutset
))
796 // TrimLeft returns a slice of the string s with all leading
797 // Unicode code points contained in cutset removed.
799 // To remove a prefix, use TrimPrefix instead.
800 func TrimLeft(s
string, cutset
string) string {
801 if s
== "" || cutset
== "" {
804 return TrimLeftFunc(s
, makeCutsetFunc(cutset
))
807 // TrimRight returns a slice of the string s, with all trailing
808 // Unicode code points contained in cutset removed.
810 // To remove a suffix, use TrimSuffix instead.
811 func TrimRight(s
string, cutset
string) string {
812 if s
== "" || cutset
== "" {
815 return TrimRightFunc(s
, makeCutsetFunc(cutset
))
818 // TrimSpace returns a slice of the string s, with all leading
819 // and trailing white space removed, as defined by Unicode.
820 func TrimSpace(s
string) string {
821 return TrimFunc(s
, unicode
.IsSpace
)
824 // TrimPrefix returns s without the provided leading prefix string.
825 // If s doesn't start with prefix, s is returned unchanged.
826 func TrimPrefix(s
, prefix
string) string {
827 if HasPrefix(s
, prefix
) {
828 return s
[len(prefix
):]
833 // TrimSuffix returns s without the provided trailing suffix string.
834 // If s doesn't end with suffix, s is returned unchanged.
835 func TrimSuffix(s
, suffix
string) string {
836 if HasSuffix(s
, suffix
) {
837 return s
[:len(s
)-len(suffix
)]
842 // Replace returns a copy of the string s with the first n
843 // non-overlapping instances of old replaced by new.
844 // If old is empty, it matches at the beginning of the string
845 // and after each UTF-8 sequence, yielding up to k+1 replacements
846 // for a k-rune string.
847 // If n < 0, there is no limit on the number of replacements.
848 func Replace(s
, old
, new string, n
int) string {
849 if old
== new || n
== 0 {
850 return s
// avoid allocation
853 // Compute number of replacements.
854 if m
:= Count(s
, old
); m
== 0 {
855 return s
// avoid allocation
856 } else if n
< 0 || m
< n
{
860 // Apply replacements to buffer.
861 t
:= make([]byte, len(s
)+n
*(len(new)-len(old
)))
864 for i
:= 0; i
< n
; i
++ {
868 _
, wid
:= utf8
.DecodeRuneInString(s
[start
:])
872 j
+= Index(s
[start
:], old
)
874 w
+= copy(t
[w
:], s
[start
:j
])
875 w
+= copy(t
[w
:], new)
878 w
+= copy(t
[w
:], s
[start
:])
879 return string(t
[0:w
])
882 // ReplaceAll returns a copy of the string s with all
883 // non-overlapping instances of old replaced by new.
884 // If old is empty, it matches at the beginning of the string
885 // and after each UTF-8 sequence, yielding up to k+1 replacements
886 // for a k-rune string.
887 func ReplaceAll(s
, old
, new string) string {
888 return Replace(s
, old
, new, -1)
891 // EqualFold reports whether s and t, interpreted as UTF-8 strings,
892 // are equal under Unicode case-folding.
893 func EqualFold(s
, t
string) bool {
894 for s
!= "" && t
!= "" {
895 // Extract first rune from each string.
897 if s
[0] < utf8
.RuneSelf
{
898 sr
, s
= rune(s
[0]), s
[1:]
900 r
, size
:= utf8
.DecodeRuneInString(s
)
903 if t
[0] < utf8
.RuneSelf
{
904 tr
, t
= rune(t
[0]), t
[1:]
906 r
, size
:= utf8
.DecodeRuneInString(t
)
910 // If they match, keep going; if not, return false.
917 // Make sr < tr to simplify what follows.
921 // Fast check for ASCII.
922 if tr
< utf8
.RuneSelf
{
923 // ASCII only, sr/tr must be upper/lower case
924 if 'A' <= sr
&& sr
<= 'Z' && tr
== sr
+'a'-'A' {
930 // General case. SimpleFold(x) returns the next equivalent rune > x
931 // or wraps around to smaller values.
932 r
:= unicode
.SimpleFold(sr
)
933 for r
!= sr
&& r
< tr
{
934 r
= unicode
.SimpleFold(r
)
942 // One string is empty. Are both?
946 // Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.
947 func Index(s
, substr
string) int {
953 return IndexByte(s
, substr
[0])
961 case n
<= bytealg
.MaxLen
:
962 // Use brute force when s and substr both are small
963 if len(s
) <= bytealg
.MaxBruteForce
{
964 return bytealg
.IndexString(s
, substr
)
973 // IndexByte is faster than bytealg.IndexString, so use it as long as
974 // we're not getting lots of false positives.
975 o
:= IndexByte(s
[i
:t
], c0
)
981 if s
[i
+1] == c1
&& s
[i
:i
+n
] == substr
{
986 // Switch to bytealg.IndexString when IndexByte produces too many false positives.
987 if fails
> bytealg
.Cutover(i
) {
988 r
:= bytealg
.IndexString(s
[i
:], substr
)
1004 o
:= IndexByte(s
[i
:t
], c0
)
1010 if s
[i
+1] == c1
&& s
[i
:i
+n
] == substr
{
1015 if fails
>= 4+i
>>4 && i
< t
{
1016 // See comment in ../bytes/bytes_generic.go.
1017 j
:= indexRabinKarp(s
[i
:], substr
)
1027 func indexRabinKarp(s
, substr
string) int {
1028 // Rabin-Karp search
1029 hashss
, pow
:= hashStr(substr
)
1032 for i
:= 0; i
< n
; i
++ {
1033 h
= h
*primeRK
+ uint32(s
[i
])
1035 if h
== hashss
&& s
[:n
] == substr
{
1038 for i
:= n
; i
< len(s
); {
1041 h
-= pow
* uint32(s
[i
-n
])
1043 if h
== hashss
&& s
[i
-n
:i
] == substr
{