github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/compiler/natives/src/strings/strings.go (about) 1 //go:build js 2 // +build js 3 4 package strings 5 6 import ( 7 "unicode/utf8" 8 9 "github.com/gopherjs/gopherjs/js" 10 ) 11 12 func IndexByte(s string, c byte) int { 13 return js.InternalObject(s).Call("indexOf", js.Global.Get("String").Call("fromCharCode", c)).Int() 14 } 15 16 func Index(s, sep string) int { 17 return js.InternalObject(s).Call("indexOf", js.InternalObject(sep)).Int() 18 } 19 20 func LastIndex(s, sep string) int { 21 return js.InternalObject(s).Call("lastIndexOf", js.InternalObject(sep)).Int() 22 } 23 24 func Count(s, sep string) int { 25 n := 0 26 // special cases 27 switch { 28 case len(sep) == 0: 29 return utf8.RuneCountInString(s) + 1 30 case len(sep) > len(s): 31 return 0 32 case len(sep) == len(s): 33 if sep == s { 34 return 1 35 } 36 return 0 37 } 38 39 for { 40 pos := Index(s, sep) 41 if pos == -1 { 42 break 43 } 44 n++ 45 s = s[pos+len(sep):] 46 } 47 return n 48 } 49 50 func (b *Builder) String() string { 51 // Upstream Builder.String relies on package unsafe. We can't do that. 52 // TODO: It's possible that the entire strings.Builder API can be implemented 53 // more efficiently for GOARCH=js specifically (avoid using []byte, instead 54 // use a String directly; or some JavaScript string builder API if one exists). 55 // But this is more work, defer doing it until there's a need shown via profiling, 56 // and there are benchmarks available (see https://github.com/golang/go/issues/18990#issuecomment-352068533). 57 return string(b.buf) 58 } 59 60 func (b *Builder) copyCheck() { 61 if b.addr == nil { 62 // Upstream copyCheck uses noescape, which performs unsafe.Pointer manipulation. 63 // We can't do that, so skip it. See https://github.com/golang/go/commit/484586c81a0196e42ac52f651bc56017ca454280. 64 b.addr = b 65 } else if b.addr != b { 66 panic("strings: illegal use of non-zero Builder copied by value") 67 } 68 } 69 70 func Clone(s string) string { 71 // Since in the JavaScript runtime we don't have access the string's 72 // baking memory, we let the engine's garbage collector deal with substring 73 // memory overheads and simply return the string as-is. 74 return s 75 }