diff options
author | Keith Randall <khr@golang.org> | 2018-03-03 10:28:58 -0800 |
---|---|---|
committer | Keith Randall <khr@golang.org> | 2018-03-04 17:49:25 +0000 |
commit | 45964e4f9c950863adcaeb62fbe49f3fa913f27d (patch) | |
tree | bb07b6ae9f12caa6156932ff236ce3c4c42147e0 /src/bytes | |
parent | 89ae7045f395de8eb4085e3ac8c1ebf59b029965 (diff) | |
download | go-45964e4f9c950863adcaeb62fbe49f3fa913f27d.tar.gz go-45964e4f9c950863adcaeb62fbe49f3fa913f27d.zip |
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/bytes')
-rw-r--r-- | src/bytes/bytes.go | 9 | ||||
-rw-r--r-- | src/bytes/bytes_amd64.go | 9 | ||||
-rw-r--r-- | src/bytes/bytes_arm64.go | 9 | ||||
-rw-r--r-- | src/bytes/bytes_generic.go | 6 | ||||
-rw-r--r-- | src/bytes/bytes_s390x.go | 6 | ||||
-rw-r--r-- | src/bytes/bytes_test.go | 8 | ||||
-rw-r--r-- | src/bytes/export_test.go | 1 |
7 files changed, 7 insertions, 41 deletions
diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go index 9af177fa88..08d8260e9e 100644 --- a/src/bytes/bytes.go +++ b/src/bytes/bytes.go @@ -7,6 +7,7 @@ package bytes import ( + "internal/bytealg" "unicode" "unicode/utf8" ) @@ -46,12 +47,16 @@ func explode(s []byte, n int) [][]byte { return a[0:na] } -// countGeneric actually implements Count -func countGeneric(s, sep []byte) int { +// Count counts the number of non-overlapping instances of sep in s. +// If sep is an empty slice, Count returns 1 + the number of UTF-8-encoded code points in s. +func Count(s, sep []byte) int { // special case if len(sep) == 0 { return utf8.RuneCount(s) + 1 } + if len(sep) == 1 { + return bytealg.Count(s, sep[0]) + } n := 0 for { i := Index(s, sep) diff --git a/src/bytes/bytes_amd64.go b/src/bytes/bytes_amd64.go index 0c9d613ef9..2fc88c68fc 100644 --- a/src/bytes/bytes_amd64.go +++ b/src/bytes/bytes_amd64.go @@ -77,12 +77,3 @@ func Index(s, sep []byte) int { } return indexRabinKarp(s, sep) } - -// Count counts the number of non-overlapping instances of sep in s. -// If sep is an empty slice, Count returns 1 + the number of UTF-8-encoded code points in s. -func Count(s, sep []byte) int { - if len(sep) == 1 && cpu.X86.HasPOPCNT { - return countByte(s, sep[0]) - } - return countGeneric(s, sep) -} diff --git a/src/bytes/bytes_arm64.go b/src/bytes/bytes_arm64.go index 3d9ed3dd22..39e9562db1 100644 --- a/src/bytes/bytes_arm64.go +++ b/src/bytes/bytes_arm64.go @@ -70,12 +70,3 @@ func Index(s, sep []byte) int { } return -1 } - -// Count counts the number of non-overlapping instances of sep in s. -// If sep is an empty slice, Count returns 1 + the number of UTF-8-encoded code points in s. -func Count(s, sep []byte) int { - if len(sep) == 1 { - return countByte(s, sep[0]) - } - return countGeneric(s, sep) -} diff --git a/src/bytes/bytes_generic.go b/src/bytes/bytes_generic.go index 0e7d33f09a..347d28473f 100644 --- a/src/bytes/bytes_generic.go +++ b/src/bytes/bytes_generic.go @@ -57,9 +57,3 @@ func Index(s, sep []byte) int { } return -1 } - -// Count counts the number of non-overlapping instances of sep in s. -// If sep is an empty slice, Count returns 1 + the number of UTF-8-encoded code points in s. -func Count(s, sep []byte) int { - return countGeneric(s, sep) -} diff --git a/src/bytes/bytes_s390x.go b/src/bytes/bytes_s390x.go index c59b891292..84f040d43d 100644 --- a/src/bytes/bytes_s390x.go +++ b/src/bytes/bytes_s390x.go @@ -78,9 +78,3 @@ func Index(s, sep []byte) int { } return indexRabinKarp(s, sep) } - -// Count counts the number of non-overlapping instances of sep in s. -// If sep is an empty slice, Count returns 1 + the number of UTF-8-encoded code points in s. -func Count(s, sep []byte) int { - return countGeneric(s, sep) -} diff --git a/src/bytes/bytes_test.go b/src/bytes/bytes_test.go index 1e56571c73..55a22bae22 100644 --- a/src/bytes/bytes_test.go +++ b/src/bytes/bytes_test.go @@ -410,10 +410,6 @@ func TestCountByte(t *testing.T) { if p != j+1 { t.Errorf("TestCountByte.Count(%q, 100) = %d", b[i:i+window], p) } - pGeneric := CountGeneric(b[i:i+window], []byte{100}) - if pGeneric != j+1 { - t.Errorf("TestCountByte.CountGeneric(%q, 100) = %d", b[i:i+window], p) - } } } @@ -461,10 +457,6 @@ func TestCountByteNoMatch(t *testing.T) { if p != 0 { t.Errorf("TestCountByteNoMatch(%q, 0) = %d", b[i:i+window], p) } - pGeneric := CountGeneric(b[i:i+window], []byte{0}) - if pGeneric != 0 { - t.Errorf("TestCountByteNoMatch.CountGeneric(%q, 100) = %d", b[i:i+window], p) - } for j := 0; j < window; j++ { b[i+j] = byte(0) } diff --git a/src/bytes/export_test.go b/src/bytes/export_test.go index 823c8b09ee..f61523e60b 100644 --- a/src/bytes/export_test.go +++ b/src/bytes/export_test.go @@ -7,4 +7,3 @@ package bytes // Export func for testing var IndexBytePortable = indexBytePortable var EqualPortable = equalPortable -var CountGeneric = countGeneric |