Refactor cleanPath switch cases

This commit is contained in:
Adrian Setyadi 2019-07-03 05:48:40 +07:00
parent 01ca625b98
commit 32ba640d75
1 changed files with 31 additions and 21 deletions

52
path.go
View File

@ -48,34 +48,44 @@ func cleanPath(p string) string {
// loop has no expensive function calls (except 1x make) // loop has no expensive function calls (except 1x make)
for r < n { for r < n {
switch { switch p[r] {
case p[r] == '/': case '/':
// empty path element, trailing slash is added after the end // empty path element, trailing slash is added after the end
r++ r++
case p[r] == '.' && r+1 == n: case '.':
trailing = true
r++
case p[r] == '.' && p[r+1] == '/':
// . element // . element
r += 2 if r+1 == n {
trailing = true
r++
continue
case p[r] == '.' && p[r+1] == '.' && (r+2 == n || p[r+2] == '/'): } else {
// .. element: remove to last / switch p[r+1] {
r += 3 case '/':
r += 2
continue
if w > 1 { case '.':
// can backtrack if r+2 == n || p[r+2] == '/' {
w-- // .. element: remove to last /
r += 3
if buf == nil { if w > 1 {
for w > 1 && p[w] != '/' { // can backtrack
w-- w--
}
} else { if buf == nil {
for w > 1 && buf[w] != '/' { for w > 1 && p[w] != '/' {
w-- w--
}
} else {
for w > 1 && buf[w] != '/' {
w--
}
}
}
continue
} }
} }
} }