Skip to content

Commit 9d8e84c

Browse files
author
Benjamin Feng
committed
Migrate most fmt.* references to fmtgen
1 parent 98e3d88 commit 9d8e84c

21 files changed

+81
-85
lines changed

doc/docgen.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
10481048
try out.write("<pre>");
10491049
try tokenizeAndPrint(tokenizer, out, code.source_token);
10501050
try out.write("</pre>");
1051-
const name_plus_ext = try std.fmt.allocPrint(allocator, "{}.zig", .{code.name});
1051+
const name_plus_ext = try std.fmtgen.allocPrint(allocator, "{}.zig", .{code.name});
10521052
const tmp_source_file_name = try fs.path.join(
10531053
allocator,
10541054
&[_][]const u8{ tmp_dir_name, name_plus_ext },
@@ -1057,7 +1057,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
10571057

10581058
switch (code.id) {
10591059
Code.Id.Exe => |expected_outcome| code_block: {
1060-
const name_plus_bin_ext = try std.fmt.allocPrint(allocator, "{}{}", .{ code.name, exe_ext });
1060+
const name_plus_bin_ext = try std.fmtgen.allocPrint(allocator, "{}{}", .{ code.name, exe_ext });
10611061
var build_args = std.ArrayList([]const u8).init(allocator);
10621062
defer build_args.deinit();
10631063
try build_args.appendSlice(&[_][]const u8{
@@ -1088,7 +1088,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
10881088
},
10891089
}
10901090
for (code.link_objects) |link_object| {
1091-
const name_with_ext = try std.fmt.allocPrint(allocator, "{}{}", .{ link_object, obj_ext });
1091+
const name_with_ext = try std.fmtgen.allocPrint(allocator, "{}{}", .{ link_object, obj_ext });
10921092
const full_path_object = try fs.path.join(
10931093
allocator,
10941094
&[_][]const u8{ tmp_dir_name, name_with_ext },
@@ -1353,15 +1353,15 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
13531353
});
13541354
},
13551355
Code.Id.Obj => |maybe_error_match| {
1356-
const name_plus_obj_ext = try std.fmt.allocPrint(allocator, "{}{}", .{ code.name, obj_ext });
1356+
const name_plus_obj_ext = try std.fmtgen.allocPrint(allocator, "{}{}", .{ code.name, obj_ext });
13571357
const tmp_obj_file_name = try fs.path.join(
13581358
allocator,
13591359
&[_][]const u8{ tmp_dir_name, name_plus_obj_ext },
13601360
);
13611361
var build_args = std.ArrayList([]const u8).init(allocator);
13621362
defer build_args.deinit();
13631363

1364-
const name_plus_h_ext = try std.fmt.allocPrint(allocator, "{}.h", .{code.name});
1364+
const name_plus_h_ext = try std.fmtgen.allocPrint(allocator, "{}.h", .{code.name});
13651365
const output_h_file_name = try fs.path.join(
13661366
allocator,
13671367
&[_][]const u8{ tmp_dir_name, name_plus_h_ext },

lib/std/atomic/queue.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ test "std.atomic.Queue dump" {
344344
sos.reset();
345345
try queue.dumpToStream(SliceOutStream.Error, &sos.stream);
346346

347-
var expected = try std.fmt.bufPrint(expected_buffer[0..],
347+
var expected = try std.fmtgen.bufPrint(expected_buffer[0..],
348348
\\head: 0x{x}=1
349349
\\ (null)
350350
\\tail: 0x{x}=1
@@ -364,7 +364,7 @@ test "std.atomic.Queue dump" {
364364
sos.reset();
365365
try queue.dumpToStream(SliceOutStream.Error, &sos.stream);
366366

367-
expected = try std.fmt.bufPrint(expected_buffer[0..],
367+
expected = try std.fmtgen.bufPrint(expected_buffer[0..],
368368
\\head: 0x{x}=1
369369
\\ 0x{x}=2
370370
\\ (null)

lib/std/buffer.zig

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,8 @@ pub const Buffer = struct {
6565
}
6666

6767
pub fn allocPrint(allocator: *Allocator, comptime format: []const u8, args: var) !Buffer {
68-
const countSize = struct {
69-
fn countSize(size: *usize, bytes: []const u8) (error{}!void) {
70-
size.* += bytes.len;
71-
}
72-
}.countSize;
73-
var size: usize = 0;
74-
std.fmt.format(&size, error{}, countSize, format, args) catch |err| switch (err) {};
75-
var self = try Buffer.initSize(allocator, size);
76-
assert((std.fmt.bufPrint(self.list.items, format, args) catch unreachable).len == size);
77-
return self;
68+
const slice = try std.fmtgen.allocPrint(allocator, format, args);
69+
return Buffer.fromOwnedSlice(allocator, slice);
7870
}
7971

8072
pub fn deinit(self: *Buffer) void {

lib/std/fifo.zig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,11 @@ pub fn LinearFifo(
294294
pub usingnamespace if (T == u8)
295295
struct {
296296
pub fn print(self: *Self, comptime format: []const u8, args: var) !void {
297-
return std.fmt.format(self, error{OutOfMemory}, Self.write, format, args);
297+
var generator = std.fmtgen.Generator([]const u8){};
298+
_ = async std.fmtgen.format(&generator, format, args);
299+
while (generator.next()) |bytes| {
300+
try self.write(bytes);
301+
}
298302
}
299303
}
300304
else

lib/std/fmtgen.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ pub fn formatType(
483483
if (ptr_info.child == u8) {
484484
if (fmt.len > 0 and fmt[0] == 's') {
485485
const len = mem.len(u8, value);
486-
return formatText(value[0..len], fmt, generator);
486+
return formatText(value[0..len], fmt, options, generator);
487487
}
488488
}
489489
return formatPtr(T.Child, @ptrToInt(value), generator);

lib/std/net.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ pub fn getAddressList(allocator: *mem.Allocator, name: []const u8, port: u16) !*
437437
const name_c = try std.cstr.addNullByte(allocator, name);
438438
defer allocator.free(name_c);
439439

440-
const port_c = try std.fmt.allocPrint(allocator, "{}\x00", .{port});
440+
const port_c = try std.fmtgen.allocPrint(allocator, "{}\x00", .{port});
441441
defer allocator.free(port_c);
442442

443443
const hints = os.addrinfo{

lib/std/os.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2605,7 +2605,7 @@ pub fn realpathC(pathname: [*:0]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealP
26052605
defer close(fd);
26062606

26072607
var procfs_buf: ["/proc/self/fd/-2147483648".len:0]u8 = undefined;
2608-
const proc_path = std.fmt.bufPrint(procfs_buf[0..], "/proc/self/fd/{}\x00", .{fd}) catch unreachable;
2608+
const proc_path = std.fmtgen.bufPrint(procfs_buf[0..], "/proc/self/fd/{}\x00", .{fd}) catch unreachable;
26092609

26102610
return readlinkC(@ptrCast([*:0]const u8, proc_path.ptr), out_buffer);
26112611
}

lib/std/progress.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ pub const Progress = struct {
130130
var end: usize = 0;
131131
if (self.columns_written > 0) {
132132
// restore cursor position
133-
end += (std.fmt.bufPrint(self.output_buffer[end..], "\x1b[{}D", .{self.columns_written}) catch unreachable).len;
133+
end += (std.fmtgen.bufPrint(self.output_buffer[end..], "\x1b[{}D", .{self.columns_written}) catch unreachable).len;
134134
self.columns_written = 0;
135135

136136
// clear rest of line
137-
end += (std.fmt.bufPrint(self.output_buffer[end..], "\x1b[0K", .{}) catch unreachable).len;
137+
end += (std.fmtgen.bufPrint(self.output_buffer[end..], "\x1b[0K", .{}) catch unreachable).len;
138138
}
139139

140140
if (!self.done) {
@@ -185,7 +185,7 @@ pub const Progress = struct {
185185
}
186186

187187
fn bufWrite(self: *Progress, end: *usize, comptime format: []const u8, args: var) void {
188-
if (std.fmt.bufPrint(self.output_buffer[end.*..], format, args)) |written| {
188+
if (std.fmtgen.bufPrint(self.output_buffer[end.*..], format, args)) |written| {
189189
const amt = written.len;
190190
end.* += amt;
191191
self.columns_written += amt;

lib/std/special/build_runner.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const root = @import("@build");
22
const std = @import("std");
33
const builtin = @import("builtin");
44
const io = std.io;
5-
const fmt = std.fmt;
5+
const fmtgen = std.fmtgen;
66
const Builder = std.build.Builder;
77
const mem = std.mem;
88
const process = std.process;
@@ -150,7 +150,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: var) !void {
150150
const allocator = builder.allocator;
151151
for (builder.top_level_steps.toSliceConst()) |top_level_step| {
152152
const name = if (&top_level_step.step == builder.default_step)
153-
try fmt.allocPrint(allocator, "{} (default)", .{top_level_step.step.name})
153+
try fmtgen.allocPrint(allocator, "{} (default)", .{top_level_step.step.name})
154154
else
155155
top_level_step.step.name;
156156
try out_stream.print(" {s:22} {}\n", .{ name, top_level_step.description });
@@ -172,7 +172,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: var) !void {
172172
try out_stream.print(" (none)\n", .{});
173173
} else {
174174
for (builder.available_options_list.toSliceConst()) |option| {
175-
const name = try fmt.allocPrint(allocator, " -D{}=[{}]", .{
175+
const name = try fmtgen.allocPrint(allocator, " -D{}=[{}]", .{
176176
option.name,
177177
Builder.typeIdName(option.type_id),
178178
});

lib/std/std.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub const elf = @import("elf.zig");
3939
pub const event = @import("event.zig");
4040
pub const fifo = @import("fifo.zig");
4141
pub const fmt = @import("fmt.zig");
42+
pub const fmtgen = @import("fmtgen.zig");
4243
pub const fs = @import("fs.zig");
4344
pub const hash = @import("hash.zig");
4445
pub const hash_map = @import("hash_map.zig");

lib/std/target.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ pub const Target = union(enum) {
321321
pub const stack_align = 16;
322322

323323
pub fn zigTriple(self: Target, allocator: *mem.Allocator) ![]u8 {
324-
return std.fmt.allocPrint(allocator, "{}{}-{}-{}", .{
324+
return std.fmtgen.allocPrint(allocator, "{}{}-{}-{}", .{
325325
@tagName(self.getArch()),
326326
Target.archSubArchName(self.getArch()),
327327
@tagName(self.getOs()),
@@ -370,15 +370,15 @@ pub const Target = union(enum) {
370370
}
371371

372372
pub fn zigTripleNoSubArch(self: Target, allocator: *mem.Allocator) ![]u8 {
373-
return std.fmt.allocPrint(allocator, "{}-{}-{}", .{
373+
return std.fmtgen.allocPrint(allocator, "{}-{}-{}", .{
374374
@tagName(self.getArch()),
375375
@tagName(self.getOs()),
376376
@tagName(self.getAbi()),
377377
});
378378
}
379379

380380
pub fn linuxTriple(self: Target, allocator: *mem.Allocator) ![]u8 {
381-
return std.fmt.allocPrint(allocator, "{}-{}-{}", .{
381+
return std.fmtgen.allocPrint(allocator, "{}-{}-{}", .{
382382
@tagName(self.getArch()),
383383
@tagName(self.getOs()),
384384
@tagName(self.getAbi()),

src-self-hosted/compilation.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ pub const Compilation = struct {
10491049
}
10501050

10511051
fn addCompileError(self: *Compilation, tree_scope: *Scope.AstTree, span: Span, comptime fmt: []const u8, args: var) !void {
1052-
const text = try std.fmt.allocPrint(self.gpa(), fmt, args);
1052+
const text = try std.fmtgen.allocPrint(self.gpa(), fmt, args);
10531053
errdefer self.gpa().free(text);
10541054

10551055
const msg = try Msg.createFromScope(self, tree_scope, span, text);
@@ -1059,7 +1059,7 @@ pub const Compilation = struct {
10591059
}
10601060

10611061
fn addCompileErrorCli(self: *Compilation, realpath: []const u8, comptime fmt: []const u8, args: var) !void {
1062-
const text = try std.fmt.allocPrint(self.gpa(), fmt, args);
1062+
const text = try std.fmtgen.allocPrint(self.gpa(), fmt, args);
10631063
errdefer self.gpa().free(text);
10641064

10651065
const msg = try Msg.createFromCli(self, realpath, text);
@@ -1152,7 +1152,7 @@ pub const Compilation = struct {
11521152
const tmp_dir = try self.getTmpDir();
11531153
const file_prefix = self.getRandomFileName();
11541154

1155-
const file_name = try std.fmt.allocPrint(self.gpa(), "{}{}", .{ file_prefix[0..], suffix });
1155+
const file_name = try std.fmtgen.allocPrint(self.gpa(), "{}{}", .{ file_prefix[0..], suffix });
11561156
defer self.gpa().free(file_name);
11571157

11581158
const full_path = try std.fs.path.join(self.gpa(), &[_][]const u8{ tmp_dir, file_name[0..] });

src-self-hosted/dep_tokenizer.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ fn printSection(out: var, label: []const u8, bytes: []const u8) !void {
894894

895895
fn printLabel(out: var, label: []const u8, bytes: []const u8) !void {
896896
var buf: [80]u8 = undefined;
897-
var text = try std.fmt.bufPrint(buf[0..], "{} {} bytes ", label, bytes.len);
897+
var text = try std.fmtgen.bufPrint(buf[0..], "{} {} bytes ", label, bytes.len);
898898
try out.write(text);
899899
var i: usize = text.len;
900900
const end = 79;

src-self-hosted/libc_installation.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ pub const LibCInstallation = struct {
393393
/// caller owns returned memory
394394
fn ccPrintFileName(allocator: *Allocator, o_file: []const u8, want_dirname: bool) ![]u8 {
395395
const cc_exe = std.os.getenv("CC") orelse "cc";
396-
const arg1 = try std.fmt.allocPrint(allocator, "-print-file-name={}", .{o_file});
396+
const arg1 = try std.fmtgen.allocPrint(allocator, "-print-file-name={}", .{o_file});
397397
defer allocator.free(arg1);
398398
const argv = [_][]const u8{ cc_exe, arg1 };
399399

src-self-hosted/link.zig

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,34 +293,34 @@ fn constructLinkerArgsCoff(ctx: *Context) !void {
293293

294294
const is_library = ctx.comp.kind == .Lib;
295295

296-
const out_arg = try std.fmt.allocPrint(&ctx.arena.allocator, "-OUT:{}\x00", .{ctx.out_file_path.toSliceConst()});
296+
const out_arg = try std.fmtgen.allocPrint(&ctx.arena.allocator, "-OUT:{}\x00", .{ctx.out_file_path.toSliceConst()});
297297
try ctx.args.append(@ptrCast([*:0]const u8, out_arg.ptr));
298298

299299
if (ctx.comp.haveLibC()) {
300-
try ctx.args.append(@ptrCast([*:0]const u8, (try std.fmt.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", .{ctx.libc.msvc_lib_dir.?})).ptr));
301-
try ctx.args.append(@ptrCast([*:0]const u8, (try std.fmt.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", .{ctx.libc.kernel32_lib_dir.?})).ptr));
302-
try ctx.args.append(@ptrCast([*:0]const u8, (try std.fmt.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", .{ctx.libc.lib_dir.?})).ptr));
300+
try ctx.args.append(@ptrCast([*:0]const u8, (try std.fmtgen.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", .{ctx.libc.msvc_lib_dir.?})).ptr));
301+
try ctx.args.append(@ptrCast([*:0]const u8, (try std.fmtgen.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", .{ctx.libc.kernel32_lib_dir.?})).ptr));
302+
try ctx.args.append(@ptrCast([*:0]const u8, (try std.fmtgen.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", .{ctx.libc.lib_dir.?})).ptr));
303303
}
304304

305305
if (ctx.link_in_crt) {
306306
const lib_str = if (ctx.comp.is_static) "lib" else "";
307307
const d_str = if (ctx.comp.build_mode == .Debug) "d" else "";
308308

309309
if (ctx.comp.is_static) {
310-
const cmt_lib_name = try std.fmt.allocPrint(&ctx.arena.allocator, "libcmt{}.lib\x00", .{d_str});
310+
const cmt_lib_name = try std.fmtgen.allocPrint(&ctx.arena.allocator, "libcmt{}.lib\x00", .{d_str});
311311
try ctx.args.append(@ptrCast([*:0]const u8, cmt_lib_name.ptr));
312312
} else {
313-
const msvcrt_lib_name = try std.fmt.allocPrint(&ctx.arena.allocator, "msvcrt{}.lib\x00", .{d_str});
313+
const msvcrt_lib_name = try std.fmtgen.allocPrint(&ctx.arena.allocator, "msvcrt{}.lib\x00", .{d_str});
314314
try ctx.args.append(@ptrCast([*:0]const u8, msvcrt_lib_name.ptr));
315315
}
316316

317-
const vcruntime_lib_name = try std.fmt.allocPrint(&ctx.arena.allocator, "{}vcruntime{}.lib\x00", .{
317+
const vcruntime_lib_name = try std.fmtgen.allocPrint(&ctx.arena.allocator, "{}vcruntime{}.lib\x00", .{
318318
lib_str,
319319
d_str,
320320
});
321321
try ctx.args.append(@ptrCast([*:0]const u8, vcruntime_lib_name.ptr));
322322

323-
const crt_lib_name = try std.fmt.allocPrint(&ctx.arena.allocator, "{}ucrt{}.lib\x00", .{ lib_str, d_str });
323+
const crt_lib_name = try std.fmtgen.allocPrint(&ctx.arena.allocator, "{}ucrt{}.lib\x00", .{ lib_str, d_str });
324324
try ctx.args.append(@ptrCast([*:0]const u8, crt_lib_name.ptr));
325325

326326
// Visual C++ 2015 Conformance Changes
@@ -380,7 +380,7 @@ fn constructLinkerArgsMachO(ctx: *Context) !void {
380380
.IPhoneOS => try ctx.args.append("-iphoneos_version_min"),
381381
.IPhoneOSSimulator => try ctx.args.append("-ios_simulator_version_min"),
382382
}
383-
const ver_str = try std.fmt.allocPrint(&ctx.arena.allocator, "{}.{}.{}\x00", .{
383+
const ver_str = try std.fmtgen.allocPrint(&ctx.arena.allocator, "{}.{}.{}\x00", .{
384384
platform.major,
385385
platform.minor,
386386
platform.micro,
@@ -442,7 +442,7 @@ fn constructLinkerArgsMachO(ctx: *Context) !void {
442442
try ctx.args.append("-lSystem");
443443
} else {
444444
if (mem.indexOfScalar(u8, lib.name, '/') == null) {
445-
const arg = try std.fmt.allocPrint(&ctx.arena.allocator, "-l{}\x00", .{lib.name});
445+
const arg = try std.fmtgen.allocPrint(&ctx.arena.allocator, "-l{}\x00", .{lib.name});
446446
try ctx.args.append(@ptrCast([*:0]const u8, arg.ptr));
447447
} else {
448448
const arg = try std.cstr.addNullByte(&ctx.arena.allocator, lib.name);

src-self-hosted/test.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub const TestContext = struct {
8181
msg: []const u8,
8282
) !void {
8383
var file_index_buf: [20]u8 = undefined;
84-
const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.incr());
84+
const file_index = try std.fmtgen.bufPrint(file_index_buf[0..], "{}", self.file_index.incr());
8585
const file1_path = try std.fs.path.join(allocator, [_][]const u8{ tmp_dir_name, file_index, file1 });
8686

8787
if (std.fs.path.dirname(file1_path)) |dirname| {
@@ -114,10 +114,10 @@ pub const TestContext = struct {
114114
expected_output: []const u8,
115115
) !void {
116116
var file_index_buf: [20]u8 = undefined;
117-
const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.incr());
117+
const file_index = try std.fmtgen.bufPrint(file_index_buf[0..], "{}", self.file_index.incr());
118118
const file1_path = try std.fs.path.join(allocator, [_][]const u8{ tmp_dir_name, file_index, file1 });
119119

120-
const output_file = try std.fmt.allocPrint(allocator, "{}-out{}", file1_path, (Target{ .Native = {} }).exeFileExt());
120+
const output_file = try std.fmtgen.allocPrint(allocator, "{}-out{}", file1_path, (Target{ .Native = {} }).exeFileExt());
121121
if (std.fs.path.dirname(file1_path)) |dirname| {
122122
try std.fs.makePath(allocator, dirname);
123123
}

0 commit comments

Comments
 (0)