C++20 Port #5
|
@ -457,6 +457,32 @@ export namespace core {
|
|||
|
||||
// Input/output operations.
|
||||
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,
|
||||
* otherwise `false`.
|
||||
|
@ -470,14 +496,14 @@ export namespace core {
|
|||
}
|
||||
|
||||
/**
|
||||
* Copies the contents of `origin` into `target`.
|
||||
*
|
||||
* *Note*: safety-checked behavior is triggered if `target` is smaller than `origin`.
|
||||
* Returns a hash code generated from the values in `bytes`.
|
||||
*/
|
||||
void copy(slice<u8> const & target, slice<u8 const> const & origin) {
|
||||
if (target.length < origin.length) core::unreachable();
|
||||
constexpr usize hash(slice<u8 const> const & bytes) {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue