]>
Commit | Line | Data |
---|---|---|
a098bb43 CP |
1 | function tablelen(tab) |
2 | local count = 0 | |
3 | ||
4 | for _, _ in pairs(tab) do | |
5 | count = count + 1 | |
6 | end | |
7 | ||
8 | return count | |
9 | end | |
10 | ||
11 | function tablesequal(a, b) | |
12 | local k, v | |
13 | ||
14 | for k, v in pairs(a) do | |
15 | if not b[k] or b[k] ~= v then | |
16 | return false | |
17 | end | |
18 | end | |
19 | ||
20 | for k, v in pairs(b) do | |
21 | if not a[k] or a[k] ~= v then | |
22 | return false | |
23 | end | |
24 | end | |
25 | ||
26 | return true | |
27 | end | |
28 | ||
29 | function trim(s) | |
30 | return (string.gsub(s, "^%s*(.-)%s*$", "%1")) | |
31 | end | |
32 | ||
33 | function explode(d,p,m) | |
34 | t={} | |
35 | ll=0 | |
36 | f=0 | |
37 | if m and m < 1 then | |
38 | return { p } | |
39 | end | |
40 | ||
41 | while true do | |
42 | l=string.find(p,d,ll+1,true) -- find the next d in the string | |
43 | if l~=nil then -- if "not not" found then.. | |
44 | table.insert(t, string.sub(p,ll,l-1)) -- Save it in our array. | |
45 | ll=l+1 -- save just after where we found it for searching next time. | |
46 | f = f + 1 | |
47 | if m and f >= m then | |
48 | table.insert(t, string.sub(p,ll)) | |
49 | break | |
50 | end | |
51 | else | |
52 | table.insert(t, string.sub(p,ll)) -- Save what's left in our array. | |
53 | break -- Break at end, as it should be, according to the lua manual. | |
54 | end | |
55 | end | |
56 | return t | |
57 | end | |
58 | ||
59 | function tableempty(table) | |
60 | for k, v in pairs(table) do | |
61 | return false | |
62 | end | |
63 | ||
64 | return true | |
65 | end | |
66 | ||
67 | function urlencode(str) | |
68 | if (str) then | |
69 | str = string.gsub (str, "\n", "\r\n") | |
70 | str = string.gsub (str, "([^%w ])", | |
71 | function (c) return string.format ("%%%02X", string.byte(c)) end) | |
72 | str = string.gsub (str, " ", "+") | |
73 | end | |
74 | return str | |
75 | end | |
76 | ||
77 | function matchtoregex(match) | |
78 | local x = match:gsub("([%^%$%(%)%%%.%[%]%+%-])", "%%%1") | |
79 | x = x:gsub("%*", ".*") | |
80 | x = x:gsub("%?", ".") | |
81 | x = "^" .. x .. "$" | |
82 | return x | |
83 | end | |
84 |