enum StrtabString [src]

Fields

none = std.math.maxInt(u31)
empty
_

Members

Source

pub const StrtabString = enum(u32) { none = std.math.maxInt(u31), empty, _, pub fn isAnon(self: StrtabString) bool { assert(self != .none); return self.toIndex() == null; } pub fn slice(self: StrtabString, builder: *const Builder) ?[]const u8 { const index = self.toIndex() orelse return null; const start = builder.strtab_string_indices.items[index]; const end = builder.strtab_string_indices.items[index + 1]; return builder.strtab_string_bytes.items[start..end]; } const FormatData = struct { string: StrtabString, builder: *const Builder, quote_behavior: ?QuoteBehavior, }; fn format(data: FormatData, w: *Writer) Writer.Error!void { assert(data.string != .none); const string_slice = data.string.slice(data.builder) orelse return w.print("{d}", .{@intFromEnum(data.string)}); const quote_behavior = data.quote_behavior orelse return w.writeAll(string_slice); return printEscapedString(string_slice, quote_behavior, w); } pub fn fmt( self: StrtabString, builder: *const Builder, quote_behavior: ?QuoteBehavior, ) std.fmt.Alt(FormatData, format) { return .{ .data = .{ .string = self, .builder = builder, .quote_behavior = quote_behavior, } }; } fn fromIndex(index: ?usize) StrtabString { return @enumFromInt(@as(u32, @intCast((index orelse return .none) + @intFromEnum(StrtabString.empty)))); } fn toIndex(self: StrtabString) ?usize { return std.math.sub(u32, @intFromEnum(self), @intFromEnum(StrtabString.empty)) catch null; } const Adapter = struct { builder: *const Builder, pub fn hash(_: Adapter, key: []const u8) u32 { return @truncate(std.hash.Wyhash.hash(0, key)); } pub fn eql(ctx: Adapter, lhs_key: []const u8, _: void, rhs_index: usize) bool { return std.mem.eql(u8, lhs_key, StrtabString.fromIndex(rhs_index).slice(ctx.builder).?); } }; }