diff options
Diffstat (limited to 'src/strings')
| -rw-r--r-- | src/strings/iter.go | 21 | ||||
| -rw-r--r-- | src/strings/iter_test.go | 52 |
2 files changed, 62 insertions, 11 deletions
diff --git a/src/strings/iter.go b/src/strings/iter.go index 3fd2c9da97..a42e78ee09 100644 --- a/src/strings/iter.go +++ b/src/strings/iter.go @@ -32,25 +32,24 @@ func Lines(s string) iter.Seq[string] { } // explodeSeq returns an iterator over the runes in s. -func explodeSeq(s string) iter.Seq[string] { - return func(yield func(string) bool) { - for len(s) > 0 { - _, size := utf8.DecodeRuneInString(s) - if !yield(s[:size]) { - return - } - s = s[size:] +func explodeSeq(s string, yield func(string) bool) { + for len(s) > 0 { + _, size := utf8.DecodeRuneInString(s) + if !yield(s[: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 string, sepSave int) iter.Seq[string] { - if len(sep) == 0 { - return explodeSeq(s) - } return func(yield func(string) bool) { + if len(sep) == 0 { + explodeSeq(s, yield) + return + } for { i := Index(s, sep) if i < 0 { diff --git a/src/strings/iter_test.go b/src/strings/iter_test.go new file mode 100644 index 0000000000..2db599377f --- /dev/null +++ b/src/strings/iter_test.go @@ -0,0 +1,52 @@ +// 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 strings_test + +import ( + . "strings" + "testing" +) + +func BenchmarkSplitSeqEmptySeparator(b *testing.B) { + for range b.N { + for range SplitSeq(benchInputHard, "") { + } + } +} + +func BenchmarkSplitSeqSingleByteSeparator(b *testing.B) { + for range b.N { + for range SplitSeq(benchInputHard, "/") { + } + } +} + +func BenchmarkSplitSeqMultiByteSeparator(b *testing.B) { + for range b.N { + for range SplitSeq(benchInputHard, "hello") { + } + } +} + +func BenchmarkSplitAfterSeqEmptySeparator(b *testing.B) { + for range b.N { + for range SplitAfterSeq(benchInputHard, "") { + } + } +} + +func BenchmarkSplitAfterSeqSingleByteSeparator(b *testing.B) { + for range b.N { + for range SplitAfterSeq(benchInputHard, "/") { + } + } +} + +func BenchmarkSplitAfterSeqMultiByteSeparator(b *testing.B) { + for range b.N { + for range SplitAfterSeq(benchInputHard, "hello") { + } + } +} |
