C++20 Port #5

Merged
kayomn merged 35 commits from cpp-port into main 2023-02-20 02:33:45 +01:00
1 changed files with 37 additions and 0 deletions
Showing only changes of commit 5394e8de67 - Show all commits

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].