Skip to content

Commit ca7c5fa

Browse files
committed
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.
1 parent 2a826c3 commit ca7c5fa

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/init.lua

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ local _define_table_impl = function(_type, _name, _columns, _options)
1414
end
1515

1616
function osm2pgsql.has_prefix(str, prefix)
17+
if str == nil then
18+
return nil
19+
end
1720
return str:sub(1, prefix:len()) == prefix
1821
end
1922

2023
function osm2pgsql.has_suffix(str, suffix)
24+
if str == nil then
25+
return nil
26+
end
2127
if suffix == '' then
2228
return true
2329
end
@@ -51,6 +57,9 @@ function osm2pgsql.way_member_ids(relation)
5157
end
5258

5359
function osm2pgsql.clamp(value, low, high)
60+
if value == nil then
61+
return nil
62+
end
5463
return math.min(math.max(value, low), high)
5564
end
5665

@@ -118,6 +127,9 @@ end
118127

119128
-- from http://lua-users.org/wiki/StringTrim
120129
function osm2pgsql.trim(str)
130+
if str == nil then
131+
return nil
132+
end
121133
local from = str:match("^%s*()")
122134
return from > #str and "" or str:match(".*%S", from)
123135
end
@@ -142,11 +154,15 @@ function osm2pgsql.split_unit(str, default_unit)
142154
end
143155

144156
function osm2pgsql.split_string(str, separator)
145-
local pattern = '([^' .. (separator or ';') .. ']+)'
146157
local result = {}
147-
for w in string.gmatch(str, pattern) do
148-
result[#result + 1] = osm2pgsql.trim(w)
158+
159+
if str ~= nil then
160+
local pattern = '([^' .. (separator or ';') .. ']+)'
161+
for w in string.gmatch(str, pattern) do
162+
result[#result + 1] = osm2pgsql.trim(w)
163+
end
149164
end
165+
150166
return result
151167
end
152168

tests/lua/tests.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ assert(o.has_prefix('addr:city', ''))
2020
assert(not o.has_prefix('name', 'addr:'))
2121
assert(o.has_prefix('a', 'a'))
2222
assert(not o.has_prefix('a', 'ab'))
23+
assert(o.has_prefix(nil, 'a') == nil)
2324

2425
-- has_suffix()
2526

@@ -29,6 +30,7 @@ assert(o.has_suffix('tiger:source', ''))
2930
assert(not o.has_suffix('name', ':source'))
3031
assert(o.has_suffix('a', 'a'))
3132
assert(not o.has_suffix('a', 'ba'))
33+
assert(o.has_suffix(nil, 'a') == nil)
3234

3335
-- ---------------------------------------------------------------------------
3436

@@ -39,6 +41,7 @@ do
3941
assert(o.clamp(3, -1, 1) == 1)
4042
assert(o.clamp(-3, -1, 1) == -1)
4143
assert(o.clamp(2.718, 0, 3.141) == 2.718)
44+
assert(o.clamp(nil, -1, 1) == nil)
4245
end
4346

4447
-- make_check_values_func without default
@@ -109,6 +112,7 @@ assert(osm2pgsql.trim(' a ') == 'a')
109112
assert(osm2pgsql.trim(' a ') == 'a')
110113
assert(osm2pgsql.trim(' ab cd ') == 'ab cd')
111114
assert(osm2pgsql.trim(' \t\r\n\f\va\000b \r\t\n\f\v') == 'a\000b')
115+
assert(osm2pgsql.trim(nil) == nil)
112116

113117
-- split_unit
114118

@@ -189,6 +193,9 @@ assert(r[1] == 'ab c;d')
189193
assert(r[2] == 'e f')
190194
assert(r[3] == 'ghi')
191195

196+
r = o.split_string(nil)
197+
assert(#r == 0)
198+
192199
-- ---------------------------------------------------------------------------
193200

194201
print("All tests successful")

0 commit comments

Comments
 (0)