authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-04-17 04:46:23 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-04-17 04:46:23 -07:00
log49cfaabc101b85d4d282a649c0b9605de20a6229
tree3dbbdbf107fb583025cd9d0583670f0c6bc75d0b
parent3f0f0b7f34922ccc63d2a8e12b4b9d2c8754218c
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

make to_hex much faster


1 files changed, 14 insertions(+), 2 deletions(-)

src/to_hex.zig+14-2
...@@ -2,11 +2,23 @@ const std = @import("std");...@@ -2,11 +2,23 @@ const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
4const rawIntBytes = extras.rawIntBytes;4const rawIntBytes = extras.rawIntBytes;
5const digits = "0123456789abcdef";
6
7const alphabet = blk: {
8 var data: [512]u8 = @splat('0');
9 for (0..16) |m| {
10 for (0..16) |n| {
11 data[(m * 16 + n) * 2 + 0] = digits[m];
12 data[(m * 16 + n) * 2 + 1] = digits[n];
13 }
14 }
15 const result = data;
16 break :blk result;
17};
518
6pub fn to_hex(array: anytype) [array.len * 2]u8 {19pub fn to_hex(array: anytype) [array.len * 2]u8 {
7 var res: [array.len * 2]u8 = undefined;20 var res: [array.len * 2]u8 = undefined;
8 var fbs = std.io.fixedBufferStream(&res);21 for (&array, 0..) |x, i| res[i * 2 ..][0..2].* = alphabet[@as(usize, x) * 2 ..][0..2].*;
9 std.fmt.format(fbs.writer(), "{x}", .{std.fmt.fmtSliceHexLower(&array)}) catch unreachable;
10 return res;22 return res;
11}23}
1224