From 4cefcbe020e83f320ecb79a3a2f700fcf52ab97f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rimas=20Misevi=C4=8Dius?= Date: Tue, 2 Jul 2024 00:10:31 +0300 Subject: [PATCH] Move the unsigned_to_str function to util.h --- include/upa/url.h | 2 +- include/upa/url_ip.h | 26 +++----------------------- include/upa/util.h | 25 +++++++++++++++++++++++-- src/url_ip.cpp | 7 ++++--- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/include/upa/url.h b/include/upa/url.h index 4eb89fd6..259e8a7c 100644 --- a/include/upa/url.h +++ b/include/upa/url.h @@ -1913,7 +1913,7 @@ inline validation_errc url_parser::url_parse(url_serializer& urls, const CharT* } // set port if not default if (urls.scheme_inf() == nullptr || urls.scheme_inf()->default_port != port) { - unsigned_to_str(port, urls.start_part(url::PORT), 10); + util::unsigned_to_str(port, urls.start_part(url::PORT), 10); urls.save_part(); urls.set_flag(url::PORT_FLAG); } else { diff --git a/include/upa/url_ip.h b/include/upa/url_ip.h index e69402e8..4e149243 100644 --- a/include/upa/url_ip.h +++ b/include/upa/url_ip.h @@ -1,4 +1,4 @@ -// Copyright 2016-2023 Rimas Misevičius +// Copyright 2016-2024 Rimas Misevičius // Distributed under the BSD-style license that can be // found in the LICENSE file. // @@ -9,34 +9,14 @@ #include "url_percent_encode.h" #include "url_result.h" #include -#include +#include #include // uint16_t, uint32_t, uint64_t #include #include +#include namespace upa { -// append unsigned integer to string - -template -inline void unsigned_to_str(UIntT num, std::string& output, UIntT base) { - static const char digit[] = "0123456789abcdef"; - - // count digits - std::size_t count = output.length() + 1; - // one division is needed to prevent the multiplication overflow - const UIntT num0 = num / base; - for (UIntT divider = 1; divider <= num0; divider *= base) - ++count; - output.resize(count); - - // convert - do { - output[--count] = digit[num % base]; - num /= base; - } while (num); -} - // The hostname ends in a number checker // https://url.spec.whatwg.org/#ends-in-a-number-checker // Optimized version diff --git a/include/upa/util.h b/include/upa/util.h index 970e9aa8..2b636855 100644 --- a/include/upa/util.h +++ b/include/upa/util.h @@ -1,4 +1,4 @@ -// Copyright 2016-2023 Rimas Misevičius +// Copyright 2016-2024 Rimas Misevičius // Distributed under the BSD-style license that can be // found in the LICENSE file. // @@ -8,7 +8,7 @@ #include "config.h" #include -#include +#include #include #include #include @@ -73,6 +73,27 @@ constexpr auto to_unsigned(T n) noexcept -> UT { return static_cast(n); } +// Append unsigned integer to string + +template +inline void unsigned_to_str(UIntT num, std::string& output, UIntT base) { + static const char digit[] = "0123456789abcdef"; + + // count digits + std::size_t count = output.length() + 1; + // one division is needed to prevent the multiplication overflow + const UIntT num0 = num / base; + for (UIntT divider = 1; divider <= num0; divider *= base) + ++count; + output.resize(count); + + // convert + do { + output[--count] = digit[num % base]; + num /= base; + } while (num); +} + // Append data to string inline std::size_t add_sizes(std::size_t size1, std::size_t size2, std::size_t max_size) { diff --git a/src/url_ip.cpp b/src/url_ip.cpp index f7432152..00e0c88a 100644 --- a/src/url_ip.cpp +++ b/src/url_ip.cpp @@ -4,6 +4,7 @@ // #include "upa/url_ip.h" +#include "upa/util.h" namespace upa { @@ -12,10 +13,10 @@ namespace upa { void ipv4_serialize(uint32_t ipv4, std::string& output) { for (unsigned shift = 24; shift != 0; shift -= 8) { - unsigned_to_str((ipv4 >> shift) & 0xFF, output, 10); + util::unsigned_to_str((ipv4 >> shift) & 0xFF, output, 10); output.push_back('.'); } - unsigned_to_str(ipv4 & 0xFF, output, 10); + util::unsigned_to_str(ipv4 & 0xFF, output, 10); } // IPv6 serializer @@ -58,7 +59,7 @@ void ipv6_serialize(const uint16_t(&address)[8], std::string& output) { it += compress_length; if (it == last) break; } - unsigned_to_str(*it, output, 16); + util::unsigned_to_str(*it, output, 16); if (++it == last) break; output.push_back(':'); }