net: use internal/bytealg insetad of linkname tricks

We are currently using go:linkname for some algorithms from
strings/bytes packages, to avoid importing strings/bytes.
But strings/bytes are just wrappers around internal/bytealg, so
we should use internal/bytealg directly.

Change-Id: I2836f779b88bf8876d5fa725043a6042bdda0390
Reviewed-on: https://go-review.googlesource.com/130515
Run-TryBot: Ilya Tocar <ilya.tocar@intel.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Ilya Tocar 2018-08-21 14:50:48 -05:00 committed by Brad Fitzpatrick
parent 08cc3dc5e7
commit 39e59da76d
12 changed files with 46 additions and 50 deletions

View file

@ -8,10 +8,10 @@
package net
import (
"internal/bytealg"
"io"
"os"
"time"
_ "unsafe" // For go:linkname
)
type file struct {
@ -80,17 +80,11 @@ func stat(name string) (mtime time.Time, size int64, err error) {
return st.ModTime(), st.Size(), nil
}
// byteIndex is strings.IndexByte. It returns the index of the
// first instance of c in s, or -1 if c is not present in s.
// strings.IndexByte is implemented in runtime/asm_$GOARCH.s
//go:linkname byteIndex strings.IndexByte
func byteIndex(s string, c byte) int
// Count occurrences in s of any bytes in t.
func countAnyByte(s string, t string) int {
n := 0
for i := 0; i < len(s); i++ {
if byteIndex(t, s[i]) >= 0 {
if bytealg.IndexByteString(t, s[i]) >= 0 {
n++
}
}
@ -103,7 +97,7 @@ func splitAtBytes(s string, t string) []string {
n := 0
last := 0
for i := 0; i < len(s); i++ {
if byteIndex(t, s[i]) >= 0 {
if bytealg.IndexByteString(t, s[i]) >= 0 {
if last < i {
a[n] = s[last:i]
n++
@ -276,7 +270,7 @@ func isSpace(b byte) bool {
// removeComment returns line, removing any '#' byte and any following
// bytes.
func removeComment(line []byte) []byte {
if i := bytesIndexByte(line, '#'); i != -1 {
if i := bytealg.IndexByte(line, '#'); i != -1 {
return line[:i]
}
return line
@ -287,7 +281,7 @@ func removeComment(line []byte) []byte {
// It returns the first non-nil error returned by fn.
func foreachLine(x []byte, fn func(line []byte) error) error {
for len(x) > 0 {
nl := bytesIndexByte(x, '\n')
nl := bytealg.IndexByte(x, '\n')
if nl == -1 {
return fn(x)
}
@ -305,7 +299,7 @@ func foreachLine(x []byte, fn func(line []byte) error) error {
func foreachField(x []byte, fn func(field []byte) error) error {
x = trimSpace(x)
for len(x) > 0 {
sp := bytesIndexByte(x, ' ')
sp := bytealg.IndexByte(x, ' ')
if sp == -1 {
return fn(x)
}
@ -319,12 +313,6 @@ func foreachField(x []byte, fn func(field []byte) error) error {
return nil
}
// bytesIndexByte is bytes.IndexByte. It returns the index of the
// first instance of c in s, or -1 if c is not present in s.
// bytes.IndexByte is implemented in runtime/asm_$GOARCH.s
//go:linkname bytesIndexByte bytes.IndexByte
func bytesIndexByte(s []byte, c byte) int
// stringsHasSuffix is strings.HasSuffix. It reports whether s ends in
// suffix.
func stringsHasSuffix(s, suffix string) bool {