diff --git a/src/ona/utf8.zig b/src/ona/utf8.zig new file mode 100644 index 0000000..352a9d6 --- /dev/null +++ b/src/ona/utf8.zig @@ -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, +};