some loop don't need to transfer to bytes slice and some use the zero copy will much better.

This commit is contained in:
mask-pp 2020-12-01 19:24:18 +08:00
parent 7742ff50e0
commit c10029a124

14
tree.go
View File

@ -254,7 +254,7 @@ walk:
// Returns -1 as index, if no wildcard was found.
func findWildcard(path string) (wildcard string, i int, valid bool) {
// Find start
for start, c := range []byte(path) {
for start, c := range path {
// A wildcard starts with ':' (param) or '*' (catch-all)
if c != ':' && c != '*' {
continue
@ -262,7 +262,7 @@ func findWildcard(path string) (wildcard string, i int, valid bool) {
// Find end and check for invalid characters
valid = true
for end, c := range []byte(path[start+1:]) {
for end, c := range path[start+1:] {
switch c {
case '/':
return path[start : start+1+end], start, valid
@ -528,7 +528,7 @@ walk: // Outer loop for walking the tree
// No handle found. Check if a handle for this path + a
// trailing slash exists for trailing slash recommendation
for i, c := range []byte(n.indices) {
for i, c := range n.indices {
if c == '/' {
n = n.children[i]
value.tsr = (len(n.path) == 1 && n.handlers != nil) ||
@ -611,7 +611,7 @@ walk: // Outer loop for walking the tree
if rb[0] != 0 {
// Old rune not finished
idxc := rb[0]
for i, c := range []byte(n.indices) {
for i, c := range bytesconv.StringToBytes(n.indices) {
if c == idxc {
// continue with child node
n = n.children[i]
@ -643,7 +643,7 @@ walk: // Outer loop for walking the tree
rb = shiftNRuneBytes(rb, off)
idxc := rb[0]
for i, c := range []byte(n.indices) {
for i, c := range bytesconv.StringToBytes(n.indices) {
// Lowercase matches
if c == idxc {
// must use a recursive approach since both the
@ -665,7 +665,7 @@ walk: // Outer loop for walking the tree
rb = shiftNRuneBytes(rb, off)
idxc := rb[0]
for i, c := range []byte(n.indices) {
for i, c := range bytesconv.StringToBytes(n.indices) {
// Uppercase matches
if c == idxc {
// Continue with child node
@ -745,7 +745,7 @@ walk: // Outer loop for walking the tree
// No handle found.
// Try to fix the path by adding a trailing slash
if fixTrailingSlash {
for i, c := range []byte(n.indices) {
for i, c := range n.indices {
if c == '/' {
n = n.children[i]
if (len(n.path) == 1 && n.handlers != nil) ||