From 588b2fc4687de80184a9dd7ff7272f3eda1dca4d Mon Sep 17 00:00:00 2001 From: skywind3000 Date: Sun, 3 Feb 2019 19:34:01 +0800 Subject: [PATCH] passed all path_join tests --- z.lua | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/z.lua b/z.lua index 0fc22c4..f967309 100755 --- a/z.lua +++ b/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 -----------------------------------------------------------------------