-
-
Notifications
You must be signed in to change notification settings - Fork 647
/
Path.hx
333 lines (281 loc) · 8.45 KB
/
Path.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.io;
/**
This class provides a convenient way of working with paths. It supports the
common path formats:
- `directory1/directory2/filename.extension`
- `directory1\directory2\filename.extension`
**/
class Path {
/**
The directory.
This is the leading part of the path that is not part of the file name
and the extension.
Does not end with a `/` or `\` separator.
If the path has no directory, the value is `null`.
**/
public var dir:Null<String>;
/**
The file name.
This is the part of the part between the directory and the extension.
If there is no file name, e.g. for `".htaccess"` or `"/dir/"`, the value
is the empty String `""`.
**/
public var file:String;
/**
The file extension.
It is separated from the file name by a dot. This dot is not part of
the extension.
If the path has no extension, the value is `null`.
**/
public var ext:Null<String>;
/**
`true` if the last directory separator is a backslash, `false` otherwise.
**/
public var backslash:Bool;
/**
Creates a new `Path` instance by parsing `path`.
Path information can be retrieved by accessing the `dir`, `file` and `ext`
properties.
**/
public function new(path:String) {
switch (path) {
case "." | "..":
dir = path;
file = "";
return;
}
var c1 = path.lastIndexOf("/");
var c2 = path.lastIndexOf("\\");
if (c1 < c2) {
dir = path.substr(0, c2);
path = path.substr(c2 + 1);
backslash = true;
} else if (c2 < c1) {
dir = path.substr(0, c1);
path = path.substr(c1 + 1);
} else
dir = null;
var cp = path.lastIndexOf(".");
if (cp != -1) {
ext = path.substr(cp + 1);
file = path.substr(0, cp);
} else {
ext = null;
file = path;
}
}
/**
Returns a String representation of `this` path.
If `this.backslash` is `true`, backslash is used as directory separator,
otherwise slash is used. This only affects the separator between
`this.dir` and `this.file`.
If `this.directory` or `this.extension` is `null`, their representation
is the empty String `""`.
**/
public function toString():String {
return (if (dir == null) "" else dir + if (backslash) "\\" else "/") + file + (if (ext == null) "" else "." + ext);
}
/**
Returns the String representation of `path` without the file extension.
If `path` is `null`, the result is unspecified.
**/
public static function withoutExtension(path:String):String {
var s = new Path(path);
s.ext = null;
return s.toString();
}
/**
Returns the String representation of `path` without the directory.
If `path` is `null`, the result is unspecified.
**/
public static function withoutDirectory(path):String {
var s = new Path(path);
s.dir = null;
return s.toString();
}
/**
Returns the directory of `path`.
If the directory is `null`, the empty String `""` is returned.
If `path` is `null`, the result is unspecified.
**/
public static function directory(path):String {
var s = new Path(path);
if (s.dir == null)
return "";
return s.dir;
}
/**
Returns the extension of `path`.
If `path` has no extension, the empty String `""` is returned.
If `path` is `null`, the result is unspecified.
**/
public static function extension(path):String {
var s = new Path(path);
if (s.ext == null)
return "";
return s.ext;
}
/**
Returns a String representation of `path` where the extension is `ext`.
If `path` has no extension, `ext` is added as extension.
If `path` or `ext` are `null`, the result is unspecified.
**/
public static function withExtension(path, ext):String {
var s = new Path(path);
s.ext = ext;
return s.toString();
}
/**
Joins all paths in `paths` together.
If `paths` is empty, the empty String `""` is returned. Otherwise the
paths are joined with a slash between them.
If `paths` is `null`, the result is unspecified.
**/
public static function join(paths:Array<String>):String {
var paths = paths.filter(function(s) return s != null && s != "");
if (paths.length == 0) {
return "";
}
var path = paths[0];
for (i in 1...paths.length) {
path = addTrailingSlash(path);
path += paths[i];
}
return normalize(path);
}
/**
Normalize a given `path` (e.g. turn `'/usr/local/../lib'` into `'/usr/lib'`).
Also replaces backslashes `\` with slashes `/` and afterwards turns
multiple slashes into a single one.
If `path` is `null`, the result is unspecified.
**/
public static function normalize(path:String):String {
var slash = "/";
path = path.split("\\").join(slash);
if (path == slash)
return slash;
var target = [];
for (token in path.split(slash)) {
if (token == '..' && target.length > 0 && target[target.length - 1] != "..") {
target.pop();
} else if (token == '') {
if (target.length > 0 || path.charCodeAt(0) == '/'.code) {
target.push(token);
}
} else if (token != '.') {
target.push(token);
}
}
var tmp = target.join(slash);
var acc = new StringBuf();
var colon = false;
var slashes = false;
#if utf16
for (c in haxe.iterators.StringIteratorUnicode.unicodeIterator(tmp)) {
switch (c) {
#else
for (i in 0...tmp.length) {
switch (StringTools.fastCodeAt(tmp, i)) {
#end
case ":".code:
acc.add(":");
colon = true;
case "/".code if (!colon):
slashes = true;
case var i:
colon = false;
if (slashes) {
acc.add("/");
slashes = false;
}
acc.addChar(i);
}
}
return acc.toString();
}
/**
Adds a trailing slash to `path`, if it does not have one already.
If the last slash in `path` is a backslash, a backslash is appended to
`path`.
If the last slash in `path` is a slash, or if no slash is found, a slash
is appended to `path`. In particular, this applies to the empty String
`""`.
If `path` is `null`, the result is unspecified.
**/
public static function addTrailingSlash(path:String):String {
if (path.length == 0)
return "/";
var c1 = path.lastIndexOf("/");
var c2 = path.lastIndexOf("\\");
return if (c1 < c2) {
if (c2 != path.length - 1)
path + "\\";
else
path;
} else {
if (c1 != path.length - 1)
path + "/";
else
path;
}
}
/**
Removes trailing slashes from `path`.
If `path` does not end with a `/` or `\`, `path` is returned unchanged.
Otherwise the substring of `path` excluding the trailing slashes or
backslashes is returned.
If `path` is `null`, the result is unspecified.
**/
public static function removeTrailingSlashes(path:String):String {
while (true) {
switch (path.charCodeAt(path.length - 1)) {
case '/'.code | '\\'.code:
path = path.substr(0, -1);
case _:
break;
}
}
return path;
}
/**
Returns `true` if the path is an absolute path, and `false` otherwise.
**/
public static function isAbsolute(path:String):Bool {
if (StringTools.startsWith(path, '/'))
return true;
if (path.charAt(1) == ':')
return true;
if (StringTools.startsWith(path, '\\\\'))
return true;
return false;
}
private static function unescape(path:String):String {
var regex = ~/-x([0-9][0-9])/g;
return regex.map(path, function(regex) return String.fromCharCode(Std.parseInt(regex.matched(1))));
}
private static function escape(path:String, allowSlashes:Bool = false):String {
var regex = allowSlashes ? ~/[^A-Za-z0-9_\/\\\.]/g : ~/[^A-Za-z0-9_\.]/g;
return regex.map(path, function(v) return '-x' + v.matched(0).charCodeAt(0));
}
}