diff options
Diffstat (limited to 'acf/path.lua')
-rw-r--r-- | acf/path.lua | 51 |
1 files changed, 31 insertions, 20 deletions
diff --git a/acf/path.lua b/acf/path.lua index b915012..3a5647d 100644 --- a/acf/path.lua +++ b/acf/path.lua @@ -8,32 +8,23 @@ module(..., package.seeall) local map = require('acf.util').map +local up = {} +wildcard = {} +local special = {['..']=up, ['*']=wildcard} + + function is_absolute(path) return string.sub(path, 1, 1) == '/' end -function to_absolute(path, base) - if not is_absolute(path) then - path = base..(base ~= '/' and '/' or '')..path - end - local comps = split(path) - local i = 1 - while i <= #comps do - if comps[i] == '..' then - if i == 1 then error('Invalid path: '..path) end - table.remove(comps, i - 1) - table.remove(comps, i - 1) - i = i - 1 - else i = i + 1 end - end - return '/'..table.concat(comps, '/') -end - -local function escape(comp) +function escape(comp) + for symbol, item in pairs(special) do + if comp == item then return symbol end + end if type(comp) == 'number' then return tostring(comp) end local res = string.gsub(comp, '([\\/])', '\\%1') - return tonumber(res) and '\\'..res or res + return (special[res] or tonumber(res)) and '\\'..res or res end @@ -52,7 +43,9 @@ function split(path) local escaped local function merge(s) - if s > '' then table.insert(res, not escaped and tonumber(s) or s) end + if s > '' then + table.insert(res, not escaped and (special[s] or tonumber(s)) or s) + end end while true do @@ -77,6 +70,24 @@ function split(path) end +function to_absolute(path, base) + if not is_absolute(path) then + path = base..(base ~= '/' and '/' or '')..path + end + local comps = split(path) + local i = 1 + while i <= #comps do + if comps[i] == up then + if i == 1 then error('Invalid path: '..path) end + table.remove(comps, i - 1) + table.remove(comps, i - 1) + i = i - 1 + else i = i + 1 end + end + return join('/', unpack(comps)) +end + + function parent(path) local comps = split(path) table.remove(comps) |