Created
January 20, 2023 21:32
-
-
Save Reijaff/90c6fbf885c9ab874aba4625d3521be2 to your computer and use it in GitHub Desktop.
convert string to djb2 hash at compile time in zig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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