Application Context Implementation #4

Closed
kayomn wants to merge 93 commits from event-loop-dev into main
1 changed files with 29 additions and 0 deletions
Showing only changes of commit 1d2356e942 - Show all commits

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,
};