authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2020-05-26 21:56:06-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2020-05-26 23:10:12-07:00
logdfafafac7b701feb154e28981e77aa6f66624e8f
treed2fec88c546003cbc9a5e82757fdfe12929d4664
parent62cfc68d2fd3c31ec7f206bc00631ce2a59b882d

std.ComptimeStringMap: Add support for void value type (i.e. a set)


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

lib/std/comptime_string_map.zig+44-1
...@@ -22,7 +22,11 @@ pub fn ComptimeStringMap(comptime V: type, comptime kvs: var) type {...@@ -22,7 +22,11 @@ pub fn ComptimeStringMap(comptime V: type, comptime kvs: var) type {
22 }22 }
23 }).lenAsc;23 }).lenAsc;
24 for (kvs) |kv, i| {24 for (kvs) |kv, i| {
25 sorted_kvs[i] = .{.key = kv.@"0", .value = kv.@"1"};25 if (V != void) {
26 sorted_kvs[i] = .{.key = kv.@"0", .value = kv.@"1"};
27 } else {
28 sorted_kvs[i] = .{.key = kv.@"0", .value = {}};
29 }
26 }30 }
27 std.sort.sort(KV, &sorted_kvs, lenAsc);31 std.sort.sort(KV, &sorted_kvs, lenAsc);
28 const min_len = sorted_kvs[0].key.len;32 const min_len = sorted_kvs[0].key.len;
...@@ -132,3 +136,42 @@ fn testMap(comptime map: var) void {...@@ -132,3 +136,42 @@ fn testMap(comptime map: var) void {
132 std.testing.expect(!map.has("missing"));136 std.testing.expect(!map.has("missing"));
133 std.testing.expect(map.has("these"));137 std.testing.expect(map.has("these"));
134}138}
139
140test "ComptimeStringMap void value type, slice of structs" {
141 const KV = struct {
142 @"0": []const u8,
143 };
144 const slice: []const KV = &[_]KV{
145 .{.@"0" = "these"},
146 .{.@"0" = "have"},
147 .{.@"0" = "nothing"},
148 .{.@"0" = "incommon"},
149 .{.@"0" = "samelen"},
150 };
151 const map = ComptimeStringMap(void, slice);
152
153 testSet(map);
154}
155
156test "ComptimeStringMap void value type, list literal of list literals" {
157 const map = ComptimeStringMap(void, .{
158 .{"these"},
159 .{"have"},
160 .{"nothing"},
161 .{"incommon"},
162 .{"samelen"},
163 });
164
165 testSet(map);
166}
167
168fn testSet(comptime map: var) void {
169 std.testing.expectEqual({}, map.get("have").?);
170 std.testing.expectEqual({}, map.get("nothing").?);
171 std.testing.expect(null == map.get("missing"));
172 std.testing.expectEqual({}, map.get("these").?);
173 std.testing.expectEqual({}, map.get("samelen").?);
174
175 std.testing.expect(!map.has("missing"));
176 std.testing.expect(map.has("these"));
177}