Skip to content

Commit aea98a5

Browse files
committed
Consolidate hex encoding functions into their own file
Consolidate multiple functions doing basically the same hex encoding and add some tests.
1 parent 71d9d36 commit aea98a5

File tree

10 files changed

+119
-29
lines changed

10 files changed

+119
-29
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ target_sources(osm2pgsql_lib PRIVATE
3232
geom-functions.cpp
3333
geom-pole-of-inaccessibility.cpp
3434
geom.cpp
35+
hex.cpp
3536
idlist.cpp
3637
input.cpp
3738
locator.cpp

src/db-copy-mgr.hpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <string>
1616

1717
#include "db-copy.hpp"
18-
#include "util.hpp"
18+
#include "hex.hpp"
1919

2020
/**
2121
* Management class that fills and manages copy buffers.
@@ -237,13 +237,7 @@ class db_copy_mgr_t
237237
*/
238238
void add_hex_geom(std::string const &wkb)
239239
{
240-
char const *const lookup_hex = "0123456789ABCDEF";
241-
242-
for (auto c : wkb) {
243-
unsigned int const num = static_cast<unsigned char>(c);
244-
m_current.buffer += lookup_hex[(num >> 4U) & 0xfU];
245-
m_current.buffer += lookup_hex[num & 0xfU];
246-
}
240+
util::encode_hex(wkb, &m_current.buffer);
247241
m_current.buffer += '\t';
248242
}
249243

src/gen/canvas.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "canvas.hpp"
1111

12+
#include "hex.hpp"
1213
#include "raster.hpp"
1314
#include "tile.hpp"
1415

@@ -139,19 +140,3 @@ void canvas_t::merge(canvas_t const &other)
139140
{
140141
cv::bitwise_or(m_rast, other.m_rast, m_rast);
141142
}
142-
143-
std::string to_hex(std::string const &in)
144-
{
145-
std::string result;
146-
result.reserve(in.size() * 2);
147-
148-
char const *const lookup_hex = "0123456789ABCDEF";
149-
150-
for (const auto c : in) {
151-
unsigned int const num = static_cast<unsigned char>(c);
152-
result += lookup_hex[(num >> 4U) & 0xfU];
153-
result += lookup_hex[num & 0xfU];
154-
}
155-
156-
return result;
157-
}

src/gen/canvas.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,4 @@ class canvas_t
7777
image_type m_rast;
7878
}; // class canvas_t
7979

80-
std::string to_hex(std::string const &in);
81-
8280
#endif // OSM2PGSQL_CANVAS_HPP

src/gen/gen-tile-builtup.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "canvas.hpp"
1313
#include "geom-functions.hpp"
14+
#include "hex.hpp"
1415
#include "logging.hpp"
1516
#include "params.hpp"
1617
#include "pgsql.hpp"
@@ -33,7 +34,7 @@ void save_image_to_table(pg_conn_t *connection, canvas_t const &canvas,
3334
std::string const &table, char const *variant,
3435
std::string const &table_prefix)
3536
{
36-
auto const wkb = to_hex(canvas.to_wkb(tile, margin));
37+
auto const wkb = util::encode_hex(canvas.to_wkb(tile, margin));
3738

3839
connection->exec("INSERT INTO \"{}_{}_{}\" (zoom, x, y, rast)"
3940
" VALUES ({}, {}, {}, '{}')",
@@ -251,7 +252,7 @@ void gen_tile_builtup_t::process(tile_t const &tile)
251252
log_gen("Write geometries to destination table...");
252253
timer(m_timer_write).start();
253254
for (auto const &geom : geometries) {
254-
auto const wkb = to_hex(geom_to_ewkb(geom));
255+
auto const wkb = util::encode_hex(geom_to_ewkb(geom));
255256
if (m_has_area_column) {
256257
connection().exec_prepared("insert_geoms", wkb, tile.x(), tile.y(),
257258
geom::area(geom));

src/gen/gen-tile-raster.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "gen-tile-raster.hpp"
1111

1212
#include "canvas.hpp"
13+
#include "hex.hpp"
1314
#include "logging.hpp"
1415
#include "params.hpp"
1516
#include "pgsql.hpp"
@@ -63,7 +64,7 @@ void save_image_to_table(pg_conn_t *connection, canvas_t const &canvas,
6364
std::string const &param, char const *variant,
6465
std::string const &table_prefix)
6566
{
66-
auto const wkb = to_hex(canvas.to_wkb(tile, margin));
67+
auto const wkb = util::encode_hex(canvas.to_wkb(tile, margin));
6768

6869
connection->exec("INSERT INTO \"{}_{}\" (type, zoom, x, y, rast)"
6970
" VALUES ('{}', {}, {}, {}, '{}')",

src/hex.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* SPDX-License-Identifier: GPL-2.0-or-later
3+
*
4+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
5+
*
6+
* Copyright (C) 2006-2025 by the osm2pgsql developer community.
7+
* For a full list of authors see the git log.
8+
*/
9+
10+
#include "hex.hpp"
11+
12+
#include <cassert>
13+
14+
namespace util {
15+
16+
void encode_hex(std::string const &in, std::string *out)
17+
{
18+
assert(out);
19+
20+
constexpr char const *const LOOKUP_HEX = "0123456789ABCDEF";
21+
22+
for (auto const c : in) {
23+
unsigned int const num = static_cast<unsigned char>(c);
24+
(*out) += LOOKUP_HEX[(num >> 4U) & 0xfU];
25+
(*out) += LOOKUP_HEX[num & 0xfU];
26+
}
27+
}
28+
29+
std::string encode_hex(std::string const &in)
30+
{
31+
std::string result;
32+
result.reserve(in.size() * 2);
33+
encode_hex(in, &result);
34+
return result;
35+
}
36+
37+
} // namespace util

src/hex.hpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef OSM2PGSQL_HEX_HPP
2+
#define OSM2PGSQL_HEX_HPP
3+
4+
/**
5+
* SPDX-License-Identifier: GPL-2.0-or-later
6+
*
7+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
8+
*
9+
* Copyright (C) 2006-2025 by the osm2pgsql developer community.
10+
* For a full list of authors see the git log.
11+
*/
12+
13+
#include <string>
14+
15+
namespace util {
16+
17+
/**
18+
* Convert content of input string to hex and append to output.
19+
*
20+
* \param in The input data.
21+
* \param out Pointer to output string.
22+
*/
23+
void encode_hex(std::string const &in, std::string *out);
24+
25+
/**
26+
* Convert content of input string to hex and return it.
27+
*
28+
* \param in The input data.
29+
* \returns Hex encoded string.
30+
*/
31+
std::string encode_hex(std::string const &in);
32+
33+
} // namespace util
34+
35+
#endif // OSM2PGSQL_HEX_HPP

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ set_test(test-geom-points LABELS NoDB)
5757
set_test(test-geom-pole-of-inaccessibility LABELS NoDB)
5858
set_test(test-geom-polygons LABELS NoDB)
5959
set_test(test-geom-transform LABELS NoDB)
60+
set_test(test-hex LABELS NoDB)
6061
set_test(test-json-writer LABELS NoDB)
6162
set_test(test-locator LABELS NoDB)
6263
set_test(test-lua-utils LABELS NoDB)

tests/test-hex.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* SPDX-License-Identifier: GPL-2.0-or-later
3+
*
4+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
5+
*
6+
* Copyright (C) 2006-2025 by the osm2pgsql developer community.
7+
* For a full list of authors see the git log.
8+
*/
9+
10+
#include <catch.hpp>
11+
12+
#include "hex.hpp"
13+
14+
#include <string>
15+
16+
TEST_CASE("hex encode a string", "[NoDB]")
17+
{
18+
std::string result;
19+
util::encode_hex("ab~", &result);
20+
REQUIRE(result.size() == 6);
21+
REQUIRE(result == "61627E");
22+
}
23+
24+
TEST_CASE("hex encode a string adding to existing string", "[NoDB]")
25+
{
26+
std::string result{"0x"};
27+
util::encode_hex("\xca\xfe", &result);
28+
REQUIRE(result.size() == 6);
29+
REQUIRE(result == "0xCAFE");
30+
}
31+
32+
TEST_CASE("hex encoding an empty string doesn't change output string", "[NoDB]")
33+
{
34+
std::string result{"foo"};
35+
util::encode_hex("", &result);
36+
REQUIRE(result == "foo");
37+
}

0 commit comments

Comments
 (0)