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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#! /usr/bin/lua
-- bart at https://stackoverflow.com/questions/1426954/split-string-in-lua
function split(inputstr, sep)
sep=sep or '%s'
local t={}
for field,s in string.gmatch(inputstr, "([^"..sep.."]*)("..sep.."?)") do
table.insert(t,field)
if s=="" then
return t
end
end
end
function commonpath(p)
local pa = {}
local pl = {}
for i, sp in ipairs(p) do
local q = split(sp, "/")
table.insert(pl,#q)
table.insert(pa,q)
end
local out = {}
for cl = 1,math.min(table.unpack(pl)) do
local ex = false
local tx = pa[1][cl]
for i, pe in ipairs(pa) do
if pe[cl] ~= tx then
ex = true
break
end
end
if ex then
break
end
table.insert(out,tx)
end
return table.concat(out,"/")
end
if commonpath({
"/a/b/c/1/x.pl",
"/a/b/c/d/e/2/x.pl",
"/a/b/c/d/3/x.pl",
"/a/b/c/4/x.pl",
"/a/b/c/d/5/x.pl"
}) == "/a/b/c" then
io.write("Pass")
else
io.write("FAIL")
end
print("")
|