authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-08 11:07:23-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-08 11:07:23-07:00
log1dcb20d27e024660d8c75e427cf5e251e3833cac
treeee6496b0873f1c0959d64ea1bd3fc486bbdd1133
parentfc335a3a48f8f4929bc336f02873a65e32c3da86

std.fmt: refactor to remove cacheString

when this kind of trick is needed, do it inline

1 files changed, 6 insertions(+), 7 deletions(-)

lib/std/fmt.zig+6-7
...@@ -167,7 +167,8 @@ pub fn format(w: *Writer, comptime fmt: []const u8, args: anytype) Writer.Error!...@@ -167,7 +167,8 @@ pub fn format(w: *Writer, comptime fmt: []const u8, args: anytype) Writer.Error!
167 comptime assert(fmt[i] == '}');167 comptime assert(fmt[i] == '}');
168 i += 1;168 i += 1;
169169
170 const placeholder = comptime Placeholder.parse(&(fmt[fmt_begin..fmt_end].*));170 const placeholder_array = fmt[fmt_begin..fmt_end].*;
171 const placeholder = comptime Placeholder.parse(&placeholder_array);
171 const arg_pos = comptime switch (placeholder.arg) {172 const arg_pos = comptime switch (placeholder.arg) {
172 .none => null,173 .none => null,
173 .number => |pos| pos,174 .number => |pos| pos,
...@@ -231,10 +232,6 @@ pub fn deprecatedFormat(writer: anytype, comptime fmt: []const u8, args: anytype...@@ -231,10 +232,6 @@ pub fn deprecatedFormat(writer: anytype, comptime fmt: []const u8, args: anytype
231 };232 };
232}233}
233234
234fn cacheString(str: anytype) []const u8 {
235 return &str;
236}
237
238pub const Placeholder = struct {235pub const Placeholder = struct {
239 specifier_arg: []const u8,236 specifier_arg: []const u8,
240 fill: u8,237 fill: u8,
...@@ -243,7 +240,7 @@ pub const Placeholder = struct {...@@ -243,7 +240,7 @@ pub const Placeholder = struct {
243 width: Specifier,240 width: Specifier,
244 precision: Specifier,241 precision: Specifier,
245242
246 pub fn parse(bytes: []const u8) Placeholder {243 pub fn parse(comptime bytes: []const u8) Placeholder {
247 var parser: Parser = .{ .bytes = bytes, .i = 0 };244 var parser: Parser = .{ .bytes = bytes, .i = 0 };
248 const arg = parser.specifier() catch |err| @compileError(@errorName(err));245 const arg = parser.specifier() catch |err| @compileError(@errorName(err));
249 const specifier_arg = parser.until(':');246 const specifier_arg = parser.until(':');
...@@ -298,8 +295,10 @@ pub const Placeholder = struct {...@@ -298,8 +295,10 @@ pub const Placeholder = struct {
298295
299 if (parser.char()) |b| @compileError("extraneous trailing character '" ++ &[1]u8{b} ++ "'");296 if (parser.char()) |b| @compileError("extraneous trailing character '" ++ &[1]u8{b} ++ "'");
300297
298 const specifier_array = specifier_arg[0..specifier_arg.len].*;
299
301 return .{300 return .{
302 .specifier_arg = cacheString(specifier_arg[0..specifier_arg.len].*),301 .specifier_arg = &specifier_array,
303 .fill = fill orelse default_fill_char,302 .fill = fill orelse default_fill_char,
304 .alignment = alignment orelse default_alignment,303 .alignment = alignment orelse default_alignment,
305 .arg = arg,304 .arg = arg,