Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2020-05-17 02:04:10 +08:00
parent 1d5b9badd9
commit 31939986b9

15
tree.go
View File

@ -9,6 +9,7 @@ import (
"strings"
"unicode"
"unicode/utf8"
"unsafe"
)
// Param is a single URL parameter, consisting of a key and a value.
@ -40,6 +41,16 @@ func (ps Params) ByName(name string) (va string) {
return
}
func strToBytes(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}
func bytesToStr(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
type methodTree struct {
method string
root *node
@ -163,7 +174,7 @@ walk:
n.children = []*node{&child}
// []byte for proper unicode char conversion, see #65
n.indices = string([]byte{n.path[i]})
n.indices = bytesToStr([]byte{n.path[i]})
n.path = path[:i]
n.handlers = nil
n.wildChild = false
@ -223,7 +234,7 @@ walk:
// Otherwise insert it
if c != ':' && c != '*' {
// []byte for proper unicode char conversion, see #65
n.indices += string([]byte{c})
n.indices += bytesToStr([]byte{c})
child := &node{
fullPath: fullPath,
}