From d636d7907c46b728b07b58669ec1fa1158105579 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Tue, 5 Apr 2016 15:43:07 -0700 Subject: 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 --- src/bytes/bytes.go | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/bytes/bytes.go') diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go index 8a4409cb6b..698d881c9d 100644 --- a/src/bytes/bytes.go +++ b/src/bytes/bytes.go @@ -83,6 +83,11 @@ func Contains(b, subslice []byte) bool { return Index(b, subslice) != -1 } +// ContainsAny reports whether any of the UTF-8-encoded Unicode code points in chars are within b. +func ContainsAny(b []byte, chars string) bool { + return IndexAny(b, chars) >= 0 +} + // Index returns the index of the first instance of sep in s, or -1 if sep is not present in s. func Index(s, sep []byte) int { n := len(sep) -- cgit v1.3 From 59af53d681845a8b0be2a728ca1b59aee5ad9ea6 Mon Sep 17 00:00:00 2001 From: Emmanuel Odeke Date: Thu, 7 Apr 2016 23:22:30 -0700 Subject: bytes: add ContainsRune Make package bytes consistent with strings by adding missing function ContainsRune. Fixes #15189 Change-Id: Ie09080b389e55bbe070c57aa3bd134053a805423 Reviewed-on: https://go-review.googlesource.com/21710 Run-TryBot: Rob Pike TryBot-Result: Gobot Gobot Reviewed-by: Rob Pike --- src/bytes/bytes.go | 5 +++++ src/bytes/bytes_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) (limited to 'src/bytes/bytes.go') diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go index 698d881c9d..305c85d9f4 100644 --- a/src/bytes/bytes.go +++ b/src/bytes/bytes.go @@ -88,6 +88,11 @@ func ContainsAny(b []byte, chars string) bool { return IndexAny(b, chars) >= 0 } +// ContainsRune reports whether the Unicode code point r is within b. +func ContainsRune(b []byte, r rune) bool { + return IndexRune(b, r) >= 0 +} + // Index returns the index of the first instance of sep in s, or -1 if sep is not present in s. func Index(s, sep []byte) int { n := len(sep) diff --git a/src/bytes/bytes_test.go b/src/bytes/bytes_test.go index 40e8d09b59..620cfd1bce 100644 --- a/src/bytes/bytes_test.go +++ b/src/bytes/bytes_test.go @@ -1245,6 +1245,30 @@ func TestContainsAny(t *testing.T) { } } +var ContainsRuneTests = []struct { + b []byte + r rune + expected bool +}{ + {[]byte(""), 'a', false}, + {[]byte("a"), 'a', true}, + {[]byte("aaa"), 'a', true}, + {[]byte("abc"), 'y', false}, + {[]byte("abc"), 'c', true}, + {[]byte("a☺b☻c☹d"), 'x', false}, + {[]byte("a☺b☻c☹d"), '☻', true}, + {[]byte("aRegExp*"), '*', true}, +} + +func TestContainsRune(t *testing.T) { + for _, ct := range ContainsRuneTests { + if ContainsRune(ct.b, ct.r) != ct.expected { + t.Errorf("ContainsRune(%q, %q) = %v, want %v", + ct.b, ct.r, !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. -- cgit v1.3