]> Cypherpunks repositories - gostls13.git/commitdiff
bytes: add ContainsRune
authorEmmanuel Odeke <emm.odeke@gmail.com>
Fri, 8 Apr 2016 06:22:30 +0000 (23:22 -0700)
committerRob Pike <r@golang.org>
Fri, 8 Apr 2016 20:24:57 +0000 (20:24 +0000)
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 <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
src/bytes/bytes.go
src/bytes/bytes_test.go

index 698d881c9dd2439367a799ed1cf6f365e821de2d..305c85d9f412fcb449272653c6931eb86af81e16 100644 (file)
@@ -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)
index 40e8d09b59eb65eddb66bcf962eca9384399ac06..620cfd1bce7a879d242e919d4dc1897e74ee3e27 100644 (file)
@@ -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.