aboutsummaryrefslogtreecommitdiff
path: root/src/bytes
diff options
context:
space:
mode:
Diffstat (limited to 'src/bytes')
-rw-r--r--src/bytes/iter.go21
-rw-r--r--src/bytes/iter_test.go56
2 files changed, 66 insertions, 11 deletions
diff --git a/src/bytes/iter.go b/src/bytes/iter.go
index 8e9ee8b98d..e3af4d2f13 100644
--- a/src/bytes/iter.go
+++ b/src/bytes/iter.go
@@ -32,25 +32,24 @@ func Lines(s []byte) iter.Seq[[]byte] {
}
// explodeSeq returns an iterator over the runes in s.
-func explodeSeq(s []byte) iter.Seq[[]byte] {
- return func(yield func([]byte) bool) {
- for len(s) > 0 {
- _, size := utf8.DecodeRune(s)
- if !yield(s[:size:size]) {
- return
- }
- s = s[size:]
+func explodeSeq(s []byte, yield func([]byte) bool) {
+ for len(s) > 0 {
+ _, size := utf8.DecodeRune(s)
+ if !yield(s[:size:size]) {
+ return
}
+ s = s[size:]
}
}
// splitSeq is SplitSeq or SplitAfterSeq, configured by how many
// bytes of sep to include in the results (none or all).
func splitSeq(s, sep []byte, sepSave int) iter.Seq[[]byte] {
- if len(sep) == 0 {
- return explodeSeq(s)
- }
return func(yield func([]byte) bool) {
+ if len(sep) == 0 {
+ explodeSeq(s, yield)
+ return
+ }
for {
i := Index(s, sep)
if i < 0 {
diff --git a/src/bytes/iter_test.go b/src/bytes/iter_test.go
new file mode 100644
index 0000000000..e37fdfb96d
--- /dev/null
+++ b/src/bytes/iter_test.go
@@ -0,0 +1,56 @@
+// Copyright 2024 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package bytes_test
+
+import (
+ . "bytes"
+ "testing"
+)
+
+func BenchmarkSplitSeqEmptySeparator(b *testing.B) {
+ for range b.N {
+ for range SplitSeq(benchInputHard, nil) {
+ }
+ }
+}
+
+func BenchmarkSplitSeqSingleByteSeparator(b *testing.B) {
+ sep := []byte("/")
+ for range b.N {
+ for range SplitSeq(benchInputHard, sep) {
+ }
+ }
+}
+
+func BenchmarkSplitSeqMultiByteSeparator(b *testing.B) {
+ sep := []byte("hello")
+ for range b.N {
+ for range SplitSeq(benchInputHard, sep) {
+ }
+ }
+}
+
+func BenchmarkSplitAfterSeqEmptySeparator(b *testing.B) {
+ for range b.N {
+ for range SplitAfterSeq(benchInputHard, nil) {
+ }
+ }
+}
+
+func BenchmarkSplitAfterSeqSingleByteSeparator(b *testing.B) {
+ sep := []byte("/")
+ for range b.N {
+ for range SplitAfterSeq(benchInputHard, sep) {
+ }
+ }
+}
+
+func BenchmarkSplitAfterSeqMultiByteSeparator(b *testing.B) {
+ sep := []byte("hello")
+ for range b.N {
+ for range SplitAfterSeq(benchInputHard, sep) {
+ }
+ }
+}