Commit e19219fa0e

Meghan Denny <hello@nektro.net>
2023-12-30 04:33:41
std.ComptimeStringMap: allow getting kv index
1 parent d9d840a
Changed files (1)
lib/std/comptime_string_map.zig
@@ -103,6 +103,10 @@ pub fn ComptimeStringMapWithEql(
 
         /// Returns the value for the key if any, else null.
         pub fn get(str: []const u8) ?V {
+            return precomputed.sorted_kvs[getIndex(str) orelse return null].value;
+        }
+
+        pub fn getIndex(str: []const u8) ?usize {
             if (str.len < precomputed.min_len or str.len > precomputed.max_len)
                 return null;
 
@@ -112,7 +116,7 @@ pub fn ComptimeStringMapWithEql(
                 if (kv.key.len != str.len)
                     return null;
                 if (eql(kv.key, str))
-                    return kv.value;
+                    return i;
                 i += 1;
                 if (i >= precomputed.sorted_kvs.len)
                     return null;