mirror of
https://github.com/skywind3000/z.lua
synced 2026-03-14 03:39:49 +00:00
passed all path_join tests
This commit is contained in:
50
z.lua
50
z.lua
@@ -576,6 +576,56 @@ function os.path.join(path1, path2)
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- split
|
||||
-----------------------------------------------------------------------
|
||||
function os.path.split(path)
|
||||
if path == '' then
|
||||
return '', ''
|
||||
end
|
||||
local pos = path:rfind('/')
|
||||
if os.path.sep == '\\' then
|
||||
local p2 = path:rfind('\\')
|
||||
if pos == nil and p2 ~= nil then
|
||||
pos = p2
|
||||
elseif p1 ~= nil and p2 ~= nil then
|
||||
pos = (pos < p2) and pos or p2
|
||||
end
|
||||
if path:match('^%a:[/\\]') and pos == nil then
|
||||
return path:sub(1, 2), path:sub(3)
|
||||
end
|
||||
end
|
||||
if pos == nil then
|
||||
if windows then
|
||||
local drive = path:match('^%a:') and path:sub(1, 2) or ''
|
||||
if drive ~= '' then
|
||||
return path:sub(1, 2), path:sub(3)
|
||||
end
|
||||
end
|
||||
return '', path
|
||||
elseif pos == 1 then
|
||||
return path:sub(1, 1), path:sub(2)
|
||||
elseif windows then
|
||||
local drive = path:match('^%a:') and path:sub(1, 2) or ''
|
||||
if pos == 3 then
|
||||
return path:sub(1, 3), path:sub(4)
|
||||
end
|
||||
end
|
||||
local head = path:sub(1, pos)
|
||||
local tail = path:sub(pos + 1)
|
||||
local test = string.rep('/', head:len())
|
||||
if head ~= test then
|
||||
head = head:gsub('/+$', '')
|
||||
elseif windows then
|
||||
test = string.rep('\\', head:len())
|
||||
if head ~= test then
|
||||
head = head:gsub('\\+$', '')
|
||||
end
|
||||
end
|
||||
return head, tail
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- check subdir
|
||||
-----------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user