diff options
Diffstat (limited to 'src/strings')
| -rw-r--r-- | src/strings/strings.go | 9 | ||||
| -rw-r--r-- | src/strings/strings_amd64.go | 9 | ||||
| -rw-r--r-- | src/strings/strings_generic.go | 6 | ||||
| -rw-r--r-- | src/strings/strings_s390x.go | 6 |
4 files changed, 7 insertions, 23 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) diff --git a/src/strings/strings_amd64.go b/src/strings/strings_amd64.go index 68a1d0125c..75e7d0c139 100644 --- a/src/strings/strings_amd64.go +++ b/src/strings/strings_amd64.go @@ -77,12 +77,3 @@ func Index(s, substr string) int { } return indexRabinKarp(s, substr) } - -// 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 { - if len(substr) == 1 && cpu.X86.HasPOPCNT { - return countByte(s, byte(substr[0])) - } - return countGeneric(s, substr) -} diff --git a/src/strings/strings_generic.go b/src/strings/strings_generic.go index b2af48bec8..ac3b8dce85 100644 --- a/src/strings/strings_generic.go +++ b/src/strings/strings_generic.go @@ -53,9 +53,3 @@ func Index(s, substr string) int { } return -1 } - -// 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 { - return countGeneric(s, substr) -} diff --git a/src/strings/strings_s390x.go b/src/strings/strings_s390x.go index 67c8e1700d..b2e459b04e 100644 --- a/src/strings/strings_s390x.go +++ b/src/strings/strings_s390x.go @@ -78,9 +78,3 @@ func Index(s, substr string) int { } return indexRabinKarp(s, substr) } - -// 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 { - return countGeneric(s, substr) -} |
