- TeCGraf
- ...
TeCGraf Computer Graphics Technology Group, PUC-Rio
TeCGraf is the result of a partnership between ... distributed among dozens of final products.... entry dofile db.lua entry entry Lua 5.2 function fwrite (fmt, ...) return io.write(string.format(fmt, unpack(arg))) end BEGIN html Programming in Lua 70 Copyright ® 2005, Translation Team, www.luachina.net function BEGIN() io.write([[
\n
\n') local href = '' if o.url then href = string.format(' HREF="%s"', o.url) end fwrite('%s\n', N, href, title) if o.title and o.org then fwrite('\n%s', o.org) end fwrite('\n
\n') if o.description then fwrite('%s', string.gsub(o.description, Programming in Lua 71 Copyright ® 2005, Translation Team, www.luachina.net '\n\n\n*', '\n')) fwrite('
\n') end if o.email then fwrite('Contact: %s\n', o.email, o.contact or o.email) elseif o.contact then fwrite('Contact: %s\n', o.contact) end end html END html function END() fwrite('\n') end entry entry BEGIN() N = 0 entry = entry0 fwrite('
- \n') dofile('db.lua') fwrite('
a title
%.4f 4 %02d 0%2d 0 lua C Lua C printf 2200..11 string string.findstring.gsub and string.gfind LuaPOSIX2regexp POSIX regexp 4000 LuaLua 500 POSIX LuaPOSIX 2 POSIXunixregexpunixPOSIXregexp Programming in Lua 142 Copyright ® 2005, Translation Team, www.luachina.net string.find subject string nil. 'hello'"hello" s = "hello world" i, j = string.find(s, "hello") print(i, j) --> 1 5 print(string.sub(s, i, j)) --> hello print(string.find(s, "world")) --> 7 11 i, j = string.find(s, "l") print(i, j) --> 3 3 print(string.find(s, "lll")) --> nil string.sub string.find string.find local t = {} -- table to store the indices local i = 0 while true do i = string.find(s, "\n", i+1) -- find 'next' newline if i == nil then break end table.insert(t, i) end string.gfind string.gsub s = string.gsub("Lua is cute", "cute", "great") print(s) --> Lua is great s = string.gsub("all lii", "l", "x") print(s) --> axx xii s = string.gsub("Lua is great", "perl", "tcl") print(s) --> Lua is great Programming in Lua 143 Copyright ® 2005, Translation Team, www.luachina.net s = string.gsub("all lii", "l", "x", 1) print(s) --> axl lii s = string.gsub("all lii", "l", "x", 2) print(s) --> axx lii string.gsub _, count = string.gsub(str, " ", " ") _ 2200..22 %d '%d%d/%d%d/%d%d%d%d' dd/mm/yyyy s = "Deadline is 30/05/1999, firm" date = "%d%d/%d%d/%d%d%d%d" print(string.sub(s, string.find(s, date))) --> 30/05/1999 Lua . %a %c %d %l %p %s %u %w %x %z 0 '%A' print(string.gsub("hello, up-down!", "%A", ".")) --> hello..up.down. 4 4 gsub gsub Lua Programming in Lua 144 Copyright ® 2005, Translation Team, www.luachina.net ( ) . % + - * ? [ ^ $ '%' '%.' '%%' '%' '%' Lua '%' '\' '\' Lua Lua char-set '[%w_]' '[01]' '[%[%]]' _, nvow = string.gsub(text, "[AEIOUaeiou]", "") char-set '%d' '[0-9]''%x' '[0-9a-fA-F]' '[0-7]' '[01234567]'(char-set) '^' '[^0-7]' '[^\n]' '%S' '[^%s]' Lua '[a-z]' '%l' 'ç' 'ã' Lua + 1 * 0 - 0 ? 0 1 '+' '%a+' print(string.gsub("one, and two; and three", "%a+", "word")) --> word, word word; word word '%d+' i, j = string.find("the number 1298 is even", "%d+") print(i,j) --> 12 15 '*' '+' 0 . Programming in Lua 145 Copyright ® 2005, Translation Team, www.luachina.net () '%(%s*%)' '%s*' 0 '%' '[_%a][_%w]*' Lua '-' '*' 0 '[_%a][_%w]-' '[_%w]-' C '/%*.*%*/' "/*" "*/" '.*' "/*" "*/" test = "int x; /* x */ int y; /* y */" print(string.gsub(test, "/%*.*%*/", "taskis to change that. function trim (s) return (string.gsub(s, "^%s*(.-)%s*$", "%1")) end '^' '$' '%s*' '.-' gsub string.gsub gsubstring.gsub gsub $varname varname function expand (s) s = string.gsub(s, "$(%w+)", function (n) Programming in Lua 148 Copyright ® 2005, Translation Team, www.luachina.net return _G[n] end) return s end name = "Lua"; status = "great" print(expand("$name is $status, isn't it?")) --> Lua is great, isn't it? string tostring function expand (s) return (string.gsub(s, "$(%w+)", function (n) return tostring(_G[n]) end)) end print(expand("print = $print; a = $a")) --> print = function: 0x8050ce0; a = nil loadstring $ s = "sin(3) = $[math.sin(3)]; 2^5 = $[2^5]" print((string.gsub(s, "$(%b[])", function (x) x = "return " .. string.sub(x, 2, -2) local f = loadstring(x) return f() end))) --> sin(3) = 0.1411200080598672; 2^5 = 32 "$[math.sin(3)]" "$[math.sin(3)]" string.sub "return math.sin(3)""$[2^5]" string.gsub words = {} string.gsub(s, "(%a+)", function (w) table.insert(words, w) Programming in Lua 149 Copyright ® 2005, Translation Team, www.luachina.net end) s "hello hi, again!" {"hello", "hi", "again"} string.gfind words = {} for w in string.gfind(s, "(%a)") do table.insert(words, w) end gfind for gfind words = {} for w in string.gfind(s, "%a") do table.insert(words, w) end URL URL HTTP URL '=''&''+' "%XX" XX 16 '+' "a+b = c" "a%2Bb+%3D+c" '=' name=value "&" name = "al"; query = "a+b = c"; q="yes or no" name=al&query=a%2Bb+%3D+c&q=yes+or+no URL function unescape (s) s = string.gsub(s, "+", " ") s = string.gsub(s, "%%(%x%x)", function (h) return string.char(tonumber(h, 16)) end) return s end '+' gsub '%' 16 16 tonumber 16 Programming in Lua 150 Copyright ® 2005, Translation Team, www.luachina.net print(unescape("a%2Bb+%3D+c")) --> a+b = c name=value gfind names values '&' '=' '[^&=]+' cgi = {} function decode (s) for n a m e , v a l u e in s t r i n g . g f i n d ( s , "([^&=]+)=([^&=]+)") do name = unescape(name) value = unescape(value) cgi[name] = value end end gfind name=value name=value name value unescape name value cgi escape '%' ASCII 16 0 '+' function escape (s) s = string.gsub(s, "([&=+%c])", function (c) return string.format("%%%02X", string.byte(c)) end) s = string.gsub(s, " ", "+") return s end function encode (t) local s = "" for k,v in pairs(t) do s = s .. "&" .. escape(k) .. "=" .. escape(v) end return string.sub(s, 2) -- remove first `&' end t = {name = "al", query = "a+b = c", q="yes or no"} print(encode(t)) --> q=yes+or+no&query=a%2Bb+%3D+c&name=al Programming in Lua 151 Copyright ® 2005, Translation Team, www.luachina.net 2200..44 TTrriicckkss ooff tthhee TTrraaddee string.gsub find quick-and-dirty C '/%*.-%*/' "/*" test = [[char s[] = "a /* here"; /* a tricky string */]] print(string.gsub(test, "/%*.-%*/", "