internal/bytealg: reimplement bytealg in pure Go

Previously, we implemented individual bytealg functions via linknaming, and had to update them every once in a while when we hit linker errors.
Instead, this change reimplements the bytealg package in pure Go.
If something is missing, it will cause a compiler error rather than a linker error.
This is easier to test and maintain.
This commit is contained in:
Jaden Weiss
2020-04-07 17:09:30 -04:00
committed by Ayke
parent 38fc340802
commit 473644d918
6 changed files with 130 additions and 48 deletions
-11
View File
@@ -1,11 +0,0 @@
package runtime
//go:linkname indexBytePortable internal/bytealg.IndexByte
func indexBytePortable(s []byte, c byte) int {
for i, b := range s {
if b == c {
return i
}
}
return -1
}
-12
View File
@@ -204,15 +204,3 @@ func decodeUTF8(s string, index uintptr) (rune, uintptr) {
return 0xfffd, 1
}
}
// indexByteString returns the index of the first instance of c in s, or -1 if c
// is not present in s.
//go:linkname indexByteString internal/bytealg.IndexByteString
func indexByteString(s string, c byte) int {
for i := 0; i < len(s); i++ {
if s[i] == c {
return i
}
}
return -1
}
-23
View File
@@ -1,23 +0,0 @@
// +build amd64 arm,go1.13 arm64 ppc64le ppc64 s390x
package runtime
// This file implements the string counting functions used by the strings
// package, for example. It must be reimplemented here as a replacement for the
// Go stdlib asm implementations, but only when the asm implementations are used
// (this varies by Go version).
// Track this file for updates:
// https://github.com/golang/go/blob/master/src/internal/bytealg/count_native.go
// countString copies the implementation from
// https://github.com/golang/go/blob/67f181bfd84dfd5942fe9a29d8a20c9ce5eb2fea/src/internal/bytealg/count_generic.go#L1
//go:linkname countString internal/bytealg.CountString
func countString(s string, c byte) int {
n := 0
for i := 0; i < len(s); i++ {
if s[i] == c {
n++
}
}
return n
}
+3 -1
View File
@@ -2,11 +2,13 @@
package runtime
import "internal/bytealg"
// indexByte provides compatibility with Go 1.11.
// See the following:
// https://github.com/tinygo-org/tinygo/issues/351
// https://github.com/golang/go/commit/ad4a58e31501bce5de2aad90a620eaecdc1eecb8
//go:linkname indexByte strings.IndexByte
func indexByte(s string, c byte) int {
return indexByteString(s, c)
return bytealg.IndexByteString(s, c)
}