From 8e783e9af31bfde1fe254ed4b01b3f1d9474fdc6 Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Fri, 6 Jan 2023 09:21:48 +0100 Subject: [PATCH 1/2] Remove the overengineered util::integer_to_buffer It is only used in a single place and easily replaced by fmt::to_string(). --- src/osmtypes.hpp | 5 +++-- src/tagtransform-c.cpp | 3 +-- src/util.hpp | 21 --------------------- tests/test-util.cpp | 18 ------------------ 4 files changed, 4 insertions(+), 43 deletions(-) diff --git a/src/osmtypes.hpp b/src/osmtypes.hpp index e21ecf719..1448ef233 100644 --- a/src/osmtypes.hpp +++ b/src/osmtypes.hpp @@ -189,9 +189,10 @@ class taglist_t } /// Add tag to list without checking for duplicates - void add_tag(char const *key, char const *value) + template + void add_tag(char const *key, T&& value) { - m_tags.emplace_back(key, value); + m_tags.emplace_back(key, std::move(value)); } /// Add tag to list if there is no tag with that key yet diff --git a/src/tagtransform-c.cpp b/src/tagtransform-c.cpp index 9da0c4102..066bcf11d 100644 --- a/src/tagtransform-c.cpp +++ b/src/tagtransform-c.cpp @@ -84,8 +84,7 @@ static void add_z_order(taglist_t *tags, bool *roads) z_order -= 100; } - util::integer_to_buffer z{z_order}; - tags->add_tag("z_order", z.c_str()); + tags->add_tag("z_order", fmt::to_string(z_order)); } c_tagtransform_t::c_tagtransform_t(options_t const *options, export_list exlist) diff --git a/src/util.hpp b/src/util.hpp index 6bad7183b..5b5219300 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -25,27 +25,6 @@ namespace util { -class integer_to_buffer -{ - // This is enough for 64 bit integers - static constexpr std::size_t buffer_size = 21; - -public: - template - explicit integer_to_buffer(T value) - { - auto const result = - fmt::format_to_n(m_buffer.begin(), buffer_size - 1, "{}", value); - assert(result.size < buffer_size); - *result.out = '\0'; - } - - char const *c_str() const noexcept { return m_buffer.data(); } - -private: - std::array m_buffer{}; -}; - class double_to_buffer { static constexpr std::size_t buffer_size = 32; diff --git a/tests/test-util.cpp b/tests/test-util.cpp index 966f6319d..0a154e214 100644 --- a/tests/test-util.cpp +++ b/tests/test-util.cpp @@ -16,24 +16,6 @@ #include #include -TEST_CASE("integer_to_buffer 1", "[NoDB]") -{ - util::integer_to_buffer buffer{1}; - REQUIRE(std::strcmp(buffer.c_str(), "1") == 0); -} - -TEST_CASE("integer_to_buffer max", "[NoDB]") -{ - util::integer_to_buffer buffer{std::numeric_limits::max()}; - REQUIRE(std::strcmp(buffer.c_str(), "9223372036854775807") == 0); -} - -TEST_CASE("integer_to_buffer min", "[NoDB]") -{ - util::integer_to_buffer buffer{std::numeric_limits::min()}; - REQUIRE(std::strcmp(buffer.c_str(), "-9223372036854775808") == 0); -} - TEST_CASE("double_to_buffer 0", "[NoDB]") { util::double_to_buffer buffer{0.0}; From 6ff0321386b983610313cf202d730a355c46d843 Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Fri, 6 Jan 2023 10:29:42 +0100 Subject: [PATCH 2/2] Simplify db_copy_mgr_t::add_value() The reason we had a different implementation for "double" values here is that std::to_string() uses the current locale for writing out numbers. For integers this doesn't matter, but for doubles this could be something different than a decimal point. The fmt::to_string() function on the other hand always uses the default locale, so it doesn't have this problem. So we can use this for integers as well as for doubles. --- src/db-copy-mgr.hpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/db-copy-mgr.hpp b/src/db-copy-mgr.hpp index b09018b6c..f7449c877 100644 --- a/src/db-copy-mgr.hpp +++ b/src/db-copy-mgr.hpp @@ -290,13 +290,7 @@ class db_copy_mgr_t template void add_value(T value) { - m_current->buffer += std::to_string(value); - } - - void add_value(double value) - { - util::double_to_buffer tmp{value}; - m_current->buffer += tmp.c_str(); + m_current->buffer += fmt::to_string(value); } void add_value(std::string const &s) { add_value(s.c_str()); }