Integrated outstreams with new formatter

master
Benjamin Feng 2020-02-29 15:35:18 -06:00
parent 7364e965f4
commit c11d1055b8
6 changed files with 27 additions and 37 deletions

View File

@ -349,16 +349,14 @@ pub const Headers = struct {
pub fn format(
self: Self,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
context: var,
comptime Errors: type,
output: fn (@TypeOf(context), []const u8) Errors!void,
) Errors!void {
options: std.fmtstream.FormatOptions,
out_stream: var,
) !void {
for (self.toSlice()) |entry| {
try output(context, entry.name);
try output(context, ": ");
try output(context, entry.value);
try output(context, "\n");
try out_stream.writeAll(entry.name);
try out_stream.writeAll(": ");
try out_stream.writeAll(entry.value);
try out_stream.writeAll("\n");
}
}
};
@ -593,5 +591,5 @@ test "Headers.format" {
\\foo: bar
\\cookie: somevalue
\\
, try std.fmt.bufPrint(buf[0..], "{}", .{h}));
, try std.fmtstream.bufPrint(buf[0..], "{}", .{h}));
}

View File

@ -25,7 +25,7 @@ pub fn OutStream(
}
pub fn print(self: Self, comptime format: []const u8, args: var) Error!void {
return std.fmt.format(self, Error, writeAll, format, args);
return std.fmtstream.format(self, format, args);
}
pub fn writeByte(self: Self, byte: u8) Error!void {

View File

@ -518,17 +518,15 @@ pub const Int = struct {
pub fn format(
self: Int,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
context: var,
comptime FmtError: type,
output: fn (@TypeOf(context), []const u8) FmtError!void,
options: std.fmtstream.FormatOptions,
out_stream: var,
) FmtError!void {
self.assertWritable();
// TODO look at fmt and support other bases
// TODO support read-only fixed integers
const str = self.toString(self.allocator.?, 10) catch @panic("TODO make this non allocating");
defer self.allocator.?.free(str);
return output(context, str);
return out_stream.print(str);
}
/// Returns -1, 0, 1 if |a| < |b|, |a| == |b| or |a| > |b| respectively.

View File

@ -268,16 +268,14 @@ pub const Address = extern union {
pub fn format(
self: Address,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
context: var,
comptime Errors: type,
comptime output: fn (@TypeOf(context), []const u8) Errors!void,
options: std.fmtstream.FormatOptions,
out_stream: var,
) !void {
switch (self.any.family) {
os.AF_INET => {
const port = mem.bigToNative(u16, self.in.port);
const bytes = @ptrCast(*const [4]u8, &self.in.addr);
try std.fmt.format(context, Errors, output, "{}.{}.{}.{}:{}", .{
try std.fmtstream.format(out_stream, "{}.{}.{}.{}:{}", .{
bytes[0],
bytes[1],
bytes[2],
@ -288,7 +286,7 @@ pub const Address = extern union {
os.AF_INET6 => {
const port = mem.bigToNative(u16, self.in6.port);
if (mem.eql(u8, self.in6.addr[0..12], &[_]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff })) {
try std.fmt.format(context, Errors, output, "[::ffff:{}.{}.{}.{}]:{}", .{
try std.fmtstream.format(out_stream, "[::ffff:{}.{}.{}.{}]:{}", .{
self.in6.addr[12],
self.in6.addr[13],
self.in6.addr[14],
@ -308,30 +306,30 @@ pub const Address = extern union {
break :blk buf;
},
};
try output(context, "[");
try out_stream.writeAll("[");
var i: usize = 0;
var abbrv = false;
while (i < native_endian_parts.len) : (i += 1) {
if (native_endian_parts[i] == 0) {
if (!abbrv) {
try output(context, if (i == 0) "::" else ":");
try out_stream.writeAll(if (i == 0) "::" else ":");
abbrv = true;
}
continue;
}
try std.fmt.format(context, Errors, output, "{x}", .{native_endian_parts[i]});
try std.fmtstream.format(out_stream, "{x}", .{native_endian_parts[i]});
if (i != native_endian_parts.len - 1) {
try output(context, ":");
try out_stream.writeAll(":");
}
}
try std.fmt.format(context, Errors, output, "]:{}", .{port});
try std.fmtstream.format(out_stream, "]:{}", .{port});
},
os.AF_UNIX => {
if (!has_unix_sockets) {
unreachable;
}
try std.fmt.format(context, Errors, output, "{}", .{&self.un.path});
try std.fmtstream.format(out_stream, "{}", .{&self.un.path});
},
else => unreachable,
}

View File

@ -29,7 +29,7 @@ test "parse and render IPv6 addresses" {
};
for (ips) |ip, i| {
var addr = net.Address.parseIp6(ip, 0) catch unreachable;
var newIp = std.fmt.bufPrint(buffer[0..], "{}", .{addr}) catch unreachable;
var newIp = std.fmtstream.bufPrint(buffer[0..], "{}", .{addr}) catch unreachable;
std.testing.expect(std.mem.eql(u8, printed[i], newIp[1 .. newIp.len - 3]));
}
@ -51,7 +51,7 @@ test "parse and render IPv4 addresses" {
"127.0.0.1",
}) |ip| {
var addr = net.Address.parseIp4(ip, 0) catch unreachable;
var newIp = std.fmt.bufPrint(buffer[0..], "{}", .{addr}) catch unreachable;
var newIp = std.fmtstream.bufPrint(buffer[0..], "{}", .{addr}) catch unreachable;
std.testing.expect(std.mem.eql(u8, ip, newIp[0 .. newIp.len - 2]));
}

View File

@ -5,8 +5,6 @@ pub const protocols = @import("uefi/protocols.zig");
pub const status = @import("uefi/status.zig");
pub const tables = @import("uefi/tables.zig");
const fmt = @import("std").fmt;
/// The EFI image's handle that is passed to its entry point.
pub var handle: Handle = undefined;
@ -29,13 +27,11 @@ pub const Guid = extern struct {
pub fn format(
self: @This(),
comptime f: []const u8,
options: fmt.FormatOptions,
context: var,
comptime Errors: type,
output: fn (@TypeOf(context), []const u8) Errors!void,
options: std.fmtstream.FormatOptions,
out_stream: var,
) Errors!void {
if (f.len == 0) {
return fmt.format(context, Errors, output, "{x:0>8}-{x:0>4}-{x:0>4}-{x:0>2}{x:0>2}-{x:0>12}", .{
return std.fmtstream.format(out_stream, "{x:0>8}-{x:0>4}-{x:0>4}-{x:0>2}{x:0>2}-{x:0>12}", .{
self.time_low,
self.time_mid,
self.time_high_and_version,