Skip to content

Commit

Permalink
compiler/natives/src/strings: Simple Builder implementation for js ar…
Browse files Browse the repository at this point in the history
…chitecture.

Can't use package unsafe, so use a simple type conversion from []byte
to string in Builder.String method.

Further optimizations are deferred for later (most likely not needed
because Go->wasm will be ready by the time they're needed).

Related to golang/go#18990.
  • Loading branch information
dmitshur committed Feb 9, 2018
1 parent f5c4d3b commit c53b5b9
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions compiler/natives/src/strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ func Count(s, sep string) int {
}
return n
}

func (b *Builder) String() string {
// Upstream Builder.String relies on package unsafe. We can't do that.
// TODO: It's possible that the entire strings.Builder API can be implemented
// more efficiently for GOARCH=js specifically (avoid using []byte, instead
// use a String directly; or some JavaScript string builder API if one exists).
// But this is more work, defer doing it until there's a need shown via profiling,
// and there are benchmarks available (see https://github.com/golang/go/issues/18990#issuecomment-352068533).
return string(b.buf)
}

0 comments on commit c53b5b9

Please sign in to comment.