aboutsummaryrefslogtreecommitdiff
path: root/challenge-112/stuart-little/lua/ch-1.lua
blob: 6e4d95111127ea3fcad7a5de76584c1a2b5d0b8d (plain)
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
#!/usr/bin/env lua

-- run <script> <path>

function deSlash(s)
   local news = s:gsub("/+","/"):gsub("(.+)/$","%1")
   return news
end

function deDot(s)
   local news = s:gsub("/%./","/"):gsub("/%.$","")
   return news
end

function deDblDotMid(s)
   local news=s:gsub("/%.%.$","/../")
   local fst = news:gmatch("([^/]*)/%.%./")()
   if not fst then return news end
   if fst == ".." or fst=="" then error("Too many double dots!") end
   local sml = news:gsub(fst.."/%.%./","/",1)
   return deDblDotMid(deSlash(sml))
end

function norm(s)
   return deSlash(deDblDotMid(deDot(s)))
end

print(norm(arg[1]))