| ... | @@ -13,21 +13,21 @@ const mem = std.mem; | ... | @@ -13,21 +13,21 @@ const mem = std.mem; |
| 13 | /// `kvs` expects a list literal containing list literals or an array/slice of structs | 13 | /// `kvs` expects a list literal containing list literals or an array/slice of structs |
| 14 | /// where `.@"0"` is the `[]const u8` key and `.@"1"` is the associated value of type `V`. | 14 | /// where `.@"0"` is the `[]const u8` key and `.@"1"` is the associated value of type `V`. |
| 15 | /// TODO: https://github.com/ziglang/zig/issues/4335 | 15 | /// TODO: https://github.com/ziglang/zig/issues/4335 |
| 16 | pub fn ComptimeStringMap(comptime V: type, comptime kvs: anytype) type { | 16 | pub fn ComptimeStringMap(comptime V: type, comptime kvs_list: anytype) type { |
| 17 | const precomputed = comptime blk: { | 17 | const precomputed = comptime blk: { |
| 18 | @setEvalBranchQuota(2000); | 18 | @setEvalBranchQuota(2000); |
| 19 | const KV = struct { | 19 | const KV = struct { |
| 20 | key: []const u8, | 20 | key: []const u8, |
| 21 | value: V, | 21 | value: V, |
| 22 | }; | 22 | }; |
| 23 | var sorted_kvs: [kvs.len]KV = undefined; | 23 | var sorted_kvs: [kvs_list.len]KV = undefined; |
| 24 | const lenAsc = (struct { | 24 | const lenAsc = (struct { |
| 25 | fn lenAsc(context: void, a: KV, b: KV) bool { | 25 | fn lenAsc(context: void, a: KV, b: KV) bool { |
| 26 | _ = context; | 26 | _ = context; |
| 27 | return a.key.len < b.key.len; | 27 | return a.key.len < b.key.len; |
| 28 | } | 28 | } |
| 29 | }).lenAsc; | 29 | }).lenAsc; |
| 30 | for (kvs) |kv, i| { | 30 | for (kvs_list) |kv, i| { |
| 31 | if (V != void) { | 31 | if (V != void) { |
| 32 | sorted_kvs[i] = .{ .key = kv.@"0", .value = kv.@"1" }; | 32 | sorted_kvs[i] = .{ .key = kv.@"0", .value = kv.@"1" }; |
| 33 | } else { | 33 | } else { |
| ... | @@ -56,6 +56,8 @@ pub fn ComptimeStringMap(comptime V: type, comptime kvs: anytype) type { | ... | @@ -56,6 +56,8 @@ pub fn ComptimeStringMap(comptime V: type, comptime kvs: anytype) type { |
| 56 | }; | 56 | }; |
| 57 | | 57 | |
| 58 | return struct { | 58 | return struct { |
| | 59 | pub const kvs = precomputed.sorted_kvs; |
| | 60 | |
| 59 | pub fn has(str: []const u8) bool { | 61 | pub fn has(str: []const u8) bool { |
| 60 | return get(str) != null; | 62 | return get(str) != null; |
| 61 | } | 63 | } |