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!
167167 comptime assert(fmt[i] == '}');
168168 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);
171172 const arg_pos = comptime switch (placeholder.arg) {
172173 .none => null,
173174 .number => |pos| pos,
......@@ -231,10 +232,6 @@ pub fn deprecatedFormat(writer: anytype, comptime fmt: []const u8, args: anytype
231232 };
232233}
233234
234fn cacheString(str: anytype) []const u8 {
235 return &str;
236}
237
238235pub const Placeholder = struct {
239236 specifier_arg: []const u8,
240237 fill: u8,
......@@ -243,7 +240,7 @@ pub const Placeholder = struct {
243240 width: Specifier,
244241 precision: Specifier,
245242
246 pub fn parse(bytes: []const u8) Placeholder {
243 pub fn parse(comptime bytes: []const u8) Placeholder {
247244 var parser: Parser = .{ .bytes = bytes, .i = 0 };
248245 const arg = parser.specifier() catch |err| @compileError(@errorName(err));
249246 const specifier_arg = parser.until(':');
......@@ -298,8 +295,10 @@ pub const Placeholder = struct {
298295
299296 if (parser.char()) |b| @compileError("extraneous trailing character '" ++ &[1]u8{b} ++ "'");
300297
298 const specifier_array = specifier_arg[0..specifier_arg.len].*;
299
301300 return .{
302 .specifier_arg = cacheString(specifier_arg[0..specifier_arg.len].*),
301 .specifier_arg = &specifier_array,
303302 .fill = fill orelse default_fill_char,
304303 .alignment = alignment orelse default_alignment,
305304 .arg = arg,