lua
[Ported from github.com/lua/lua] Cast in 'rl_readline_name'
Brought by drakestapleton
| 1 | -- $Id: testes/pm.lua $ |
| 2 | -- See Copyright Notice in file lua.h |
| 3 | |
| 4 | -- UTF-8 file |
| 5 | |
| 6 | |
| 7 | print('testing pattern matching') |
| 8 | |
| 9 | global <const> * |
| 10 | |
| 11 | local function checkerror (msg, f, ...) |
| 12 | local s, err = pcall(f, ...) |
| 13 | assert(not s and string.find(err, msg)) |
| 14 | end |
| 15 | |
| 16 | |
| 17 | local function f (s, p) |
| 18 | local i,e = string.find(s, p) |
| 19 | if i then return string.sub(s, i, e) end |
| 20 | end |
| 21 | |
| 22 | local a,b = string.find('', '') -- empty patterns are tricky |
| 23 | assert(a == 1 and b == 0); |
| 24 | a,b = string.find('alo', '') |
| 25 | assert(a == 1 and b == 0) |
| 26 | a,b = string.find('a\0o a\0o a\0o', 'a', 1) -- first position |
| 27 | assert(a == 1 and b == 1) |
| 28 | a,b = string.find('a\0o a\0o a\0o', 'a\0o', 2) -- starts in the middle |
| 29 | assert(a == 5 and b == 7) |
| 30 | a,b = string.find('a\0o a\0o a\0o', 'a\0o', 9) -- starts in the middle |
| 31 | assert(a == 9 and b == 11) |
| 32 | a,b = string.find('a\0a\0a\0a\0\0ab', '\0ab', 2); -- finds at the end |
| 33 | assert(a == 9 and b == 11); |
| 34 | a,b = string.find('a\0a\0a\0a\0\0ab', 'b') -- last position |
| 35 | assert(a == 11 and b == 11) |
| 36 | assert(not string.find('a\0a\0a\0a\0\0ab', 'b\0')) -- check ending |
| 37 | assert(not string.find('', '\0')) |
| 38 | assert(string.find('alo123alo', '12') == 4) |
| 39 | assert(not string.find('alo123alo', '^12')) |
| 40 | |
| 41 | assert(string.match("aaab", ".*b") == "aaab") |
| 42 | assert(string.match("aaa", ".*a") == "aaa") |
| 43 | assert(string.match("b", ".*b") == "b") |
| 44 | |
| 45 | assert(string.match("aaab", ".+b") == "aaab") |
| 46 | assert(string.match("aaa", ".+a") == "aaa") |
| 47 | assert(not string.match("b", ".+b")) |
| 48 | |
| 49 | assert(string.match("aaab", ".?b") == "ab") |
| 50 | assert(string.match("aaa", ".?a") == "aa") |
| 51 | assert(string.match("b", ".?b") == "b") |
| 52 | |
| 53 | assert(f('aloALO', '%l*') == 'alo') |
| 54 | assert(f('aLo_ALO', '%a*') == 'aLo') |
| 55 | |
| 56 | assert(f(" \n\r*&\n\r xuxu \n\n", "%g%g%g+") == "xuxu") |
| 57 | |
| 58 | |
| 59 | -- Adapt a pattern to UTF-8 |
| 60 | local function PU (p) |
| 61 | -- distribute '?' into each individual byte of a character. |
| 62 | -- (For instance, "á?" becomes "\195?\161?".) |
| 63 | p = string.gsub(p, "(" .. utf8.charpattern .. ")%?", function (c) |
| 64 | return string.gsub(c, ".", "%0?") |
| 65 | end) |
| 66 | -- change '.' to utf-8 character patterns |
| 67 | p = string.gsub(p, "%.", utf8.charpattern) |
| 68 | return p |
| 69 | end |
| 70 | |
| 71 | |
| 72 | assert(f('aaab', 'a*') == 'aaa'); |
| 73 | assert(f('aaa', '^.*$') == 'aaa'); |
| 74 | assert(f('aaa', 'b*') == ''); |
| 75 | assert(f('aaa', 'ab*a') == 'aa') |
| 76 | assert(f('aba', 'ab*a') == 'aba') |
| 77 | assert(f('aaab', 'a+') == 'aaa') |
| 78 | assert(f('aaa', '^.+$') == 'aaa') |
| 79 | assert(not f('aaa', 'b+')) |
| 80 | assert(not f('aaa', 'ab+a')) |
| 81 | assert(f('aba', 'ab+a') == 'aba') |
| 82 | assert(f('a$a', '.$') == 'a') |
| 83 | assert(f('a$a', '.%$') == 'a$') |
| 84 | assert(f('a$a', '.$.') == 'a$a') |
| 85 | assert(not f('a$a', '$$')) |
| 86 | assert(not f('a$b', 'a$')) |
| 87 | assert(f('a$a', '$') == '') |
| 88 | assert(f('', 'b*') == '') |
| 89 | assert(not f('aaa', 'bb*')) |
| 90 | assert(f('aaab', 'a-') == '') |
| 91 | assert(f('aaa', '^.-$') == 'aaa') |
| 92 | assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab') |
| 93 | assert(f('aabaaabaaabaaaba', 'b.-b') == 'baaab') |
| 94 | assert(f('alo xo', '.o$') == 'xo') |
| 95 | assert(f(' \n isto é assim', '%S%S*') == 'isto') |
| 96 | assert(f(' \n isto é assim', '%S*$') == 'assim') |
| 97 | assert(f(' \n isto é assim', '[a-z]*$') == 'assim') |
| 98 | assert(f('um caracter ? extra', '[^%sa-z]') == '?') |
| 99 | assert(f('', 'a?') == '') |
| 100 | assert(f('á', PU'á?') == 'á') |
| 101 | assert(f('ábl', PU'á?b?l?') == 'ábl') |
| 102 | assert(f(' ábl', PU'á?b?l?') == '') |
| 103 | assert(f('aa', '^aa?a?a') == 'aa') |
| 104 | assert(f(']]]áb', '[^]]+') == 'áb') |
| 105 | assert(f("0alo alo", "%x*") == "0a") |
| 106 | assert(f("alo alo", "%C+") == "alo alo") |
| 107 | print('+') |
| 108 | |
| 109 | |
| 110 | local function f1 (s, p) |
| 111 | p = string.gsub(p, "%%([0-9])", function (s) |
| 112 | return "%" .. (tonumber(s)+1) |
| 113 | end) |
| 114 | p = string.gsub(p, "^(^?)", "%1()", 1) |
| 115 | p = string.gsub(p, "($?)$", "()%1", 1) |
| 116 | local t = {string.match(s, p)} |
| 117 | return string.sub(s, t[1], t[#t] - 1) |
| 118 | end |
| 119 | |
| 120 | assert(f1('alo alx 123 b\0o b\0o', '(..*) %1') == "b\0o b\0o") |
| 121 | assert(f1('axz123= 4= 4 34', '(.+)=(.*)=%2 %1') == '3= 4= 4 3') |
| 122 | assert(f1('=======', '^(=*)=%1$') == '=======') |
| 123 | assert(not string.match('==========', '^([=]*)=%1$')) |
| 124 | |
| 125 | local function range (i, j) |
| 126 | if i <= j then |
| 127 | return i, range(i+1, j) |
| 128 | end |
| 129 | end |
| 130 | |
| 131 | local abc = string.char(range(0, 127)) .. string.char(range(128, 255)); |
| 132 | |
| 133 | assert(string.len(abc) == 256) |
| 134 | |
| 135 | local function strset (p) |
| 136 | local res = {s=''} |
| 137 | string.gsub(abc, p, function (c) res.s = res.s .. c end) |
| 138 | return res.s |
| 139 | end; |
| 140 | |
| 141 | assert(string.len(strset('[\200-\210]')) == 11) |
| 142 | |
| 143 | assert(strset('[a-z]') == "abcdefghijklmnopqrstuvwxyz") |
| 144 | assert(strset('[a-z%d]') == strset('[%da-uu-z]')) |
| 145 | assert(strset('[a-]') == "-a") |
| 146 | assert(strset('[^%W]') == strset('[%w]')) |
| 147 | assert(strset('[]%%]') == '%]') |
| 148 | assert(strset('[a%-z]') == '-az') |
| 149 | assert(strset('[%^%[%-a%]%-b]') == '-[]^ab') |
| 150 | assert(strset('%Z') == strset('[\1-\255]')) |
| 151 | assert(strset('.') == strset('[\1-\255%z]')) |
| 152 | print('+'); |
| 153 | |
| 154 | assert(string.match("alo xyzK", "(%w+)K") == "xyz") |
| 155 | assert(string.match("254 K", "(%d*)K") == "") |
| 156 | assert(string.match("alo ", "(%w*)$") == "") |
| 157 | assert(not string.match("alo ", "(%w+)$")) |
| 158 | assert(string.find("(álo)", "%(á") == 1) |
| 159 | local a, b, c, d, e = string.match("âlo alo", PU"^(((.).). (%w*))$") |
| 160 | assert(a == 'âlo alo' and b == 'âl' and c == 'â' and d == 'alo' and e == nil) |
| 161 | a, b, c, d = string.match('0123456789', '(.+(.?)())') |
| 162 | assert(a == '0123456789' and b == '' and c == 11 and d == nil) |
| 163 | print('+') |
| 164 | |
| 165 | assert(string.gsub('ülo ülo', 'ü', 'x') == 'xlo xlo') |
| 166 | assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim |
| 167 | assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim |
| 168 | assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ') |
| 169 | local t = "abç d" |
| 170 | a, b = string.gsub(t, PU'(.)', '%1@') |
| 171 | assert(a == "a@b@ç@ @d@" and b == 5) |
| 172 | a, b = string.gsub('abçd', PU'(.)', '%0@', 2) |
| 173 | assert(a == 'a@b@çd' and b == 2) |
| 174 | assert(string.gsub('alo alo', '()[al]', '%1') == '12o 56o') |
| 175 | assert(string.gsub("abc=xyz", "(%w*)(%p)(%w+)", "%3%2%1-%0") == |
| 176 | "xyz=abc-abc=xyz") |
| 177 | assert(string.gsub("abc", "%w", "%1%0") == "aabbcc") |
| 178 | assert(string.gsub("abc", "%w+", "%0%1") == "abcabc") |
| 179 | assert(string.gsub('áéí', '$', '\0óú') == 'áéí\0óú') |
| 180 | assert(string.gsub('', '^', 'r') == 'r') |
| 181 | assert(string.gsub('', '$', 'r') == 'r') |
| 182 | print('+') |
| 183 | |
| 184 | |
| 185 | do -- new (5.3.3) semantics for empty matches |
| 186 | assert(string.gsub("a b cd", " *", "-") == "-a-b-c-d-") |
| 187 | |
| 188 | local res = "" |
| 189 | local sub = "a \nbc\t\td" |
| 190 | local i = 1 |
| 191 | for p, e in string.gmatch(sub, "()%s*()") do |
| 192 | res = res .. string.sub(sub, i, p - 1) .. "-" |
| 193 | i = e |
| 194 | end |
| 195 | assert(res == "-a-b-c-d-") |
| 196 | end |
| 197 | |
| 198 | |
| 199 | assert(string.gsub("um (dois) tres (quatro)", "(%(%w+%))", string.upper) == |
| 200 | "um (DOIS) tres (QUATRO)") |
| 201 | |
| 202 | do |
| 203 | local function setglobal (n,v) rawset(_G, n, v) end |
| 204 | string.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal) |
| 205 | assert(_G.a=="roberto" and _G.roberto=="a") |
| 206 | _G.a = nil; _G.roberto = nil |
| 207 | end |
| 208 | |
| 209 | function f(a,b) return string.gsub(a,'.',b) end |
| 210 | assert(string.gsub("trocar tudo em |teste|b| é |beleza|al|", "|([^|]*)|([^|]*)|", f) == |
| 211 | "trocar tudo em bbbbb é alalalalalal") |
| 212 | |
| 213 | local function dostring (s) return load(s, "")() or "" end |
| 214 | assert(string.gsub("alo $a='x'$ novamente $return a$", |
| 215 | "$([^$]*)%$", |
| 216 | dostring) == "alo novamente x") |
| 217 | |
| 218 | local x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$", |
| 219 | "$([^$]*)%$", dostring) |
| 220 | assert(x == ' assim vai para ALO') |
| 221 | _G.a, _G.x = nil |
| 222 | |
| 223 | local t = {} |
| 224 | local s = 'a alo jose joao' |
| 225 | local r = string.gsub(s, '()(%w+)()', function (a,w,b) |
| 226 | assert(string.len(w) == b-a); |
| 227 | t[a] = b-a; |
| 228 | end) |
| 229 | assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4) |
| 230 | |
| 231 | |
| 232 | local function isbalanced (s) |
| 233 | return not string.find(string.gsub(s, "%b()", ""), "[()]") |
| 234 | end |
| 235 | |
| 236 | assert(isbalanced("(9 ((8))(\0) 7) \0\0 a b ()(c)() a")) |
| 237 | assert(not isbalanced("(9 ((8) 7) a b (\0 c) a")) |
| 238 | assert(string.gsub("alo 'oi' alo", "%b''", '"') == 'alo " alo') |
| 239 | |
| 240 | |
| 241 | local t = {"apple", "orange", "lime"; n=0} |
| 242 | assert(string.gsub("x and x and x", "x", function () t.n=t.n+1; return t[t.n] end) |
| 243 | == "apple and orange and lime") |
| 244 | |
| 245 | t = {n=0} |
| 246 | string.gsub("first second word", "%w%w*", function (w) t.n=t.n+1; t[t.n] = w end) |
| 247 | assert(t[1] == "first" and t[2] == "second" and t[3] == "word" and t.n == 3) |
| 248 | |
| 249 | t = {n=0} |
| 250 | assert(string.gsub("first second word", "%w+", |
| 251 | function (w) t.n=t.n+1; t[t.n] = w end, 2) == "first second word") |
| 252 | assert(t[1] == "first" and t[2] == "second" and t[3] == undef) |
| 253 | |
| 254 | checkerror("invalid replacement value %(a table%)", |
| 255 | string.gsub, "alo", ".", {a = {}}) |
| 256 | checkerror("invalid capture index %%2", string.gsub, "alo", ".", "%2") |
| 257 | checkerror("invalid capture index %%0", string.gsub, "alo", "(%0)", "a") |
| 258 | checkerror("invalid capture index %%1", string.gsub, "alo", "(%1)", "a") |
| 259 | checkerror("invalid use of '%%'", string.gsub, "alo", ".", "%x") |
| 260 | |
| 261 | |
| 262 | if not _soft then |
| 263 | print("big strings") |
| 264 | local a = string.rep('a', 300000) |
| 265 | assert(string.find(a, '^a*.?$')) |
| 266 | assert(not string.find(a, '^a*.?b$')) |
| 267 | assert(string.find(a, '^a-.?$')) |
| 268 | |
| 269 | -- bug in 5.1.2 |
| 270 | a = string.rep('a', 10000) .. string.rep('b', 10000) |
| 271 | assert(not pcall(string.gsub, a, 'b')) |
| 272 | end |
| 273 | |
| 274 | -- recursive nest of gsubs |
| 275 | local function rev (s) |
| 276 | return string.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end) |
| 277 | end |
| 278 | |
| 279 | local x = "abcdef" |
| 280 | assert(rev(rev(x)) == x) |
| 281 | |
| 282 | |
| 283 | -- gsub with tables |
| 284 | assert(string.gsub("alo alo", ".", {}) == "alo alo") |
| 285 | assert(string.gsub("alo alo", "(.)", {a="AA", l=""}) == "AAo AAo") |
| 286 | assert(string.gsub("alo alo", "(.).", {a="AA", l="K"}) == "AAo AAo") |
| 287 | assert(string.gsub("alo alo", "((.)(.?))", {al="AA", o=false}) == "AAo AAo") |
| 288 | |
| 289 | assert(string.gsub("alo alo", "().", {'x','yy','zzz'}) == "xyyzzz alo") |
| 290 | |
| 291 | t = {}; setmetatable(t, {__index = function (t,s) return string.upper(s) end}) |
| 292 | assert(string.gsub("a alo b hi", "%w%w+", t) == "a ALO b HI") |
| 293 | |
| 294 | |
| 295 | -- tests for gmatch |
| 296 | local a = 0 |
| 297 | for i in string.gmatch('abcde', '()') do assert(i == a+1); a=i end |
| 298 | assert(a==6) |
| 299 | |
| 300 | t = {n=0} |
| 301 | for w in string.gmatch("first second word", "%w+") do |
| 302 | t.n=t.n+1; t[t.n] = w |
| 303 | end |
| 304 | assert(t[1] == "first" and t[2] == "second" and t[3] == "word") |
| 305 | |
| 306 | t = {3, 6, 9} |
| 307 | for i in string.gmatch ("xuxx uu ppar r", "()(.)%2") do |
| 308 | assert(i == table.remove(t, 1)) |
| 309 | end |
| 310 | assert(#t == 0) |
| 311 | |
| 312 | t = {} |
| 313 | for i,j in string.gmatch("13 14 10 = 11, 15= 16, 22=23", "(%d+)%s*=%s*(%d+)") do |
| 314 | t[tonumber(i)] = tonumber(j) |
| 315 | end |
| 316 | a = 0 |
| 317 | for k,v in pairs(t) do assert(k+1 == v+0); a=a+1 end |
| 318 | assert(a == 3) |
| 319 | |
| 320 | |
| 321 | do -- init parameter in gmatch |
| 322 | local s = 0 |
| 323 | for k in string.gmatch("10 20 30", "%d+", 3) do |
| 324 | s = s + tonumber(k) |
| 325 | end |
| 326 | assert(s == 50) |
| 327 | |
| 328 | s = 0 |
| 329 | for k in string.gmatch("11 21 31", "%d+", -4) do |
| 330 | s = s + tonumber(k) |
| 331 | end |
| 332 | assert(s == 32) |
| 333 | |
| 334 | -- there is an empty string at the end of the subject |
| 335 | s = 0 |
| 336 | for k in string.gmatch("11 21 31", "%w*", 9) do |
| 337 | s = s + 1 |
| 338 | end |
| 339 | assert(s == 1) |
| 340 | |
| 341 | -- there are no empty strings after the end of the subject |
| 342 | s = 0 |
| 343 | for k in string.gmatch("11 21 31", "%w*", 10) do |
| 344 | s = s + 1 |
| 345 | end |
| 346 | assert(s == 0) |
| 347 | end |
| 348 | |
| 349 | |
| 350 | do -- bug since 5.3 |
| 351 | local N = 20000 |
| 352 | local iter = string.gmatch(string.rep("a", N), string.rep("a?", N)) |
| 353 | pcall(iter) -- error for pattern too complex |
| 354 | -- calling function again found recursion count ('matchdepth') equal |
| 355 | -- to -1, so it did not detect next C-stack overflow |
| 356 | pcall(iter) |
| 357 | end |
| 358 | |
| 359 | |
| 360 | -- tests for `%f' (`frontiers') |
| 361 | |
| 362 | assert(string.gsub("aaa aa a aaa a", "%f[%w]a", "x") == "xaa xa x xaa x") |
| 363 | assert(string.gsub("[[]] [][] [[[[", "%f[[].", "x") == "x[]] x]x] x[[[") |
| 364 | assert(string.gsub("01abc45de3", "%f[%d]", ".") == ".01abc.45de.3") |
| 365 | assert(string.gsub("01abc45 de3x", "%f[%D]%w", ".") == "01.bc45 de3.") |
| 366 | assert(string.gsub("function", "%f[\1-\255]%w", ".") == ".unction") |
| 367 | assert(string.gsub("function", "%f[^\1-\255]", ".") == "function.") |
| 368 | |
| 369 | assert(string.find("a", "%f[a]") == 1) |
| 370 | assert(string.find("a", "%f[^%z]") == 1) |
| 371 | assert(string.find("a", "%f[^%l]") == 2) |
| 372 | assert(string.find("aba", "%f[a%z]") == 3) |
| 373 | assert(string.find("aba", "%f[%z]") == 4) |
| 374 | assert(not string.find("aba", "%f[%l%z]")) |
| 375 | assert(not string.find("aba", "%f[^%l%z]")) |
| 376 | |
| 377 | local i, e = string.find(" alo aalo allo", "%f[%S].-%f[%s].-%f[%S]") |
| 378 | assert(i == 2 and e == 5) |
| 379 | local k = string.match(" alo aalo allo", "%f[%S](.-%f[%s].-%f[%S])") |
| 380 | assert(k == 'alo ') |
| 381 | |
| 382 | local a = {1, 5, 9, 14, 17,} |
| 383 | for k in string.gmatch("alo alo th02 is 1hat", "()%f[%w%d]") do |
| 384 | assert(table.remove(a, 1) == k) |
| 385 | end |
| 386 | assert(#a == 0) |
| 387 | |
| 388 | |
| 389 | -- malformed patterns |
| 390 | local function malform (p, m) |
| 391 | m = m or "malformed" |
| 392 | local r, msg = pcall(string.find, "a", p) |
| 393 | assert(not r and string.find(msg, m)) |
| 394 | end |
| 395 | |
| 396 | malform("(.", "unfinished capture") |
| 397 | malform(".)", "invalid pattern capture") |
| 398 | malform("[a") |
| 399 | malform("[]") |
| 400 | malform("[^]") |
| 401 | malform("[a%]") |
| 402 | malform("[a%") |
| 403 | malform("%b") |
| 404 | malform("%ba") |
| 405 | malform("%") |
| 406 | malform("%f", "missing") |
| 407 | |
| 408 | -- \0 in patterns |
| 409 | assert(string.match("ab\0\1\2c", "[\0-\2]+") == "\0\1\2") |
| 410 | assert(string.match("ab\0\1\2c", "[\0-\0]+") == "\0") |
| 411 | assert(string.find("b$a", "$\0?") == 2) |
| 412 | assert(string.find("abc\0efg", "%\0") == 4) |
| 413 | assert(string.match("abc\0efg\0\1e\1g", "%b\0\1") == "\0efg\0\1e\1") |
| 414 | assert(string.match("abc\0\0\0", "%\0+") == "\0\0\0") |
| 415 | assert(string.match("abc\0\0\0", "%\0%\0?") == "\0\0") |
| 416 | |
| 417 | -- magic char after \0 |
| 418 | assert(string.find("abc\0\0","\0.") == 4) |
| 419 | assert(string.find("abcx\0\0abc\0abc","x\0\0abc\0a.") == 4) |
| 420 | |
| 421 | |
| 422 | do -- test reuse of original string in gsub |
| 423 | local s = string.rep("a", 100) |
| 424 | local r = string.gsub(s, "b", "c") -- no match |
| 425 | assert(string.format("%p", s) == string.format("%p", r)) |
| 426 | |
| 427 | r = string.gsub(s, ".", {x = "y"}) -- no substitutions |
| 428 | assert(string.format("%p", s) == string.format("%p", r)) |
| 429 | |
| 430 | local count = 0 |
| 431 | r = string.gsub(s, ".", function (x) |
| 432 | assert(x == "a") |
| 433 | count = count + 1 |
| 434 | return nil -- no substitution |
| 435 | end) |
| 436 | r = string.gsub(r, ".", {b = 'x'}) -- "a" is not a key; no subst. |
| 437 | assert(count == 100) |
| 438 | assert(string.format("%p", s) == string.format("%p", r)) |
| 439 | |
| 440 | count = 0 |
| 441 | r = string.gsub(s, ".", function (x) |
| 442 | assert(x == "a") |
| 443 | count = count + 1 |
| 444 | return x -- substitution... |
| 445 | end) |
| 446 | assert(count == 100) |
| 447 | -- no reuse in this case |
| 448 | assert(r == s and string.format("%p", s) ~= string.format("%p", r)) |
| 449 | end |
| 450 | |
| 451 | print('OK') |
| 452 |