diff options
| author | Rob Pike <r@golang.org> | 2009-04-07 00:32:16 -0700 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2009-04-07 00:32:16 -0700 |
| commit | 640f3f25dcd238bfc6b4fd99b1808071cfa12888 (patch) | |
| tree | 6c8c025a1458d096bd41028ccaab2184691e2f3a /src/lib/strings.go | |
| parent | a888d4d233d98c009aabf0dacaca5f364fcbc56c (diff) | |
| download | go-640f3f25dcd238bfc6b4fd99b1808071cfa12888.tar.xz | |
add error case in doc for Index. simplify code slightly.
R=rsc
DELTA=5 (1 added, 0 deleted, 4 changed)
OCL=27148
CL=27151
Diffstat (limited to 'src/lib/strings.go')
| -rw-r--r-- | src/lib/strings.go | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/src/lib/strings.go b/src/lib/strings.go index 1acbed425e..06a923427a 100644 --- a/src/lib/strings.go +++ b/src/lib/strings.go @@ -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 } } |
