diff options
author | spag <spag@golwen.net> | 2013-01-16 10:31:01 +0100 |
---|---|---|
committer | spag <spag@golwen.net> | 2013-01-16 10:31:01 +0100 |
commit | 473e6c52eff0576829138b59b99f83917ff6b0e9 (patch) | |
tree | dcfb3f01c4f943bd77055a90b410ab287b8a7e6e /misc | |
parent | 3880e410353f1e80a938e110c04c2b3629ab5eb6 (diff) |
faster try function
Diffstat (limited to 'misc')
-rw-r--r-- | misc/freeswitch/scripts/common/str.lua | 22 |
1 files changed, 6 insertions, 16 deletions
diff --git a/misc/freeswitch/scripts/common/str.lua b/misc/freeswitch/scripts/common/str.lua index ca6dcd9..c366fda 100644 --- a/misc/freeswitch/scripts/common/str.lua +++ b/misc/freeswitch/scripts/common/str.lua @@ -5,23 +5,13 @@ module(...,package.seeall) function try(array, arguments) - local argument = arguments:match('^(.-)%.') or arguments; - local remaining_arguments = arguments:match('%.(.-)$'); - argument = tonumber(argument) or argument; + local result = array; - if argument and type(array) == 'table' then - if remaining_arguments then - if type(array[argument]) == 'table' then - return try(array[argument], remaining_arguments); - else - return nil; - end - else - return array[argument]; - end - end - - return nil; + arguments:gsub('([^%.]+)', function(entry) + local success, result = pcall(function() result = (result[tonumber(entry) or entry]); end); + end); + + return result; end -- to number |