Skip to content

Commit fcd30e0

Browse files
authored
Merge pull request #1318 from joto/lua-helper
Add trim(), split_unit() and split_string() Lua helper functions
2 parents f5fd8f3 + ca7c5fa commit fcd30e0

2 files changed

Lines changed: 149 additions & 1 deletion

File tree

src/init.lua

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ 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
24-
return str:sub(- suffix:len()) == suffix
30+
return str:sub(- #suffix) == suffix
2531
end
2632

2733
function osm2pgsql.define_node_table(_name, _columns, _options)
@@ -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

@@ -116,6 +125,47 @@ function osm2pgsql.make_clean_tags_func(keys)
116125
end
117126
end
118127

128+
-- from http://lua-users.org/wiki/StringTrim
129+
function osm2pgsql.trim(str)
130+
if str == nil then
131+
return nil
132+
end
133+
local from = str:match("^%s*()")
134+
return from > #str and "" or str:match(".*%S", from)
135+
end
136+
137+
function osm2pgsql.split_unit(str, default_unit)
138+
if str == nil then
139+
return nil
140+
end
141+
142+
local val, unit = string.match(str, "^(-?[0-9.]+) ?(%a*)$")
143+
if val == nil then
144+
return nil
145+
end
146+
147+
if unit == '' then
148+
unit = default_unit
149+
end
150+
151+
val = tonumber(val)
152+
153+
return val, unit
154+
end
155+
156+
function osm2pgsql.split_string(str, separator)
157+
local result = {}
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
164+
end
165+
166+
return result
167+
end
168+
119169
-- This will be the metatable for the OSM objects given to the process callback
120170
-- functions.
121171
local inner_metatable = {

tests/lua/tests.lua

Lines changed: 98 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
@@ -98,6 +101,101 @@ do
98101
assert(num == 3)
99102
end
100103

104+
-- trim
105+
assert(osm2pgsql.trim('') == '')
106+
assert(osm2pgsql.trim(' ') == '')
107+
assert(osm2pgsql.trim(' ') == '')
108+
assert(osm2pgsql.trim('a') == 'a')
109+
assert(osm2pgsql.trim(' a') == 'a')
110+
assert(osm2pgsql.trim('a ') == 'a')
111+
assert(osm2pgsql.trim(' a ') == 'a')
112+
assert(osm2pgsql.trim(' a ') == 'a')
113+
assert(osm2pgsql.trim(' ab cd ') == 'ab cd')
114+
assert(osm2pgsql.trim(' \t\r\n\f\va\000b \r\t\n\f\v') == 'a\000b')
115+
assert(osm2pgsql.trim(nil) == nil)
116+
117+
-- split_unit
118+
119+
v, u = o.split_unit('20m', '')
120+
assert(v == 20 and u == 'm')
121+
122+
v, u = o.split_unit('20 m')
123+
assert(v == 20 and u == 'm')
124+
125+
v, u = o.split_unit('20ft', '')
126+
assert(v == 20 and u == 'ft')
127+
128+
v, u = o.split_unit('23.4 ft', '')
129+
assert(v == 23.4 and u == 'ft')
130+
131+
v, u = o.split_unit('20 ft', 'm')
132+
assert(v == 20 and u == 'ft')
133+
134+
v, u = o.split_unit('20km', 'm')
135+
assert(v == 20 and u == 'km')
136+
137+
v, u = o.split_unit('20')
138+
assert(v == 20 and u == nil)
139+
140+
v, u = o.split_unit('20', 'm')
141+
assert(v == 20 and u == 'm')
142+
143+
v, u = o.split_unit('0', 'm')
144+
assert(v == 0 and u == 'm')
145+
146+
v, u = o.split_unit('-20000', 'leagues')
147+
assert(v == -20000 and u == 'leagues')
148+
149+
v, u = o.split_unit('20xx20', '')
150+
assert(v == nil)
151+
152+
v, u = o.split_unit('20-20', '')
153+
assert(v == nil)
154+
155+
v, u = o.split_unit('20xx20', 'foo')
156+
assert(v == nil)
157+
158+
v, u = o.split_unit('abc', 'def')
159+
assert(v == nil)
160+
161+
v, u = o.split_unit(nil)
162+
assert(v == nil and u == nil)
163+
164+
v, u = o.split_unit(nil, 'foo')
165+
assert(v == nil and u == nil)
166+
167+
-- split_string
168+
169+
r = o.split_string('ab c;d;e f;ghi')
170+
assert(#r == 4)
171+
assert(r[1] == 'ab c')
172+
assert(r[2] == 'd')
173+
assert(r[3] == 'e f')
174+
assert(r[4] == 'ghi')
175+
176+
r = o.split_string('ab c;d ; e f; ghi')
177+
assert(#r == 4)
178+
assert(r[1] == 'ab c')
179+
assert(r[2] == 'd')
180+
assert(r[3] == 'e f')
181+
assert(r[4] == 'ghi')
182+
183+
r = o.split_string('ab c ')
184+
assert(#r == 1)
185+
assert(r[1] == 'ab c')
186+
187+
r = o.split_string('')
188+
assert(#r == 0)
189+
190+
r = o.split_string('ab c;d , e f, ghi', ',')
191+
assert(#r == 3)
192+
assert(r[1] == 'ab c;d')
193+
assert(r[2] == 'e f')
194+
assert(r[3] == 'ghi')
195+
196+
r = o.split_string(nil)
197+
assert(#r == 0)
198+
101199
-- ---------------------------------------------------------------------------
102200

103201
print("All tests successful")

0 commit comments

Comments
 (0)