MCPcopy
hub / github.com/julienschmidt/httprouter / CleanPath

Function CleanPath

path.go:21–110  ·  view source on GitHub ↗

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 curren

(p string)

Source from the content-addressed store, hash-verified

19//
20// If the result of this process is an empty string, "/" is returned
21func CleanPath(p string) string {
22 // Turn empty string into "/"
23 if p == "" {
24 return "/"
25 }
26
27 n := len(p)
28 var buf []byte
29
30 // Invariants:
31 // reading from path; r is index of next byte to process.
32 // writing to buf; w is index of next byte to write.
33
34 // path must start with '/'
35 r := 1
36 w := 1
37
38 if p[0] != '/' {
39 r = 0
40 buf = make([]byte, n+1)
41 buf[0] = '/'
42 }
43
44 trailing := n > 1 && p[n-1] == '/'
45
46 // A bit more clunky without a 'lazybuf' like the path package, but the loop
47 // gets completely inlined (bufApp). So in contrast to the path package this
48 // loop has no expensive function calls (except 1x make)
49
50 for r < n {
51 switch {
52 case p[r] == '/':
53 // empty path element, trailing slash is added after the end
54 r++
55
56 case p[r] == '.' && r+1 == n:
57 trailing = true
58 r++
59
60 case p[r] == '.' && p[r+1] == '/':
61 // . element
62 r += 2
63
64 case p[r] == '.' && p[r+1] == '.' && (r+2 == n || p[r+2] == '/'):
65 // .. element: remove to last /
66 r += 3
67
68 if w > 1 {
69 // can backtrack
70 w--
71
72 if buf == nil {
73 for w > 1 && p[w] != '/' {
74 w--
75 }
76 } else {
77 for w > 1 && buf[w] != '/' {
78 w--

Callers 3

ServeHTTPMethod · 0.85
TestPathCleanFunction · 0.85
TestPathCleanMallocsFunction · 0.85

Calls 1

bufAppFunction · 0.85

Tested by 2

TestPathCleanFunction · 0.68
TestPathCleanMallocsFunction · 0.68