1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package path implements utility routines for manipulating slash-separated
8 // The path package should only be used for paths separated by forward
9 // slashes, such as the paths in URLs. This package does not deal with
10 // Windows paths with drive letters or backslashes; to manipulate
11 // operating system paths, use the path/filepath package.
14 // A lazybuf is a lazily constructed path buffer.
15 // It supports append, reading previously appended bytes,
16 // and retrieving the final string. It does not allocate a buffer
17 // to hold the output until that output diverges from s.
24 func (b
*lazybuf
) index(i
int) byte {
31 func (b
*lazybuf
) append(c
byte) {
33 if b
.w
< len(b
.s
) && b
.s
[b
.w
] == c
{
37 b
.buf
= make([]byte, len(b
.s
))
38 copy(b
.buf
, b
.s
[:b
.w
])
44 func (b
*lazybuf
) string() string {
48 return string(b
.buf
[:b
.w
])
51 // Clean returns the shortest path name equivalent to path
52 // by purely lexical processing. It applies the following rules
53 // iteratively until no further processing can be done:
55 // 1. Replace multiple slashes with a single slash.
56 // 2. Eliminate each . path name element (the current directory).
57 // 3. Eliminate each inner .. path name element (the parent directory)
58 // along with the non-.. element that precedes it.
59 // 4. Eliminate .. elements that begin a rooted path:
60 // that is, replace "/.." by "/" at the beginning of a path.
62 // The returned path ends in a slash only if it is the root "/".
64 // If the result of this process is an empty string, Clean
65 // returns the string ".".
67 // See also Rob Pike, ``Lexical File Names in Plan 9 or
68 // Getting Dot-Dot Right,''
69 // https://9p.io/sys/doc/lexnames.html
70 func Clean(path
string) string {
75 rooted
:= path
[0] == '/'
79 // reading from path; r is index of next byte to process.
80 // writing to buf; w is index of next byte to write.
81 // dotdot is index in buf where .. must stop, either because
82 // it is the leading slash or it is a leading ../../.. prefix.
83 out
:= lazybuf
{s
: path
}
95 case path
[r
] == '.' && (r
+1 == n || path
[r
+1] == '/'):
98 case path
[r
] == '.' && path
[r
+1] == '.' && (r
+2 == n || path
[r
+2] == '/'):
99 // .. element: remove to last /
105 for out
.w
> dotdot
&& out
.index(out
.w
) != '/' {
109 // cannot backtrack, but not rooted, so append .. element.
118 // real path element.
119 // add slash if needed
120 if rooted
&& out
.w
!= 1 ||
!rooted
&& out
.w
!= 0 {
124 for ; r
< n
&& path
[r
] != '/'; r
++ {
130 // Turn empty string into "."
138 // lastSlash(s) is strings.LastIndex(s, "/") but we can't import strings.
139 func lastSlash(s
string) int {
141 for i
>= 0 && s
[i
] != '/' {
147 // Split splits path immediately following the final slash,
148 // separating it into a directory and file name component.
149 // If there is no slash in path, Split returns an empty dir and
151 // The returned values have the property that path = dir+file.
152 func Split(path
string) (dir
, file
string) {
154 return path
[:i
+1], path
[i
+1:]
157 // Join joins any number of path elements into a single path,
158 // separating them with slashes. Empty elements are ignored.
159 // The result is Cleaned. However, if the argument list is
160 // empty or all its elements are empty, Join returns
162 func Join(elem
...string) string {
164 for _
, e
:= range elem
{
170 buf
:= make([]byte, 0, size
+len(elem
)-1)
171 for _
, e
:= range elem
{
172 if len(buf
) > 0 || e
!= "" {
174 buf
= append(buf
, '/')
176 buf
= append(buf
, e
...)
179 return Clean(string(buf
))
182 // Ext returns the file name extension used by path.
183 // The extension is the suffix beginning at the final dot
184 // in the final slash-separated element of path;
185 // it is empty if there is no dot.
186 func Ext(path
string) string {
187 for i
:= len(path
) - 1; i
>= 0 && path
[i
] != '/'; i
-- {
195 // Base returns the last element of path.
196 // Trailing slashes are removed before extracting the last element.
197 // If the path is empty, Base returns ".".
198 // If the path consists entirely of slashes, Base returns "/".
199 func Base(path
string) string {
203 // Strip trailing slashes.
204 for len(path
) > 0 && path
[len(path
)-1] == '/' {
205 path
= path
[0 : len(path
)-1]
207 // Find the last element
208 if i
:= lastSlash(path
); i
>= 0 {
211 // If empty now, it had only slashes.
218 // IsAbs reports whether the path is absolute.
219 func IsAbs(path
string) bool {
220 return len(path
) > 0 && path
[0] == '/'
223 // Dir returns all but the last element of path, typically the path's directory.
224 // After dropping the final element using Split, the path is Cleaned and trailing
225 // slashes are removed.
226 // If the path is empty, Dir returns ".".
227 // If the path consists entirely of slashes followed by non-slash bytes, Dir
228 // returns a single slash. In any other case, the returned path does not end in a
230 func Dir(path
string) string {
231 dir
, _
:= Split(path
)