Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

StrtabString

Builder.StrtabString
pub const StrtabString = enum(u32)

File

lib/std/zig/llvm/Builder.zig:2222

Code

pub const StrtabString = enum(u32) {
    none = 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}", .{@backingInt(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 @fromBackingInt(@intCast(@as(u32, @intCast((index orelse return .none) +
            @backingInt(StrtabString.empty)))));
    }

    fn toIndex(self: StrtabString) ?usize {
        return std.math.sub(u32, @backingInt(self), @backingInt(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).?);
        }
    };
}