| 1 |
// +build js |
| 2 |
|
| 3 |
package strings |
| 4 |
|
| 5 |
import ( |
| 6 |
"unicode/utf8" |
| 7 |
|
| 8 |
"github.com/gopherjs/gopherjs/js" |
| 9 |
) |
| 10 |
|
| 11 |
func IndexByte(s string, c byte) int { |
| 12 |
return js.InternalObject(s).Call("indexOf", js.Global.Get("String").Call("fromCharCode", c)).Int() |
| 13 |
} |
| 14 |
|
| 15 |
func Index(s, sep string) int { |
| 16 |
return js.InternalObject(s).Call("indexOf", js.InternalObject(sep)).Int() |
| 17 |
} |
| 18 |
|
| 19 |
func LastIndex(s, sep string) int { |
| 20 |
return js.InternalObject(s).Call("lastIndexOf", js.InternalObject(sep)).Int() |
| 21 |
} |
| 22 |
|
| 23 |
func Count(s, sep string) int { |
| 24 |
n := 0 |
| 25 |
// special cases |
| 26 |
switch { |
| 27 |
case len(sep) == 0: |
| 28 |
return utf8.RuneCountInString(s) + 1 |
| 29 |
case len(sep) > len(s): |
| 30 |
return 0 |
| 31 |
case len(sep) == len(s): |
| 32 |
if sep == s { |
| 33 |
return 1 |
| 34 |
} |
| 35 |
return 0 |
| 36 |
} |
| 37 |
|
| 38 |
for { |
| 39 |
pos := Index(s, sep) |
| 40 |
if pos == -1 { |
| 41 |
break |
| 42 |
} |
| 43 |
n++ |
| 44 |
s = s[pos+len(sep):] |
| 45 |
} |
| 46 |
return n |
| 47 |
} |