mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
bytes: add ContainsAny
This function is present in the strings package but missing from bytes, and we would like to keep the two packages consistent. Add it to bytes, and copy the test over as well. Fixes #15140 Change-Id: I5dbd28da83a9fe741885794ed15f2af2f826cb3c Reviewed-on: https://go-review.googlesource.com/21562 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
0c81248bf4
commit
d636d7907c
2 changed files with 32 additions and 0 deletions
|
|
@ -1218,6 +1218,33 @@ func TestContains(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
var ContainsAnyTests = []struct {
|
||||
b []byte
|
||||
substr string
|
||||
expected bool
|
||||
}{
|
||||
{[]byte(""), "", false},
|
||||
{[]byte(""), "a", false},
|
||||
{[]byte(""), "abc", false},
|
||||
{[]byte("a"), "", false},
|
||||
{[]byte("a"), "a", true},
|
||||
{[]byte("aaa"), "a", true},
|
||||
{[]byte("abc"), "xyz", false},
|
||||
{[]byte("abc"), "xcz", true},
|
||||
{[]byte("a☺b☻c☹d"), "uvw☻xyz", true},
|
||||
{[]byte("aRegExp*"), ".(|)*+?^$[]", true},
|
||||
{[]byte(dots + dots + dots), " ", false},
|
||||
}
|
||||
|
||||
func TestContainsAny(t *testing.T) {
|
||||
for _, ct := range ContainsAnyTests {
|
||||
if ContainsAny(ct.b, ct.substr) != ct.expected {
|
||||
t.Errorf("ContainsAny(%s, %s) = %v, want %v",
|
||||
ct.b, ct.substr, !ct.expected, ct.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var makeFieldsInput = func() []byte {
|
||||
x := make([]byte, 1<<20)
|
||||
// Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue