Remove some functions that have the same effect as the bytes package (#2387)

This commit is contained in:
bestgopher 2020-05-23 22:19:37 +08:00 committed by GitHub
parent acbb3d4f42
commit 7bffae1d3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 33 deletions

42
tree.go
View File

@ -7,11 +7,11 @@ package gin
import ( import (
"bytes" "bytes"
"net/url" "net/url"
"reflect"
"strings" "strings"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"unsafe"
"github.com/gin-gonic/gin/internal/bytesconv"
) )
var ( var (
@ -80,36 +80,12 @@ func longestCommonPrefix(a, b string) int {
return i return i
} }
// bytesToStr converts byte slice to a string without memory allocation.
// See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ .
//
// Note it may break if string and/or slice header will change
// in the future go versions.
func bytesToStr(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// strToBytes converts string to a byte slice without memory allocation.
//
// Note it may break if string and/or slice header will change
// in the future go versions.
func strToBytes(s string) (b []byte) {
/* #nosec G103 */
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
/* #nosec G103 */
sh := *(*reflect.StringHeader)(unsafe.Pointer(&s))
bh.Data = sh.Data
bh.Len = sh.Len
bh.Cap = sh.Len
return b
}
func countParams(path string) uint16 { func countParams(path string) uint16 {
var n uint var n uint16
s := strToBytes(path) s := bytesconv.StringToBytes(path)
n += uint(bytes.Count(s, strColon)) n += uint16(bytes.Count(s, strColon))
n += uint(bytes.Count(s, strStar)) n += uint16(bytes.Count(s, strStar))
return uint16(n) return n
} }
type nodeType uint8 type nodeType uint8
@ -192,7 +168,7 @@ walk:
n.children = []*node{&child} n.children = []*node{&child}
// []byte for proper unicode char conversion, see #65 // []byte for proper unicode char conversion, see #65
n.indices = bytesToStr([]byte{n.path[i]}) n.indices = bytesconv.BytesToString([]byte{n.path[i]})
n.path = path[:i] n.path = path[:i]
n.handlers = nil n.handlers = nil
n.wildChild = false n.wildChild = false
@ -252,7 +228,7 @@ walk:
// Otherwise insert it // Otherwise insert it
if c != ':' && c != '*' { if c != ':' && c != '*' {
// []byte for proper unicode char conversion, see #65 // []byte for proper unicode char conversion, see #65
n.indices += bytesToStr([]byte{c}) n.indices += bytesconv.BytesToString([]byte{c})
child := &node{ child := &node{
fullPath: fullPath, fullPath: fullPath,
} }