OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [path/] [path.go] - Blame information for rev 801

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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.
4
 
5
// Package path implements utility routines for manipulating slash-separated
6
// filename paths.
7
package path
8
 
9
import (
10
        "strings"
11
)
12
 
13
// Clean returns the shortest path name equivalent to path
14
// by purely lexical processing.  It applies the following rules
15
// iteratively until no further processing can be done:
16
//
17
//      1. Replace multiple slashes with a single slash.
18
//      2. Eliminate each . path name element (the current directory).
19
//      3. Eliminate each inner .. path name element (the parent directory)
20
//         along with the non-.. element that precedes it.
21
//      4. Eliminate .. elements that begin a rooted path:
22
//         that is, replace "/.." by "/" at the beginning of a path.
23
//
24
// If the result of this process is an empty string, Clean
25
// returns the string ".".
26
//
27
// See also Rob Pike, ``Lexical File Names in Plan 9 or
28
// Getting Dot-Dot right,''
29
// http://plan9.bell-labs.com/sys/doc/lexnames.html
30
func Clean(path string) string {
31
        if path == "" {
32
                return "."
33
        }
34
 
35
        rooted := path[0] == '/'
36
        n := len(path)
37
 
38
        // Invariants:
39
        //      reading from path; r is index of next byte to process.
40
        //      writing to buf; w is index of next byte to write.
41
        //      dotdot is index in buf where .. must stop, either because
42
        //              it is the leading slash or it is a leading ../../.. prefix.
43
        buf := []byte(path)
44
        r, w, dotdot := 0, 0, 0
45
        if rooted {
46
                r, w, dotdot = 1, 1, 1
47
        }
48
 
49
        for r < n {
50
                switch {
51
                case path[r] == '/':
52
                        // empty path element
53
                        r++
54
                case path[r] == '.' && (r+1 == n || path[r+1] == '/'):
55
                        // . element
56
                        r++
57
                case path[r] == '.' && path[r+1] == '.' && (r+2 == n || path[r+2] == '/'):
58
                        // .. element: remove to last /
59
                        r += 2
60
                        switch {
61
                        case w > dotdot:
62
                                // can backtrack
63
                                w--
64
                                for w > dotdot && buf[w] != '/' {
65
                                        w--
66
                                }
67
                        case !rooted:
68
                                // cannot backtrack, but not rooted, so append .. element.
69
                                if w > 0 {
70
                                        buf[w] = '/'
71
                                        w++
72
                                }
73
                                buf[w] = '.'
74
                                w++
75
                                buf[w] = '.'
76
                                w++
77
                                dotdot = w
78
                        }
79
                default:
80
                        // real path element.
81
                        // add slash if needed
82
                        if rooted && w != 1 || !rooted && w != 0 {
83
                                buf[w] = '/'
84
                                w++
85
                        }
86
                        // copy element
87
                        for ; r < n && path[r] != '/'; r++ {
88
                                buf[w] = path[r]
89
                                w++
90
                        }
91
                }
92
        }
93
 
94
        // Turn empty string into "."
95
        if w == 0 {
96
                buf[w] = '.'
97
                w++
98
        }
99
 
100
        return string(buf[0:w])
101
}
102
 
103
// Split splits path immediately following the final path separator,
104
// separating it into a directory and file name component.
105
// If there is no separator in path, Split returns an empty dir and
106
// file set to path.
107
func Split(path string) (dir, file string) {
108
        i := strings.LastIndex(path, "/")
109
        return path[:i+1], path[i+1:]
110
}
111
 
112
// Join joins any number of path elements into a single path, adding a
113
// separating slash if necessary.  All empty strings are ignored.
114
func Join(elem ...string) string {
115
        for i, e := range elem {
116
                if e != "" {
117
                        return Clean(strings.Join(elem[i:], "/"))
118
                }
119
        }
120
        return ""
121
}
122
 
123
// Ext returns the file name extension used by path.
124
// The extension is the suffix beginning at the final dot
125
// in the final slash-separated element of path;
126
// it is empty if there is no dot.
127
func Ext(path string) string {
128
        for i := len(path) - 1; i >= 0 && path[i] != '/'; i-- {
129
                if path[i] == '.' {
130
                        return path[i:]
131
                }
132
        }
133
        return ""
134
}
135
 
136
// Base returns the last element of path.
137
// Trailing slashes are removed before extracting the last element.
138
// If the path is empty, Base returns ".".
139
// If the path consists entirely of slashes, Base returns "/".
140
func Base(path string) string {
141
        if path == "" {
142
                return "."
143
        }
144
        // Strip trailing slashes.
145
        for len(path) > 0 && path[len(path)-1] == '/' {
146
                path = path[0 : len(path)-1]
147
        }
148
        // Find the last element
149
        if i := strings.LastIndex(path, "/"); i >= 0 {
150
                path = path[i+1:]
151
        }
152
        // If empty now, it had only slashes.
153
        if path == "" {
154
                return "/"
155
        }
156
        return path
157
}
158
 
159
// IsAbs returns true if the path is absolute.
160
func IsAbs(path string) bool {
161
        return len(path) > 0 && path[0] == '/'
162
}
163
 
164
// Dir returns the all but the last element of path, typically the path's directory.
165
// Trailing path separators are removed before processing.
166
// If the path is empty, Dir returns ".".
167
// If the path consists entirely of separators, Dir returns a single separator.
168
// The returned path does not end in a separator unless it is the root directory.
169
func Dir(path string) string {
170
        dir, _ := Split(path)
171
        dir = Clean(dir)
172
        last := len(dir) - 1
173
        if last > 0 && dir[last] == '/' {
174
                dir = dir[:last]
175
        }
176
        if dir == "" {
177
                dir = "."
178
        }
179
        return dir
180
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.