Skip to content

Commit

Permalink
use lookup table in alphaIdx() (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
travisstaloch authored Aug 22, 2023
1 parent 66a854b commit cfaf26a
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ulid.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit cfaf26a

Please sign in to comment.