Skip to content

Commit

Permalink
docs(path): improve comments (gin-gonic#2223)
Browse files Browse the repository at this point in the history
* chore(path): improve comments

copy from julienschmidt/httprouter@15782a7

* fix typo

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy authored and ThomasObenaus committed Feb 19, 2020
1 parent 938dce2 commit 09068f0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
24 changes: 17 additions & 7 deletions path.go
Expand Up @@ -53,8 +53,9 @@ func cleanPath(p string) string {
trailing := n > 1 && p[n-1] == '/'

// 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
// loop has no expensive function calls (except 1x make)
// gets completely inlined (bufApp calls).
// 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 {
switch {
Expand Down Expand Up @@ -90,14 +91,14 @@ func cleanPath(p string) string {
}

default:
// real path element.
// add slash if needed
// Real path element.
// Add slash if needed
if w > 1 {
bufApp(&buf, p, w, '/')
w++
}

// copy element
// Copy element
for r < n && p[r] != '/' {
bufApp(&buf, p, w, p[r])
w++
Expand All @@ -106,26 +107,35 @@ func cleanPath(p string) string {
}
}

// re-append trailing slash
// Re-append trailing slash
if trailing && w > 1 {
bufApp(&buf, p, 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 {
return p[: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) {
b := *buf
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 {
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) {
*buf = make([]byte, len(s))
} else {
Expand Down
2 changes: 1 addition & 1 deletion tree.go
Expand Up @@ -401,7 +401,7 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle
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.handlers = handlers
n.fullPath = fullPath
Expand Down

0 comments on commit 09068f0

Please sign in to comment.