From cfaf26a230567748ac3ea2b52080ac87fdb17b1d Mon Sep 17 00:00:00 2001 From: travisstaloch Date: Tue, 22 Aug 2023 16:59:46 -0700 Subject: [PATCH] use lookup table in alphaIdx() (#1) --- ulid.zig | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ulid.zig b/ulid.zig index 4f30cee..a33d252 100644 --- a/ulid.zig +++ b/ulid.zig @@ -12,6 +12,15 @@ const text_time_bytes = 10; const text_rand_bytes = 16; fn alphaIdx(c: u8) ?u8 { + const alpha_idxs = comptime blk: { + var idxs = [1]?u8{null} ** 256; + for (0..256) |i| idxs[i] = _alphaIdx(@truncate(i)); + break :blk idxs; + }; + return alpha_idxs[c]; +} + +fn _alphaIdx(c: u8) ?u8 { return switch (c) { // inline 0...47 => null, inline '0'...'9' => |i| i - 48,