Commit 640f3f25 authored by Rob Pike's avatar Rob Pike

add error case in doc for Index. simplify code slightly.

R=rsc
DELTA=5  (1 added, 0 deleted, 4 changed)
OCL=27148
CL=27151
parent a888d4d2
...@@ -37,14 +37,15 @@ func Count(s, sep string) int { ...@@ -37,14 +37,15 @@ func Count(s, sep string) int {
return n 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 { func Index(s, sep string) int {
if sep == "" { n := len(sep);
if n == 0 {
return 0 return 0
} }
c := sep[0]; c := sep[0];
for i := 0; i+len(sep) <= len(s); i++ { for i := 0; i+n <= len(s); i++ {
if s[i] == c && (len(sep) == 1 || s[i:i+len(sep)] == sep) { if s[i] == c && (n == 1 || s[i:i+n] == sep) {
return i return i
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment