authorgravatar for datamanrb@gmail.comdata-man <datamanrb@gmail.com> 2020-06-20 15:02:48+05:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-06-20 18:23:57+00:00
log5229f6ec68708c3e66393a50ad9f9032ee7f4257
tree086214f4f2ff071befce86b7b91f72bbfd3bf0f8
parentaa894cea2c4aab32cc137adbe22c5f25411cc723

Use writer in std.fmt


1 files changed, 175 insertions(+), 175 deletions(-)

lib/std/fmt.zig+175-175
......@@ -69,14 +69,14 @@ fn peekIsAlign(comptime fmt: []const u8) bool {
6969///
7070/// If a formatted user type contains a function of the type
7171/// ```
72/// pub fn format(value: ?, comptime fmt: []const u8, options: std.fmt.FormatOptions, out_stream: var) !void
72/// pub fn format(value: ?, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: var) !void
7373/// ```
7474/// with `?` being the type formatted, this function will be called instead of the default implementation.
7575/// This allows user types to be formatted in a logical manner instead of dumping all fields of the type.
7676///
7777/// A user type may be a `struct`, `vector`, `union` or `enum` type.
7878pub fn format(
79 out_stream: var,
79 writer: var,
8080 comptime fmt: []const u8,
8181 args: var,
8282) !void {
......@@ -136,7 +136,7 @@ pub fn format(
136136 .Start => switch (c) {
137137 '{' => {
138138 if (start_index < i) {
139 try out_stream.writeAll(fmt[start_index..i]);
139 try writer.writeAll(fmt[start_index..i]);
140140 }
141141
142142 start_index = i;
......@@ -148,7 +148,7 @@ pub fn format(
148148 },
149149 '}' => {
150150 if (start_index < i) {
151 try out_stream.writeAll(fmt[start_index..i]);
151 try writer.writeAll(fmt[start_index..i]);
152152 }
153153 state = .CloseBrace;
154154 },
......@@ -183,7 +183,7 @@ pub fn format(
183183 args[arg_to_print],
184184 fmt[0..0],
185185 options,
186 out_stream,
186 writer,
187187 default_max_depth,
188188 );
189189
......@@ -214,7 +214,7 @@ pub fn format(
214214 args[arg_to_print],
215215 fmt[specifier_start..i],
216216 options,
217 out_stream,
217 writer,
218218 default_max_depth,
219219 );
220220 state = .Start;
......@@ -259,7 +259,7 @@ pub fn format(
259259 args[arg_to_print],
260260 fmt[specifier_start..specifier_end],
261261 options,
262 out_stream,
262 writer,
263263 default_max_depth,
264264 );
265265 state = .Start;
......@@ -285,7 +285,7 @@ pub fn format(
285285 args[arg_to_print],
286286 fmt[specifier_start..specifier_end],
287287 options,
288 out_stream,
288 writer,
289289 default_max_depth,
290290 );
291291 state = .Start;
......@@ -306,7 +306,7 @@ pub fn format(
306306 }
307307 }
308308 if (start_index < fmt.len) {
309 try out_stream.writeAll(fmt[start_index..]);
309 try writer.writeAll(fmt[start_index..]);
310310 }
311311}
312312
......@@ -314,140 +314,140 @@ pub fn formatType(
314314 value: var,
315315 comptime fmt: []const u8,
316316 options: FormatOptions,
317 out_stream: var,
317 writer: var,
318318 max_depth: usize,
319) @TypeOf(out_stream).Error!void {
319) @TypeOf(writer).Error!void {
320320 if (comptime std.mem.eql(u8, fmt, "*")) {
321 try out_stream.writeAll(@typeName(@TypeOf(value).Child));
322 try out_stream.writeAll("@");
323 try formatInt(@ptrToInt(value), 16, false, FormatOptions{}, out_stream);
321 try writer.writeAll(@typeName(@TypeOf(value).Child));
322 try writer.writeAll("@");
323 try formatInt(@ptrToInt(value), 16, false, FormatOptions{}, writer);
324324 return;
325325 }
326326
327327 const T = @TypeOf(value);
328328 if (comptime std.meta.trait.hasFn("format")(T)) {
329 return try value.format(fmt, options, out_stream);
329 return try value.format(fmt, options, writer);
330330 }
331331
332332 switch (@typeInfo(T)) {
333333 .ComptimeInt, .Int, .ComptimeFloat, .Float => {
334 return formatValue(value, fmt, options, out_stream);
334 return formatValue(value, fmt, options, writer);
335335 },
336336 .Void => {
337 return formatBuf("void", options, out_stream);
337 return formatBuf("void", options, writer);
338338 },
339339 .Bool => {
340 return formatBuf(if (value) "true" else "false", options, out_stream);
340 return formatBuf(if (value) "true" else "false", options, writer);
341341 },
342342 .Optional => {
343343 if (value) |payload| {
344 return formatType(payload, fmt, options, out_stream, max_depth);
344 return formatType(payload, fmt, options, writer, max_depth);
345345 } else {
346 return formatBuf("null", options, out_stream);
346 return formatBuf("null", options, writer);
347347 }
348348 },
349349 .ErrorUnion => {
350350 if (value) |payload| {
351 return formatType(payload, fmt, options, out_stream, max_depth);
351 return formatType(payload, fmt, options, writer, max_depth);
352352 } else |err| {
353 return formatType(err, fmt, options, out_stream, max_depth);
353 return formatType(err, fmt, options, writer, max_depth);
354354 }
355355 },
356356 .ErrorSet => {
357 try out_stream.writeAll("error.");
358 return out_stream.writeAll(@errorName(value));
357 try writer.writeAll("error.");
358 return writer.writeAll(@errorName(value));
359359 },
360360 .Enum => |enumInfo| {
361 try out_stream.writeAll(@typeName(T));
361 try writer.writeAll(@typeName(T));
362362 if (enumInfo.is_exhaustive) {
363 try out_stream.writeAll(".");
364 try out_stream.writeAll(@tagName(value));
363 try writer.writeAll(".");
364 try writer.writeAll(@tagName(value));
365365 return;
366366 }
367367
368368 // Use @tagName only if value is one of known fields
369369 inline for (enumInfo.fields) |enumField| {
370370 if (@enumToInt(value) == enumField.value) {
371 try out_stream.writeAll(".");
372 try out_stream.writeAll(@tagName(value));
371 try writer.writeAll(".");
372 try writer.writeAll(@tagName(value));
373373 return;
374374 }
375375 }
376376
377 try out_stream.writeAll("(");
378 try formatType(@enumToInt(value), fmt, options, out_stream, max_depth);
379 try out_stream.writeAll(")");
377 try writer.writeAll("(");
378 try formatType(@enumToInt(value), fmt, options, writer, max_depth);
379 try writer.writeAll(")");
380380 },
381381 .Union => {
382 try out_stream.writeAll(@typeName(T));
382 try writer.writeAll(@typeName(T));
383383 if (max_depth == 0) {
384 return out_stream.writeAll("{ ... }");
384 return writer.writeAll("{ ... }");
385385 }
386386 const info = @typeInfo(T).Union;
387387 if (info.tag_type) |UnionTagType| {
388 try out_stream.writeAll("{ .");
389 try out_stream.writeAll(@tagName(@as(UnionTagType, value)));
390 try out_stream.writeAll(" = ");
388 try writer.writeAll("{ .");
389 try writer.writeAll(@tagName(@as(UnionTagType, value)));
390 try writer.writeAll(" = ");
391391 inline for (info.fields) |u_field| {
392392 if (@enumToInt(@as(UnionTagType, value)) == u_field.enum_field.?.value) {
393 try formatType(@field(value, u_field.name), fmt, options, out_stream, max_depth - 1);
393 try formatType(@field(value, u_field.name), fmt, options, writer, max_depth - 1);
394394 }
395395 }
396 try out_stream.writeAll(" }");
396 try writer.writeAll(" }");
397397 } else {
398 try format(out_stream, "@{x}", .{@ptrToInt(&value)});
398 try format(writer, "@{x}", .{@ptrToInt(&value)});
399399 }
400400 },
401401 .Struct => |StructT| {
402 try out_stream.writeAll(@typeName(T));
402 try writer.writeAll(@typeName(T));
403403 if (max_depth == 0) {
404 return out_stream.writeAll("{ ... }");
404 return writer.writeAll("{ ... }");
405405 }
406 try out_stream.writeAll("{");
406 try writer.writeAll("{");
407407 inline for (StructT.fields) |f, i| {
408408 if (i == 0) {
409 try out_stream.writeAll(" .");
409 try writer.writeAll(" .");
410410 } else {
411 try out_stream.writeAll(", .");
411 try writer.writeAll(", .");
412412 }
413 try out_stream.writeAll(f.name);
414 try out_stream.writeAll(" = ");
415 try formatType(@field(value, f.name), fmt, options, out_stream, max_depth - 1);
413 try writer.writeAll(f.name);
414 try writer.writeAll(" = ");
415 try formatType(@field(value, f.name), fmt, options, writer, max_depth - 1);
416416 }
417 try out_stream.writeAll(" }");
417 try writer.writeAll(" }");
418418 },
419419 .Pointer => |ptr_info| switch (ptr_info.size) {
420420 .One => switch (@typeInfo(ptr_info.child)) {
421421 .Array => |info| {
422422 if (info.child == u8) {
423 return formatText(value, fmt, options, out_stream);
423 return formatText(value, fmt, options, writer);
424424 }
425 return format(out_stream, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) });
425 return format(writer, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) });
426426 },
427427 .Enum, .Union, .Struct => {
428 return formatType(value.*, fmt, options, out_stream, max_depth);
428 return formatType(value.*, fmt, options, writer, max_depth);
429429 },
430 else => return format(out_stream, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }),
430 else => return format(writer, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }),
431431 },
432432 .Many, .C => {
433433 if (ptr_info.sentinel) |sentinel| {
434 return formatType(mem.span(value), fmt, options, out_stream, max_depth);
434 return formatType(mem.span(value), fmt, options, writer, max_depth);
435435 }
436436 if (ptr_info.child == u8) {
437437 if (fmt.len > 0 and fmt[0] == 's') {
438 return formatText(mem.span(value), fmt, options, out_stream);
438 return formatText(mem.span(value), fmt, options, writer);
439439 }
440440 }
441 return format(out_stream, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) });
441 return format(writer, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) });
442442 },
443443 .Slice => {
444444 if (fmt.len > 0 and ((fmt[0] == 'x') or (fmt[0] == 'X'))) {
445 return formatText(value, fmt, options, out_stream);
445 return formatText(value, fmt, options, writer);
446446 }
447447 if (ptr_info.child == u8) {
448 return formatText(value, fmt, options, out_stream);
448 return formatText(value, fmt, options, writer);
449449 }
450 return format(out_stream, "{}@{x}", .{ @typeName(ptr_info.child), @ptrToInt(value.ptr) });
450 return format(writer, "{}@{x}", .{ @typeName(ptr_info.child), @ptrToInt(value.ptr) });
451451 },
452452 },
453453 .Array => |info| {
......@@ -462,27 +462,27 @@ pub fn formatType(
462462 .sentinel = null,
463463 },
464464 });
465 return formatType(@as(Slice, &value), fmt, options, out_stream, max_depth);
465 return formatType(@as(Slice, &value), fmt, options, writer, max_depth);
466466 },
467467 .Vector => {
468468 const len = @typeInfo(T).Vector.len;
469 try out_stream.writeAll("{ ");
469 try writer.writeAll("{ ");
470470 var i: usize = 0;
471471 while (i < len) : (i += 1) {
472 try formatValue(value[i], fmt, options, out_stream);
472 try formatValue(value[i], fmt, options, writer);
473473 if (i < len - 1) {
474 try out_stream.writeAll(", ");
474 try writer.writeAll(", ");
475475 }
476476 }
477 try out_stream.writeAll(" }");
477 try writer.writeAll(" }");
478478 },
479479 .Fn => {
480 return format(out_stream, "{}@{x}", .{ @typeName(T), @ptrToInt(value) });
480 return format(writer, "{}@{x}", .{ @typeName(T), @ptrToInt(value) });
481481 },
482 .Type => return out_stream.writeAll(@typeName(T)),
482 .Type => return writer.writeAll(@typeName(T)),
483483 .EnumLiteral => {
484484 const buffer = [_]u8{'.'} ++ @tagName(value);
485 return formatType(buffer, fmt, options, out_stream, max_depth);
485 return formatType(buffer, fmt, options, writer, max_depth);
486486 },
487487 else => @compileError("Unable to format type '" ++ @typeName(T) ++ "'"),
488488 }
......@@ -492,19 +492,19 @@ fn formatValue(
492492 value: var,
493493 comptime fmt: []const u8,
494494 options: FormatOptions,
495 out_stream: var,
495 writer: var,
496496) !void {
497497 if (comptime std.mem.eql(u8, fmt, "B")) {
498 return formatBytes(value, options, 1000, out_stream);
498 return formatBytes(value, options, 1000, writer);
499499 } else if (comptime std.mem.eql(u8, fmt, "Bi")) {
500 return formatBytes(value, options, 1024, out_stream);
500 return formatBytes(value, options, 1024, writer);
501501 }
502502
503503 const T = @TypeOf(value);
504504 switch (@typeInfo(T)) {
505 .Float, .ComptimeFloat => return formatFloatValue(value, fmt, options, out_stream),
506 .Int, .ComptimeInt => return formatIntValue(value, fmt, options, out_stream),
507 .Bool => return formatBuf(if (value) "true" else "false", options, out_stream),
505 .Float, .ComptimeFloat => return formatFloatValue(value, fmt, options, writer),
506 .Int, .ComptimeInt => return formatIntValue(value, fmt, options, writer),
507 .Bool => return formatBuf(if (value) "true" else "false", options, writer),
508508 else => comptime unreachable,
509509 }
510510}
......@@ -513,7 +513,7 @@ pub fn formatIntValue(
513513 value: var,
514514 comptime fmt: []const u8,
515515 options: FormatOptions,
516 out_stream: var,
516 writer: var,
517517) !void {
518518 comptime var radix = 10;
519519 comptime var uppercase = false;
......@@ -529,7 +529,7 @@ pub fn formatIntValue(
529529 uppercase = false;
530530 } else if (comptime std.mem.eql(u8, fmt, "c")) {
531531 if (@TypeOf(int_value).bit_count <= 8) {
532 return formatAsciiChar(@as(u8, int_value), options, out_stream);
532 return formatAsciiChar(@as(u8, int_value), options, writer);
533533 } else {
534534 @compileError("Cannot print integer that is larger than 8 bits as a ascii");
535535 }
......@@ -546,19 +546,19 @@ pub fn formatIntValue(
546546 @compileError("Unknown format string: '" ++ fmt ++ "'");
547547 }
548548
549 return formatInt(int_value, radix, uppercase, options, out_stream);
549 return formatInt(int_value, radix, uppercase, options, writer);
550550}
551551
552552fn formatFloatValue(
553553 value: var,
554554 comptime fmt: []const u8,
555555 options: FormatOptions,
556 out_stream: var,
556 writer: var,
557557) !void {
558558 if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "e")) {
559 return formatFloatScientific(value, options, out_stream);
559 return formatFloatScientific(value, options, writer);
560560 } else if (comptime std.mem.eql(u8, fmt, "d")) {
561 return formatFloatDecimal(value, options, out_stream);
561 return formatFloatDecimal(value, options, writer);
562562 } else {
563563 @compileError("Unknown format string: '" ++ fmt ++ "'");
564564 }
......@@ -568,13 +568,13 @@ pub fn formatText(
568568 bytes: []const u8,
569569 comptime fmt: []const u8,
570570 options: FormatOptions,
571 out_stream: var,
571 writer: var,
572572) !void {
573573 if (comptime std.mem.eql(u8, fmt, "s") or (fmt.len == 0)) {
574 return formatBuf(bytes, options, out_stream);
574 return formatBuf(bytes, options, writer);
575575 } else if (comptime (std.mem.eql(u8, fmt, "x") or std.mem.eql(u8, fmt, "X"))) {
576576 for (bytes) |c| {
577 try formatInt(c, 16, fmt[0] == 'X', FormatOptions{ .width = 2, .fill = '0' }, out_stream);
577 try formatInt(c, 16, fmt[0] == 'X', FormatOptions{ .width = 2, .fill = '0' }, writer);
578578 }
579579 return;
580580 } else {
......@@ -585,38 +585,38 @@ pub fn formatText(
585585pub fn formatAsciiChar(
586586 c: u8,
587587 options: FormatOptions,
588 out_stream: var,
588 writer: var,
589589) !void {
590 return out_stream.writeAll(@as(*const [1]u8, &c));
590 return writer.writeAll(@as(*const [1]u8, &c));
591591}
592592
593593pub fn formatBuf(
594594 buf: []const u8,
595595 options: FormatOptions,
596 out_stream: var,
596 writer: var,
597597) !void {
598598 const width = options.width orelse buf.len;
599599 var padding = if (width > buf.len) (width - buf.len) else 0;
600600 const pad_byte = [1]u8{options.fill};
601601 switch (options.alignment) {
602602 .Left => {
603 try out_stream.writeAll(buf);
603 try writer.writeAll(buf);
604604 while (padding > 0) : (padding -= 1) {
605 try out_stream.writeAll(&pad_byte);
605 try writer.writeAll(&pad_byte);
606606 }
607607 },
608608 .Center => {
609609 const padl = padding / 2;
610610 var i: usize = 0;
611 while (i < padl) : (i += 1) try out_stream.writeAll(&pad_byte);
612 try out_stream.writeAll(buf);
613 while (i < padding) : (i += 1) try out_stream.writeAll(&pad_byte);
611 while (i < padl) : (i += 1) try writer.writeAll(&pad_byte);
612 try writer.writeAll(buf);
613 while (i < padding) : (i += 1) try writer.writeAll(&pad_byte);
614614 },
615615 .Right => {
616616 while (padding > 0) : (padding -= 1) {
617 try out_stream.writeAll(&pad_byte);
617 try writer.writeAll(&pad_byte);
618618 }
619 try out_stream.writeAll(buf);
619 try writer.writeAll(buf);
620620 },
621621 }
622622}
......@@ -627,38 +627,38 @@ pub fn formatBuf(
627627pub fn formatFloatScientific(
628628 value: var,
629629 options: FormatOptions,
630 out_stream: var,
630 writer: var,
631631) !void {
632632 var x = @floatCast(f64, value);
633633
634634 // Errol doesn't handle these special cases.
635635 if (math.signbit(x)) {
636 try out_stream.writeAll("-");
636 try writer.writeAll("-");
637637 x = -x;
638638 }
639639
640640 if (math.isNan(x)) {
641 return out_stream.writeAll("nan");
641 return writer.writeAll("nan");
642642 }
643643 if (math.isPositiveInf(x)) {
644 return out_stream.writeAll("inf");
644 return writer.writeAll("inf");
645645 }
646646 if (x == 0.0) {
647 try out_stream.writeAll("0");
647 try writer.writeAll("0");
648648
649649 if (options.precision) |precision| {
650650 if (precision != 0) {
651 try out_stream.writeAll(".");
651 try writer.writeAll(".");
652652 var i: usize = 0;
653653 while (i < precision) : (i += 1) {
654 try out_stream.writeAll("0");
654 try writer.writeAll("0");
655655 }
656656 }
657657 } else {
658 try out_stream.writeAll(".0");
658 try writer.writeAll(".0");
659659 }
660660
661 try out_stream.writeAll("e+00");
661 try writer.writeAll("e+00");
662662 return;
663663 }
664664
......@@ -668,50 +668,50 @@ pub fn formatFloatScientific(
668668 if (options.precision) |precision| {
669669 errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Scientific);
670670
671 try out_stream.writeAll(float_decimal.digits[0..1]);
671 try writer.writeAll(float_decimal.digits[0..1]);
672672
673673 // {e0} case prints no `.`
674674 if (precision != 0) {
675 try out_stream.writeAll(".");
675 try writer.writeAll(".");
676676
677677 var printed: usize = 0;
678678 if (float_decimal.digits.len > 1) {
679679 const num_digits = math.min(float_decimal.digits.len, precision + 1);
680 try out_stream.writeAll(float_decimal.digits[1..num_digits]);
680 try writer.writeAll(float_decimal.digits[1..num_digits]);
681681 printed += num_digits - 1;
682682 }
683683
684684 while (printed < precision) : (printed += 1) {
685 try out_stream.writeAll("0");
685 try writer.writeAll("0");
686686 }
687687 }
688688 } else {
689 try out_stream.writeAll(float_decimal.digits[0..1]);
690 try out_stream.writeAll(".");
689 try writer.writeAll(float_decimal.digits[0..1]);
690 try writer.writeAll(".");
691691 if (float_decimal.digits.len > 1) {
692692 const num_digits = if (@TypeOf(value) == f32) math.min(@as(usize, 9), float_decimal.digits.len) else float_decimal.digits.len;
693693
694 try out_stream.writeAll(float_decimal.digits[1..num_digits]);
694 try writer.writeAll(float_decimal.digits[1..num_digits]);
695695 } else {
696 try out_stream.writeAll("0");
696 try writer.writeAll("0");
697697 }
698698 }
699699
700 try out_stream.writeAll("e");
700 try writer.writeAll("e");
701701 const exp = float_decimal.exp - 1;
702702
703703 if (exp >= 0) {
704 try out_stream.writeAll("+");
704 try writer.writeAll("+");
705705 if (exp > -10 and exp < 10) {
706 try out_stream.writeAll("0");
706 try writer.writeAll("0");
707707 }
708 try formatInt(exp, 10, false, FormatOptions{ .width = 0 }, out_stream);
708 try formatInt(exp, 10, false, FormatOptions{ .width = 0 }, writer);
709709 } else {
710 try out_stream.writeAll("-");
710 try writer.writeAll("-");
711711 if (exp > -10 and exp < 10) {
712 try out_stream.writeAll("0");
712 try writer.writeAll("0");
713713 }
714 try formatInt(-exp, 10, false, FormatOptions{ .width = 0 }, out_stream);
714 try formatInt(-exp, 10, false, FormatOptions{ .width = 0 }, writer);
715715 }
716716}
717717
......@@ -720,34 +720,34 @@ pub fn formatFloatScientific(
720720pub fn formatFloatDecimal(
721721 value: var,
722722 options: FormatOptions,
723 out_stream: var,
723 writer: var,
724724) !void {
725725 var x = @as(f64, value);
726726
727727 // Errol doesn't handle these special cases.
728728 if (math.signbit(x)) {
729 try out_stream.writeAll("-");
729 try writer.writeAll("-");
730730 x = -x;
731731 }
732732
733733 if (math.isNan(x)) {
734 return out_stream.writeAll("nan");
734 return writer.writeAll("nan");
735735 }
736736 if (math.isPositiveInf(x)) {
737 return out_stream.writeAll("inf");
737 return writer.writeAll("inf");
738738 }
739739 if (x == 0.0) {
740 try out_stream.writeAll("0");
740 try writer.writeAll("0");
741741
742742 if (options.precision) |precision| {
743743 if (precision != 0) {
744 try out_stream.writeAll(".");
744 try writer.writeAll(".");
745745 var i: usize = 0;
746746 while (i < precision) : (i += 1) {
747 try out_stream.writeAll("0");
747 try writer.writeAll("0");
748748 }
749749 } else {
750 try out_stream.writeAll(".0");
750 try writer.writeAll(".0");
751751 }
752752 }
753753
......@@ -769,14 +769,14 @@ pub fn formatFloatDecimal(
769769
770770 if (num_digits_whole > 0) {
771771 // We may have to zero pad, for instance 1e4 requires zero padding.
772 try out_stream.writeAll(float_decimal.digits[0..num_digits_whole_no_pad]);
772 try writer.writeAll(float_decimal.digits[0..num_digits_whole_no_pad]);
773773
774774 var i = num_digits_whole_no_pad;
775775 while (i < num_digits_whole) : (i += 1) {
776 try out_stream.writeAll("0");
776 try writer.writeAll("0");
777777 }
778778 } else {
779 try out_stream.writeAll("0");
779 try writer.writeAll("0");
780780 }
781781
782782 // {.0} special case doesn't want a trailing '.'
......@@ -784,7 +784,7 @@ pub fn formatFloatDecimal(
784784 return;
785785 }
786786
787 try out_stream.writeAll(".");
787 try writer.writeAll(".");
788788
789789 // Keep track of fractional count printed for case where we pre-pad then post-pad with 0's.
790790 var printed: usize = 0;
......@@ -796,7 +796,7 @@ pub fn formatFloatDecimal(
796796
797797 var i: usize = 0;
798798 while (i < zeros_to_print) : (i += 1) {
799 try out_stream.writeAll("0");
799 try writer.writeAll("0");
800800 printed += 1;
801801 }
802802
......@@ -808,14 +808,14 @@ pub fn formatFloatDecimal(
808808 // Remaining fractional portion, zero-padding if insufficient.
809809 assert(precision >= printed);
810810 if (num_digits_whole_no_pad + precision - printed < float_decimal.digits.len) {
811 try out_stream.writeAll(float_decimal.digits[num_digits_whole_no_pad .. num_digits_whole_no_pad + precision - printed]);
811 try writer.writeAll(float_decimal.digits[num_digits_whole_no_pad .. num_digits_whole_no_pad + precision - printed]);
812812 return;
813813 } else {
814 try out_stream.writeAll(float_decimal.digits[num_digits_whole_no_pad..]);
814 try writer.writeAll(float_decimal.digits[num_digits_whole_no_pad..]);
815815 printed += float_decimal.digits.len - num_digits_whole_no_pad;
816816
817817 while (printed < precision) : (printed += 1) {
818 try out_stream.writeAll("0");
818 try writer.writeAll("0");
819819 }
820820 }
821821 } else {
......@@ -827,14 +827,14 @@ pub fn formatFloatDecimal(
827827
828828 if (num_digits_whole > 0) {
829829 // We may have to zero pad, for instance 1e4 requires zero padding.
830 try out_stream.writeAll(float_decimal.digits[0..num_digits_whole_no_pad]);
830 try writer.writeAll(float_decimal.digits[0..num_digits_whole_no_pad]);
831831
832832 var i = num_digits_whole_no_pad;
833833 while (i < num_digits_whole) : (i += 1) {
834 try out_stream.writeAll("0");
834 try writer.writeAll("0");
835835 }
836836 } else {
837 try out_stream.writeAll("0");
837 try writer.writeAll("0");
838838 }
839839
840840 // Omit `.` if no fractional portion
......@@ -842,7 +842,7 @@ pub fn formatFloatDecimal(
842842 return;
843843 }
844844
845 try out_stream.writeAll(".");
845 try writer.writeAll(".");
846846
847847 // Zero-fill until we reach significant digits or run out of precision.
848848 if (float_decimal.exp < 0) {
......@@ -850,11 +850,11 @@ pub fn formatFloatDecimal(
850850
851851 var i: usize = 0;
852852 while (i < zero_digit_count) : (i += 1) {
853 try out_stream.writeAll("0");
853 try writer.writeAll("0");
854854 }
855855 }
856856
857 try out_stream.writeAll(float_decimal.digits[num_digits_whole_no_pad..]);
857 try writer.writeAll(float_decimal.digits[num_digits_whole_no_pad..]);
858858 }
859859}
860860
......@@ -862,10 +862,10 @@ pub fn formatBytes(
862862 value: var,
863863 options: FormatOptions,
864864 comptime radix: usize,
865 out_stream: var,
865 writer: var,
866866) !void {
867867 if (value == 0) {
868 return out_stream.writeAll("0B");
868 return writer.writeAll("0B");
869869 }
870870
871871 const is_float = comptime std.meta.trait.is(.Float)(@TypeOf(value));
......@@ -885,10 +885,10 @@ pub fn formatBytes(
885885 else => unreachable,
886886 };
887887
888 try formatFloatDecimal(new_value, options, out_stream);
888 try formatFloatDecimal(new_value, options, writer);
889889
890890 if (suffix == ' ') {
891 return out_stream.writeAll("B");
891 return writer.writeAll("B");
892892 }
893893
894894 const buf = switch (radix) {
......@@ -896,7 +896,7 @@ pub fn formatBytes(
896896 1024 => &[_]u8{ suffix, 'i', 'B' },
897897 else => unreachable,
898898 };
899 return out_stream.writeAll(buf);
899 return writer.writeAll(buf);
900900}
901901
902902pub fn formatInt(
......@@ -904,7 +904,7 @@ pub fn formatInt(
904904 base: u8,
905905 uppercase: bool,
906906 options: FormatOptions,
907 out_stream: var,
907 writer: var,
908908) !void {
909909 const int_value = if (@TypeOf(value) == comptime_int) blk: {
910910 const Int = math.IntFittingRange(value, value);
......@@ -913,9 +913,9 @@ pub fn formatInt(
913913 value;
914914
915915 if (@TypeOf(int_value).is_signed) {
916 return formatIntSigned(int_value, base, uppercase, options, out_stream);
916 return formatIntSigned(int_value, base, uppercase, options, writer);
917917 } else {
918 return formatIntUnsigned(int_value, base, uppercase, options, out_stream);
918 return formatIntUnsigned(int_value, base, uppercase, options, writer);
919919 }
920920}
921921
......@@ -924,7 +924,7 @@ fn formatIntSigned(
924924 base: u8,
925925 uppercase: bool,
926926 options: FormatOptions,
927 out_stream: var,
927 writer: var,
928928) !void {
929929 const new_options = FormatOptions{
930930 .width = if (options.width) |w| (if (w == 0) 0 else w - 1) else null,
......@@ -934,15 +934,15 @@ fn formatIntSigned(
934934 const bit_count = @typeInfo(@TypeOf(value)).Int.bits;
935935 const Uint = std.meta.Int(false, bit_count);
936936 if (value < 0) {
937 try out_stream.writeAll("-");
937 try writer.writeAll("-");
938938 const new_value = math.absCast(value);
939 return formatIntUnsigned(new_value, base, uppercase, new_options, out_stream);
939 return formatIntUnsigned(new_value, base, uppercase, new_options, writer);
940940 } else if (options.width == null or options.width.? == 0) {
941 return formatIntUnsigned(@intCast(Uint, value), base, uppercase, options, out_stream);
941 return formatIntUnsigned(@intCast(Uint, value), base, uppercase, options, writer);
942942 } else {
943 try out_stream.writeAll("+");
943 try writer.writeAll("+");
944944 const new_value = @intCast(Uint, value);
945 return formatIntUnsigned(new_value, base, uppercase, new_options, out_stream);
945 return formatIntUnsigned(new_value, base, uppercase, new_options, writer);
946946 }
947947}
948948
......@@ -951,7 +951,7 @@ fn formatIntUnsigned(
951951 base: u8,
952952 uppercase: bool,
953953 options: FormatOptions,
954 out_stream: var,
954 writer: var,
955955) !void {
956956 assert(base >= 2);
957957 var buf: [math.max(@TypeOf(value).bit_count, 1)]u8 = undefined;
......@@ -976,22 +976,22 @@ fn formatIntUnsigned(
976976 const zero_byte: u8 = options.fill;
977977 var leftover_padding = padding - index;
978978 while (true) {
979 try out_stream.writeAll(@as(*const [1]u8, &zero_byte)[0..]);
979 try writer.writeAll(@as(*const [1]u8, &zero_byte)[0..]);
980980 leftover_padding -= 1;
981981 if (leftover_padding == 0) break;
982982 }
983983 mem.set(u8, buf[0..index], options.fill);
984 return out_stream.writeAll(&buf);
984 return writer.writeAll(&buf);
985985 } else {
986986 const padded_buf = buf[index - padding ..];
987987 mem.set(u8, padded_buf[0..padding], options.fill);
988 return out_stream.writeAll(padded_buf);
988 return writer.writeAll(padded_buf);
989989 }
990990}
991991
992992pub fn formatIntBuf(out_buf: []u8, value: var, base: u8, uppercase: bool, options: FormatOptions) usize {
993993 var fbs = std.io.fixedBufferStream(out_buf);
994 formatInt(value, base, uppercase, options, fbs.outStream()) catch unreachable;
994 formatInt(value, base, uppercase, options, fbs.writer()) catch unreachable;
995995 return fbs.pos;
996996}
997997
......@@ -1098,15 +1098,15 @@ pub const BufPrintError = error{
10981098};
10991099pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: var) BufPrintError![]u8 {
11001100 var fbs = std.io.fixedBufferStream(buf);
1101 try format(fbs.outStream(), fmt, args);
1101 try format(fbs.writer(), fmt, args);
11021102 return fbs.getWritten();
11031103}
11041104
11051105// Count the characters needed for format. Useful for preallocating memory
11061106pub fn count(comptime fmt: []const u8, args: var) u64 {
1107 var counting_stream = std.io.countingOutStream(std.io.null_out_stream);
1108 format(counting_stream.outStream(), fmt, args) catch |err| switch (err) {};
1109 return counting_stream.bytes_written;
1107 var counting_writer = std.io.countingWriter(std.io.null_writer);
1108 format(counting_writer.writer(), fmt, args) catch |err| switch (err) {};
1109 return counting_writer.bytes_written;
11101110}
11111111
11121112pub const AllocPrintError = error{OutOfMemory};
......@@ -1215,15 +1215,15 @@ test "buffer" {
12151215 {
12161216 var buf1: [32]u8 = undefined;
12171217 var fbs = std.io.fixedBufferStream(&buf1);
1218 try formatType(1234, "", FormatOptions{}, fbs.outStream(), default_max_depth);
1218 try formatType(1234, "", FormatOptions{}, fbs.writer(), default_max_depth);
12191219 std.testing.expect(mem.eql(u8, fbs.getWritten(), "1234"));
12201220
12211221 fbs.reset();
1222 try formatType('a', "c", FormatOptions{}, fbs.outStream(), default_max_depth);
1222 try formatType('a', "c", FormatOptions{}, fbs.writer(), default_max_depth);
12231223 std.testing.expect(mem.eql(u8, fbs.getWritten(), "a"));
12241224
12251225 fbs.reset();
1226 try formatType(0b1100, "b", FormatOptions{}, fbs.outStream(), default_max_depth);
1226 try formatType(0b1100, "b", FormatOptions{}, fbs.writer(), default_max_depth);
12271227 std.testing.expect(mem.eql(u8, fbs.getWritten(), "1100"));
12281228 }
12291229}
......@@ -1413,12 +1413,12 @@ test "custom" {
14131413 self: SelfType,
14141414 comptime fmt: []const u8,
14151415 options: FormatOptions,
1416 out_stream: var,
1416 writer: var,
14171417 ) !void {
14181418 if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "p")) {
1419 return std.fmt.format(out_stream, "({d:.3},{d:.3})", .{ self.x, self.y });
1419 return std.fmt.format(writer, "({d:.3},{d:.3})", .{ self.x, self.y });
14201420 } else if (comptime std.mem.eql(u8, fmt, "d")) {
1421 return std.fmt.format(out_stream, "{d:.3}x{d:.3}", .{ self.x, self.y });
1421 return std.fmt.format(writer, "{d:.3}x{d:.3}", .{ self.x, self.y });
14221422 } else {
14231423 @compileError("Unknown format character: '" ++ fmt ++ "'");
14241424 }
......@@ -1604,7 +1604,7 @@ test "formatIntValue with comptime_int" {
16041604
16051605 var buf: [20]u8 = undefined;
16061606 var fbs = std.io.fixedBufferStream(&buf);
1607 try formatIntValue(value, "", FormatOptions{}, fbs.outStream());
1607 try formatIntValue(value, "", FormatOptions{}, fbs.writer());
16081608 std.testing.expect(mem.eql(u8, fbs.getWritten(), "123456789123456789"));
16091609}
16101610
......@@ -1613,7 +1613,7 @@ test "formatFloatValue with comptime_float" {
16131613
16141614 var buf: [20]u8 = undefined;
16151615 var fbs = std.io.fixedBufferStream(&buf);
1616 try formatFloatValue(value, "", FormatOptions{}, fbs.outStream());
1616 try formatFloatValue(value, "", FormatOptions{}, fbs.writer());
16171617 std.testing.expect(mem.eql(u8, fbs.getWritten(), "1.0e+00"));
16181618
16191619 try testFmt("1.0e+00", "{}", .{value});
......@@ -1630,10 +1630,10 @@ test "formatType max_depth" {
16301630 self: SelfType,
16311631 comptime fmt: []const u8,
16321632 options: FormatOptions,
1633 out_stream: var,
1633 writer: var,
16341634 ) !void {
16351635 if (fmt.len == 0) {
1636 return std.fmt.format(out_stream, "({d:.3},{d:.3})", .{ self.x, self.y });
1636 return std.fmt.format(writer, "({d:.3},{d:.3})", .{ self.x, self.y });
16371637 } else {
16381638 @compileError("Unknown format string: '" ++ fmt ++ "'");
16391639 }
......@@ -1669,19 +1669,19 @@ test "formatType max_depth" {
16691669
16701670 var buf: [1000]u8 = undefined;
16711671 var fbs = std.io.fixedBufferStream(&buf);
1672 try formatType(inst, "", FormatOptions{}, fbs.outStream(), 0);
1672 try formatType(inst, "", FormatOptions{}, fbs.writer(), 0);
16731673 std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ ... }"));
16741674
16751675 fbs.reset();
1676 try formatType(inst, "", FormatOptions{}, fbs.outStream(), 1);
1676 try formatType(inst, "", FormatOptions{}, fbs.writer(), 1);
16771677 std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }"));
16781678
16791679 fbs.reset();
1680 try formatType(inst, "", FormatOptions{}, fbs.outStream(), 2);
1680 try formatType(inst, "", FormatOptions{}, fbs.writer(), 2);
16811681 std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ .a = S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ ... } }, .e = E.Two, .vec = (10.200,2.220) }"));
16821682
16831683 fbs.reset();
1684 try formatType(inst, "", FormatOptions{}, fbs.outStream(), 3);
1684 try formatType(inst, "", FormatOptions{}, fbs.writer(), 3);
16851685 std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ .a = S{ .a = S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ ... } }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ .ptr = TU{ ... } } }, .e = E.Two, .vec = (10.200,2.220) }"));
16861686}
16871687