summaryrefslogtreecommitdiff
path: root/src/pkg/strings
diff options
context:
space:
mode:
authorSteve Newman <devnull@localhost>2009-06-09 10:58:58 -0700
committerSteve Newman <devnull@localhost>2009-06-09 10:58:58 -0700
commitd655b1252ad194cda5ed7f6ddd6fc823127ae1dd (patch)
tree53e10a9a65585d23f1eee670ad1285847764c371 /src/pkg/strings
parent046e2de9ba461c3b517b86e1e5962d40295264ab (diff)
downloadgolang-d655b1252ad194cda5ed7f6ddd6fc823127ae1dd.tar.gz
Basic HTTP client.
R=rsc APPROVED=rsc DELTA=392 (386 added, 2 deleted, 4 changed) OCL=29963 CL=30107
Diffstat (limited to 'src/pkg/strings')
-rw-r--r--src/pkg/strings/strings.go15
-rw-r--r--src/pkg/strings/strings_test.go55
2 files changed, 70 insertions, 0 deletions
diff --git a/src/pkg/strings/strings.go b/src/pkg/strings/strings.go
index 2e3dc0215..035090777 100644
--- a/src/pkg/strings/strings.go
+++ b/src/pkg/strings/strings.go
@@ -53,6 +53,21 @@ func Index(s, sep string) int {
return -1
}
+// Index returns the index of the last instance of sep in s, or -1 if sep is not present in s.
+func LastIndex(s, sep string) int {
+ n := len(sep);
+ if n == 0 {
+ return len(s)
+ }
+ c := sep[0];
+ for i := len(s)-n; i >= 0; i-- {
+ if s[i] == c && (n == 1 || s[i:i+n] == sep) {
+ return i
+ }
+ }
+ return -1
+}
+
// Split returns the array representing the substrings of s separated by string sep. Adjacent
// occurrences of sep produce empty substrings. If sep is empty, it is the same as Explode.
func Split(s, sep string) []string {
diff --git a/src/pkg/strings/strings_test.go b/src/pkg/strings/strings_test.go
index 05e662032..6464ca399 100644
--- a/src/pkg/strings/strings_test.go
+++ b/src/pkg/strings/strings_test.go
@@ -26,6 +26,61 @@ var faces = "☺☻☹";
var commas = "1,2,3,4";
var dots = "1....2....3....4";
+type IndexTest struct {
+ s string;
+ sep string;
+ out int;
+}
+
+var indexTests = []IndexTest {
+ IndexTest{"", "", 0},
+ IndexTest{"", "a", -1},
+ IndexTest{"", "foo", -1},
+ IndexTest{"fo", "foo", -1},
+ IndexTest{"foo", "foo", 0},
+ IndexTest{"oofofoofooo", "f", 2},
+ IndexTest{"oofofoofooo", "foo", 4},
+ IndexTest{"barfoobarfoo", "foo", 3},
+ IndexTest{"foo", "", 0},
+ IndexTest{"foo", "o", 1},
+ IndexTest{"abcABCabc", "A", 3},
+}
+
+var lastIndexTests = []IndexTest {
+ IndexTest{"", "", 0},
+ IndexTest{"", "a", -1},
+ IndexTest{"", "foo", -1},
+ IndexTest{"fo", "foo", -1},
+ IndexTest{"foo", "foo", 0},
+ IndexTest{"oofofoofooo", "f", 7},
+ IndexTest{"oofofoofooo", "foo", 7},
+ IndexTest{"barfoobarfoo", "foo", 9},
+ IndexTest{"foo", "", 3},
+ IndexTest{"foo", "o", 2},
+ IndexTest{"abcABCabc", "A", 3},
+ IndexTest{"abcABCabc", "a", 6},
+}
+
+// Execute f on each test case. funcName should be the name of f; it's used
+// in failure reports.
+func runIndexTests(t *testing.T, f func(s, sep string) int, funcName string, testCases []IndexTest) {
+ for i,test := range testCases {
+ actual := f(test.s, test.sep);
+ if (actual != test.out) {
+ t.Errorf("%s(%q,%q) = %v; want %v", funcName, test.s, test.sep, actual, test.out);
+ }
+ }
+}
+
+func TestIndex(t *testing.T) {
+ runIndexTests(t, Index, "Index", indexTests);
+}
+
+func TestLastIndex(t *testing.T) {
+ runIndexTests(t, LastIndex, "LastIndex", lastIndexTests);
+}
+
+
type ExplodeTest struct {
s string;
a []string;