diff --git a/source/core.cpp b/source/core.cpp index 89de699..7a6f66d 100644 --- a/source/core.cpp +++ b/source/core.cpp @@ -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 const & a, slice const & b) { + usize const range = min(a.length, b.length); + + for (usize index = 0; index < range; index += 1) { + size const difference = static_cast(a[index]) - static_cast(b[index]); + + if (difference != 0) return difference; + } + + return static_cast(a.length) - static_cast(b.length); + } + + /** + * Copies the contents of `origin` into `target`. + * + * *Note*: safety-checked behavior is triggered if `target` is smaller than `origin`. + */ + void copy(slice const & target, slice 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 const & target, slice const & origin) { - if (target.length < origin.length) core::unreachable(); + constexpr usize hash(slice 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; } /**