1print('testing pattern matching')
2
3function f(s, p)
4  local i,e = string.find(s, p)
5  if i then return string.sub(s, i, e) end
6end
7
8function f1(s, p)
9  p = string.gsub(p, "%%([0-9])", function (s) return "%" .. (s+1) end)
10  p = string.gsub(p, "^(^?)", "%1()", 1)
11  p = string.gsub(p, "($?)$", "()%1", 1)
12  local t = {string.match(s, p)}
13  return string.sub(s, t[1], t[#t] - 1)
14end
15
16a,b = string.find('', '')    -- empty patterns are tricky
17assert(a == 1 and b == 0);
18a,b = string.find('alo', '')
19assert(a == 1 and b == 0)
20a,b = string.find('a\0o a\0o a\0o', 'a', 1)   -- first position
21assert(a == 1 and b == 1)
22a,b = string.find('a\0o a\0o a\0o', 'a\0o', 2)   -- starts in the midle
23assert(a == 5 and b == 7)
24a,b = string.find('a\0o a\0o a\0o', 'a\0o', 9)   -- starts in the midle
25assert(a == 9 and b == 11)
26a,b = string.find('a\0a\0a\0a\0\0ab', '\0ab', 2);  -- finds at the end
27assert(a == 9 and b == 11);
28a,b = string.find('a\0a\0a\0a\0\0ab', 'b')    -- last position
29assert(a == 11 and b == 11)
30assert(string.find('a\0a\0a\0a\0\0ab', 'b\0') == nil)   -- check ending
31assert(string.find('', '\0') == nil)
32assert(string.find('alo123alo', '12') == 4)
33assert(string.find('alo123alo', '^12') == nil)
34
35assert(f('aloALO', '%l*') == 'alo')
36assert(f('aLo_ALO', '%a*') == 'aLo')
37
38assert(f('aaab', 'a*') == 'aaa');
39assert(f('aaa', '^.*$') == 'aaa');
40assert(f('aaa', 'b*') == '');
41assert(f('aaa', 'ab*a') == 'aa')
42assert(f('aba', 'ab*a') == 'aba')
43assert(f('aaab', 'a+') == 'aaa')
44assert(f('aaa', '^.+$') == 'aaa')
45assert(f('aaa', 'b+') == nil)
46assert(f('aaa', 'ab+a') == nil)
47assert(f('aba', 'ab+a') == 'aba')
48assert(f('a$a', '.$') == 'a')
49assert(f('a$a', '.%$') == 'a$')
50assert(f('a$a', '.$.') == 'a$a')
51assert(f('a$a', '$$') == nil)
52assert(f('a$b', 'a$') == nil)
53assert(f('a$a', '$') == '')
54assert(f('', 'b*') == '')
55assert(f('aaa', 'bb*') == nil)
56assert(f('aaab', 'a-') == '')
57assert(f('aaa', '^.-$') == 'aaa')
58assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab')
59assert(f('aabaaabaaabaaaba', 'b.-b') == 'baaab')
60assert(f('alo xo', '.o$') == 'xo')
61assert(f(' \n isto � assim', '%S%S*') == 'isto')
62assert(f(' \n isto � assim', '%S*$') == 'assim')
63assert(f(' \n isto � assim', '[a-z]*$') == 'assim')
64assert(f('um caracter ? extra', '[^%sa-z]') == '?')
65assert(f('', 'a?') == '')
66assert(f('�', '�?') == '�')
67assert(f('�bl', '�?b?l?') == '�bl')
68assert(f('  �bl', '�?b?l?') == '')
69assert(f('aa', '^aa?a?a') == 'aa')
70-- assert(f(']]]�b', '[^]]') == '�')
71assert(f(']]]�b', '[^%]]') == '�')
72assert(f("0alo alo", "%x*") == "0a")
73assert(f("alo alo", "%C+") == "alo alo")
74print('+')
75
76assert(f1('alo alx 123 b\0o b\0o', '(..*) %1') == "b\0o b\0o")
77assert(f1('axz123= 4= 4 34', '(.+)=(.*)=%2 %1') == '3= 4= 4 3')
78assert(f1('=======', '^(=*)=%1$') == '=======')
79assert(string.match('==========', '^([=]*)=%1$') == nil)
80
81local function range (i, j)
82  if i <= j then
83    return i, range(i+1, j)
84  end
85end
86
87local function range (i, j)
88  local ret = {}
89  for k=i, j do; table.insert(ret, k); end
90  return unpack(ret)
91end
92
93local abc = string.char(range(0, 255));
94
95assert(string.len(abc) == 256)
96
97function strset (p)
98  local res = {s=''}
99  string.gsub(abc, p, function (c) res.s = res.s .. c end)
100  return res.s
101end;
102
103assert(string.len(strset('[\200-\210]')) == 11)
104
105assert(strset('[a-z]') == "abcdefghijklmnopqrstuvwxyz")
106assert(strset('[a-z%d]') == strset('[%da-uu-z]'))
107-- assert(strset('[a-]') == "-a")
108assert(strset('[a%-]') == "-a")
109assert(strset('[^%W]') == strset('[%w]'))
110-- assert(strset('[]%%]') == '%]')
111assert(strset('[%]%%]') == '%]')
112assert(strset('[a%-z]') == '-az')
113assert(strset('[%^%[%-a%]%-b]') == '-[]^ab')
114assert(strset('%Z') == strset('[\1-\255]'))
115assert(strset('.') == strset('[\1-\255%z]'))
116print('+');
117
118assert(string.match("alo xyzK", "(%w+)K") == "xyz")
119assert(string.match("254 K", "(%d*)K") == "")
120assert(string.match("alo ", "(%w*)$") == "")
121assert(string.match("alo ", "(%w+)$") == nil)
122assert(string.find("(�lo)", "%(�") == 1)
123local a, b, c, d, e = string.match("�lo alo", "^(((.).).* (%w*))$")
124assert(a == '�lo alo' and b == '�l' and c == '�' and d == 'alo' and e == nil)
125a, b, c, d  = string.match('0123456789', '(.+(.?)())')
126assert(a == '0123456789' and b == '' and c == 11 and d == nil)
127print('+')
128
129assert(string.gsub('�lo �lo', '�', 'x') == 'xlo xlo')
130assert(string.gsub('alo �lo  ', ' +$', '') == 'alo �lo')  -- trim
131assert(string.gsub('  alo alo  ', '^%s*(.-)%s*$', '%1') == 'alo alo')  -- double trim
132assert(string.gsub('alo  alo  \n 123\n ', '%s+', ' ') == 'alo alo 123 ')
133t = "ab� d"
134a, b = string.gsub(t, '(.)', '%1@')
135assert('@'..a == string.gsub(t, '', '@') and b == 5)
136a, b = string.gsub('ab�d', '(.)', '%0@', 2)
137assert(a == 'a@b@�d' and b == 2)
138assert(string.gsub('alo alo', '()[al]', '%1') == '12o 56o')
139assert(string.gsub("abc=xyz", "(%w*)(%p)(%w+)", "%3%2%1-%0") ==
140              "xyz=abc-abc=xyz")
141assert(string.gsub("abc", "%w", "%1%0") == "aabbcc")
142assert(string.gsub("abc", "%w+", "%0%1") == "abcabc")
143assert(string.gsub('���', '$', '\0��') == '���\0��')
144assert(string.gsub('', '^', 'r') == 'r')
145assert(string.gsub('', '$', 'r') == 'r')
146print('+')
147
148assert(string.gsub("um (dois) tres (quatro)", "(%(%w+%))", string.upper) ==
149            "um (DOIS) tres (QUATRO)")
150
151do
152  local function setglobal (n,v) rawset(_G, n, v) end
153  string.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal)
154  assert(_G.a=="roberto" and _G.roberto=="a")
155end
156
157function f(a,b) return string.gsub(a,'.',b) end
158assert(string.gsub("trocar tudo em |teste|b| � |beleza|al|", "|([^|]*)|([^|]*)|", f) ==
159            "trocar tudo em bbbbb � alalalalalal")
160
161local function dostring (s) return loadstring(s)() or "" end
162assert(string.gsub("alo $a=1$ novamente $return a$", "$([^$]*)%$", dostring) ==
163            "alo  novamente 1")
164
165x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$",
166         "$([^$]*)%$", dostring)
167assert(x == ' assim vai para ALO')
168
169t = {}
170s = 'a alo jose  joao'
171r = string.gsub(s, '()(%w+)()', function (a,w,b)
172      assert(string.len(w) == b-a);
173      t[a] = b-a;
174    end)
175assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4)
176
177
178function isbalanced (s)
179  return string.find(string.gsub(s, "%b()", ""), "[()]") == nil
180end
181
182assert(isbalanced("(9 ((8))(\0) 7) \0\0 a b ()(c)() a"))
183assert(not isbalanced("(9 ((8) 7) a b (\0 c) a"))
184assert(string.gsub("alo 'oi' alo", "%b''", '"') == 'alo " alo')
185
186
187local t = {"apple", "orange", "lime"; n=0}
188assert(string.gsub("x and x and x", "x", function () t.n=t.n+1; return t[t.n] end)
189        == "apple and orange and lime")
190
191t = {n=0}
192string.gsub("first second word", "%w%w*", function (w) t.n=t.n+1; t[t.n] = w end)
193assert(t[1] == "first" and t[2] == "second" and t[3] == "word" and t.n == 3)
194
195t = {n=0}
196assert(string.gsub("first second word", "%w+",
197         function (w) t.n=t.n+1; t[t.n] = w end, 2) == "first second word")
198assert(t[1] == "first" and t[2] == "second" and t[3] == nil)
199
200assert(not pcall(string.gsub, "alo", "(.", print))
201assert(not pcall(string.gsub, "alo", ".)", print))
202assert(not pcall(string.gsub, "alo", "(.", {}))
203assert(not pcall(string.gsub, "alo", "(.)", "%2"))
204assert(not pcall(string.gsub, "alo", "(%1)", "a"))
205assert(not pcall(string.gsub, "alo", "(%0)", "a"))
206
207-- big strings
208local a = string.rep('a', 300000)
209assert(string.find(a, '^a*.?$'))
210assert(not string.find(a, '^a*.?b$'))
211assert(string.find(a, '^a-.?$'))
212
213-- deep nest of gsubs
214function rev (s)
215  return string.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end)
216end
217
218local x = string.rep('012345', 10)
219assert(rev(rev(x)) == x)
220
221
222-- gsub with tables
223assert(string.gsub("alo alo", ".", {}) == "alo alo")
224assert(string.gsub("alo alo", "(.)", {a="AA", l=""}) == "AAo AAo")
225assert(string.gsub("alo alo", "(.).", {a="AA", l="K"}) == "AAo AAo")
226assert(string.gsub("alo alo", "((.)(.?))", {al="AA", o=false}) == "AAo AAo")
227
228assert(string.gsub("alo alo", "().", {2,5,6}) == "256 alo")
229
230t = {}; setmetatable(t, {__index = function (t,s) return string.upper(s) end})
231assert(string.gsub("a alo b hi", "%w%w+", t) == "a ALO b HI")
232
233
234-- tests for gmatch
235assert(string.gfind == string.gmatch)
236local a = 0
237for i in string.gmatch('abcde', '()') do assert(i == a+1); a=i end
238assert(a==6)
239
240t = {n=0}
241for w in string.gmatch("first second word", "%w+") do
242      t.n=t.n+1; t[t.n] = w
243end
244assert(t[1] == "first" and t[2] == "second" and t[3] == "word")
245
246t = {3, 6, 9}
247for i in string.gmatch ("xuxx uu ppar r", "()(.)%2") do
248  assert(i == table.remove(t, 1))
249end
250assert(table.getn(t) == 0)
251
252t = {}
253for i,j in string.gmatch("13 14 10 = 11, 15= 16, 22=23", "(%d+)%s*=%s*(%d+)") do
254  t[i] = j
255end
256a = 0
257for k,v in pairs(t) do assert(k+1 == v+0); a=a+1 end
258assert(a == 3)
259
260
261-- tests for `%f' (`frontiers')
262
263-- assert(string.gsub("aaa aa a aaa a", "%f[%w]a", "x") == "xaa xa x xaa x")
264-- assert(string.gsub("[[]] [][] [[[[", "%f[[].", "x") == "x[]] x]x] x[[[")
265-- assert(string.gsub("01abc45de3", "%f[%d]", ".") == ".01abc.45de.3")
266-- assert(string.gsub("01abc45 de3x", "%f[%D]%w", ".") == "01.bc45 de3.")
267-- assert(string.gsub("function", "%f[\1-\255]%w", ".") == ".unction")
268-- assert(string.gsub("function", "%f[^\1-\255]", ".") == "function.")
269--
270-- local i, e = string.find(" alo aalo allo", "%f[%S].-%f[%s].-%f[%S]")
271-- assert(i == 2 and e == 5)
272-- local k = string.match(" alo aalo allo", "%f[%S](.-%f[%s].-%f[%S])")
273-- assert(k == 'alo ')
274--
275-- local a = {1, 5, 9, 14, 17,}
276-- for k in string.gmatch("alo alo th02 is 1hat", "()%f[%w%d]") do
277--   assert(table.remove(a, 1) == k)
278-- end
279-- assert(table.getn(a) == 0)
280
281
282print('OK')
283