aboutsummaryrefslogtreecommitdiff
path: root/src/strings/strings.go
diff options
context:
space:
mode:
authorKeith Randall <khr@golang.org>2018-03-03 10:28:58 -0800
committerKeith Randall <khr@golang.org>2018-03-04 17:49:25 +0000
commit45964e4f9c950863adcaeb62fbe49f3fa913f27d (patch)
treebb07b6ae9f12caa6156932ff236ce3c4c42147e0 /src/strings/strings.go
parent89ae7045f395de8eb4085e3ac8c1ebf59b029965 (diff)
downloadgo-45964e4f9c950863adcaeb62fbe49f3fa913f27d.tar.xz
internal/bytealg: move Count to bytealg
Move bytes.Count and strings.Count to bytealg. Update #19792 Change-Id: I3e4e14b504a0b71758885bb131e5656e342cf8cb Reviewed-on: https://go-review.googlesource.com/98495 Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/strings/strings.go')
-rw-r--r--src/strings/strings.go9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/strings/strings.go b/src/strings/strings.go
index 02c032046b..7d3ed37edd 100644
--- a/src/strings/strings.go
+++ b/src/strings/strings.go
@@ -8,6 +8,7 @@
package strings
import (
+ "internal/bytealg"
"unicode"
"unicode/utf8"
)
@@ -72,12 +73,16 @@ func hashStrRev(sep string) (uint32, uint32) {
return hash, pow
}
-// countGeneric implements Count.
-func countGeneric(s, substr string) int {
+// Count counts the number of non-overlapping instances of substr in s.
+// If substr is an empty string, Count returns 1 + the number of Unicode code points in s.
+func Count(s, substr string) int {
// special case
if len(substr) == 0 {
return utf8.RuneCountInString(s) + 1
}
+ if len(substr) == 1 {
+ return bytealg.CountString(s, substr[0])
+ }
n := 0
for {
i := Index(s, substr)