]> Cypherpunks repositories - gostls13.git/commitdiff
strings: Add ContainsAny and ContainsRune to correspond to IndexAny etc.
authorScott Lawrence <bytbox@gmail.com>
Thu, 24 Nov 2011 04:20:14 +0000 (20:20 -0800)
committerRob Pike <r@golang.org>
Thu, 24 Nov 2011 04:20:14 +0000 (20:20 -0800)
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5430046

src/pkg/strings/strings.go
src/pkg/strings/strings_test.go

index b4d920714ac4ef5662e6ffd22773023414e08a3e..53fdeadf97b42659e501e24bc504cee533d84c67 100644 (file)
@@ -64,7 +64,17 @@ func Count(s, sep string) int {
 
 // Contains returns true if substr is within s.
 func Contains(s, substr string) bool {
-       return Index(s, substr) != -1
+       return Index(s, substr) >= 0
+}
+
+// ContainsAny returns true if any Unicode code points in chars are within s.
+func ContainsAny(s, chars string) bool {
+       return IndexAny(s, chars) >= 0
+}
+
+// ContainsRune returns true if the Unicode code point r is within s.
+func ContainsRune(s string, r rune) bool {
+       return IndexRune(s, r) >= 0
 }
 
 // Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
@@ -269,7 +279,7 @@ func FieldsFunc(s string, f func(rune) bool) []string {
                        fieldStart = i
                }
        }
-       if fieldStart != -1 { // Last field might end at EOF.
+       if fieldStart >= 0 { // Last field might end at EOF.
                a[na] = s[fieldStart:]
        }
        return a
@@ -512,7 +522,7 @@ func lastIndexFunc(s string, f func(rune) bool, truth bool) int {
 }
 
 func makeCutsetFunc(cutset string) func(rune) bool {
-       return func(r rune) bool { return IndexRune(cutset, r) != -1 }
+       return func(r rune) bool { return IndexRune(cutset, r) >= 0 }
 }
 
 // Trim returns a slice of the string s with all leading and
index 96207f5a2da6f995938dab15be67e5806023a4c2..5308e8b7e412733626873ee28f1ff86199d67325 100644 (file)
@@ -908,6 +908,56 @@ func TestContains(t *testing.T) {
        }
 }
 
+var ContainsAnyTests = []struct {
+       str, substr string
+       expected    bool
+}{
+       {"", "", false},
+       {"", "a", false},
+       {"", "abc", false},
+       {"a", "", false},
+       {"a", "a", true},
+       {"aaa", "a", true},
+       {"abc", "xyz", false},
+       {"abc", "xcz", true},
+       {"a☺b☻c☹d", "uvw☻xyz", true},
+       {"aRegExp*", ".(|)*+?^$[]", true},
+       {dots + dots + dots, " ", false},
+}
+
+func TestContainsAny(t *testing.T) {
+       for _, ct := range ContainsAnyTests {
+               if ContainsAny(ct.str, ct.substr) != ct.expected {
+                       t.Errorf("ContainsAny(%s, %s) = %v, want %v",
+                               ct.str, ct.substr, !ct.expected, ct.expected)
+               }
+       }
+}
+
+var ContainsRuneTests = []struct {
+       str      string
+       r        rune
+       expected bool
+}{
+       {"", 'a', false},
+       {"a", 'a', true},
+       {"aaa", 'a', true},
+       {"abc", 'y', false},
+       {"abc", 'c', true},
+       {"a☺b☻c☹d", 'x', false},
+       {"a☺b☻c☹d", '☻', true},
+       {"aRegExp*", '*', true},
+}
+
+func TestContainsRune(t *testing.T) {
+       for _, ct := range ContainsRuneTests {
+               if ContainsRune(ct.str, ct.r) != ct.expected {
+                       t.Errorf("ContainsRune(%s, %s) = %v, want %v",
+                               ct.str, ct.r, !ct.expected, ct.expected)
+               }
+       }
+}
+
 var EqualFoldTests = []struct {
        s, t string
        out  bool