Add unsigned integer printing to core library

This commit is contained in:
kayomn 2023-02-19 16:21:44 +00:00
parent f6ae40617e
commit 5394e8de67
1 changed files with 37 additions and 0 deletions

View File

@ -515,6 +515,15 @@ export namespace core {
return hash_code; return hash_code;
} }
/**
* Swaps the values of `element` in `a` and `b` around using copy semantics.
*/
template<typename element> constexpr void swap(element & a, element & b) {
element const temp = a;
a = b;
b = temp;
}
/** /**
* Streams the data from `input` to `output`, using `buffer` as temporary transfer space. * Streams the data from `input` to `output`, using `buffer` as temporary transfer space.
* *
@ -550,6 +559,34 @@ export namespace core {
return written; return written;
} }
/**
* Attempts to format and print `value` as an unsigned integer out to `output`.
*
* The returned [expected] can be used to introspect if `output` encountered any issues during
* printing, otherwise it will contain the number of characters used to print `value` as text.
*/
expected<usize, io_error> print_unsigned(writable const & output, u64 value) {
if (value == 0) return output(slice{"0"}.as_bytes());
u8 buffer[20]{0};
usize buffer_count{0};
while (value != 0) {
constexpr usize radix{10};
buffer[buffer_count] = static_cast<u8>((value % radix) + '0');
value = (value / radix);
buffer_count += 1;
}
usize const half_buffer_count{buffer_count / 2};
for (usize i = 0; i < half_buffer_count; i += 1)
swap(buffer[i], buffer[buffer_count - i - 1]);
return output(slice{buffer, buffer_count});
}
/** /**
* Returns a reference to a shared [allocator] which will always return `nullptr` on calls to * Returns a reference to a shared [allocator] which will always return `nullptr` on calls to
* [allocator::reallocate]. * [allocator::reallocate].