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.
18 // A lazybuf is a lazily constructed path buffer.
19 // It supports append, reading previously appended bytes,
20 // and retrieving the final string. It does not allocate a buffer
21 // to hold the output until that output diverges from s.
28 func (b
*lazybuf
) index(i
int) byte {
35 func (b
*lazybuf
) append(c
byte) {
37 if b
.w
< len(b
.s
) && b
.s
[b
.w
] == c
{
41 b
.buf
= make([]byte, len(b
.s
))
42 copy(b
.buf
, b
.s
[:b
.w
])
48 func (b
*lazybuf
) string() string {
52 return string(b
.buf
[:b
.w
])
55 // Clean returns the shortest path name equivalent to path
56 // by purely lexical processing. It applies the following rules
57 // iteratively until no further processing can be done:
59 // 1. Replace multiple slashes with a single slash.
60 // 2. Eliminate each . path name element (the current directory).
61 // 3. Eliminate each inner .. path name element (the parent directory)
62 // along with the non-.. element that precedes it.
63 // 4. Eliminate .. elements that begin a rooted path:
64 // that is, replace "/.." by "/" at the beginning of a path.
66 // The returned path ends in a slash only if it is the root "/".
68 // If the result of this process is an empty string, Clean
69 // returns the string ".".
71 // See also Rob Pike, ``Lexical File Names in Plan 9 or
72 // Getting Dot-Dot Right,''
73 // https://9p.io/sys/doc/lexnames.html
74 func Clean(path
string) string {
79 rooted
:= path
[0] == '/'
83 // reading from path; r is index of next byte to process.
84 // writing to buf; w is index of next byte to write.
85 // dotdot is index in buf where .. must stop, either because
86 // it is the leading slash or it is a leading ../../.. prefix.
87 out
:= lazybuf
{s
: path
}
99 case path
[r
] == '.' && (r
+1 == n || path
[r
+1] == '/'):
102 case path
[r
] == '.' && path
[r
+1] == '.' && (r
+2 == n || path
[r
+2] == '/'):
103 // .. element: remove to last /
109 for out
.w
> dotdot
&& out
.index(out
.w
) != '/' {
113 // cannot backtrack, but not rooted, so append .. element.
122 // real path element.
123 // add slash if needed
124 if rooted
&& out
.w
!= 1 ||
!rooted
&& out
.w
!= 0 {
128 for ; r
< n
&& path
[r
] != '/'; r
++ {
134 // Turn empty string into "."
142 // Split splits path immediately following the final slash,
143 // separating it into a directory and file name component.
144 // If there is no slash in path, Split returns an empty dir and
146 // The returned values have the property that path = dir+file.
147 func Split(path
string) (dir
, file
string) {
148 i
:= strings
.LastIndex(path
, "/")
149 return path
[:i
+1], path
[i
+1:]
152 // Join joins any number of path elements into a single path, adding a
153 // separating slash if necessary. The result is Cleaned; in particular,
154 // all empty strings are ignored.
155 func Join(elem
...string) string {
156 for i
, e
:= range elem
{
158 return Clean(strings
.Join(elem
[i
:], "/"))
164 // Ext returns the file name extension used by path.
165 // The extension is the suffix beginning at the final dot
166 // in the final slash-separated element of path;
167 // it is empty if there is no dot.
168 func Ext(path
string) string {
169 for i
:= len(path
) - 1; i
>= 0 && path
[i
] != '/'; i
-- {
177 // Base returns the last element of path.
178 // Trailing slashes are removed before extracting the last element.
179 // If the path is empty, Base returns ".".
180 // If the path consists entirely of slashes, Base returns "/".
181 func Base(path
string) string {
185 // Strip trailing slashes.
186 for len(path
) > 0 && path
[len(path
)-1] == '/' {
187 path
= path
[0 : len(path
)-1]
189 // Find the last element
190 if i
:= strings
.LastIndex(path
, "/"); i
>= 0 {
193 // If empty now, it had only slashes.
200 // IsAbs reports whether the path is absolute.
201 func IsAbs(path
string) bool {
202 return len(path
) > 0 && path
[0] == '/'
205 // Dir returns all but the last element of path, typically the path's directory.
206 // After dropping the final element using Split, the path is Cleaned and trailing
207 // slashes are removed.
208 // If the path is empty, Dir returns ".".
209 // If the path consists entirely of slashes followed by non-slash bytes, Dir
210 // returns a single slash. In any other case, the returned path does not end in a
212 func Dir(path
string) string {
213 dir
, _
:= Split(path
)