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 32 additions and 6 deletions
Showing only changes of commit 44ee0591a2 - Show all commits

View File

@ -457,6 +457,32 @@ export namespace core {
// Input/output operations. // Input/output operations.
export namespace core { export namespace core {
/**
* Compares `a` and `b`, returning the difference between them or `0` if they are identical.
*/
constexpr size compare(slice<u8 const> const & a, slice<u8 const> const & b) {
usize const range = min(a.length, b.length);
for (usize index = 0; index < range; index += 1) {
size const difference = static_cast<size>(a[index]) - static_cast<size>(b[index]);
if (difference != 0) return difference;
}
return static_cast<size>(a.length) - static_cast<size>(b.length);
}
/**
* Copies the contents of `origin` into `target`.
*
* *Note*: safety-checked behavior is triggered if `target` is smaller than `origin`.
*/
void copy(slice<u8> const & target, slice<u8 const> const & origin) {
if (target.length < origin.length) core::unreachable();
for (usize i = 0; i < origin.length; i += 1) target[i] = origin[i];
}
/** /**
* Tests the equality of `a` against `b`, returning `true` if they contain identical bytes, * Tests the equality of `a` against `b`, returning `true` if they contain identical bytes,
* otherwise `false`. * otherwise `false`.
@ -470,14 +496,14 @@ export namespace core {
} }
/** /**
* Copies the contents of `origin` into `target`. * Returns a hash code generated from the values in `bytes`.
*
* *Note*: safety-checked behavior is triggered if `target` is smaller than `origin`.
*/ */
void copy(slice<u8> const & target, slice<u8 const> const & origin) { constexpr usize hash(slice<u8 const> const & bytes) {
if (target.length < origin.length) core::unreachable(); usize hash_code = 5381;
for (usize i = 0; i < origin.length; i += 1) target[i] = origin[i]; for (u8 const byte : bytes) hash_code = ((hash_code << 5) + hash_code) + byte;
return hash_code;
} }
/** /**