Skip to content

Instantly share code, notes, and snippets.

@Reijaff
Created January 20, 2023 21:32
Show Gist options
  • Select an option

  • Save Reijaff/90c6fbf885c9ab874aba4625d3521be2 to your computer and use it in GitHub Desktop.

Select an option

Save Reijaff/90c6fbf885c9ab874aba4625d3521be2 to your computer and use it in GitHub Desktop.
convert string to djb2 hash at compile time in zig
const std = @import("std");
fn hash_string_djb2(comptime string: []const u8) u64 {
var hash: u64 = 5381;
inline for (string) |chr| {
hash = ((hash << 5) + hash) + chr;
}
return hash;
}
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
try stdout.print("{d}\n", .{hash_string_djb2("hello my dear friend")});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment