1
0
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:
skywind3000
2019-02-03 19:34:01 +08:00
parent 99529516fb
commit 588b2fc468

50
z.lua
View File

@@ -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
-----------------------------------------------------------------------