Skip to content

Commit

Permalink
Move the unsigned_to_str function to util.h
Browse files Browse the repository at this point in the history
  • Loading branch information
rmisev committed Jul 1, 2024
1 parent 8c29f98 commit 4cefcbe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion include/upa/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 3 additions & 23 deletions include/upa/url_ip.h
Original file line number Diff line number Diff line change
@@ -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.
//
Expand All @@ -9,34 +9,14 @@
#include "url_percent_encode.h"
#include "url_result.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint> // uint16_t, uint32_t, uint64_t
#include <limits>
#include <string>
#include <type_traits>

namespace upa {

// append unsigned integer to string

template <typename UIntT>
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
Expand Down
25 changes: 23 additions & 2 deletions include/upa/util.h
Original file line number Diff line number Diff line change
@@ -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.
//
Expand All @@ -8,7 +8,7 @@

#include "config.h"
#include <algorithm>
#include <iterator>
#include <cstddef>
#include <limits>
#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -73,6 +73,27 @@ constexpr auto to_unsigned(T n) noexcept -> UT {
return static_cast<UT>(n);
}

// Append unsigned integer to string

template <typename UIntT>
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) {
Expand Down
7 changes: 4 additions & 3 deletions src/url_ip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//

#include "upa/url_ip.h"
#include "upa/util.h"

namespace upa {

Expand All @@ -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<uint32_t>((ipv4 >> shift) & 0xFF, output, 10);
util::unsigned_to_str<uint32_t>((ipv4 >> shift) & 0xFF, output, 10);
output.push_back('.');
}
unsigned_to_str<uint32_t>(ipv4 & 0xFF, output, 10);
util::unsigned_to_str<uint32_t>(ipv4 & 0xFF, output, 10);
}

// IPv6 serializer
Expand Down Expand Up @@ -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<uint32_t>(*it, output, 16);
util::unsigned_to_str<uint32_t>(*it, output, 16);
if (++it == last) break;
output.push_back(':');
}
Expand Down

0 comments on commit 4cefcbe

Please sign in to comment.