Use the length operator (#) instead of table.getn.

table.getn was deprecated in Lua 5.1 in favor of #, the length operator.
See: http://www.lua.org/manual/5.1/manual.html#7.2
This commit is contained in:
Gerardo Marset 2013-02-25 20:28:28 -02:00
parent d548a78e55
commit 56893e9dcd
11 changed files with 19 additions and 19 deletions

View File

@ -11,7 +11,7 @@ dispatch.TIMEOUT = 10
-- make sure the user knows how to invoke us -- make sure the user knows how to invoke us
arg = arg or {} arg = arg or {}
if table.getn(arg) < 1 then if #arg < 1 then
print("Usage:\n luasocket check-links.lua [-n] {<url>}") print("Usage:\n luasocket check-links.lua [-n] {<url>}")
exit() exit()
end end

View File

@ -76,7 +76,7 @@ local function newset()
insert = function(set, value) insert = function(set, value)
if not reverse[value] then if not reverse[value] then
table.insert(set, value) table.insert(set, value)
reverse[value] = table.getn(set) reverse[value] = #set
end end
end, end,
remove = function(set, value) remove = function(set, value)

View File

@ -3,7 +3,7 @@ local dispatch = require("dispatch")
local handler = dispatch.newhandler() local handler = dispatch.newhandler()
-- make sure the user knows how to invoke us -- make sure the user knows how to invoke us
if table.getn(arg) < 1 then if #arg < 1 then
print("Usage") print("Usage")
print(" lua forward.lua <iport:ohost:oport> ...") print(" lua forward.lua <iport:ohost:oport> ...")
os.exit(1) os.exit(1)

View File

@ -135,7 +135,7 @@ end
-- main program -- main program
arg = arg or {} arg = arg or {}
if table.getn(arg) < 1 then if #arg < 1 then
io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n") io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n")
os.exit(1) os.exit(1)
else get(arg[1], arg[2]) end else get(arg[1], arg[2]) end

View File

@ -29,7 +29,7 @@ end
do do
local opt = {} local opt = {}
local pat = "[%s%c%p]*([%w]*)=([\"]?[%w%s_!@#$%%^&*()<>:;]+[\"]\?\.?)" local pat = "[%s%c%p]*([%w]*)=([\"]?[%w%s_!@#$%%^&*()<>:;]+[\"]\?\.?)"
for i = 2, table.getn(arg), 1 do for i = 2, #arg, 1 do
string.gsub(arg[i], pat, function(name, value) opt[name] = value end) string.gsub(arg[i], pat, function(name, value) opt[name] = value end)
end end
if not arg[2] then if not arg[2] then

View File

@ -31,7 +31,7 @@ function newset()
insert = function(set, value) insert = function(set, value)
if not reverse[value] then if not reverse[value] then
table.insert(set, value) table.insert(set, value)
reverse[value] = table.getn(set) reverse[value] = #set
end end
end, end,
remove = function(set, value) remove = function(set, value)

View File

@ -34,7 +34,7 @@ end
function Public.parse_headers(headers_s) function Public.parse_headers(headers_s)
local headers_t = Public.split_headers(headers_s) local headers_t = Public.split_headers(headers_s)
local headers = {} local headers = {}
for i = 1, table.getn(headers_t) do for i = 1, #headers_t do
local name, value = Public.parse_header(headers_t[i]) local name, value = Public.parse_header(headers_t[i])
if name then if name then
name = string.lower(name) name = string.lower(name)
@ -74,7 +74,7 @@ end
function Public.parse(mbox_s) function Public.parse(mbox_s)
local mbox = Public.split_mbox(mbox_s) local mbox = Public.split_mbox(mbox_s)
for i = 1, table.getn(mbox) do for i = 1, #mbox do
mbox[i] = Public.parse_message(mbox[i]) mbox[i] = Public.parse_message(mbox[i])
end end
return mbox return mbox

View File

@ -259,7 +259,7 @@ function parse_path(path)
path = path or "" path = path or ""
--path = string.gsub(path, "%s", "") --path = string.gsub(path, "%s", "")
string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end) string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end)
for i = 1, table.getn(parsed) do for i = 1, #parsed do
parsed[i] = unescape(parsed[i]) parsed[i] = unescape(parsed[i])
end end
if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end
@ -277,7 +277,7 @@ end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
function build_path(parsed, unsafe) function build_path(parsed, unsafe)
local path = "" local path = ""
local n = table.getn(parsed) local n = #parsed
if unsafe then if unsafe then
for i = 1, n-1 do for i = 1, n-1 do
path = path .. parsed[i] path = path .. parsed[i]

View File

@ -66,7 +66,7 @@ local check_request = function(request, expect, ignore)
local response = {} local response = {}
response.code, response.headers, response.status = response.code, response.headers, response.status =
socket.skip(1, http.request(request)) socket.skip(1, http.request(request))
if t and table.getn(t) > 0 then response.body = table.concat(t) end if t and #t > 0 then response.body = table.concat(t) end
check_result(response, expect, ignore) check_result(response, expect, ignore)
end end

View File

@ -20,7 +20,7 @@ dofile("testsupport.lua")
local total = function() local total = function()
local t = 0 local t = 0
for i = 1, table.getn(sent) do for i = 1, #sent do
t = t + sent[i].count t = t + sent[i].count
end end
return t return t
@ -83,7 +83,7 @@ end
local check = function(sent, m) local check = function(sent, m)
io.write("checking ", m.headers.title, ": ") io.write("checking ", m.headers.title, ": ")
for i = 1, table.getn(sent) do for i = 1, #sent do
local s = sent[i] local s = sent[i]
if s.title == m.headers.title and s.count > 0 then if s.title == m.headers.title and s.count > 0 then
check_headers(s.headers, m.headers) check_headers(s.headers, m.headers)
@ -98,7 +98,7 @@ end
local insert = function(sent, message) local insert = function(sent, message)
if type(message.rcpt) == "table" then if type(message.rcpt) == "table" then
message.count = table.getn(message.rcpt) message.count = #message.rcpt
else message.count = 1 end else message.count = 1 end
message.headers = message.headers or {} message.headers = message.headers or {}
message.headers.title = message.title message.headers.title = message.title
@ -115,7 +115,7 @@ local wait = function(sentinel, n)
io.write("waiting for ", n, " messages: ") io.write("waiting for ", n, " messages: ")
while 1 do while 1 do
local mbox = parse(get()) local mbox = parse(get())
if n == table.getn(mbox) then break end if n == #mbox then break end
if socket.time() - sentinel.time > 50 then if socket.time() - sentinel.time > 50 then
to = 1 to = 1
break break
@ -237,7 +237,7 @@ empty()
print("ok") print("ok")
io.write("sending messages: ") io.write("sending messages: ")
for i = 1, table.getn(sent) do for i = 1, #sent do
ret, err = socket.smtp.mail(sent[i]) ret, err = socket.smtp.mail(sent[i])
if not ret then fail(err) end if not ret then fail(err) end
io.write("+") io.write("+")
@ -249,9 +249,9 @@ wait(mark(), total())
io.write("parsing mailbox: ") io.write("parsing mailbox: ")
local mbox = parse(get()) local mbox = parse(get())
print(table.getn(mbox) .. " messages found!") print(#mbox .. " messages found!")
for i = 1, table.getn(mbox) do for i = 1, #mbox do
check(sent, mbox[i]) check(sent, mbox[i])
end end

View File

@ -34,7 +34,7 @@ end
local check_parse_path = function(path, expect) local check_parse_path = function(path, expect)
local parsed = socket.url.parse_path(path) local parsed = socket.url.parse_path(path)
for i = 1, math.max(table.getn(parsed), table.getn(expect)) do for i = 1, math.max(#parsed, #expect) do
if parsed[i] ~= expect[i] then if parsed[i] ~= expect[i] then
print(path) print(path)
os.exit() os.exit()