Merge branch 'master' into negotiate-formats

This commit is contained in:
thinkerou 2020-02-05 22:09:55 +08:00 committed by GitHub
commit 1452befad0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions

24
path.go
View File

@ -53,8 +53,9 @@ func cleanPath(p string) string {
trailing := n > 1 && p[n-1] == '/' trailing := n > 1 && p[n-1] == '/'
// A bit more clunky without a 'lazybuf' like the path package, but the loop // A bit more clunky without a 'lazybuf' like the path package, but the loop
// gets completely inlined (bufApp). So in contrast to the path package this // gets completely inlined (bufApp calls).
// loop has no expensive function calls (except 1x make) // loop has no expensive function calls (except 1x make) // So in contrast to the path package this loop has no expensive function
// calls (except make, if needed).
for r < n { for r < n {
switch { switch {
@ -90,14 +91,14 @@ func cleanPath(p string) string {
} }
default: default:
// real path element. // Real path element.
// add slash if needed // Add slash if needed
if w > 1 { if w > 1 {
bufApp(&buf, p, w, '/') bufApp(&buf, p, w, '/')
w++ w++
} }
// copy element // Copy element
for r < n && p[r] != '/' { for r < n && p[r] != '/' {
bufApp(&buf, p, w, p[r]) bufApp(&buf, p, w, p[r])
w++ w++
@ -106,26 +107,35 @@ func cleanPath(p string) string {
} }
} }
// re-append trailing slash // Re-append trailing slash
if trailing && w > 1 { if trailing && w > 1 {
bufApp(&buf, p, w, '/') bufApp(&buf, p, w, '/')
w++ w++
} }
// If the original string was not modified (or only shortened at the end),
// return the respective substring of the original string.
// Otherwise return a new string from the buffer.
if len(buf) == 0 { if len(buf) == 0 {
return p[:w] return p[:w]
} }
return string(buf[:w]) return string(buf[:w])
} }
// internal helper to lazily create a buffer if necessary. // Internal helper to lazily create a buffer if necessary.
// Calls to this function get inlined.
func bufApp(buf *[]byte, s string, w int, c byte) { func bufApp(buf *[]byte, s string, w int, c byte) {
b := *buf b := *buf
if len(b) == 0 { if len(b) == 0 {
// No modification of the original string so far.
// If the next character is the same as in the original string, we do
// not yet have to allocate a buffer.
if s[w] == c { if s[w] == c {
return return
} }
// Otherwise use either the stack buffer, if it is large enough, or
// allocate a new buffer on the heap, and copy all previous characters.
if l := len(s); l > cap(b) { if l := len(s); l > cap(b) {
*buf = make([]byte, len(s)) *buf = make([]byte, len(s))
} else { } else {

View File

@ -401,7 +401,7 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle
return return
} }
// If no wildcard was found, simple insert the path and handle // If no wildcard was found, simply insert the path and handle
n.path = path n.path = path
n.handlers = handlers n.handlers = handlers
n.fullPath = fullPath n.fullPath = fullPath
@ -464,7 +464,6 @@ walk: // Outer loop for walking the tree
for i, max := 0, len(indices); i < max; i++ { for i, max := 0, len(indices); i < max; i++ {
if c == indices[i] { if c == indices[i] {
n = n.children[i] n = n.children[i]
prefix = n.path
continue walk continue walk
} }
} }
@ -508,7 +507,6 @@ walk: // Outer loop for walking the tree
if len(n.children) > 0 { if len(n.children) > 0 {
path = path[end:] path = path[end:]
n = n.children[0] n = n.children[0]
prefix = n.path
continue walk continue walk
} }