authorgravatar for quail@cursedquail.comA cursed quail <quail@cursedquail.com> 2025-07-26 10:32:17-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-26 21:53:23-07:00
loge12dc4947c411d25c4c56d887e62d0e2b9addcb8
treee73d52a1bac71cbaa710951f6c04caa3dde4e457
parent04614d6ea17fff69ead42223c35a257da25462de

std.zig: fmtId returns a FormatId

Changes fmtId to return the FormatId type directly, and renames the FormatId.render function to FormatId.format, so it can be used in a format expression directly. Why? Since `render` is private, you can't create functions that wrap `fmtId` or `fmtIdFlags`, since you can't name the return type of those functions outside of std itself. The current setup _might_ be intentional? In which case I can live with it, but I figured I'd make a small contrib to upstream zig :)

1 files changed, 9 insertions(+), 9 deletions(-)

lib/std/zig.zig+9-9
......@@ -385,23 +385,23 @@ pub fn serializeCpuAlloc(ally: Allocator, cpu: std.Target.Cpu) Allocator.Error![
385385/// Return a Formatter for a Zig identifier, escaping it with `@""` syntax if needed.
386386///
387387/// See also `fmtIdFlags`.
388pub fn fmtId(bytes: []const u8) std.fmt.Formatter(FormatId, FormatId.render) {
389 return .{ .data = .{ .bytes = bytes, .flags = .{} } };
388pub fn fmtId(bytes: []const u8) FormatId {
389 return .{ .bytes = bytes, .flags = .{} };
390390}
391391
392392/// Return a Formatter for a Zig identifier, escaping it with `@""` syntax if needed.
393393///
394394/// See also `fmtId`.
395pub fn fmtIdFlags(bytes: []const u8, flags: FormatId.Flags) std.fmt.Formatter(FormatId, FormatId.render) {
396 return .{ .data = .{ .bytes = bytes, .flags = flags } };
395pub fn fmtIdFlags(bytes: []const u8, flags: FormatId.Flags) FormatId {
396 return .{ .bytes = bytes, .flags = flags };
397397}
398398
399pub fn fmtIdPU(bytes: []const u8) std.fmt.Formatter(FormatId, FormatId.render) {
400 return .{ .data = .{ .bytes = bytes, .flags = .{ .allow_primitive = true, .allow_underscore = true } } };
399pub fn fmtIdPU(bytes: []const u8) FormatId {
400 return .{ .bytes = bytes, .flags = .{ .allow_primitive = true, .allow_underscore = true } };
401401}
402402
403pub fn fmtIdP(bytes: []const u8) std.fmt.Formatter(FormatId, FormatId.render) {
404 return .{ .data = .{ .bytes = bytes, .flags = .{ .allow_primitive = true } } };
403pub fn fmtIdP(bytes: []const u8) FormatId {
404 return .{ .bytes = bytes, .flags = .{ .allow_primitive = true } };
405405}
406406
407407test fmtId {
......@@ -447,7 +447,7 @@ pub const FormatId = struct {
447447 };
448448
449449 /// Print the string as a Zig identifier, escaping it with `@""` syntax if needed.
450 fn render(ctx: FormatId, writer: *Writer) Writer.Error!void {
450 pub fn format(ctx: FormatId, writer: *Writer) Writer.Error!void {
451451 const bytes = ctx.bytes;
452452 if (isValidId(bytes) and
453453 (ctx.flags.allow_primitive or !std.zig.isPrimitive(bytes)) and