diff --git a/source/core.cpp b/source/core.cpp index 3e6cc0f..2b3ce38 100644 --- a/source/core.cpp +++ b/source/core.cpp @@ -515,6 +515,15 @@ export namespace core { return hash_code; } + /** + * Swaps the values of `element` in `a` and `b` around using copy semantics. + */ + template 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. * @@ -550,6 +559,34 @@ export namespace core { 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 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((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 * [allocator::reallocate].