authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-12-29 19:33:41-08:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-01-02 17:40:30+02:00
loge19219fa0e0a563dfda4a5f00737e0aadd7ca44e
tree038416f03164a5835be43199694ec804befac78b
parentd9d840a33ac8abb0e616de862f592821a7f4a35e

std.ComptimeStringMap: allow getting kv index


1 files changed, 5 insertions(+), 1 deletions(-)

lib/std/comptime_string_map.zig+5-1
...@@ -103,6 +103,10 @@ pub fn ComptimeStringMapWithEql(...@@ -103,6 +103,10 @@ pub fn ComptimeStringMapWithEql(
103103
104 /// Returns the value for the key if any, else null.104 /// Returns the value for the key if any, else null.
105 pub fn get(str: []const u8) ?V {105 pub fn get(str: []const u8) ?V {
106 return precomputed.sorted_kvs[getIndex(str) orelse return null].value;
107 }
108
109 pub fn getIndex(str: []const u8) ?usize {
106 if (str.len < precomputed.min_len or str.len > precomputed.max_len)110 if (str.len < precomputed.min_len or str.len > precomputed.max_len)
107 return null;111 return null;
108112
...@@ -112,7 +116,7 @@ pub fn ComptimeStringMapWithEql(...@@ -112,7 +116,7 @@ pub fn ComptimeStringMapWithEql(
112 if (kv.key.len != str.len)116 if (kv.key.len != str.len)
113 return null;117 return null;
114 if (eql(kv.key, str))118 if (eql(kv.key, str))
115 return kv.value;119 return i;
116 i += 1;120 i += 1;
117 if (i >= precomputed.sorted_kvs.len)121 if (i >= precomputed.sorted_kvs.len)
118 return null;122 return null;