|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +const String = @import("../../string.zig").String; |
| 4 | + |
| 5 | +const js = @import("../js/js.zig"); |
| 6 | +const Page = @import("../Page.zig"); |
| 7 | + |
| 8 | +const Allocator = std.mem.Allocator; |
| 9 | + |
| 10 | +pub fn registerTypes() []const type { |
| 11 | + return &.{ |
| 12 | + KeyIterator, |
| 13 | + ValueIterator, |
| 14 | + EntryIterator, |
| 15 | + }; |
| 16 | +} |
| 17 | + |
| 18 | +pub const KeyValueList = @This(); |
| 19 | + |
| 20 | +_entries: std.ArrayListUnmanaged(Entry) = .empty, |
| 21 | + |
| 22 | +pub const empty: KeyValueList = .{ |
| 23 | + ._entries = .empty, |
| 24 | +}; |
| 25 | + |
| 26 | +pub const Entry = struct { |
| 27 | + name: String, |
| 28 | + value: String, |
| 29 | +}; |
| 30 | + |
| 31 | +pub fn init() KeyValueList { |
| 32 | + return .{}; |
| 33 | +} |
| 34 | + |
| 35 | +pub fn ensureTotalCapacity(self: *KeyValueList, allocator: Allocator, n: usize) !void { |
| 36 | + return self._entries.ensureTotalCapacity(allocator, n); |
| 37 | +} |
| 38 | + |
| 39 | +pub fn get(self: *const KeyValueList, name: []const u8) ?[]const u8 { |
| 40 | + for (self._entries.items) |*entry| { |
| 41 | + if (entry.name.eqlSlice(name)) { |
| 42 | + return entry.value.str(); |
| 43 | + } |
| 44 | + } |
| 45 | + return null; |
| 46 | +} |
| 47 | + |
| 48 | +pub fn getAll(self: *const KeyValueList, name: []const u8, page: *Page) ![]const []const u8 { |
| 49 | + const arena = page.call_arena; |
| 50 | + var arr: std.ArrayList([]const u8) = .empty; |
| 51 | + for (self._entries.items) |*entry| { |
| 52 | + if (entry.name.eqlSlice(name)) { |
| 53 | + try arr.append(arena, entry.value.str()); |
| 54 | + } |
| 55 | + } |
| 56 | + return arr.items; |
| 57 | +} |
| 58 | + |
| 59 | +pub fn has(self: *const KeyValueList, name: []const u8) bool { |
| 60 | + for (self._entries.items) |*entry| { |
| 61 | + if (entry.name.eqlSlice(name)) { |
| 62 | + return true; |
| 63 | + } |
| 64 | + } |
| 65 | + return false; |
| 66 | +} |
| 67 | + |
| 68 | +pub fn append(self: *KeyValueList, allocator: Allocator, name: []const u8, value: []const u8) !void { |
| 69 | + try self._entries.append(allocator, .{ |
| 70 | + .name = try String.init(allocator, name, .{}), |
| 71 | + .value = try String.init(allocator, value, .{}), |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +pub fn appendAssumeCapacity(self: *KeyValueList, allocator: Allocator, name: []const u8, value: []const u8) !void { |
| 76 | + self._entries.appendAssumeCapacity(.{ |
| 77 | + .name = try String.init(allocator, name, .{}), |
| 78 | + .value = try String.init(allocator, value, .{}), |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +pub fn delete(self: *KeyValueList, name: []const u8, value: ?[]const u8) void { |
| 83 | + var i: usize = 0; |
| 84 | + while (i < self._entries.items.len) { |
| 85 | + const entry = self._entries.items[i]; |
| 86 | + if (entry.name.eqlSlice(name)) { |
| 87 | + if (value == null or entry.value.eqlSlice(value.?)) { |
| 88 | + _ = self._entries.swapRemove(i); |
| 89 | + continue; |
| 90 | + } |
| 91 | + } |
| 92 | + i += 1; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +pub fn set(self: *KeyValueList, allocator: Allocator, name: []const u8, value: []const u8) !void { |
| 97 | + self.delete(name, null); |
| 98 | + try self.append(allocator, name, value); |
| 99 | +} |
| 100 | + |
| 101 | +pub fn len(self: *const KeyValueList) usize { |
| 102 | + return self._entries.items.len; |
| 103 | +} |
| 104 | + |
| 105 | +pub fn items(self: *const KeyValueList) []const Entry { |
| 106 | + return self._entries.items; |
| 107 | +} |
| 108 | + |
| 109 | +pub const Iterator = struct { |
| 110 | + index: u32 = 0, |
| 111 | + kv: *KeyValueList, |
| 112 | + |
| 113 | + // Why? Because whenever an Iterator is created, we need to increment the |
| 114 | + // RC of what it's iterating. And when the iterator is destroyed, we need |
| 115 | + // to decrement it. The generic iterator which will wrap this handles that |
| 116 | + // by using this "list" field. Most things that use the GenericIterator can |
| 117 | + // just set `list: *ZigCollection`, and everything will work. But KeyValueList |
| 118 | + // is being composed by various types, so it can't reference those types. |
| 119 | + // Using *anyopaque here is "dangerous", in that it requires the composer |
| 120 | + // to pass the right value, which normally would be itself (`*Self`), but |
| 121 | + // only because (as of now) everyting that uses KeyValueList has no prototype |
| 122 | + list: *anyopaque, |
| 123 | + |
| 124 | + pub const Entry = struct { []const u8, []const u8 }; |
| 125 | + |
| 126 | + pub fn next(self: *Iterator, _: *const Page) ?Iterator.Entry { |
| 127 | + const index = self.index; |
| 128 | + const entries = self.kv._entries.items; |
| 129 | + if (index >= entries.len) { |
| 130 | + return null; |
| 131 | + } |
| 132 | + self.index = index + 1; |
| 133 | + |
| 134 | + const e = &entries[index]; |
| 135 | + return .{ e.name.str(), e.value.str() }; |
| 136 | + } |
| 137 | +}; |
| 138 | + |
| 139 | +pub fn iterator(self: *const KeyValueList) Iterator { |
| 140 | + return .{ .list = self }; |
| 141 | +} |
| 142 | + |
| 143 | +const GenericIterator = @import("collections/iterator.zig").Entry; |
| 144 | +pub const KeyIterator = GenericIterator(Iterator, "0"); |
| 145 | +pub const ValueIterator = GenericIterator(Iterator, "1"); |
| 146 | +pub const EntryIterator = GenericIterator(Iterator, null); |
0 commit comments