From 1d2356e942fb565d4ab52ba40cef82741c266947 Mon Sep 17 00:00:00 2001 From: kayomn Date: Mon, 17 Oct 2022 14:42:41 +0100 Subject: [PATCH] Add library support for UTF-8 to Ona module --- src/ona/utf8.zig | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/ona/utf8.zig 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, +};