forked from coordcn/luaio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.lua
317 lines (262 loc) · 7.19 KB
/
path.lua
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
local strlib = require('strlib')
local split = strlib.split
local SLASH_CODE = strlib.CODE['/']
local BACKSLASH_CODE = strlib.CODE['\\']
local function normalize_array(parts)
local skip = 0
for i = #parts, 1, -1 do
local part = parts[i]
if part == '.' then
table.remove(parts, i)
elseif part == '..' then
table.remove(parts, i)
skip = skip + 1
elseif skip > 0 then
table.remove(parts, i)
skip = skip - 1
end
end
return skip
end
-- @example local dir, name, ext = string.match(str, reg)
-- dirname = dir
-- basename = name .. ext
-- extname = ext
local posix_split_reg = '^(.*)/([^/]*)(%.[^%./]*)(/*)$'
local posix = {}
posix.sep = '/'
posix.delimiter = ':'
function posix.dirname(path)
local dir, name, ext = path:match(posix_split_reg)
return dir or '.'
end
function posix.basename(path, extname)
local dir, name, ext = path:match(posix_split_reg)
return ext == extname and name or name .. ext
end
-- @TODO maybe rewrite by c
function posix.extname(path)
local dir, name, ext = path:match(posix_split_reg)
return ext
end
function posix.isAbsolute(path)
if not path or path == '' then return false end
return path:byte(1) == SLASH_CODE
end
function posix.parse(path)
return path:match(posix_split_reg)
end
-- @TODO maybe rewrite by c
function posix.normalize(path)
if not path or path == '' then return '.' end
local len = #path;
if len == 1 then return path end
local is_absolute = path:byte(1) == SLASH_CODE
local tailing_slash = path:byte(len) == SLASH_CODE
local parts = split(path, '/')
local skip = normalize_array(parts)
local filepath = table.concat(parts, '/')
if is_absolute then
if #filepath == 0 then return '/' end
filepath = '/' .. filepath
else
if skip == 0 then
if #filepath == 0 then filepath = '.' end
else
local temp = string.rep('..', skip, '/')
if #filepath == 0 then
filepath = temp
else
filepath = temp .. '/' .. filepath
end
end
end
if trailing_slash then
filepath = filepath .. '/'
end
return filepath
end
function posix.join(...)
local parts = {...}
local filepath = table.concat(parts, '/')
return posix.normalize(filepath)
end
function posix.resolve(...)
local parts = {...}
local len = #parts;
local is_absolute = false;
local resolved_path = ''
for i = len, 1, -1 do
local path = parts[i]
if path ~= '' then
resolved_path = path .. '/' .. resolved_path
if path:byte(1) == SLASH_CODE then
is_absolute = true
break
end
end
end
if not is_absolute then
return posix.join(process.cwd, resolved_path)
end
return posix.normalize(resolved_path)
end
-- @example local dir, name, ext = string.match(str, reg)
-- dirname = dir
-- basename = name .. ext
-- extname = ext
local windows_split_reg = '^(.*)[/\\]([^/\\]*)(%.[^%./\\]*)([/\\]*)$'
local drive_reg = '^[%a]:'
local UNC_reg = '^[\\/][\\/][^?\\/.]'
local UNC_long_reg = '^[\\/][\\/]?[\\/]UNC'
local drive_long_reg = '^[\\/][\\/]?[\\/][%a]:'
local windows = {}
windows.sep = '\\'
windows.delimiter = ';'
function windows.dirname(path)
local dir, name, ext = path:match(windows_split_reg)
return dir or '.'
end
function windows.basename(path, extname)
local dir, name, ext = path:match(windows_split_reg)
return ext == extname and name or name .. ext
end
function windows.extname(path)
local dir, name, ext = path:match(windows_split_reg)
return ext
end
local WINDOWS_ROOT_TYPE_NO_ROOT = 1
local WINDOWS_ROOT_TYPE_DRIVE = 2
local WINDOWS_ROOT_TYPE_DRIVE_RELATIVE = 3
local WINDOWS_ROOT_TYPE_UNC = 4
local WINDOWS_ROOT_TYPE_UNC_LONG = 5
local WINDOWS_ROOT_TYPE_DRIVE_LONG = 6
local function get_root(path)
if not path or path == '' then
return WINDOWS_ROOT_TYPE_NO_ROOT, nil
end
local len = #path
if len == 1 then
return WINDOWS_ROOT_TYPE_NO_ROOT, nil
end
local drive = path:match(drive_reg)
if drive then
if len == 2 then
return WINDOWS_ROOT_TYPE_DRIVE, drive
end
local next_char = path:byte(3)
if next_char == '\\' or next_char == '/' then
return WINDOWS_ROOT_TYPE_DRIVE, drive
else
return WINDOWS_ROOT_TYPE_DRIVE_RELATIVE, drive
end
end
if len == 2 then
return WINDOWS_ROOT_TYPE_NO_ROOT, nil
end
local UNC = path:match(UNC_reg)
if UNC then
return WINDOWS_ROOT_TYPE_UNC, UNC:gsub('/', '\\')
end
local UNC_long = path:match(UNC_long_reg)
if UNC_long then
return WINDOWS_ROOT_TYPE_UNC_LONG, UNC_long:gsub('/', '\\')
end
local drive_long = path:match(drive_long_reg)
if drive_long then
return WINDOWS_ROOT_TYPE_DRIVE_LONG, drive_long:gsub('/', '\\')
end
return WINDOWS_ROOT_TYPE_NO_ROOT, nil
end
function windows.isAbsolute(path)
local root_type, root = get_root(path)
if root_type == WINDOWS_ROOT_TYPE_NO_ROOT or root_type == WINDOWS_ROOT_TYPE_DRIVE_RELATIVE then
return false
else
return true
end
end
function windows.parse(path)
return path:match(windows_split_reg)
end
function windows.normalize(path)
if not path or path == '' then return '.' end
local root_type, root = get_root(path)
if root_type ~= WINDOWS_ROOT_TYPE_NO_ROOT then
path = path:sub(#root + 1)
end
path = path:gsub('\\', '/')
local tailing_slash = path:byte(#path) == SLASH_CODE
local parts = split(path, '/')
local skip = normalize_array(parts)
local filepath = table.concat(parts, '\\')
if root_type ~= WINDOWS_ROOT_TYPE_NO_ROOT then
if skip == 0 then
if #filepath == 0 then filepath = '.' end
else
local temp = string.rep('..', skip, '\\')
if #filepath == 0 then
filepath = temp
else
filepath = temp .. '\\' .. filepath
end
end
else
if root_type == WINDOWS_ROOT_TYPE_DRIVE_RELATIVE then
if #filepath == 0 then return root end
filepath = root .. filepath
else
if #filepath == 0 then return root .. '\\' end
filepath = root .. '\\' .. filepath
end
end
if trailing_slash then
filepath = filepath .. '\\'
end
return filepath
end
function windows.join(...)
local parts = {...}
local filepath = table.concat(parts, '/')
return windows.normalize(filepath)
end
function windows.resolve(...)
local parts = {...}
local len = #parts;
local is_absolute = false;
local resolved_path = ''
for i = len, 1, -1 do
local path = parts[i]
if path ~= '' then
resolved_path = path .. '/' .. resolved_path
local root_type, root = get_root(path)
if root_type ~= WINDOWS_ROOT_TYPE_NO_ROOT then
is_absolute = true
break
end
end
end
if not is_absolute then
return windows.join(process.cwd, resolved_path)
end
return windows.normalize(resolved_path)
end
function windows.makelong(path)
if not path then return '' end
local resolved_path = windows.resolve(path)
local root_type, root = get_root(resolved_path)
if root_type == WINDOWS_ROOT_TYPE_DRIVE or root_type == WINDOWS_ROOT_TYPE_UNC then
return resolved_path
else
return path
end
end
if system.type == 'Windows' then
windows.posix = posix
windows.windows = windows
return windows
else
posix.posix = posix
posix.windows = windows
return posix
end