cleanPath is the URL version of path.Clean, it returns a canonical URL path for p, eliminating . and .. elements. The following rules are applied iteratively until no further processing can be done: 1. Replace multiple slashes with a single slash. 2. Eliminate each . path name element (the current
(p string)
| 21 | // |
| 22 | // If the result of this process is an empty string, "/" is returned. |
| 23 | func cleanPath(p string) string { |
| 24 | // Turn empty string into "/" |
| 25 | if p == "" { |
| 26 | return "/" |
| 27 | } |
| 28 | |
| 29 | // Reasonably sized buffer on stack to avoid allocations in the common case. |
| 30 | // If a larger buffer is required, it gets allocated dynamically. |
| 31 | buf := make([]byte, 0, stackBufSize) |
| 32 | |
| 33 | n := len(p) |
| 34 | |
| 35 | // Invariants: |
| 36 | // reading from path; r is index of next byte to process. |
| 37 | // writing to buf; w is index of next byte to write. |
| 38 | |
| 39 | // path must start with '/' |
| 40 | r := 1 |
| 41 | w := 1 |
| 42 | |
| 43 | if p[0] != '/' { |
| 44 | r = 0 |
| 45 | |
| 46 | if n+1 > stackBufSize { |
| 47 | buf = make([]byte, n+1) |
| 48 | } else { |
| 49 | buf = buf[:n+1] |
| 50 | } |
| 51 | buf[0] = '/' |
| 52 | } |
| 53 | |
| 54 | trailing := n > 1 && p[n-1] == '/' |
| 55 | |
| 56 | // A bit more clunky without a 'lazybuf' like the path package, but the loop |
| 57 | // gets completely inlined (bufApp calls). |
| 58 | // loop has no expensive function calls (except 1x make) // So in contrast to the path package this loop has no expensive function |
| 59 | // calls (except make, if needed). |
| 60 | |
| 61 | for r < n { |
| 62 | switch { |
| 63 | case p[r] == '/': |
| 64 | // empty path element, trailing slash is added after the end |
| 65 | r++ |
| 66 | |
| 67 | case p[r] == '.' && r+1 == n: |
| 68 | trailing = true |
| 69 | r++ |
| 70 | |
| 71 | case p[r] == '.' && p[r+1] == '/': |
| 72 | // . element |
| 73 | r += 2 |
| 74 | |
| 75 | case p[r] == '.' && p[r+1] == '.' && (r+2 == n || p[r+2] == '/'): |
| 76 | // .. element: remove to last / |
| 77 | r += 3 |
| 78 | |
| 79 | if w > 1 { |
| 80 | // can backtrack |