path.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package router
  2. // CleanPath is the URL version of path.Clean, it returns a canonical URL path
  3. // for p, eliminating . and .. elements.
  4. //
  5. // The following rules are applied iteratively until no further processing can
  6. // be done:
  7. // 1. Replace multiple slashes with a single slash.
  8. // 2. Eliminate each . path name element (the current directory).
  9. // 3. Eliminate each inner .. path name element (the parent directory)
  10. // along with the non-.. element that precedes it.
  11. // 4. Eliminate .. elements that begin a rooted path:
  12. // that is, replace "/.." by "/" at the beginning of a path.
  13. //
  14. // If the result of this process is an empty string, "/" is returned
  15. func CleanPath(p string) string {
  16. const stackBufSize = 128
  17. // Turn empty string into "/"
  18. if p == "" {
  19. return "/"
  20. }
  21. // Reasonably sized buffer on stack to avoid allocations in the common case.
  22. // If a larger buffer is required, it gets allocated dynamically.
  23. buf := make([]byte, 0, stackBufSize)
  24. n := len(p)
  25. // Invariants:
  26. // reading from path; r is index of next byte to process.
  27. // writing to buf; w is index of next byte to write.
  28. // path must start with '/'
  29. r := 1
  30. w := 1
  31. if p[0] != '/' {
  32. r = 0
  33. if n+1 > stackBufSize {
  34. buf = make([]byte, n+1)
  35. } else {
  36. buf = buf[:n+1]
  37. }
  38. buf[0] = '/'
  39. }
  40. trailing := n > 1 && p[n-1] == '/'
  41. // A bit more clunky without a 'lazybuf' like the path package, but the loop
  42. // gets completely inlined (bufApp calls).
  43. // So in contrast to the path package this loop has no expensive function
  44. // calls (except make, if needed).
  45. for r < n {
  46. switch {
  47. case p[r] == '/':
  48. // empty path element, trailing slash is added after the end
  49. r++
  50. case p[r] == '.' && r+1 == n:
  51. trailing = true
  52. r++
  53. case p[r] == '.' && p[r+1] == '/':
  54. // . element
  55. r += 2
  56. case p[r] == '.' && p[r+1] == '.' && (r+2 == n || p[r+2] == '/'):
  57. // .. element: remove to last /
  58. r += 3
  59. if w > 1 {
  60. // can backtrack
  61. w--
  62. if len(buf) == 0 {
  63. for w > 1 && p[w] != '/' {
  64. w--
  65. }
  66. } else {
  67. for w > 1 && buf[w] != '/' {
  68. w--
  69. }
  70. }
  71. }
  72. default:
  73. // Real path element.
  74. // Add slash if needed
  75. if w > 1 {
  76. bufApp(&buf, p, w, '/')
  77. w++
  78. }
  79. // Copy element
  80. for r < n && p[r] != '/' {
  81. bufApp(&buf, p, w, p[r])
  82. w++
  83. r++
  84. }
  85. }
  86. }
  87. // Re-append trailing slash
  88. if trailing && w > 1 {
  89. bufApp(&buf, p, w, '/')
  90. w++
  91. }
  92. // If the original string was not modified (or only shortened at the end),
  93. // return the respective substring of the original string.
  94. // Otherwise return a new string from the buffer.
  95. if len(buf) == 0 {
  96. return p[:w]
  97. }
  98. return string(buf[:w])
  99. }
  100. // Internal helper to lazily create a buffer if necessary.
  101. // Calls to this function get inlined.
  102. func bufApp(buf *[]byte, s string, w int, c byte) {
  103. b := *buf
  104. if len(b) == 0 {
  105. // No modification of the original string so far.
  106. // If the next character is the same as in the original string, we do
  107. // not yet have to allocate a buffer.
  108. if s[w] == c {
  109. return
  110. }
  111. // Otherwise use either the stack buffer, if it is large enough, or
  112. // allocate a new buffer on the heap, and copy all previous characters.
  113. if l := len(s); l > cap(b) {
  114. *buf = make([]byte, len(s))
  115. } else {
  116. *buf = (*buf)[:l]
  117. }
  118. b = *buf
  119. copy(b, s[:w])
  120. }
  121. b[w] = c
  122. }