pm.lua 9.3 KB

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