enumstd.zig.llvm.Builder.StrtabString[src]

Fields

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

Functions

FunctionisAnon[src]

pub fn isAnon(self: StrtabString) bool

Parameters

Source Code

Source code
pub fn isAnon(self: StrtabString) bool {
    assert(self != .none);
    return self.toIndex() == null;
}

Functionslice[src]

pub fn slice(self: StrtabString, builder: *const Builder) ?[]const u8

Parameters

builder: *const Builder

Source Code

Source code
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];
}

Functionfmt[src]

pub fn fmt(self: StrtabString, builder: *const Builder) std.fmt.Formatter(format)

Parameters

builder: *const Builder

Source Code

Source code
pub fn fmt(self: StrtabString, builder: *const Builder) std.fmt.Formatter(format) {
    return .{ .data = .{ .string = self, .builder = builder } };
}

Source Code

Source code
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,
    };
    fn format(
        data: FormatData,
        comptime fmt_str: []const u8,
        _: std.fmt.FormatOptions,
        writer: anytype,
    ) @TypeOf(writer).Error!void {
        if (comptime std.mem.indexOfNone(u8, fmt_str, "\"r")) |_|
            @compileError("invalid format string: '" ++ fmt_str ++ "'");
        assert(data.string != .none);
        const string_slice = data.string.slice(data.builder) orelse
            return writer.print("{d}", .{@intFromEnum(data.string)});
        if (comptime std.mem.indexOfScalar(u8, fmt_str, 'r')) |_|
            return writer.writeAll(string_slice);
        try printEscapedString(
            string_slice,
            if (comptime std.mem.indexOfScalar(u8, fmt_str, '"')) |_|
                .always_quote
            else
                .quote_unless_valid_identifier,
            writer,
        );
    }
    pub fn fmt(self: StrtabString, builder: *const Builder) std.fmt.Formatter(format) {
        return .{ .data = .{ .string = self, .builder = builder } };
    }

    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).?);
        }
    };
}