[ox/std] Slight optimization

This commit is contained in:
2026-01-23 01:38:31 -06:00
parent 7477ede222
commit 6a42303239

View File

@@ -17,7 +17,7 @@
namespace ox { namespace ox {
template<Integer_c Integer> template<Integer_c Integer>
constexpr ox::Error writeItoa(Integer v, ox::Writer_c auto &writer) noexcept { constexpr ox::Error writeItoa(Integer const v, ox::Writer_c auto &writer) noexcept {
if (v) { if (v) {
ox::ResizedInt_t<Integer, 64> mod = 1000000000000000000; ox::ResizedInt_t<Integer, 64> mod = 1000000000000000000;
ox::ResizedInt_t<Integer, 64> val = v; ox::ResizedInt_t<Integer, 64> val = v;
@@ -27,17 +27,23 @@ constexpr ox::Error writeItoa(Integer v, ox::Writer_c auto &writer) noexcept {
OX_RETURN_ERROR(writer.put('-')); OX_RETURN_ERROR(writer.put('-'));
val = ~val + 1; val = ~val + 1;
} }
while (mod) { if constexpr(sizeof(v) == 8 && !ox::is_signed_v<Integer>) {
auto digit = val / mod; auto digit = val / mod;
val %= mod; val %= mod;
mod /= base; mod /= base;
if (it || digit) { if (digit) {
constexpr auto start = '0';
if (digit >= 10) [[unlikely]] {
digit -= 10; digit -= 10;
OX_RETURN_ERROR(writer.put('1')); OX_RETURN_ERROR(writer.put('1'));
OX_RETURN_ERROR(writer.put(static_cast<char>('0' + digit)));
++it;
} }
OX_RETURN_ERROR(writer.put(static_cast<char>(start + digit))); }
while (mod) {
auto const digit = val / mod;
val %= mod;
mod /= base;
if (it || digit) {
OX_RETURN_ERROR(writer.put(static_cast<char>('0' + digit)));
++it; ++it;
} }
} }