| ... | @@ -69,14 +69,14 @@ fn peekIsAlign(comptime fmt: []const u8) bool { | ... | @@ -69,14 +69,14 @@ fn peekIsAlign(comptime fmt: []const u8) bool { |
| 69 | /// | 69 | /// |
| 70 | /// If a formatted user type contains a function of the type | 70 | /// If a formatted user type contains a function of the type |
| 71 | /// ``` | 71 | /// ``` |
| 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 |
| 73 | /// ``` | 73 | /// ``` |
| 74 | /// with `?` being the type formatted, this function will be called instead of the default implementation. | 74 | /// with `?` being the type formatted, this function will be called instead of the default implementation. |
| 75 | /// This allows user types to be formatted in a logical manner instead of dumping all fields of the type. | 75 | /// This allows user types to be formatted in a logical manner instead of dumping all fields of the type. |
| 76 | /// | 76 | /// |
| 77 | /// A user type may be a `struct`, `vector`, `union` or `enum` type. | 77 | /// A user type may be a `struct`, `vector`, `union` or `enum` type. |
| 78 | pub fn format( | 78 | pub fn format( |
| 79 | out_stream: var, | 79 | writer: var, |
| 80 | comptime fmt: []const u8, | 80 | comptime fmt: []const u8, |
| 81 | args: var, | 81 | args: var, |
| 82 | ) !void { | 82 | ) !void { |
| ... | @@ -136,7 +136,7 @@ pub fn format( | ... | @@ -136,7 +136,7 @@ pub fn format( |
| 136 | .Start => switch (c) { | 136 | .Start => switch (c) { |
| 137 | '{' => { | 137 | '{' => { |
| 138 | if (start_index < i) { | 138 | if (start_index < i) { |
| 139 | try out_stream.writeAll(fmt[start_index..i]); | 139 | try writer.writeAll(fmt[start_index..i]); |
| 140 | } | 140 | } |
| 141 | | 141 | |
| 142 | start_index = i; | 142 | start_index = i; |
| ... | @@ -148,7 +148,7 @@ pub fn format( | ... | @@ -148,7 +148,7 @@ pub fn format( |
| 148 | }, | 148 | }, |
| 149 | '}' => { | 149 | '}' => { |
| 150 | if (start_index < i) { | 150 | if (start_index < i) { |
| 151 | try out_stream.writeAll(fmt[start_index..i]); | 151 | try writer.writeAll(fmt[start_index..i]); |
| 152 | } | 152 | } |
| 153 | state = .CloseBrace; | 153 | state = .CloseBrace; |
| 154 | }, | 154 | }, |
| ... | @@ -183,7 +183,7 @@ pub fn format( | ... | @@ -183,7 +183,7 @@ pub fn format( |
| 183 | args[arg_to_print], | 183 | args[arg_to_print], |
| 184 | fmt[0..0], | 184 | fmt[0..0], |
| 185 | options, | 185 | options, |
| 186 | out_stream, | 186 | writer, |
| 187 | default_max_depth, | 187 | default_max_depth, |
| 188 | ); | 188 | ); |
| 189 | | 189 | |
| ... | @@ -214,7 +214,7 @@ pub fn format( | ... | @@ -214,7 +214,7 @@ pub fn format( |
| 214 | args[arg_to_print], | 214 | args[arg_to_print], |
| 215 | fmt[specifier_start..i], | 215 | fmt[specifier_start..i], |
| 216 | options, | 216 | options, |
| 217 | out_stream, | 217 | writer, |
| 218 | default_max_depth, | 218 | default_max_depth, |
| 219 | ); | 219 | ); |
| 220 | state = .Start; | 220 | state = .Start; |
| ... | @@ -259,7 +259,7 @@ pub fn format( | ... | @@ -259,7 +259,7 @@ pub fn format( |
| 259 | args[arg_to_print], | 259 | args[arg_to_print], |
| 260 | fmt[specifier_start..specifier_end], | 260 | fmt[specifier_start..specifier_end], |
| 261 | options, | 261 | options, |
| 262 | out_stream, | 262 | writer, |
| 263 | default_max_depth, | 263 | default_max_depth, |
| 264 | ); | 264 | ); |
| 265 | state = .Start; | 265 | state = .Start; |
| ... | @@ -285,7 +285,7 @@ pub fn format( | ... | @@ -285,7 +285,7 @@ pub fn format( |
| 285 | args[arg_to_print], | 285 | args[arg_to_print], |
| 286 | fmt[specifier_start..specifier_end], | 286 | fmt[specifier_start..specifier_end], |
| 287 | options, | 287 | options, |
| 288 | out_stream, | 288 | writer, |
| 289 | default_max_depth, | 289 | default_max_depth, |
| 290 | ); | 290 | ); |
| 291 | state = .Start; | 291 | state = .Start; |
| ... | @@ -306,7 +306,7 @@ pub fn format( | ... | @@ -306,7 +306,7 @@ pub fn format( |
| 306 | } | 306 | } |
| 307 | } | 307 | } |
| 308 | if (start_index < fmt.len) { | 308 | if (start_index < fmt.len) { |
| 309 | try out_stream.writeAll(fmt[start_index..]); | 309 | try writer.writeAll(fmt[start_index..]); |
| 310 | } | 310 | } |
| 311 | } | 311 | } |
| 312 | | 312 | |
| ... | @@ -314,140 +314,140 @@ pub fn formatType( | ... | @@ -314,140 +314,140 @@ pub fn formatType( |
| 314 | value: var, | 314 | value: var, |
| 315 | comptime fmt: []const u8, | 315 | comptime fmt: []const u8, |
| 316 | options: FormatOptions, | 316 | options: FormatOptions, |
| 317 | out_stream: var, | 317 | writer: var, |
| 318 | max_depth: usize, | 318 | max_depth: usize, |
| 319 | ) @TypeOf(out_stream).Error!void { | 319 | ) @TypeOf(writer).Error!void { |
| 320 | if (comptime std.mem.eql(u8, fmt, "*")) { | 320 | if (comptime std.mem.eql(u8, fmt, "*")) { |
| 321 | try out_stream.writeAll(@typeName(@TypeOf(value).Child)); | 321 | try writer.writeAll(@typeName(@TypeOf(value).Child)); |
| 322 | try out_stream.writeAll("@"); | 322 | try writer.writeAll("@"); |
| 323 | try formatInt(@ptrToInt(value), 16, false, FormatOptions{}, out_stream); | 323 | try formatInt(@ptrToInt(value), 16, false, FormatOptions{}, writer); |
| 324 | return; | 324 | return; |
| 325 | } | 325 | } |
| 326 | | 326 | |
| 327 | const T = @TypeOf(value); | 327 | const T = @TypeOf(value); |
| 328 | if (comptime std.meta.trait.hasFn("format")(T)) { | 328 | 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); |
| 330 | } | 330 | } |
| 331 | | 331 | |
| 332 | switch (@typeInfo(T)) { | 332 | switch (@typeInfo(T)) { |
| 333 | .ComptimeInt, .Int, .ComptimeFloat, .Float => { | 333 | .ComptimeInt, .Int, .ComptimeFloat, .Float => { |
| 334 | return formatValue(value, fmt, options, out_stream); | 334 | return formatValue(value, fmt, options, writer); |
| 335 | }, | 335 | }, |
| 336 | .Void => { | 336 | .Void => { |
| 337 | return formatBuf("void", options, out_stream); | 337 | return formatBuf("void", options, writer); |
| 338 | }, | 338 | }, |
| 339 | .Bool => { | 339 | .Bool => { |
| 340 | return formatBuf(if (value) "true" else "false", options, out_stream); | 340 | return formatBuf(if (value) "true" else "false", options, writer); |
| 341 | }, | 341 | }, |
| 342 | .Optional => { | 342 | .Optional => { |
| 343 | if (value) |payload| { | 343 | if (value) |payload| { |
| 344 | return formatType(payload, fmt, options, out_stream, max_depth); | 344 | return formatType(payload, fmt, options, writer, max_depth); |
| 345 | } else { | 345 | } else { |
| 346 | return formatBuf("null", options, out_stream); | 346 | return formatBuf("null", options, writer); |
| 347 | } | 347 | } |
| 348 | }, | 348 | }, |
| 349 | .ErrorUnion => { | 349 | .ErrorUnion => { |
| 350 | if (value) |payload| { | 350 | if (value) |payload| { |
| 351 | return formatType(payload, fmt, options, out_stream, max_depth); | 351 | return formatType(payload, fmt, options, writer, max_depth); |
| 352 | } else |err| { | 352 | } else |err| { |
| 353 | return formatType(err, fmt, options, out_stream, max_depth); | 353 | return formatType(err, fmt, options, writer, max_depth); |
| 354 | } | 354 | } |
| 355 | }, | 355 | }, |
| 356 | .ErrorSet => { | 356 | .ErrorSet => { |
| 357 | try out_stream.writeAll("error."); | 357 | try writer.writeAll("error."); |
| 358 | return out_stream.writeAll(@errorName(value)); | 358 | return writer.writeAll(@errorName(value)); |
| 359 | }, | 359 | }, |
| 360 | .Enum => |enumInfo| { | 360 | .Enum => |enumInfo| { |
| 361 | try out_stream.writeAll(@typeName(T)); | 361 | try writer.writeAll(@typeName(T)); |
| 362 | if (enumInfo.is_exhaustive) { | 362 | if (enumInfo.is_exhaustive) { |
| 363 | try out_stream.writeAll("."); | 363 | try writer.writeAll("."); |
| 364 | try out_stream.writeAll(@tagName(value)); | 364 | try writer.writeAll(@tagName(value)); |
| 365 | return; | 365 | return; |
| 366 | } | 366 | } |
| 367 | | 367 | |
| 368 | // Use @tagName only if value is one of known fields | 368 | // Use @tagName only if value is one of known fields |
| 369 | inline for (enumInfo.fields) |enumField| { | 369 | inline for (enumInfo.fields) |enumField| { |
| 370 | if (@enumToInt(value) == enumField.value) { | 370 | if (@enumToInt(value) == enumField.value) { |
| 371 | try out_stream.writeAll("."); | 371 | try writer.writeAll("."); |
| 372 | try out_stream.writeAll(@tagName(value)); | 372 | try writer.writeAll(@tagName(value)); |
| 373 | return; | 373 | return; |
| 374 | } | 374 | } |
| 375 | } | 375 | } |
| 376 | | 376 | |
| 377 | try out_stream.writeAll("("); | 377 | try writer.writeAll("("); |
| 378 | try formatType(@enumToInt(value), fmt, options, out_stream, max_depth); | 378 | try formatType(@enumToInt(value), fmt, options, writer, max_depth); |
| 379 | try out_stream.writeAll(")"); | 379 | try writer.writeAll(")"); |
| 380 | }, | 380 | }, |
| 381 | .Union => { | 381 | .Union => { |
| 382 | try out_stream.writeAll(@typeName(T)); | 382 | try writer.writeAll(@typeName(T)); |
| 383 | if (max_depth == 0) { | 383 | if (max_depth == 0) { |
| 384 | return out_stream.writeAll("{ ... }"); | 384 | return writer.writeAll("{ ... }"); |
| 385 | } | 385 | } |
| 386 | const info = @typeInfo(T).Union; | 386 | const info = @typeInfo(T).Union; |
| 387 | if (info.tag_type) |UnionTagType| { | 387 | if (info.tag_type) |UnionTagType| { |
| 388 | try out_stream.writeAll("{ ."); | 388 | try writer.writeAll("{ ."); |
| 389 | try out_stream.writeAll(@tagName(@as(UnionTagType, value))); | 389 | try writer.writeAll(@tagName(@as(UnionTagType, value))); |
| 390 | try out_stream.writeAll(" = "); | 390 | try writer.writeAll(" = "); |
| 391 | inline for (info.fields) |u_field| { | 391 | inline for (info.fields) |u_field| { |
| 392 | if (@enumToInt(@as(UnionTagType, value)) == u_field.enum_field.?.value) { | 392 | 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); |
| 394 | } | 394 | } |
| 395 | } | 395 | } |
| 396 | try out_stream.writeAll(" }"); | 396 | try writer.writeAll(" }"); |
| 397 | } else { | 397 | } else { |
| 398 | try format(out_stream, "@{x}", .{@ptrToInt(&value)}); | 398 | try format(writer, "@{x}", .{@ptrToInt(&value)}); |
| 399 | } | 399 | } |
| 400 | }, | 400 | }, |
| 401 | .Struct => |StructT| { | 401 | .Struct => |StructT| { |
| 402 | try out_stream.writeAll(@typeName(T)); | 402 | try writer.writeAll(@typeName(T)); |
| 403 | if (max_depth == 0) { | 403 | if (max_depth == 0) { |
| 404 | return out_stream.writeAll("{ ... }"); | 404 | return writer.writeAll("{ ... }"); |
| 405 | } | 405 | } |
| 406 | try out_stream.writeAll("{"); | 406 | try writer.writeAll("{"); |
| 407 | inline for (StructT.fields) |f, i| { | 407 | inline for (StructT.fields) |f, i| { |
| 408 | if (i == 0) { | 408 | if (i == 0) { |
| 409 | try out_stream.writeAll(" ."); | 409 | try writer.writeAll(" ."); |
| 410 | } else { | 410 | } else { |
| 411 | try out_stream.writeAll(", ."); | 411 | try writer.writeAll(", ."); |
| 412 | } | 412 | } |
| 413 | try out_stream.writeAll(f.name); | 413 | try writer.writeAll(f.name); |
| 414 | try out_stream.writeAll(" = "); | 414 | try writer.writeAll(" = "); |
| 415 | try formatType(@field(value, f.name), fmt, options, out_stream, max_depth - 1); | 415 | try formatType(@field(value, f.name), fmt, options, writer, max_depth - 1); |
| 416 | } | 416 | } |
| 417 | try out_stream.writeAll(" }"); | 417 | try writer.writeAll(" }"); |
| 418 | }, | 418 | }, |
| 419 | .Pointer => |ptr_info| switch (ptr_info.size) { | 419 | .Pointer => |ptr_info| switch (ptr_info.size) { |
| 420 | .One => switch (@typeInfo(ptr_info.child)) { | 420 | .One => switch (@typeInfo(ptr_info.child)) { |
| 421 | .Array => |info| { | 421 | .Array => |info| { |
| 422 | if (info.child == u8) { | 422 | if (info.child == u8) { |
| 423 | return formatText(value, fmt, options, out_stream); | 423 | return formatText(value, fmt, options, writer); |
| 424 | } | 424 | } |
| 425 | return format(out_stream, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }); | 425 | return format(writer, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }); |
| 426 | }, | 426 | }, |
| 427 | .Enum, .Union, .Struct => { | 427 | .Enum, .Union, .Struct => { |
| 428 | return formatType(value.*, fmt, options, out_stream, max_depth); | 428 | return formatType(value.*, fmt, options, writer, max_depth); |
| 429 | }, | 429 | }, |
| 430 | else => return format(out_stream, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }), | 430 | else => return format(writer, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }), |
| 431 | }, | 431 | }, |
| 432 | .Many, .C => { | 432 | .Many, .C => { |
| 433 | if (ptr_info.sentinel) |sentinel| { | 433 | 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); |
| 435 | } | 435 | } |
| 436 | if (ptr_info.child == u8) { | 436 | if (ptr_info.child == u8) { |
| 437 | if (fmt.len > 0 and fmt[0] == 's') { | 437 | 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); |
| 439 | } | 439 | } |
| 440 | } | 440 | } |
| 441 | return format(out_stream, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }); | 441 | return format(writer, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }); |
| 442 | }, | 442 | }, |
| 443 | .Slice => { | 443 | .Slice => { |
| 444 | if (fmt.len > 0 and ((fmt[0] == 'x') or (fmt[0] == 'X'))) { | 444 | 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); |
| 446 | } | 446 | } |
| 447 | if (ptr_info.child == u8) { | 447 | if (ptr_info.child == u8) { |
| 448 | return formatText(value, fmt, options, out_stream); | 448 | return formatText(value, fmt, options, writer); |
| 449 | } | 449 | } |
| 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) }); |
| 451 | }, | 451 | }, |
| 452 | }, | 452 | }, |
| 453 | .Array => |info| { | 453 | .Array => |info| { |
| ... | @@ -462,27 +462,27 @@ pub fn formatType( | ... | @@ -462,27 +462,27 @@ pub fn formatType( |
| 462 | .sentinel = null, | 462 | .sentinel = null, |
| 463 | }, | 463 | }, |
| 464 | }); | 464 | }); |
| 465 | return formatType(@as(Slice, &value), fmt, options, out_stream, max_depth); | 465 | return formatType(@as(Slice, &value), fmt, options, writer, max_depth); |
| 466 | }, | 466 | }, |
| 467 | .Vector => { | 467 | .Vector => { |
| 468 | const len = @typeInfo(T).Vector.len; | 468 | const len = @typeInfo(T).Vector.len; |
| 469 | try out_stream.writeAll("{ "); | 469 | try writer.writeAll("{ "); |
| 470 | var i: usize = 0; | 470 | var i: usize = 0; |
| 471 | while (i < len) : (i += 1) { | 471 | while (i < len) : (i += 1) { |
| 472 | try formatValue(value[i], fmt, options, out_stream); | 472 | try formatValue(value[i], fmt, options, writer); |
| 473 | if (i < len - 1) { | 473 | if (i < len - 1) { |
| 474 | try out_stream.writeAll(", "); | 474 | try writer.writeAll(", "); |
| 475 | } | 475 | } |
| 476 | } | 476 | } |
| 477 | try out_stream.writeAll(" }"); | 477 | try writer.writeAll(" }"); |
| 478 | }, | 478 | }, |
| 479 | .Fn => { | 479 | .Fn => { |
| 480 | return format(out_stream, "{}@{x}", .{ @typeName(T), @ptrToInt(value) }); | 480 | return format(writer, "{}@{x}", .{ @typeName(T), @ptrToInt(value) }); |
| 481 | }, | 481 | }, |
| 482 | .Type => return out_stream.writeAll(@typeName(T)), | 482 | .Type => return writer.writeAll(@typeName(T)), |
| 483 | .EnumLiteral => { | 483 | .EnumLiteral => { |
| 484 | const buffer = [_]u8{'.'} ++ @tagName(value); | 484 | const buffer = [_]u8{'.'} ++ @tagName(value); |
| 485 | return formatType(buffer, fmt, options, out_stream, max_depth); | 485 | return formatType(buffer, fmt, options, writer, max_depth); |
| 486 | }, | 486 | }, |
| 487 | else => @compileError("Unable to format type '" ++ @typeName(T) ++ "'"), | 487 | else => @compileError("Unable to format type '" ++ @typeName(T) ++ "'"), |
| 488 | } | 488 | } |
| ... | @@ -492,19 +492,19 @@ fn formatValue( | ... | @@ -492,19 +492,19 @@ fn formatValue( |
| 492 | value: var, | 492 | value: var, |
| 493 | comptime fmt: []const u8, | 493 | comptime fmt: []const u8, |
| 494 | options: FormatOptions, | 494 | options: FormatOptions, |
| 495 | out_stream: var, | 495 | writer: var, |
| 496 | ) !void { | 496 | ) !void { |
| 497 | if (comptime std.mem.eql(u8, fmt, "B")) { | 497 | if (comptime std.mem.eql(u8, fmt, "B")) { |
| 498 | return formatBytes(value, options, 1000, out_stream); | 498 | return formatBytes(value, options, 1000, writer); |
| 499 | } else if (comptime std.mem.eql(u8, fmt, "Bi")) { | 499 | } else if (comptime std.mem.eql(u8, fmt, "Bi")) { |
| 500 | return formatBytes(value, options, 1024, out_stream); | 500 | return formatBytes(value, options, 1024, writer); |
| 501 | } | 501 | } |
| 502 | | 502 | |
| 503 | const T = @TypeOf(value); | 503 | const T = @TypeOf(value); |
| 504 | switch (@typeInfo(T)) { | 504 | switch (@typeInfo(T)) { |
| 505 | .Float, .ComptimeFloat => return formatFloatValue(value, fmt, options, out_stream), | 505 | .Float, .ComptimeFloat => return formatFloatValue(value, fmt, options, writer), |
| 506 | .Int, .ComptimeInt => return formatIntValue(value, fmt, options, out_stream), | 506 | .Int, .ComptimeInt => return formatIntValue(value, fmt, options, writer), |
| 507 | .Bool => return formatBuf(if (value) "true" else "false", options, out_stream), | 507 | .Bool => return formatBuf(if (value) "true" else "false", options, writer), |
| 508 | else => comptime unreachable, | 508 | else => comptime unreachable, |
| 509 | } | 509 | } |
| 510 | } | 510 | } |
| ... | @@ -513,7 +513,7 @@ pub fn formatIntValue( | ... | @@ -513,7 +513,7 @@ pub fn formatIntValue( |
| 513 | value: var, | 513 | value: var, |
| 514 | comptime fmt: []const u8, | 514 | comptime fmt: []const u8, |
| 515 | options: FormatOptions, | 515 | options: FormatOptions, |
| 516 | out_stream: var, | 516 | writer: var, |
| 517 | ) !void { | 517 | ) !void { |
| 518 | comptime var radix = 10; | 518 | comptime var radix = 10; |
| 519 | comptime var uppercase = false; | 519 | comptime var uppercase = false; |
| ... | @@ -529,7 +529,7 @@ pub fn formatIntValue( | ... | @@ -529,7 +529,7 @@ pub fn formatIntValue( |
| 529 | uppercase = false; | 529 | uppercase = false; |
| 530 | } else if (comptime std.mem.eql(u8, fmt, "c")) { | 530 | } else if (comptime std.mem.eql(u8, fmt, "c")) { |
| 531 | if (@TypeOf(int_value).bit_count <= 8) { | 531 | 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); |
| 533 | } else { | 533 | } else { |
| 534 | @compileError("Cannot print integer that is larger than 8 bits as a ascii"); | 534 | @compileError("Cannot print integer that is larger than 8 bits as a ascii"); |
| 535 | } | 535 | } |
| ... | @@ -546,19 +546,19 @@ pub fn formatIntValue( | ... | @@ -546,19 +546,19 @@ pub fn formatIntValue( |
| 546 | @compileError("Unknown format string: '" ++ fmt ++ "'"); | 546 | @compileError("Unknown format string: '" ++ fmt ++ "'"); |
| 547 | } | 547 | } |
| 548 | | 548 | |
| 549 | return formatInt(int_value, radix, uppercase, options, out_stream); | 549 | return formatInt(int_value, radix, uppercase, options, writer); |
| 550 | } | 550 | } |
| 551 | | 551 | |
| 552 | fn formatFloatValue( | 552 | fn formatFloatValue( |
| 553 | value: var, | 553 | value: var, |
| 554 | comptime fmt: []const u8, | 554 | comptime fmt: []const u8, |
| 555 | options: FormatOptions, | 555 | options: FormatOptions, |
| 556 | out_stream: var, | 556 | writer: var, |
| 557 | ) !void { | 557 | ) !void { |
| 558 | if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "e")) { | 558 | 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); |
| 560 | } else if (comptime std.mem.eql(u8, fmt, "d")) { | 560 | } else if (comptime std.mem.eql(u8, fmt, "d")) { |
| 561 | return formatFloatDecimal(value, options, out_stream); | 561 | return formatFloatDecimal(value, options, writer); |
| 562 | } else { | 562 | } else { |
| 563 | @compileError("Unknown format string: '" ++ fmt ++ "'"); | 563 | @compileError("Unknown format string: '" ++ fmt ++ "'"); |
| 564 | } | 564 | } |
| ... | @@ -568,13 +568,13 @@ pub fn formatText( | ... | @@ -568,13 +568,13 @@ pub fn formatText( |
| 568 | bytes: []const u8, | 568 | bytes: []const u8, |
| 569 | comptime fmt: []const u8, | 569 | comptime fmt: []const u8, |
| 570 | options: FormatOptions, | 570 | options: FormatOptions, |
| 571 | out_stream: var, | 571 | writer: var, |
| 572 | ) !void { | 572 | ) !void { |
| 573 | if (comptime std.mem.eql(u8, fmt, "s") or (fmt.len == 0)) { | 573 | 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); |
| 575 | } else if (comptime (std.mem.eql(u8, fmt, "x") or std.mem.eql(u8, fmt, "X"))) { | 575 | } else if (comptime (std.mem.eql(u8, fmt, "x") or std.mem.eql(u8, fmt, "X"))) { |
| 576 | for (bytes) |c| { | 576 | 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); |
| 578 | } | 578 | } |
| 579 | return; | 579 | return; |
| 580 | } else { | 580 | } else { |
| ... | @@ -585,38 +585,38 @@ pub fn formatText( | ... | @@ -585,38 +585,38 @@ pub fn formatText( |
| 585 | pub fn formatAsciiChar( | 585 | pub fn formatAsciiChar( |
| 586 | c: u8, | 586 | c: u8, |
| 587 | options: FormatOptions, | 587 | options: FormatOptions, |
| 588 | out_stream: var, | 588 | writer: var, |
| 589 | ) !void { | 589 | ) !void { |
| 590 | return out_stream.writeAll(@as(*const [1]u8, &c)); | 590 | return writer.writeAll(@as(*const [1]u8, &c)); |
| 591 | } | 591 | } |
| 592 | | 592 | |
| 593 | pub fn formatBuf( | 593 | pub fn formatBuf( |
| 594 | buf: []const u8, | 594 | buf: []const u8, |
| 595 | options: FormatOptions, | 595 | options: FormatOptions, |
| 596 | out_stream: var, | 596 | writer: var, |
| 597 | ) !void { | 597 | ) !void { |
| 598 | const width = options.width orelse buf.len; | 598 | const width = options.width orelse buf.len; |
| 599 | var padding = if (width > buf.len) (width - buf.len) else 0; | 599 | var padding = if (width > buf.len) (width - buf.len) else 0; |
| 600 | const pad_byte = [1]u8{options.fill}; | 600 | const pad_byte = [1]u8{options.fill}; |
| 601 | switch (options.alignment) { | 601 | switch (options.alignment) { |
| 602 | .Left => { | 602 | .Left => { |
| 603 | try out_stream.writeAll(buf); | 603 | try writer.writeAll(buf); |
| 604 | while (padding > 0) : (padding -= 1) { | 604 | while (padding > 0) : (padding -= 1) { |
| 605 | try out_stream.writeAll(&pad_byte); | 605 | try writer.writeAll(&pad_byte); |
| 606 | } | 606 | } |
| 607 | }, | 607 | }, |
| 608 | .Center => { | 608 | .Center => { |
| 609 | const padl = padding / 2; | 609 | const padl = padding / 2; |
| 610 | var i: usize = 0; | 610 | var i: usize = 0; |
| 611 | while (i < padl) : (i += 1) try out_stream.writeAll(&pad_byte); | 611 | while (i < padl) : (i += 1) try writer.writeAll(&pad_byte); |
| 612 | try out_stream.writeAll(buf); | 612 | try writer.writeAll(buf); |
| 613 | while (i < padding) : (i += 1) try out_stream.writeAll(&pad_byte); | 613 | while (i < padding) : (i += 1) try writer.writeAll(&pad_byte); |
| 614 | }, | 614 | }, |
| 615 | .Right => { | 615 | .Right => { |
| 616 | while (padding > 0) : (padding -= 1) { | 616 | while (padding > 0) : (padding -= 1) { |
| 617 | try out_stream.writeAll(&pad_byte); | 617 | try writer.writeAll(&pad_byte); |
| 618 | } | 618 | } |
| 619 | try out_stream.writeAll(buf); | 619 | try writer.writeAll(buf); |
| 620 | }, | 620 | }, |
| 621 | } | 621 | } |
| 622 | } | 622 | } |
| ... | @@ -627,38 +627,38 @@ pub fn formatBuf( | ... | @@ -627,38 +627,38 @@ pub fn formatBuf( |
| 627 | pub fn formatFloatScientific( | 627 | pub fn formatFloatScientific( |
| 628 | value: var, | 628 | value: var, |
| 629 | options: FormatOptions, | 629 | options: FormatOptions, |
| 630 | out_stream: var, | 630 | writer: var, |
| 631 | ) !void { | 631 | ) !void { |
| 632 | var x = @floatCast(f64, value); | 632 | var x = @floatCast(f64, value); |
| 633 | | 633 | |
| 634 | // Errol doesn't handle these special cases. | 634 | // Errol doesn't handle these special cases. |
| 635 | if (math.signbit(x)) { | 635 | if (math.signbit(x)) { |
| 636 | try out_stream.writeAll("-"); | 636 | try writer.writeAll("-"); |
| 637 | x = -x; | 637 | x = -x; |
| 638 | } | 638 | } |
| 639 | | 639 | |
| 640 | if (math.isNan(x)) { | 640 | if (math.isNan(x)) { |
| 641 | return out_stream.writeAll("nan"); | 641 | return writer.writeAll("nan"); |
| 642 | } | 642 | } |
| 643 | if (math.isPositiveInf(x)) { | 643 | if (math.isPositiveInf(x)) { |
| 644 | return out_stream.writeAll("inf"); | 644 | return writer.writeAll("inf"); |
| 645 | } | 645 | } |
| 646 | if (x == 0.0) { | 646 | if (x == 0.0) { |
| 647 | try out_stream.writeAll("0"); | 647 | try writer.writeAll("0"); |
| 648 | | 648 | |
| 649 | if (options.precision) |precision| { | 649 | if (options.precision) |precision| { |
| 650 | if (precision != 0) { | 650 | if (precision != 0) { |
| 651 | try out_stream.writeAll("."); | 651 | try writer.writeAll("."); |
| 652 | var i: usize = 0; | 652 | var i: usize = 0; |
| 653 | while (i < precision) : (i += 1) { | 653 | while (i < precision) : (i += 1) { |
| 654 | try out_stream.writeAll("0"); | 654 | try writer.writeAll("0"); |
| 655 | } | 655 | } |
| 656 | } | 656 | } |
| 657 | } else { | 657 | } else { |
| 658 | try out_stream.writeAll(".0"); | 658 | try writer.writeAll(".0"); |
| 659 | } | 659 | } |
| 660 | | 660 | |
| 661 | try out_stream.writeAll("e+00"); | 661 | try writer.writeAll("e+00"); |
| 662 | return; | 662 | return; |
| 663 | } | 663 | } |
| 664 | | 664 | |
| ... | @@ -668,50 +668,50 @@ pub fn formatFloatScientific( | ... | @@ -668,50 +668,50 @@ pub fn formatFloatScientific( |
| 668 | if (options.precision) |precision| { | 668 | if (options.precision) |precision| { |
| 669 | errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Scientific); | 669 | errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Scientific); |
| 670 | | 670 | |
| 671 | try out_stream.writeAll(float_decimal.digits[0..1]); | 671 | try writer.writeAll(float_decimal.digits[0..1]); |
| 672 | | 672 | |
| 673 | // {e0} case prints no `.` | 673 | // {e0} case prints no `.` |
| 674 | if (precision != 0) { | 674 | if (precision != 0) { |
| 675 | try out_stream.writeAll("."); | 675 | try writer.writeAll("."); |
| 676 | | 676 | |
| 677 | var printed: usize = 0; | 677 | var printed: usize = 0; |
| 678 | if (float_decimal.digits.len > 1) { | 678 | if (float_decimal.digits.len > 1) { |
| 679 | const num_digits = math.min(float_decimal.digits.len, precision + 1); | 679 | 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]); |
| 681 | printed += num_digits - 1; | 681 | printed += num_digits - 1; |
| 682 | } | 682 | } |
| 683 | | 683 | |
| 684 | while (printed < precision) : (printed += 1) { | 684 | while (printed < precision) : (printed += 1) { |
| 685 | try out_stream.writeAll("0"); | 685 | try writer.writeAll("0"); |
| 686 | } | 686 | } |
| 687 | } | 687 | } |
| 688 | } else { | 688 | } else { |
| 689 | try out_stream.writeAll(float_decimal.digits[0..1]); | 689 | try writer.writeAll(float_decimal.digits[0..1]); |
| 690 | try out_stream.writeAll("."); | 690 | try writer.writeAll("."); |
| 691 | if (float_decimal.digits.len > 1) { | 691 | if (float_decimal.digits.len > 1) { |
| 692 | const num_digits = if (@TypeOf(value) == f32) math.min(@as(usize, 9), float_decimal.digits.len) else float_decimal.digits.len; | 692 | const num_digits = if (@TypeOf(value) == f32) math.min(@as(usize, 9), float_decimal.digits.len) else float_decimal.digits.len; |
| 693 | | 693 | |
| 694 | try out_stream.writeAll(float_decimal.digits[1..num_digits]); | 694 | try writer.writeAll(float_decimal.digits[1..num_digits]); |
| 695 | } else { | 695 | } else { |
| 696 | try out_stream.writeAll("0"); | 696 | try writer.writeAll("0"); |
| 697 | } | 697 | } |
| 698 | } | 698 | } |
| 699 | | 699 | |
| 700 | try out_stream.writeAll("e"); | 700 | try writer.writeAll("e"); |
| 701 | const exp = float_decimal.exp - 1; | 701 | const exp = float_decimal.exp - 1; |
| 702 | | 702 | |
| 703 | if (exp >= 0) { | 703 | if (exp >= 0) { |
| 704 | try out_stream.writeAll("+"); | 704 | try writer.writeAll("+"); |
| 705 | if (exp > -10 and exp < 10) { | 705 | if (exp > -10 and exp < 10) { |
| 706 | try out_stream.writeAll("0"); | 706 | try writer.writeAll("0"); |
| 707 | } | 707 | } |
| 708 | try formatInt(exp, 10, false, FormatOptions{ .width = 0 }, out_stream); | 708 | try formatInt(exp, 10, false, FormatOptions{ .width = 0 }, writer); |
| 709 | } else { | 709 | } else { |
| 710 | try out_stream.writeAll("-"); | 710 | try writer.writeAll("-"); |
| 711 | if (exp > -10 and exp < 10) { | 711 | if (exp > -10 and exp < 10) { |
| 712 | try out_stream.writeAll("0"); | 712 | try writer.writeAll("0"); |
| 713 | } | 713 | } |
| 714 | try formatInt(-exp, 10, false, FormatOptions{ .width = 0 }, out_stream); | 714 | try formatInt(-exp, 10, false, FormatOptions{ .width = 0 }, writer); |
| 715 | } | 715 | } |
| 716 | } | 716 | } |
| 717 | | 717 | |
| ... | @@ -720,34 +720,34 @@ pub fn formatFloatScientific( | ... | @@ -720,34 +720,34 @@ pub fn formatFloatScientific( |
| 720 | pub fn formatFloatDecimal( | 720 | pub fn formatFloatDecimal( |
| 721 | value: var, | 721 | value: var, |
| 722 | options: FormatOptions, | 722 | options: FormatOptions, |
| 723 | out_stream: var, | 723 | writer: var, |
| 724 | ) !void { | 724 | ) !void { |
| 725 | var x = @as(f64, value); | 725 | var x = @as(f64, value); |
| 726 | | 726 | |
| 727 | // Errol doesn't handle these special cases. | 727 | // Errol doesn't handle these special cases. |
| 728 | if (math.signbit(x)) { | 728 | if (math.signbit(x)) { |
| 729 | try out_stream.writeAll("-"); | 729 | try writer.writeAll("-"); |
| 730 | x = -x; | 730 | x = -x; |
| 731 | } | 731 | } |
| 732 | | 732 | |
| 733 | if (math.isNan(x)) { | 733 | if (math.isNan(x)) { |
| 734 | return out_stream.writeAll("nan"); | 734 | return writer.writeAll("nan"); |
| 735 | } | 735 | } |
| 736 | if (math.isPositiveInf(x)) { | 736 | if (math.isPositiveInf(x)) { |
| 737 | return out_stream.writeAll("inf"); | 737 | return writer.writeAll("inf"); |
| 738 | } | 738 | } |
| 739 | if (x == 0.0) { | 739 | if (x == 0.0) { |
| 740 | try out_stream.writeAll("0"); | 740 | try writer.writeAll("0"); |
| 741 | | 741 | |
| 742 | if (options.precision) |precision| { | 742 | if (options.precision) |precision| { |
| 743 | if (precision != 0) { | 743 | if (precision != 0) { |
| 744 | try out_stream.writeAll("."); | 744 | try writer.writeAll("."); |
| 745 | var i: usize = 0; | 745 | var i: usize = 0; |
| 746 | while (i < precision) : (i += 1) { | 746 | while (i < precision) : (i += 1) { |
| 747 | try out_stream.writeAll("0"); | 747 | try writer.writeAll("0"); |
| 748 | } | 748 | } |
| 749 | } else { | 749 | } else { |
| 750 | try out_stream.writeAll(".0"); | 750 | try writer.writeAll(".0"); |
| 751 | } | 751 | } |
| 752 | } | 752 | } |
| 753 | | 753 | |
| ... | @@ -769,14 +769,14 @@ pub fn formatFloatDecimal( | ... | @@ -769,14 +769,14 @@ pub fn formatFloatDecimal( |
| 769 | | 769 | |
| 770 | if (num_digits_whole > 0) { | 770 | if (num_digits_whole > 0) { |
| 771 | // We may have to zero pad, for instance 1e4 requires zero padding. | 771 | // 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]); |
| 773 | | 773 | |
| 774 | var i = num_digits_whole_no_pad; | 774 | var i = num_digits_whole_no_pad; |
| 775 | while (i < num_digits_whole) : (i += 1) { | 775 | while (i < num_digits_whole) : (i += 1) { |
| 776 | try out_stream.writeAll("0"); | 776 | try writer.writeAll("0"); |
| 777 | } | 777 | } |
| 778 | } else { | 778 | } else { |
| 779 | try out_stream.writeAll("0"); | 779 | try writer.writeAll("0"); |
| 780 | } | 780 | } |
| 781 | | 781 | |
| 782 | // {.0} special case doesn't want a trailing '.' | 782 | // {.0} special case doesn't want a trailing '.' |
| ... | @@ -784,7 +784,7 @@ pub fn formatFloatDecimal( | ... | @@ -784,7 +784,7 @@ pub fn formatFloatDecimal( |
| 784 | return; | 784 | return; |
| 785 | } | 785 | } |
| 786 | | 786 | |
| 787 | try out_stream.writeAll("."); | 787 | try writer.writeAll("."); |
| 788 | | 788 | |
| 789 | // Keep track of fractional count printed for case where we pre-pad then post-pad with 0's. | 789 | // Keep track of fractional count printed for case where we pre-pad then post-pad with 0's. |
| 790 | var printed: usize = 0; | 790 | var printed: usize = 0; |
| ... | @@ -796,7 +796,7 @@ pub fn formatFloatDecimal( | ... | @@ -796,7 +796,7 @@ pub fn formatFloatDecimal( |
| 796 | | 796 | |
| 797 | var i: usize = 0; | 797 | var i: usize = 0; |
| 798 | while (i < zeros_to_print) : (i += 1) { | 798 | while (i < zeros_to_print) : (i += 1) { |
| 799 | try out_stream.writeAll("0"); | 799 | try writer.writeAll("0"); |
| 800 | printed += 1; | 800 | printed += 1; |
| 801 | } | 801 | } |
| 802 | | 802 | |
| ... | @@ -808,14 +808,14 @@ pub fn formatFloatDecimal( | ... | @@ -808,14 +808,14 @@ pub fn formatFloatDecimal( |
| 808 | // Remaining fractional portion, zero-padding if insufficient. | 808 | // Remaining fractional portion, zero-padding if insufficient. |
| 809 | assert(precision >= printed); | 809 | assert(precision >= printed); |
| 810 | if (num_digits_whole_no_pad + precision - printed < float_decimal.digits.len) { | 810 | 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]); |
| 812 | return; | 812 | return; |
| 813 | } else { | 813 | } 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..]); |
| 815 | printed += float_decimal.digits.len - num_digits_whole_no_pad; | 815 | printed += float_decimal.digits.len - num_digits_whole_no_pad; |
| 816 | | 816 | |
| 817 | while (printed < precision) : (printed += 1) { | 817 | while (printed < precision) : (printed += 1) { |
| 818 | try out_stream.writeAll("0"); | 818 | try writer.writeAll("0"); |
| 819 | } | 819 | } |
| 820 | } | 820 | } |
| 821 | } else { | 821 | } else { |
| ... | @@ -827,14 +827,14 @@ pub fn formatFloatDecimal( | ... | @@ -827,14 +827,14 @@ pub fn formatFloatDecimal( |
| 827 | | 827 | |
| 828 | if (num_digits_whole > 0) { | 828 | if (num_digits_whole > 0) { |
| 829 | // We may have to zero pad, for instance 1e4 requires zero padding. | 829 | // 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]); |
| 831 | | 831 | |
| 832 | var i = num_digits_whole_no_pad; | 832 | var i = num_digits_whole_no_pad; |
| 833 | while (i < num_digits_whole) : (i += 1) { | 833 | while (i < num_digits_whole) : (i += 1) { |
| 834 | try out_stream.writeAll("0"); | 834 | try writer.writeAll("0"); |
| 835 | } | 835 | } |
| 836 | } else { | 836 | } else { |
| 837 | try out_stream.writeAll("0"); | 837 | try writer.writeAll("0"); |
| 838 | } | 838 | } |
| 839 | | 839 | |
| 840 | // Omit `.` if no fractional portion | 840 | // Omit `.` if no fractional portion |
| ... | @@ -842,7 +842,7 @@ pub fn formatFloatDecimal( | ... | @@ -842,7 +842,7 @@ pub fn formatFloatDecimal( |
| 842 | return; | 842 | return; |
| 843 | } | 843 | } |
| 844 | | 844 | |
| 845 | try out_stream.writeAll("."); | 845 | try writer.writeAll("."); |
| 846 | | 846 | |
| 847 | // Zero-fill until we reach significant digits or run out of precision. | 847 | // Zero-fill until we reach significant digits or run out of precision. |
| 848 | if (float_decimal.exp < 0) { | 848 | if (float_decimal.exp < 0) { |
| ... | @@ -850,11 +850,11 @@ pub fn formatFloatDecimal( | ... | @@ -850,11 +850,11 @@ pub fn formatFloatDecimal( |
| 850 | | 850 | |
| 851 | var i: usize = 0; | 851 | var i: usize = 0; |
| 852 | while (i < zero_digit_count) : (i += 1) { | 852 | while (i < zero_digit_count) : (i += 1) { |
| 853 | try out_stream.writeAll("0"); | 853 | try writer.writeAll("0"); |
| 854 | } | 854 | } |
| 855 | } | 855 | } |
| 856 | | 856 | |
| 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..]); |
| 858 | } | 858 | } |
| 859 | } | 859 | } |
| 860 | | 860 | |
| ... | @@ -862,10 +862,10 @@ pub fn formatBytes( | ... | @@ -862,10 +862,10 @@ pub fn formatBytes( |
| 862 | value: var, | 862 | value: var, |
| 863 | options: FormatOptions, | 863 | options: FormatOptions, |
| 864 | comptime radix: usize, | 864 | comptime radix: usize, |
| 865 | out_stream: var, | 865 | writer: var, |
| 866 | ) !void { | 866 | ) !void { |
| 867 | if (value == 0) { | 867 | if (value == 0) { |
| 868 | return out_stream.writeAll("0B"); | 868 | return writer.writeAll("0B"); |
| 869 | } | 869 | } |
| 870 | | 870 | |
| 871 | const is_float = comptime std.meta.trait.is(.Float)(@TypeOf(value)); | 871 | const is_float = comptime std.meta.trait.is(.Float)(@TypeOf(value)); |
| ... | @@ -885,10 +885,10 @@ pub fn formatBytes( | ... | @@ -885,10 +885,10 @@ pub fn formatBytes( |
| 885 | else => unreachable, | 885 | else => unreachable, |
| 886 | }; | 886 | }; |
| 887 | | 887 | |
| 888 | try formatFloatDecimal(new_value, options, out_stream); | 888 | try formatFloatDecimal(new_value, options, writer); |
| 889 | | 889 | |
| 890 | if (suffix == ' ') { | 890 | if (suffix == ' ') { |
| 891 | return out_stream.writeAll("B"); | 891 | return writer.writeAll("B"); |
| 892 | } | 892 | } |
| 893 | | 893 | |
| 894 | const buf = switch (radix) { | 894 | const buf = switch (radix) { |
| ... | @@ -896,7 +896,7 @@ pub fn formatBytes( | ... | @@ -896,7 +896,7 @@ pub fn formatBytes( |
| 896 | 1024 => &[_]u8{ suffix, 'i', 'B' }, | 896 | 1024 => &[_]u8{ suffix, 'i', 'B' }, |
| 897 | else => unreachable, | 897 | else => unreachable, |
| 898 | }; | 898 | }; |
| 899 | return out_stream.writeAll(buf); | 899 | return writer.writeAll(buf); |
| 900 | } | 900 | } |
| 901 | | 901 | |
| 902 | pub fn formatInt( | 902 | pub fn formatInt( |
| ... | @@ -904,7 +904,7 @@ pub fn formatInt( | ... | @@ -904,7 +904,7 @@ pub fn formatInt( |
| 904 | base: u8, | 904 | base: u8, |
| 905 | uppercase: bool, | 905 | uppercase: bool, |
| 906 | options: FormatOptions, | 906 | options: FormatOptions, |
| 907 | out_stream: var, | 907 | writer: var, |
| 908 | ) !void { | 908 | ) !void { |
| 909 | const int_value = if (@TypeOf(value) == comptime_int) blk: { | 909 | const int_value = if (@TypeOf(value) == comptime_int) blk: { |
| 910 | const Int = math.IntFittingRange(value, value); | 910 | const Int = math.IntFittingRange(value, value); |
| ... | @@ -913,9 +913,9 @@ pub fn formatInt( | ... | @@ -913,9 +913,9 @@ pub fn formatInt( |
| 913 | value; | 913 | value; |
| 914 | | 914 | |
| 915 | if (@TypeOf(int_value).is_signed) { | 915 | 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); |
| 917 | } else { | 917 | } else { |
| 918 | return formatIntUnsigned(int_value, base, uppercase, options, out_stream); | 918 | return formatIntUnsigned(int_value, base, uppercase, options, writer); |
| 919 | } | 919 | } |
| 920 | } | 920 | } |
| 921 | | 921 | |
| ... | @@ -924,7 +924,7 @@ fn formatIntSigned( | ... | @@ -924,7 +924,7 @@ fn formatIntSigned( |
| 924 | base: u8, | 924 | base: u8, |
| 925 | uppercase: bool, | 925 | uppercase: bool, |
| 926 | options: FormatOptions, | 926 | options: FormatOptions, |
| 927 | out_stream: var, | 927 | writer: var, |
| 928 | ) !void { | 928 | ) !void { |
| 929 | const new_options = FormatOptions{ | 929 | const new_options = FormatOptions{ |
| 930 | .width = if (options.width) |w| (if (w == 0) 0 else w - 1) else null, | 930 | .width = if (options.width) |w| (if (w == 0) 0 else w - 1) else null, |
| ... | @@ -934,15 +934,15 @@ fn formatIntSigned( | ... | @@ -934,15 +934,15 @@ fn formatIntSigned( |
| 934 | const bit_count = @typeInfo(@TypeOf(value)).Int.bits; | 934 | const bit_count = @typeInfo(@TypeOf(value)).Int.bits; |
| 935 | const Uint = std.meta.Int(false, bit_count); | 935 | const Uint = std.meta.Int(false, bit_count); |
| 936 | if (value < 0) { | 936 | if (value < 0) { |
| 937 | try out_stream.writeAll("-"); | 937 | try writer.writeAll("-"); |
| 938 | const new_value = math.absCast(value); | 938 | 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); |
| 940 | } else if (options.width == null or options.width.? == 0) { | 940 | } 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); |
| 942 | } else { | 942 | } else { |
| 943 | try out_stream.writeAll("+"); | 943 | try writer.writeAll("+"); |
| 944 | const new_value = @intCast(Uint, value); | 944 | 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); |
| 946 | } | 946 | } |
| 947 | } | 947 | } |
| 948 | | 948 | |
| ... | @@ -951,7 +951,7 @@ fn formatIntUnsigned( | ... | @@ -951,7 +951,7 @@ fn formatIntUnsigned( |
| 951 | base: u8, | 951 | base: u8, |
| 952 | uppercase: bool, | 952 | uppercase: bool, |
| 953 | options: FormatOptions, | 953 | options: FormatOptions, |
| 954 | out_stream: var, | 954 | writer: var, |
| 955 | ) !void { | 955 | ) !void { |
| 956 | assert(base >= 2); | 956 | assert(base >= 2); |
| 957 | var buf: [math.max(@TypeOf(value).bit_count, 1)]u8 = undefined; | 957 | var buf: [math.max(@TypeOf(value).bit_count, 1)]u8 = undefined; |
| ... | @@ -976,22 +976,22 @@ fn formatIntUnsigned( | ... | @@ -976,22 +976,22 @@ fn formatIntUnsigned( |
| 976 | const zero_byte: u8 = options.fill; | 976 | const zero_byte: u8 = options.fill; |
| 977 | var leftover_padding = padding - index; | 977 | var leftover_padding = padding - index; |
| 978 | while (true) { | 978 | 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..]); |
| 980 | leftover_padding -= 1; | 980 | leftover_padding -= 1; |
| 981 | if (leftover_padding == 0) break; | 981 | if (leftover_padding == 0) break; |
| 982 | } | 982 | } |
| 983 | mem.set(u8, buf[0..index], options.fill); | 983 | mem.set(u8, buf[0..index], options.fill); |
| 984 | return out_stream.writeAll(&buf); | 984 | return writer.writeAll(&buf); |
| 985 | } else { | 985 | } else { |
| 986 | const padded_buf = buf[index - padding ..]; | 986 | const padded_buf = buf[index - padding ..]; |
| 987 | mem.set(u8, padded_buf[0..padding], options.fill); | 987 | mem.set(u8, padded_buf[0..padding], options.fill); |
| 988 | return out_stream.writeAll(padded_buf); | 988 | return writer.writeAll(padded_buf); |
| 989 | } | 989 | } |
| 990 | } | 990 | } |
| 991 | | 991 | |
| 992 | pub fn formatIntBuf(out_buf: []u8, value: var, base: u8, uppercase: bool, options: FormatOptions) usize { | 992 | pub fn formatIntBuf(out_buf: []u8, value: var, base: u8, uppercase: bool, options: FormatOptions) usize { |
| 993 | var fbs = std.io.fixedBufferStream(out_buf); | 993 | 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; |
| 995 | return fbs.pos; | 995 | return fbs.pos; |
| 996 | } | 996 | } |
| 997 | | 997 | |
| ... | @@ -1098,15 +1098,15 @@ pub const BufPrintError = error{ | ... | @@ -1098,15 +1098,15 @@ pub const BufPrintError = error{ |
| 1098 | }; | 1098 | }; |
| 1099 | pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: var) BufPrintError![]u8 { | 1099 | pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: var) BufPrintError![]u8 { |
| 1100 | var fbs = std.io.fixedBufferStream(buf); | 1100 | var fbs = std.io.fixedBufferStream(buf); |
| 1101 | try format(fbs.outStream(), fmt, args); | 1101 | try format(fbs.writer(), fmt, args); |
| 1102 | return fbs.getWritten(); | 1102 | return fbs.getWritten(); |
| 1103 | } | 1103 | } |
| 1104 | | 1104 | |
| 1105 | // Count the characters needed for format. Useful for preallocating memory | 1105 | // Count the characters needed for format. Useful for preallocating memory |
| 1106 | pub fn count(comptime fmt: []const u8, args: var) u64 { | 1106 | pub fn count(comptime fmt: []const u8, args: var) u64 { |
| 1107 | var counting_stream = std.io.countingOutStream(std.io.null_out_stream); | 1107 | var counting_writer = std.io.countingWriter(std.io.null_writer); |
| 1108 | format(counting_stream.outStream(), fmt, args) catch |err| switch (err) {}; | 1108 | format(counting_writer.writer(), fmt, args) catch |err| switch (err) {}; |
| 1109 | return counting_stream.bytes_written; | 1109 | return counting_writer.bytes_written; |
| 1110 | } | 1110 | } |
| 1111 | | 1111 | |
| 1112 | pub const AllocPrintError = error{OutOfMemory}; | 1112 | pub const AllocPrintError = error{OutOfMemory}; |
| ... | @@ -1215,15 +1215,15 @@ test "buffer" { | ... | @@ -1215,15 +1215,15 @@ test "buffer" { |
| 1215 | { | 1215 | { |
| 1216 | var buf1: [32]u8 = undefined; | 1216 | var buf1: [32]u8 = undefined; |
| 1217 | var fbs = std.io.fixedBufferStream(&buf1); | 1217 | 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); |
| 1219 | std.testing.expect(mem.eql(u8, fbs.getWritten(), "1234")); | 1219 | std.testing.expect(mem.eql(u8, fbs.getWritten(), "1234")); |
| 1220 | | 1220 | |
| 1221 | fbs.reset(); | 1221 | fbs.reset(); |
| 1222 | try formatType('a', "c", FormatOptions{}, fbs.outStream(), default_max_depth); | 1222 | try formatType('a', "c", FormatOptions{}, fbs.writer(), default_max_depth); |
| 1223 | std.testing.expect(mem.eql(u8, fbs.getWritten(), "a")); | 1223 | std.testing.expect(mem.eql(u8, fbs.getWritten(), "a")); |
| 1224 | | 1224 | |
| 1225 | fbs.reset(); | 1225 | fbs.reset(); |
| 1226 | try formatType(0b1100, "b", FormatOptions{}, fbs.outStream(), default_max_depth); | 1226 | try formatType(0b1100, "b", FormatOptions{}, fbs.writer(), default_max_depth); |
| 1227 | std.testing.expect(mem.eql(u8, fbs.getWritten(), "1100")); | 1227 | std.testing.expect(mem.eql(u8, fbs.getWritten(), "1100")); |
| 1228 | } | 1228 | } |
| 1229 | } | 1229 | } |
| ... | @@ -1413,12 +1413,12 @@ test "custom" { | ... | @@ -1413,12 +1413,12 @@ test "custom" { |
| 1413 | self: SelfType, | 1413 | self: SelfType, |
| 1414 | comptime fmt: []const u8, | 1414 | comptime fmt: []const u8, |
| 1415 | options: FormatOptions, | 1415 | options: FormatOptions, |
| 1416 | out_stream: var, | 1416 | writer: var, |
| 1417 | ) !void { | 1417 | ) !void { |
| 1418 | if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "p")) { | 1418 | 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 }); |
| 1420 | } else if (comptime std.mem.eql(u8, fmt, "d")) { | 1420 | } 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 }); |
| 1422 | } else { | 1422 | } else { |
| 1423 | @compileError("Unknown format character: '" ++ fmt ++ "'"); | 1423 | @compileError("Unknown format character: '" ++ fmt ++ "'"); |
| 1424 | } | 1424 | } |
| ... | @@ -1604,7 +1604,7 @@ test "formatIntValue with comptime_int" { | ... | @@ -1604,7 +1604,7 @@ test "formatIntValue with comptime_int" { |
| 1604 | | 1604 | |
| 1605 | var buf: [20]u8 = undefined; | 1605 | var buf: [20]u8 = undefined; |
| 1606 | var fbs = std.io.fixedBufferStream(&buf); | 1606 | var fbs = std.io.fixedBufferStream(&buf); |
| 1607 | try formatIntValue(value, "", FormatOptions{}, fbs.outStream()); | 1607 | try formatIntValue(value, "", FormatOptions{}, fbs.writer()); |
| 1608 | std.testing.expect(mem.eql(u8, fbs.getWritten(), "123456789123456789")); | 1608 | std.testing.expect(mem.eql(u8, fbs.getWritten(), "123456789123456789")); |
| 1609 | } | 1609 | } |
| 1610 | | 1610 | |
| ... | @@ -1613,7 +1613,7 @@ test "formatFloatValue with comptime_float" { | ... | @@ -1613,7 +1613,7 @@ test "formatFloatValue with comptime_float" { |
| 1613 | | 1613 | |
| 1614 | var buf: [20]u8 = undefined; | 1614 | var buf: [20]u8 = undefined; |
| 1615 | var fbs = std.io.fixedBufferStream(&buf); | 1615 | var fbs = std.io.fixedBufferStream(&buf); |
| 1616 | try formatFloatValue(value, "", FormatOptions{}, fbs.outStream()); | 1616 | try formatFloatValue(value, "", FormatOptions{}, fbs.writer()); |
| 1617 | std.testing.expect(mem.eql(u8, fbs.getWritten(), "1.0e+00")); | 1617 | std.testing.expect(mem.eql(u8, fbs.getWritten(), "1.0e+00")); |
| 1618 | | 1618 | |
| 1619 | try testFmt("1.0e+00", "{}", .{value}); | 1619 | try testFmt("1.0e+00", "{}", .{value}); |
| ... | @@ -1630,10 +1630,10 @@ test "formatType max_depth" { | ... | @@ -1630,10 +1630,10 @@ test "formatType max_depth" { |
| 1630 | self: SelfType, | 1630 | self: SelfType, |
| 1631 | comptime fmt: []const u8, | 1631 | comptime fmt: []const u8, |
| 1632 | options: FormatOptions, | 1632 | options: FormatOptions, |
| 1633 | out_stream: var, | 1633 | writer: var, |
| 1634 | ) !void { | 1634 | ) !void { |
| 1635 | if (fmt.len == 0) { | 1635 | 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 }); |
| 1637 | } else { | 1637 | } else { |
| 1638 | @compileError("Unknown format string: '" ++ fmt ++ "'"); | 1638 | @compileError("Unknown format string: '" ++ fmt ++ "'"); |
| 1639 | } | 1639 | } |
| ... | @@ -1669,19 +1669,19 @@ test "formatType max_depth" { | ... | @@ -1669,19 +1669,19 @@ test "formatType max_depth" { |
| 1669 | | 1669 | |
| 1670 | var buf: [1000]u8 = undefined; | 1670 | var buf: [1000]u8 = undefined; |
| 1671 | var fbs = std.io.fixedBufferStream(&buf); | 1671 | var fbs = std.io.fixedBufferStream(&buf); |
| 1672 | try formatType(inst, "", FormatOptions{}, fbs.outStream(), 0); | 1672 | try formatType(inst, "", FormatOptions{}, fbs.writer(), 0); |
| 1673 | std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ ... }")); | 1673 | std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ ... }")); |
| 1674 | | 1674 | |
| 1675 | fbs.reset(); | 1675 | fbs.reset(); |
| 1676 | try formatType(inst, "", FormatOptions{}, fbs.outStream(), 1); | 1676 | try formatType(inst, "", FormatOptions{}, fbs.writer(), 1); |
| 1677 | std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }")); | 1677 | std.testing.expect(mem.eql(u8, fbs.getWritten(), "S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }")); |
| 1678 | | 1678 | |
| 1679 | fbs.reset(); | 1679 | fbs.reset(); |
| 1680 | try formatType(inst, "", FormatOptions{}, fbs.outStream(), 2); | 1680 | try formatType(inst, "", FormatOptions{}, fbs.writer(), 2); |
| 1681 | 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) }")); | 1681 | 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) }")); |
| 1682 | | 1682 | |
| 1683 | fbs.reset(); | 1683 | fbs.reset(); |
| 1684 | try formatType(inst, "", FormatOptions{}, fbs.outStream(), 3); | 1684 | try formatType(inst, "", FormatOptions{}, fbs.writer(), 3); |
| 1685 | 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) }")); | 1685 | 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) }")); |
| 1686 | } | 1686 | } |
| 1687 | | 1687 | |