Add library support for UTF-8 to Ona module

This commit is contained in:
kayomn 2022-10-17 14:42:41 +01:00
parent 1997c38e97
commit 1d2356e942
1 changed files with 29 additions and 0 deletions

29
src/ona/utf8.zig Normal file
View File

@ -0,0 +1,29 @@
const std = @import("std");
const table = @import("./table.zig");
///
/// Tests if the contents of `this_utf8_sequence` lexically equals the contents of
/// `that_utf8_sequence`.
///
pub fn equals(this_utf8_sequence: []const u8, that_utf8_sequence: []const u8) bool {
return std.mem.eql(u8, this_utf8_sequence, that_utf8_sequence);
}
///
/// Returns a deterministic hash for `utf8_sequence`.
///
pub fn hash(utf8_sequence: []const u8) usize {
var utf8_hash = @as(usize, 5381);
for (utf8_sequence) |utf8_code| utf8_hash = ((utf8_hash << 5) + utf8_hash) + utf8_code;
return utf8_hash;
}
///
/// A [table.KeyContext] for handling UTF-8 character sequences.
///
pub const key_context = table.KeyContext([]const u8){
.hash = hash,
.equals = equals,
};