authorgravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-02-29 15:35:18-06:00
committergravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-03-12 10:26:28-05:00
logc11d1055b868add74cc58f3bf745e93110ae6071
tree4af26a48d8fa267a0d99cd06792e423708dadd21
parent7364e965f473147a86cc62ccf47feac4878a15de

Integrated outstreams with new formatter


6 files changed, 27 insertions(+), 37 deletions(-)

lib/std/http/headers.zig+8-10
......@@ -349,16 +349,14 @@ pub const Headers = struct {
349349 pub fn format(
350350 self: Self,
351351 comptime fmt: []const u8,
352 options: std.fmt.FormatOptions,
353 context: var,
354 comptime Errors: type,
355 output: fn (@TypeOf(context), []const u8) Errors!void,
356 ) Errors!void {
352 options: std.fmtstream.FormatOptions,
353 out_stream: var,
354 ) !void {
357355 for (self.toSlice()) |entry| {
358 try output(context, entry.name);
359 try output(context, ": ");
360 try output(context, entry.value);
361 try output(context, "\n");
356 try out_stream.writeAll(entry.name);
357 try out_stream.writeAll(": ");
358 try out_stream.writeAll(entry.value);
359 try out_stream.writeAll("\n");
362360 }
363361 }
364362};
......@@ -593,5 +591,5 @@ test "Headers.format" {
593591 \\foo: bar
594592 \\cookie: somevalue
595593 \\
596 , try std.fmt.bufPrint(buf[0..], "{}", .{h}));
594 , try std.fmtstream.bufPrint(buf[0..], "{}", .{h}));
597595}
lib/std/io/out_stream.zig+1-1
......@@ -25,7 +25,7 @@ pub fn OutStream(
2525 }
2626
2727 pub fn print(self: Self, comptime format: []const u8, args: var) Error!void {
28 return std.fmt.format(self, Error, writeAll, format, args);
28 return std.fmtstream.format(self, format, args);
2929 }
3030
3131 pub fn writeByte(self: Self, byte: u8) Error!void {
lib/std/math/big/int.zig+3-5
......@@ -518,17 +518,15 @@ pub const Int = struct {
518518 pub fn format(
519519 self: Int,
520520 comptime fmt: []const u8,
521 options: std.fmt.FormatOptions,
522 context: var,
523 comptime FmtError: type,
524 output: fn (@TypeOf(context), []const u8) FmtError!void,
521 options: std.fmtstream.FormatOptions,
522 out_stream: var,
525523 ) FmtError!void {
526524 self.assertWritable();
527525 // TODO look at fmt and support other bases
528526 // TODO support read-only fixed integers
529527 const str = self.toString(self.allocator.?, 10) catch @panic("TODO make this non allocating");
530528 defer self.allocator.?.free(str);
531 return output(context, str);
529 return out_stream.print(str);
532530 }
533531
534532 /// Returns -1, 0, 1 if |a| < |b|, |a| == |b| or |a| > |b| respectively.
lib/std/net.zig+10-12
......@@ -268,16 +268,14 @@ pub const Address = extern union {
268268 pub fn format(
269269 self: Address,
270270 comptime fmt: []const u8,
271 options: std.fmt.FormatOptions,
272 context: var,
273 comptime Errors: type,
274 comptime output: fn (@TypeOf(context), []const u8) Errors!void,
271 options: std.fmtstream.FormatOptions,
272 out_stream: var,
275273 ) !void {
276274 switch (self.any.family) {
277275 os.AF_INET => {
278276 const port = mem.bigToNative(u16, self.in.port);
279277 const bytes = @ptrCast(*const [4]u8, &self.in.addr);
280 try std.fmt.format(context, Errors, output, "{}.{}.{}.{}:{}", .{
278 try std.fmtstream.format(out_stream, "{}.{}.{}.{}:{}", .{
281279 bytes[0],
282280 bytes[1],
283281 bytes[2],
......@@ -288,7 +286,7 @@ pub const Address = extern union {
288286 os.AF_INET6 => {
289287 const port = mem.bigToNative(u16, self.in6.port);
290288 if (mem.eql(u8, self.in6.addr[0..12], &[_]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff })) {
291 try std.fmt.format(context, Errors, output, "[::ffff:{}.{}.{}.{}]:{}", .{
289 try std.fmtstream.format(out_stream, "[::ffff:{}.{}.{}.{}]:{}", .{
292290 self.in6.addr[12],
293291 self.in6.addr[13],
294292 self.in6.addr[14],
......@@ -308,30 +306,30 @@ pub const Address = extern union {
308306 break :blk buf;
309307 },
310308 };
311 try output(context, "[");
309 try out_stream.writeAll("[");
312310 var i: usize = 0;
313311 var abbrv = false;
314312 while (i < native_endian_parts.len) : (i += 1) {
315313 if (native_endian_parts[i] == 0) {
316314 if (!abbrv) {
317 try output(context, if (i == 0) "::" else ":");
315 try out_stream.writeAll(if (i == 0) "::" else ":");
318316 abbrv = true;
319317 }
320318 continue;
321319 }
322 try std.fmt.format(context, Errors, output, "{x}", .{native_endian_parts[i]});
320 try std.fmtstream.format(out_stream, "{x}", .{native_endian_parts[i]});
323321 if (i != native_endian_parts.len - 1) {
324 try output(context, ":");
322 try out_stream.writeAll(":");
325323 }
326324 }
327 try std.fmt.format(context, Errors, output, "]:{}", .{port});
325 try std.fmtstream.format(out_stream, "]:{}", .{port});
328326 },
329327 os.AF_UNIX => {
330328 if (!has_unix_sockets) {
331329 unreachable;
332330 }
333331
334 try std.fmt.format(context, Errors, output, "{}", .{&self.un.path});
332 try std.fmtstream.format(out_stream, "{}", .{&self.un.path});
335333 },
336334 else => unreachable,
337335 }
lib/std/net/test.zig+2-2
......@@ -29,7 +29,7 @@ test "parse and render IPv6 addresses" {
2929 };
3030 for (ips) |ip, i| {
3131 var addr = net.Address.parseIp6(ip, 0) catch unreachable;
32 var newIp = std.fmt.bufPrint(buffer[0..], "{}", .{addr}) catch unreachable;
32 var newIp = std.fmtstream.bufPrint(buffer[0..], "{}", .{addr}) catch unreachable;
3333 std.testing.expect(std.mem.eql(u8, printed[i], newIp[1 .. newIp.len - 3]));
3434 }
3535
......@@ -51,7 +51,7 @@ test "parse and render IPv4 addresses" {
5151 "127.0.0.1",
5252 }) |ip| {
5353 var addr = net.Address.parseIp4(ip, 0) catch unreachable;
54 var newIp = std.fmt.bufPrint(buffer[0..], "{}", .{addr}) catch unreachable;
54 var newIp = std.fmtstream.bufPrint(buffer[0..], "{}", .{addr}) catch unreachable;
5555 std.testing.expect(std.mem.eql(u8, ip, newIp[0 .. newIp.len - 2]));
5656 }
5757
lib/std/os/uefi.zig+3-7
......@@ -5,8 +5,6 @@ pub const protocols = @import("uefi/protocols.zig");
55pub const status = @import("uefi/status.zig");
66pub const tables = @import("uefi/tables.zig");
77
8const fmt = @import("std").fmt;
9
108/// The EFI image's handle that is passed to its entry point.
119pub var handle: Handle = undefined;
1210
......@@ -29,13 +27,11 @@ pub const Guid = extern struct {
2927 pub fn format(
3028 self: @This(),
3129 comptime f: []const u8,
32 options: fmt.FormatOptions,
33 context: var,
34 comptime Errors: type,
35 output: fn (@TypeOf(context), []const u8) Errors!void,
30 options: std.fmtstream.FormatOptions,
31 out_stream: var,
3632 ) Errors!void {
3733 if (f.len == 0) {
38 return fmt.format(context, Errors, output, "{x:0>8}-{x:0>4}-{x:0>4}-{x:0>2}{x:0>2}-{x:0>12}", .{
34 return std.fmtstream.format(out_stream, "{x:0>8}-{x:0>4}-{x:0>4}-{x:0>2}{x:0>2}-{x:0>12}", .{
3935 self.time_low,
4036 self.time_mid,
4137 self.time_high_and_version,