diff options
| author | Ryan Liptak <squeek502@hotmail.com> | 2020-05-26 21:56:06 -0700 |
|---|---|---|
| committer | Ryan Liptak <squeek502@hotmail.com> | 2020-05-26 23:10:12 -0700 |
| commit | dfafafac7b701feb154e28981e77aa6f66624e8f (patch) | |
| tree | d2fec88c546003cbc9a5e82757fdfe12929d4664 /lib/std/comptime_string_map.zig | |
| parent | 62cfc68d2fd3c31ec7f206bc00631ce2a59b882d (diff) | |
| download | zig-dfafafac7b701feb154e28981e77aa6f66624e8f.tar.gz zig-dfafafac7b701feb154e28981e77aa6f66624e8f.zip | |
std.ComptimeStringMap: Add support for void value type (i.e. a set)
Diffstat (limited to 'lib/std/comptime_string_map.zig')
| -rw-r--r-- | lib/std/comptime_string_map.zig | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/lib/std/comptime_string_map.zig b/lib/std/comptime_string_map.zig index b1e5513a3b..313f1fcdda 100644 --- a/lib/std/comptime_string_map.zig +++ b/lib/std/comptime_string_map.zig @@ -22,7 +22,11 @@ pub fn ComptimeStringMap(comptime V: type, comptime kvs: var) type { } }).lenAsc; for (kvs) |kv, i| { - sorted_kvs[i] = .{.key = kv.@"0", .value = kv.@"1"}; + if (V != void) { + sorted_kvs[i] = .{.key = kv.@"0", .value = kv.@"1"}; + } else { + sorted_kvs[i] = .{.key = kv.@"0", .value = {}}; + } } std.sort.sort(KV, &sorted_kvs, lenAsc); const min_len = sorted_kvs[0].key.len; @@ -132,3 +136,42 @@ fn testMap(comptime map: var) void { std.testing.expect(!map.has("missing")); std.testing.expect(map.has("these")); } + +test "ComptimeStringMap void value type, slice of structs" { + const KV = struct { + @"0": []const u8, + }; + const slice: []const KV = &[_]KV{ + .{.@"0" = "these"}, + .{.@"0" = "have"}, + .{.@"0" = "nothing"}, + .{.@"0" = "incommon"}, + .{.@"0" = "samelen"}, + }; + const map = ComptimeStringMap(void, slice); + + testSet(map); +} + +test "ComptimeStringMap void value type, list literal of list literals" { + const map = ComptimeStringMap(void, .{ + .{"these"}, + .{"have"}, + .{"nothing"}, + .{"incommon"}, + .{"samelen"}, + }); + + testSet(map); +} + +fn testSet(comptime map: var) void { + std.testing.expectEqual({}, map.get("have").?); + std.testing.expectEqual({}, map.get("nothing").?); + std.testing.expect(null == map.get("missing")); + std.testing.expectEqual({}, map.get("these").?); + std.testing.expectEqual({}, map.get("samelen").?); + + std.testing.expect(!map.has("missing")); + std.testing.expect(map.has("these")); +} |
