diff options
author | 2022-08-10 21:26:20 -0700 | |
---|---|---|
committer | 2022-08-10 21:26:20 -0700 | |
commit | d9ae284463c25f6c1e6056c2ee4d5bf81f1d11cb (patch) | |
tree | e4221d5571233981da5b28f17b9076c5d183c550 /src/comptime_string_map.zig | |
parent | 00d5f6699b39799b383dc305d4120da7d79c7bce (diff) | |
download | bun-d9ae284463c25f6c1e6056c2ee4d5bf81f1d11cb.tar.gz bun-d9ae284463c25f6c1e6056c2ee4d5bf81f1d11cb.tar.zst bun-d9ae284463c25f6c1e6056c2ee4d5bf81f1d11cb.zip |
Fix console.log with typed arrays
Diffstat (limited to 'src/comptime_string_map.zig')
-rw-r--r-- | src/comptime_string_map.zig | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/comptime_string_map.zig b/src/comptime_string_map.zig index 546f928c9..18b06d9de 100644 --- a/src/comptime_string_map.zig +++ b/src/comptime_string_map.zig @@ -96,6 +96,29 @@ pub fn ComptimeStringMapWithKeyType(comptime KeyType: type, comptime V: type, co return null; } + pub fn getWithLengthAndEql(str: anytype, comptime len: usize, comptime eqls: anytype) ?V { + const end = comptime brk: { + var i = len_indexes[len]; + @setEvalBranchQuota(99999); + + while (i < kvs.len and kvs[i].key.len == len) : (i += 1) {} + + break :brk i; + }; + + comptime var i = len_indexes[len]; + + // This benchmarked faster for both small and large lists of strings than using a big switch statement + // But only so long as the keys are a sorted list. + inline while (i < end) : (i += 1) { + if (eqls(str, kvs[i].key)) { + return kvs[i].value; + } + } + + return null; + } + pub fn get(str: []const KeyType) ?V { if (str.len < precomputed.min_len or str.len > precomputed.max_len) return null; @@ -109,6 +132,20 @@ pub fn ComptimeStringMapWithKeyType(comptime KeyType: type, comptime V: type, co return null; } + + pub fn getWithEql(input: anytype, comptime eql: anytype) ?V { + if (input.len < precomputed.min_len or input.len > precomputed.max_len) + return null; + + comptime var i: usize = precomputed.min_len; + inline while (i <= precomputed.max_len) : (i += 1) { + if (input.len == i) { + return getWithLengthAndEql(input, i, eql); + } + } + + return null; + } }; } |