zig/src-self-hosted/package.zig
Andrew Kelley 2e806682f4
(breaking) std.Buffer => std.ArrayListSentineled(u8, 0)
This new name (and the fact that it is a function returning a type) will
make it more clear which use cases are better suited for ArrayList and
which are better suited for ArrayListSentineled.

Also for consistency with ArrayList,
 * `append` => `appendSlice`
 * `appendByte` => `append`

Thanks daurnimator for pointing out the confusion of std.Buffer.
2020-04-01 13:30:07 -04:00

32 lines
1.1 KiB
Zig

const std = @import("std");
const mem = std.mem;
const assert = std.debug.assert;
const ArrayListSentineled = std.ArrayListSentineled;
pub const Package = struct {
root_src_dir: ArrayListSentineled(u8, 0),
root_src_path: ArrayListSentineled(u8, 0),
/// relative to root_src_dir
table: Table,
pub const Table = std.StringHashMap(*Package);
/// makes internal copies of root_src_dir and root_src_path
/// allocator should be an arena allocator because Package never frees anything
pub fn create(allocator: *mem.Allocator, root_src_dir: []const u8, root_src_path: []const u8) !*Package {
const ptr = try allocator.create(Package);
ptr.* = Package{
.root_src_dir = try ArrayListSentineled(u8, 0).init(allocator, root_src_dir),
.root_src_path = try ArrayListSentineled(u8, 0).init(allocator, root_src_path),
.table = Table.init(allocator),
};
return ptr;
}
pub fn add(self: *Package, name: []const u8, package: *Package) !void {
const entry = try self.table.put(try mem.dupe(self.table.allocator, u8, name), package);
assert(entry == null);
}
};