enum String [src]

Fields

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

Members

Source

pub const String = enum(u32) { none = std.math.maxInt(u31), empty, _, pub fn isAnon(self: String) bool { assert(self != .none); return self.toIndex() == null; } pub fn slice(self: String, builder: *const Builder) ?[]const u8 { const index = self.toIndex() orelse return null; const start = builder.string_indices.items[index]; const end = builder.string_indices.items[index + 1]; return builder.string_bytes.items[start..end]; } const FormatData = struct { string: String, 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: String, builder: *const Builder) std.fmt.Alt(FormatData, format) { return .{ .data = .{ .string = self, .builder = builder, .quote_behavior = .quote_unless_valid_identifier, } }; } pub fn fmtQ(self: String, builder: *const Builder) std.fmt.Alt(FormatData, format) { return .{ .data = .{ .string = self, .builder = builder, .quote_behavior = .always_quote, } }; } pub fn fmtRaw(self: String, builder: *const Builder) std.fmt.Alt(FormatData, format) { return .{ .data = .{ .string = self, .builder = builder, .quote_behavior = null, } }; } fn fromIndex(index: ?usize) String { return @enumFromInt(@as(u32, @intCast((index orelse return .none) + @intFromEnum(String.empty)))); } fn toIndex(self: String) ?usize { return std.math.sub(u32, @intFromEnum(self), @intFromEnum(String.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, String.fromIndex(rhs_index).slice(ctx.builder).?); } }; }