diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-12-28 18:38:20 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-12-28 18:38:20 -0300 |
commit | b5ab31a475ccf5f96c2ffb34fccc1d6592913794 (patch) | |
tree | ef6d2b1844ca672dc81ab4411e37abbef2b7c48f /testes/pm.lua | |
parent | 140cdcced5e28bde7a89e88fcae428f318565f1d (diff) | |
parent | 314745ed8438d1276c6c928d5f9d4be018dfadb6 (diff) | |
download | lua-b5ab31a475ccf5f96c2ffb34fccc1d6592913794.tar.gz lua-b5ab31a475ccf5f96c2ffb34fccc1d6592913794.tar.bz2 lua-b5ab31a475ccf5f96c2ffb34fccc1d6592913794.zip |
Merge branch 'master' into nextversion
Diffstat (limited to 'testes/pm.lua')
-rw-r--r-- | testes/pm.lua | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/testes/pm.lua b/testes/pm.lua index 94bb63ca..795596d4 100644 --- a/testes/pm.lua +++ b/testes/pm.lua | |||
@@ -9,12 +9,12 @@ local function checkerror (msg, f, ...) | |||
9 | end | 9 | end |
10 | 10 | ||
11 | 11 | ||
12 | function f(s, p) | 12 | local function f (s, p) |
13 | local i,e = string.find(s, p) | 13 | local i,e = string.find(s, p) |
14 | if i then return string.sub(s, i, e) end | 14 | if i then return string.sub(s, i, e) end |
15 | end | 15 | end |
16 | 16 | ||
17 | a,b = string.find('', '') -- empty patterns are tricky | 17 | local a,b = string.find('', '') -- empty patterns are tricky |
18 | assert(a == 1 and b == 0); | 18 | assert(a == 1 and b == 0); |
19 | a,b = string.find('alo', '') | 19 | a,b = string.find('alo', '') |
20 | assert(a == 1 and b == 0) | 20 | assert(a == 1 and b == 0) |
@@ -88,7 +88,7 @@ assert(f("alo alo", "%C+") == "alo alo") | |||
88 | print('+') | 88 | print('+') |
89 | 89 | ||
90 | 90 | ||
91 | function f1(s, p) | 91 | local function f1 (s, p) |
92 | p = string.gsub(p, "%%([0-9])", function (s) | 92 | p = string.gsub(p, "%%([0-9])", function (s) |
93 | return "%" .. (tonumber(s)+1) | 93 | return "%" .. (tonumber(s)+1) |
94 | end) | 94 | end) |
@@ -113,7 +113,7 @@ local abc = string.char(range(0, 127)) .. string.char(range(128, 255)); | |||
113 | 113 | ||
114 | assert(string.len(abc) == 256) | 114 | assert(string.len(abc) == 256) |
115 | 115 | ||
116 | function strset (p) | 116 | local function strset (p) |
117 | local res = {s=''} | 117 | local res = {s=''} |
118 | string.gsub(abc, p, function (c) res.s = res.s .. c end) | 118 | string.gsub(abc, p, function (c) res.s = res.s .. c end) |
119 | return res.s | 119 | return res.s |
@@ -147,7 +147,7 @@ assert(string.gsub('ülo ülo', 'ü', 'x') == 'xlo xlo') | |||
147 | assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim | 147 | assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim |
148 | assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim | 148 | assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim |
149 | assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ') | 149 | assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ') |
150 | t = "abç d" | 150 | local t = "abç d" |
151 | a, b = string.gsub(t, '(.)', '%1@') | 151 | a, b = string.gsub(t, '(.)', '%1@') |
152 | assert('@'..a == string.gsub(t, '', '@') and b == 5) | 152 | assert('@'..a == string.gsub(t, '', '@') and b == 5) |
153 | a, b = string.gsub('abçd', '(.)', '%0@', 2) | 153 | a, b = string.gsub('abçd', '(.)', '%0@', 2) |
@@ -184,6 +184,7 @@ do | |||
184 | local function setglobal (n,v) rawset(_G, n, v) end | 184 | local function setglobal (n,v) rawset(_G, n, v) end |
185 | string.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal) | 185 | string.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal) |
186 | assert(_G.a=="roberto" and _G.roberto=="a") | 186 | assert(_G.a=="roberto" and _G.roberto=="a") |
187 | _G.a = nil; _G.roberto = nil | ||
187 | end | 188 | end |
188 | 189 | ||
189 | function f(a,b) return string.gsub(a,'.',b) end | 190 | function f(a,b) return string.gsub(a,'.',b) end |
@@ -195,20 +196,21 @@ assert(string.gsub("alo $a='x'$ novamente $return a$", | |||
195 | "$([^$]*)%$", | 196 | "$([^$]*)%$", |
196 | dostring) == "alo novamente x") | 197 | dostring) == "alo novamente x") |
197 | 198 | ||
198 | x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$", | 199 | local x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$", |
199 | "$([^$]*)%$", dostring) | 200 | "$([^$]*)%$", dostring) |
200 | assert(x == ' assim vai para ALO') | 201 | assert(x == ' assim vai para ALO') |
201 | 202 | _G.a, _G.x = nil | |
202 | t = {} | 203 | |
203 | s = 'a alo jose joao' | 204 | local t = {} |
204 | r = string.gsub(s, '()(%w+)()', function (a,w,b) | 205 | local s = 'a alo jose joao' |
205 | assert(string.len(w) == b-a); | 206 | local r = string.gsub(s, '()(%w+)()', function (a,w,b) |
206 | t[a] = b-a; | 207 | assert(string.len(w) == b-a); |
207 | end) | 208 | t[a] = b-a; |
209 | end) | ||
208 | assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4) | 210 | assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4) |
209 | 211 | ||
210 | 212 | ||
211 | function isbalanced (s) | 213 | local function isbalanced (s) |
212 | return not string.find(string.gsub(s, "%b()", ""), "[()]") | 214 | return not string.find(string.gsub(s, "%b()", ""), "[()]") |
213 | end | 215 | end |
214 | 216 | ||
@@ -251,7 +253,7 @@ if not _soft then | |||
251 | end | 253 | end |
252 | 254 | ||
253 | -- recursive nest of gsubs | 255 | -- recursive nest of gsubs |
254 | function rev (s) | 256 | local function rev (s) |
255 | return string.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end) | 257 | return string.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end) |
256 | end | 258 | end |
257 | 259 | ||