]> Cypherpunks repositories - gostls13.git/commitdiff
add error case in doc for Index. simplify code slightly.
authorRob Pike <r@golang.org>
Tue, 7 Apr 2009 07:32:16 +0000 (00:32 -0700)
committerRob Pike <r@golang.org>
Tue, 7 Apr 2009 07:32:16 +0000 (00:32 -0700)
R=rsc
DELTA=5  (1 added, 0 deleted, 4 changed)
OCL=27148
CL=27151

src/lib/strings.go

index 1acbed425efc3407009b176b8c9bb7466adac1ca..06a923427abf9ec89559f74baf5a94a2f4d8eb72 100644 (file)
@@ -37,14 +37,15 @@ func Count(s, sep string) int {
        return n
 }
 
-// Index returns the index of the first instance of sep in s.
+// 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 string) int {
-       if sep == "" {
+       n := len(sep);
+       if n == 0 {
                return 0
        }
        c := sep[0];
-       for i := 0; i+len(sep) <= len(s); i++ {
-               if s[i] == c && (len(sep) == 1 || s[i:i+len(sep)] == sep) {
+       for i := 0; i+n <= len(s); i++ {
+               if s[i] == c && (n == 1 || s[i:i+n] == sep) {
                        return i
                }
        }