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 {...@@ -349,16 +349,14 @@ pub const Headers = struct {
349 pub fn format(349 pub fn format(
350 self: Self,350 self: Self,
351 comptime fmt: []const u8,351 comptime fmt: []const u8,
352 options: std.fmt.FormatOptions,352 options: std.fmtstream.FormatOptions,
353 context: var,353 out_stream: var,
354 comptime Errors: type,354 ) !void {
355 output: fn (@TypeOf(context), []const u8) Errors!void,
356 ) Errors!void {
357 for (self.toSlice()) |entry| {355 for (self.toSlice()) |entry| {
358 try output(context, entry.name);356 try out_stream.writeAll(entry.name);
359 try output(context, ": ");357 try out_stream.writeAll(": ");
360 try output(context, entry.value);358 try out_stream.writeAll(entry.value);
361 try output(context, "\n");359 try out_stream.writeAll("\n");
362 }360 }
363 }361 }
364};362};
...@@ -593,5 +591,5 @@ test "Headers.format" {...@@ -593,5 +591,5 @@ test "Headers.format" {
593 \\foo: bar591 \\foo: bar
594 \\cookie: somevalue592 \\cookie: somevalue
595 \\593 \\
596 , try std.fmt.bufPrint(buf[0..], "{}", .{h}));594 , try std.fmtstream.bufPrint(buf[0..], "{}", .{h}));
597}595}
lib/std/io/out_stream.zig+1-1
...@@ -25,7 +25,7 @@ pub fn OutStream(...@@ -25,7 +25,7 @@ pub fn OutStream(
25 }25 }
2626
27 pub fn print(self: Self, comptime format: []const u8, args: var) Error!void {27 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);
29 }29 }
3030
31 pub fn writeByte(self: Self, byte: u8) Error!void {31 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 {...@@ -518,17 +518,15 @@ pub const Int = struct {
518 pub fn format(518 pub fn format(
519 self: Int,519 self: Int,
520 comptime fmt: []const u8,520 comptime fmt: []const u8,
521 options: std.fmt.FormatOptions,521 options: std.fmtstream.FormatOptions,
522 context: var,522 out_stream: var,
523 comptime FmtError: type,
524 output: fn (@TypeOf(context), []const u8) FmtError!void,
525 ) FmtError!void {523 ) FmtError!void {
526 self.assertWritable();524 self.assertWritable();
527 // TODO look at fmt and support other bases525 // TODO look at fmt and support other bases
528 // TODO support read-only fixed integers526 // TODO support read-only fixed integers
529 const str = self.toString(self.allocator.?, 10) catch @panic("TODO make this non allocating");527 const str = self.toString(self.allocator.?, 10) catch @panic("TODO make this non allocating");
530 defer self.allocator.?.free(str);528 defer self.allocator.?.free(str);
531 return output(context, str);529 return out_stream.print(str);
532 }530 }
533531
534 /// Returns -1, 0, 1 if |a| < |b|, |a| == |b| or |a| > |b| respectively.532 /// 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 {...@@ -268,16 +268,14 @@ pub const Address = extern union {
268 pub fn format(268 pub fn format(
269 self: Address,269 self: Address,
270 comptime fmt: []const u8,270 comptime fmt: []const u8,
271 options: std.fmt.FormatOptions,271 options: std.fmtstream.FormatOptions,
272 context: var,272 out_stream: var,
273 comptime Errors: type,
274 comptime output: fn (@TypeOf(context), []const u8) Errors!void,
275 ) !void {273 ) !void {
276 switch (self.any.family) {274 switch (self.any.family) {
277 os.AF_INET => {275 os.AF_INET => {
278 const port = mem.bigToNative(u16, self.in.port);276 const port = mem.bigToNative(u16, self.in.port);
279 const bytes = @ptrCast(*const [4]u8, &self.in.addr);277 const bytes = @ptrCast(*const [4]u8, &self.in.addr);
280 try std.fmt.format(context, Errors, output, "{}.{}.{}.{}:{}", .{278 try std.fmtstream.format(out_stream, "{}.{}.{}.{}:{}", .{
281 bytes[0],279 bytes[0],
282 bytes[1],280 bytes[1],
283 bytes[2],281 bytes[2],
...@@ -288,7 +286,7 @@ pub const Address = extern union {...@@ -288,7 +286,7 @@ pub const Address = extern union {
288 os.AF_INET6 => {286 os.AF_INET6 => {
289 const port = mem.bigToNative(u16, self.in6.port);287 const port = mem.bigToNative(u16, self.in6.port);
290 if (mem.eql(u8, self.in6.addr[0..12], &[_]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff })) {288 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:{}.{}.{}.{}]:{}", .{
292 self.in6.addr[12],290 self.in6.addr[12],
293 self.in6.addr[13],291 self.in6.addr[13],
294 self.in6.addr[14],292 self.in6.addr[14],
...@@ -308,30 +306,30 @@ pub const Address = extern union {...@@ -308,30 +306,30 @@ pub const Address = extern union {
308 break :blk buf;306 break :blk buf;
309 },307 },
310 };308 };
311 try output(context, "[");309 try out_stream.writeAll("[");
312 var i: usize = 0;310 var i: usize = 0;
313 var abbrv = false;311 var abbrv = false;
314 while (i < native_endian_parts.len) : (i += 1) {312 while (i < native_endian_parts.len) : (i += 1) {
315 if (native_endian_parts[i] == 0) {313 if (native_endian_parts[i] == 0) {
316 if (!abbrv) {314 if (!abbrv) {
317 try output(context, if (i == 0) "::" else ":");315 try out_stream.writeAll(if (i == 0) "::" else ":");
318 abbrv = true;316 abbrv = true;
319 }317 }
320 continue;318 continue;
321 }319 }
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]});
323 if (i != native_endian_parts.len - 1) {321 if (i != native_endian_parts.len - 1) {
324 try output(context, ":");322 try out_stream.writeAll(":");
325 }323 }
326 }324 }
327 try std.fmt.format(context, Errors, output, "]:{}", .{port});325 try std.fmtstream.format(out_stream, "]:{}", .{port});
328 },326 },
329 os.AF_UNIX => {327 os.AF_UNIX => {
330 if (!has_unix_sockets) {328 if (!has_unix_sockets) {
331 unreachable;329 unreachable;
332 }330 }
333331
334 try std.fmt.format(context, Errors, output, "{}", .{&self.un.path});332 try std.fmtstream.format(out_stream, "{}", .{&self.un.path});
335 },333 },
336 else => unreachable,334 else => unreachable,
337 }335 }
lib/std/net/test.zig+2-2
...@@ -29,7 +29,7 @@ test "parse and render IPv6 addresses" {...@@ -29,7 +29,7 @@ test "parse and render IPv6 addresses" {
29 };29 };
30 for (ips) |ip, i| {30 for (ips) |ip, i| {
31 var addr = net.Address.parseIp6(ip, 0) catch unreachable;31 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;
33 std.testing.expect(std.mem.eql(u8, printed[i], newIp[1 .. newIp.len - 3]));33 std.testing.expect(std.mem.eql(u8, printed[i], newIp[1 .. newIp.len - 3]));
34 }34 }
3535
...@@ -51,7 +51,7 @@ test "parse and render IPv4 addresses" {...@@ -51,7 +51,7 @@ test "parse and render IPv4 addresses" {
51 "127.0.0.1",51 "127.0.0.1",
52 }) |ip| {52 }) |ip| {
53 var addr = net.Address.parseIp4(ip, 0) catch unreachable;53 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;
55 std.testing.expect(std.mem.eql(u8, ip, newIp[0 .. newIp.len - 2]));55 std.testing.expect(std.mem.eql(u8, ip, newIp[0 .. newIp.len - 2]));
56 }56 }
5757
lib/std/os/uefi.zig+3-7
...@@ -5,8 +5,6 @@ pub const protocols = @import("uefi/protocols.zig");...@@ -5,8 +5,6 @@ pub const protocols = @import("uefi/protocols.zig");
5pub const status = @import("uefi/status.zig");5pub const status = @import("uefi/status.zig");
6pub const tables = @import("uefi/tables.zig");6pub const tables = @import("uefi/tables.zig");
77
8const fmt = @import("std").fmt;
9
10/// The EFI image's handle that is passed to its entry point.8/// The EFI image's handle that is passed to its entry point.
11pub var handle: Handle = undefined;9pub var handle: Handle = undefined;
1210
...@@ -29,13 +27,11 @@ pub const Guid = extern struct {...@@ -29,13 +27,11 @@ pub const Guid = extern struct {
29 pub fn format(27 pub fn format(
30 self: @This(),28 self: @This(),
31 comptime f: []const u8,29 comptime f: []const u8,
32 options: fmt.FormatOptions,30 options: std.fmtstream.FormatOptions,
33 context: var,31 out_stream: var,
34 comptime Errors: type,
35 output: fn (@TypeOf(context), []const u8) Errors!void,
36 ) Errors!void {32 ) Errors!void {
37 if (f.len == 0) {33 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}", .{
39 self.time_low,35 self.time_low,
40 self.time_mid,36 self.time_mid,
41 self.time_high_and_version,37 self.time_high_and_version,