Make several Lua helper functions handle nil
The Lua helper functions clamp(), has_prefix(), has_suffix(), trim(), and split_string() now all return nil if they are called with nil as (first) argument. This makes them more robust, for instance when called on non-existent tags and so simplifies user code.appveyor-switch-sourceses
parent
2a826c3aae
commit
ca7c5faed4
22
src/init.lua
22
src/init.lua
|
@ -14,10 +14,16 @@ local _define_table_impl = function(_type, _name, _columns, _options)
|
|||
end
|
||||
|
||||
function osm2pgsql.has_prefix(str, prefix)
|
||||
if str == nil then
|
||||
return nil
|
||||
end
|
||||
return str:sub(1, prefix:len()) == prefix
|
||||
end
|
||||
|
||||
function osm2pgsql.has_suffix(str, suffix)
|
||||
if str == nil then
|
||||
return nil
|
||||
end
|
||||
if suffix == '' then
|
||||
return true
|
||||
end
|
||||
|
@ -51,6 +57,9 @@ function osm2pgsql.way_member_ids(relation)
|
|||
end
|
||||
|
||||
function osm2pgsql.clamp(value, low, high)
|
||||
if value == nil then
|
||||
return nil
|
||||
end
|
||||
return math.min(math.max(value, low), high)
|
||||
end
|
||||
|
||||
|
@ -118,6 +127,9 @@ end
|
|||
|
||||
-- from http://lua-users.org/wiki/StringTrim
|
||||
function osm2pgsql.trim(str)
|
||||
if str == nil then
|
||||
return nil
|
||||
end
|
||||
local from = str:match("^%s*()")
|
||||
return from > #str and "" or str:match(".*%S", from)
|
||||
end
|
||||
|
@ -142,11 +154,15 @@ function osm2pgsql.split_unit(str, default_unit)
|
|||
end
|
||||
|
||||
function osm2pgsql.split_string(str, separator)
|
||||
local pattern = '([^' .. (separator or ';') .. ']+)'
|
||||
local result = {}
|
||||
for w in string.gmatch(str, pattern) do
|
||||
result[#result + 1] = osm2pgsql.trim(w)
|
||||
|
||||
if str ~= nil then
|
||||
local pattern = '([^' .. (separator or ';') .. ']+)'
|
||||
for w in string.gmatch(str, pattern) do
|
||||
result[#result + 1] = osm2pgsql.trim(w)
|
||||
end
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ assert(o.has_prefix('addr:city', ''))
|
|||
assert(not o.has_prefix('name', 'addr:'))
|
||||
assert(o.has_prefix('a', 'a'))
|
||||
assert(not o.has_prefix('a', 'ab'))
|
||||
assert(o.has_prefix(nil, 'a') == nil)
|
||||
|
||||
-- has_suffix()
|
||||
|
||||
|
@ -29,6 +30,7 @@ assert(o.has_suffix('tiger:source', ''))
|
|||
assert(not o.has_suffix('name', ':source'))
|
||||
assert(o.has_suffix('a', 'a'))
|
||||
assert(not o.has_suffix('a', 'ba'))
|
||||
assert(o.has_suffix(nil, 'a') == nil)
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
|
||||
|
@ -39,6 +41,7 @@ do
|
|||
assert(o.clamp(3, -1, 1) == 1)
|
||||
assert(o.clamp(-3, -1, 1) == -1)
|
||||
assert(o.clamp(2.718, 0, 3.141) == 2.718)
|
||||
assert(o.clamp(nil, -1, 1) == nil)
|
||||
end
|
||||
|
||||
-- make_check_values_func without default
|
||||
|
@ -109,6 +112,7 @@ assert(osm2pgsql.trim(' a ') == 'a')
|
|||
assert(osm2pgsql.trim(' a ') == 'a')
|
||||
assert(osm2pgsql.trim(' ab cd ') == 'ab cd')
|
||||
assert(osm2pgsql.trim(' \t\r\n\f\va\000b \r\t\n\f\v') == 'a\000b')
|
||||
assert(osm2pgsql.trim(nil) == nil)
|
||||
|
||||
-- split_unit
|
||||
|
||||
|
@ -189,6 +193,9 @@ assert(r[1] == 'ab c;d')
|
|||
assert(r[2] == 'e f')
|
||||
assert(r[3] == 'ghi')
|
||||
|
||||
r = o.split_string(nil)
|
||||
assert(#r == 0)
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
|
||||
print("All tests successful")
|
||||
|
|
Loading…
Reference in New Issue