diff options
| author | leaf corcoran <leafot@gmail.com> | 2019-09-21 18:43:47 -0700 |
|---|---|---|
| committer | leaf <leafot@gmail.com> | 2020-04-25 14:13:42 -0700 |
| commit | dcd5c253ce2839c10cc4e40ca98202fc29270762 (patch) | |
| tree | 27c59ed5b3a3171c828699aac53825b4da6f3a19 | |
| parent | 3cdf14a8ddd283e9738860359fbdc5ba4e618972 (diff) | |
| download | lua-cjson-dcd5c253ce2839c10cc4e40ca98202fc29270762.tar.gz lua-cjson-dcd5c253ce2839c10cc4e40ca98202fc29270762.tar.bz2 lua-cjson-dcd5c253ce2839c10cc4e40ca98202fc29270762.zip | |
Lua 5.2+ maxn support
| -rw-r--r-- | lua/cjson/util.lua | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/lua/cjson/util.lua b/lua/cjson/util.lua index 48fa33c..2405257 100644 --- a/lua/cjson/util.lua +++ b/lua/cjson/util.lua | |||
| @@ -2,6 +2,16 @@ local json = require "cjson" | |||
| 2 | 2 | ||
| 3 | local unpack = unpack or table.unpack | 3 | local unpack = unpack or table.unpack |
| 4 | 4 | ||
| 5 | local maxn = table.maxn or function(t) | ||
| 6 | local max = 0 | ||
| 7 | for k,v in pairs(t) do | ||
| 8 | if type(k) == "number" and k > max then | ||
| 9 | max = k | ||
| 10 | end | ||
| 11 | end | ||
| 12 | return max | ||
| 13 | end | ||
| 14 | |||
| 5 | -- Various common routines used by the Lua CJSON package | 15 | -- Various common routines used by the Lua CJSON package |
| 6 | -- | 16 | -- |
| 7 | -- Mark Pulford <mark@kyne.com.au> | 17 | -- Mark Pulford <mark@kyne.com.au> |
| @@ -194,7 +204,7 @@ local function run_test(testname, func, input, should_work, output) | |||
| 194 | local result = {} | 204 | local result = {} |
| 195 | local tmp = { pcall(func, unpack(input)) } | 205 | local tmp = { pcall(func, unpack(input)) } |
| 196 | local success = tmp[1] | 206 | local success = tmp[1] |
| 197 | for i = 2, table.maxn(tmp) do | 207 | for i = 2, maxn(tmp) do |
| 198 | result[i - 1] = tmp[i] | 208 | result[i - 1] = tmp[i] |
| 199 | end | 209 | end |
| 200 | 210 | ||
