summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lualibs-lpeg.lua36
1 files changed, 27 insertions, 9 deletions
diff --git a/lualibs-lpeg.lua b/lualibs-lpeg.lua
index c203d80..79e75a7 100644
--- a/lualibs-lpeg.lua
+++ b/lualibs-lpeg.lua
@@ -897,17 +897,35 @@ end
function lpeg.utfchartabletopattern(list) -- goes to util-lpg
local tree = { }
local hash = { }
- for i=1,#list do
- local t = tree
- for c in gmatch(list[i],".") do
- local tc = t[c]
- if not tc then
- tc = { }
- t[c] = tc
+ local n = #list
+ if n == 0 then
+ -- we could always use this branch
+ for s in next, list do
+ local t = tree
+ for c in gmatch(s,".") do
+ local tc = t[c]
+ if not tc then
+ tc = { }
+ t[c] = tc
+ end
+ t = tc
+ end
+ hash[t] = s
+ end
+ else
+ for i=1,n do
+ local t = tree
+ local s = list[i]
+ for c in gmatch(s,".") do
+ local tc = t[c]
+ if not tc then
+ tc = { }
+ t[c] = tc
+ end
+ t = tc
end
- t = tc
+ hash[t] = s
end
- hash[t] = list[i]
end
return make(tree,hash)
end