| ... | ... | @@ -69,19 +69,17 @@ fn peekIsAlign(comptime fmt: []const u8) bool { |
| 69 | 69 | /// |
| 70 | 70 | /// If a formatted user type contains a function of the type |
| 71 | 71 | /// ``` |
| 72 | | /// fn format(value: ?, comptime fmt: []const u8, options: std.fmt.FormatOptions, context: var, comptime Errors: type, comptime output: fn (@TypeOf(context), []const u8) Errors!void) Errors!void |
| 72 | /// fn format(value: ?, comptime fmt: []const u8, options: std.fmtstream.FormatOptions, out_stream: var) !void |
| 73 | 73 | /// ``` |
| 74 | 74 | /// with `?` being the type formatted, this function will be called instead of the default implementation. |
| 75 | 75 | /// This allows user types to be formatted in a logical manner instead of dumping all fields of the type. |
| 76 | 76 | /// |
| 77 | 77 | /// A user type may be a `struct`, `vector`, `union` or `enum` type. |
| 78 | 78 | pub fn format( |
| 79 | | context: var, |
| 80 | | comptime Errors: type, |
| 81 | | comptime output: fn (@TypeOf(context), []const u8) Errors!void, |
| 79 | out_stream: var, |
| 82 | 80 | comptime fmt: []const u8, |
| 83 | 81 | args: var, |
| 84 | | ) Errors!void { |
| 82 | ) !void { |
| 85 | 83 | const ArgSetType = u32; |
| 86 | 84 | if (@typeInfo(@TypeOf(args)) != .Struct) { |
| 87 | 85 | @compileError("Expected tuple or struct argument, found " ++ @typeName(@TypeOf(args))); |
| ... | ... | @@ -138,7 +136,7 @@ pub fn format( |
| 138 | 136 | .Start => switch (c) { |
| 139 | 137 | '{' => { |
| 140 | 138 | if (start_index < i) { |
| 141 | | try output(context, fmt[start_index..i]); |
| 139 | try out_stream.writeAll(fmt[start_index..i]); |
| 142 | 140 | } |
| 143 | 141 | |
| 144 | 142 | start_index = i; |
| ... | ... | @@ -150,7 +148,7 @@ pub fn format( |
| 150 | 148 | }, |
| 151 | 149 | '}' => { |
| 152 | 150 | if (start_index < i) { |
| 153 | | try output(context, fmt[start_index..i]); |
| 151 | try out_stream.writeAll(fmt[start_index..i]); |
| 154 | 152 | } |
| 155 | 153 | state = .CloseBrace; |
| 156 | 154 | }, |
| ... | ... | @@ -185,9 +183,7 @@ pub fn format( |
| 185 | 183 | args[arg_to_print], |
| 186 | 184 | fmt[0..0], |
| 187 | 185 | options, |
| 188 | | context, |
| 189 | | Errors, |
| 190 | | output, |
| 186 | out_stream, |
| 191 | 187 | default_max_depth, |
| 192 | 188 | ); |
| 193 | 189 | |
| ... | ... | @@ -218,9 +214,7 @@ pub fn format( |
| 218 | 214 | args[arg_to_print], |
| 219 | 215 | fmt[specifier_start..i], |
| 220 | 216 | options, |
| 221 | | context, |
| 222 | | Errors, |
| 223 | | output, |
| 217 | out_stream, |
| 224 | 218 | default_max_depth, |
| 225 | 219 | ); |
| 226 | 220 | state = .Start; |
| ... | ... | @@ -265,9 +259,7 @@ pub fn format( |
| 265 | 259 | args[arg_to_print], |
| 266 | 260 | fmt[specifier_start..specifier_end], |
| 267 | 261 | options, |
| 268 | | context, |
| 269 | | Errors, |
| 270 | | output, |
| 262 | out_stream, |
| 271 | 263 | default_max_depth, |
| 272 | 264 | ); |
| 273 | 265 | state = .Start; |
| ... | ... | @@ -293,9 +285,7 @@ pub fn format( |
| 293 | 285 | args[arg_to_print], |
| 294 | 286 | fmt[specifier_start..specifier_end], |
| 295 | 287 | options, |
| 296 | | context, |
| 297 | | Errors, |
| 298 | | output, |
| 288 | out_stream, |
| 299 | 289 | default_max_depth, |
| 300 | 290 | ); |
| 301 | 291 | state = .Start; |
| ... | ... | @@ -316,7 +306,7 @@ pub fn format( |
| 316 | 306 | } |
| 317 | 307 | } |
| 318 | 308 | if (start_index < fmt.len) { |
| 319 | | try output(context, fmt[start_index..]); |
| 309 | try out_stream.writeAll(fmt[start_index..]); |
| 320 | 310 | } |
| 321 | 311 | } |
| 322 | 312 | |
| ... | ... | @@ -324,121 +314,119 @@ pub fn formatType( |
| 324 | 314 | value: var, |
| 325 | 315 | comptime fmt: []const u8, |
| 326 | 316 | options: FormatOptions, |
| 327 | | context: var, |
| 328 | | comptime Errors: type, |
| 329 | | comptime output: fn (@TypeOf(context), []const u8) Errors!void, |
| 317 | out_stream: var, |
| 330 | 318 | max_depth: usize, |
| 331 | | ) Errors!void { |
| 319 | ) !void { |
| 332 | 320 | if (comptime std.mem.eql(u8, fmt, "*")) { |
| 333 | | try output(context, @typeName(@TypeOf(value).Child)); |
| 334 | | try output(context, "@"); |
| 335 | | try formatInt(@ptrToInt(value), 16, false, FormatOptions{}, context, Errors, output); |
| 321 | try out_stream.writeAll(@typeName(@TypeOf(value).Child)); |
| 322 | try out_stream.writeAll("@"); |
| 323 | try formatInt(@ptrToInt(value), 16, false, FormatOptions{}, out_stream); |
| 336 | 324 | return; |
| 337 | 325 | } |
| 338 | 326 | |
| 339 | 327 | const T = @TypeOf(value); |
| 340 | 328 | switch (@typeInfo(T)) { |
| 341 | 329 | .ComptimeInt, .Int, .Float => { |
| 342 | | return formatValue(value, fmt, options, context, Errors, output); |
| 330 | return formatValue(value, fmt, options, out_stream); |
| 343 | 331 | }, |
| 344 | 332 | .Void => { |
| 345 | | return output(context, "void"); |
| 333 | return out_stream.writeAll("void"); |
| 346 | 334 | }, |
| 347 | 335 | .Bool => { |
| 348 | | return output(context, if (value) "true" else "false"); |
| 336 | return out_stream.writeAll(if (value) "true" else "false"); |
| 349 | 337 | }, |
| 350 | 338 | .Optional => { |
| 351 | 339 | if (value) |payload| { |
| 352 | | return formatType(payload, fmt, options, context, Errors, output, max_depth); |
| 340 | return formatType(payload, fmt, options, out_stream, max_depth); |
| 353 | 341 | } else { |
| 354 | | return output(context, "null"); |
| 342 | return out_stream.writeAll("null"); |
| 355 | 343 | } |
| 356 | 344 | }, |
| 357 | 345 | .ErrorUnion => { |
| 358 | 346 | if (value) |payload| { |
| 359 | | return formatType(payload, fmt, options, context, Errors, output, max_depth); |
| 347 | return formatType(payload, fmt, options, out_stream, max_depth); |
| 360 | 348 | } else |err| { |
| 361 | | return formatType(err, fmt, options, context, Errors, output, max_depth); |
| 349 | return formatType(err, fmt, options, out_stream, max_depth); |
| 362 | 350 | } |
| 363 | 351 | }, |
| 364 | 352 | .ErrorSet => { |
| 365 | | try output(context, "error."); |
| 366 | | return output(context, @errorName(value)); |
| 353 | try out_stream.writeAll("error."); |
| 354 | return out_stream.writeAll(@errorName(value)); |
| 367 | 355 | }, |
| 368 | 356 | .Enum => |enumInfo| { |
| 369 | 357 | if (comptime std.meta.trait.hasFn("format")(T)) { |
| 370 | | return value.format(fmt, options, context, Errors, output); |
| 358 | return value.format(fmt, options, out_stream); |
| 371 | 359 | } |
| 372 | 360 | |
| 373 | | try output(context, @typeName(T)); |
| 361 | try out_stream.writeAll(@typeName(T)); |
| 374 | 362 | if (enumInfo.is_exhaustive) { |
| 375 | | try output(context, "."); |
| 376 | | try output(context, @tagName(value)); |
| 363 | try out_stream.writeAll("."); |
| 364 | try out_stream.writeAll(@tagName(value)); |
| 377 | 365 | } else { |
| 378 | 366 | // TODO: when @tagName works on exhaustive enums print known enum strings |
| 379 | | try output(context, "("); |
| 380 | | try formatType(@enumToInt(value), fmt, options, context, Errors, output, max_depth); |
| 381 | | try output(context, ")"); |
| 367 | try out_stream.writeAll("("); |
| 368 | try formatType(@enumToInt(value), fmt, options, out_stream, max_depth); |
| 369 | try out_stream.writeAll(")"); |
| 382 | 370 | } |
| 383 | 371 | }, |
| 384 | 372 | .Union => { |
| 385 | 373 | if (comptime std.meta.trait.hasFn("format")(T)) { |
| 386 | | return value.format(fmt, options, context, Errors, output); |
| 374 | return value.format(fmt, options, out_stream); |
| 387 | 375 | } |
| 388 | 376 | |
| 389 | | try output(context, @typeName(T)); |
| 377 | try out_stream.writeAll(@typeName(T)); |
| 390 | 378 | if (max_depth == 0) { |
| 391 | | return output(context, "{ ... }"); |
| 379 | return out_stream.writeAll("{ ... }"); |
| 392 | 380 | } |
| 393 | 381 | const info = @typeInfo(T).Union; |
| 394 | 382 | if (info.tag_type) |UnionTagType| { |
| 395 | | try output(context, "{ ."); |
| 396 | | try output(context, @tagName(@as(UnionTagType, value))); |
| 397 | | try output(context, " = "); |
| 383 | try out_stream.writeAll("{ ."); |
| 384 | try out_stream.writeAll(@tagName(@as(UnionTagType, value))); |
| 385 | try out_stream.writeAll(" = "); |
| 398 | 386 | inline for (info.fields) |u_field| { |
| 399 | 387 | if (@enumToInt(@as(UnionTagType, value)) == u_field.enum_field.?.value) { |
| 400 | | try formatType(@field(value, u_field.name), fmt, options, context, Errors, output, max_depth - 1); |
| 388 | try formatType(@field(value, u_field.name), fmt, options, out_stream, max_depth - 1); |
| 401 | 389 | } |
| 402 | 390 | } |
| 403 | | try output(context, " }"); |
| 391 | try out_stream.writeAll(" }"); |
| 404 | 392 | } else { |
| 405 | | try format(context, Errors, output, "@{x}", .{@ptrToInt(&value)}); |
| 393 | try format(out_stream, "@{x}", .{@ptrToInt(&value)}); |
| 406 | 394 | } |
| 407 | 395 | }, |
| 408 | 396 | .Struct => |StructT| { |
| 409 | 397 | if (comptime std.meta.trait.hasFn("format")(T)) { |
| 410 | | return value.format(fmt, options, context, Errors, output); |
| 398 | return value.format(fmt, options, out_stream); |
| 411 | 399 | } |
| 412 | 400 | |
| 413 | | try output(context, @typeName(T)); |
| 401 | try out_stream.writeAll(@typeName(T)); |
| 414 | 402 | if (max_depth == 0) { |
| 415 | | return output(context, "{ ... }"); |
| 403 | return out_stream.writeAll("{ ... }"); |
| 416 | 404 | } |
| 417 | | try output(context, "{"); |
| 405 | try out_stream.writeAll("{"); |
| 418 | 406 | inline for (StructT.fields) |f, i| { |
| 419 | 407 | if (i == 0) { |
| 420 | | try output(context, " ."); |
| 408 | try out_stream.writeAll(" ."); |
| 421 | 409 | } else { |
| 422 | | try output(context, ", ."); |
| 410 | try out_stream.writeAll(", ."); |
| 423 | 411 | } |
| 424 | | try output(context, f.name); |
| 425 | | try output(context, " = "); |
| 426 | | try formatType(@field(value, f.name), fmt, options, context, Errors, output, max_depth - 1); |
| 412 | try out_stream.writeAll(f.name); |
| 413 | try out_stream.writeAll(" = "); |
| 414 | try formatType(@field(value, f.name), fmt, options, out_stream, max_depth - 1); |
| 427 | 415 | } |
| 428 | | try output(context, " }"); |
| 416 | try out_stream.writeAll(" }"); |
| 429 | 417 | }, |
| 430 | 418 | .Pointer => |ptr_info| switch (ptr_info.size) { |
| 431 | 419 | .One => switch (@typeInfo(ptr_info.child)) { |
| 432 | 420 | .Array => |info| { |
| 433 | 421 | if (info.child == u8) { |
| 434 | | return formatText(value, fmt, options, context, Errors, output); |
| 422 | return formatText(value, fmt, options, out_stream); |
| 435 | 423 | } |
| 436 | | return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }); |
| 424 | return format(out_stream, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }); |
| 437 | 425 | }, |
| 438 | 426 | .Enum, .Union, .Struct => { |
| 439 | | return formatType(value.*, fmt, options, context, Errors, output, max_depth); |
| 427 | return formatType(value.*, fmt, options, out_stream, max_depth); |
| 440 | 428 | }, |
| 441 | | else => return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }), |
| 429 | else => return format(out_stream, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }), |
| 442 | 430 | }, |
| 443 | 431 | .Many, .C => { |
| 444 | 432 | if (ptr_info.sentinel) |sentinel| { |
| ... | ... | @@ -446,19 +434,19 @@ pub fn formatType( |
| 446 | 434 | } |
| 447 | 435 | if (ptr_info.child == u8) { |
| 448 | 436 | if (fmt.len > 0 and fmt[0] == 's') { |
| 449 | | return formatText(mem.span(value), fmt, options, context, Errors, output); |
| 437 | return formatText(mem.span(value), fmt, options, out_stream); |
| 450 | 438 | } |
| 451 | 439 | } |
| 452 | | return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }); |
| 440 | return format(out_stream, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }); |
| 453 | 441 | }, |
| 454 | 442 | .Slice => { |
| 455 | 443 | if (fmt.len > 0 and ((fmt[0] == 'x') or (fmt[0] == 'X'))) { |
| 456 | | return formatText(value, fmt, options, context, Errors, output); |
| 444 | return formatText(value, fmt, options, out_stream); |
| 457 | 445 | } |
| 458 | 446 | if (ptr_info.child == u8) { |
| 459 | | return formatText(value, fmt, options, context, Errors, output); |
| 447 | return formatText(value, fmt, options, out_stream); |
| 460 | 448 | } |
| 461 | | return format(context, Errors, output, "{}@{x}", .{ @typeName(ptr_info.child), @ptrToInt(value.ptr) }); |
| 449 | return format(out_stream, "{}@{x}", .{ @typeName(ptr_info.child), @ptrToInt(value.ptr) }); |
| 462 | 450 | }, |
| 463 | 451 | }, |
| 464 | 452 | .Array => |info| { |
| ... | ... | @@ -473,24 +461,24 @@ pub fn formatType( |
| 473 | 461 | .sentinel = null, |
| 474 | 462 | }, |
| 475 | 463 | }); |
| 476 | | return formatType(@as(Slice, &value), fmt, options, context, Errors, output, max_depth); |
| 464 | return formatType(@as(Slice, &value), fmt, options, out_stream, max_depth); |
| 477 | 465 | }, |
| 478 | 466 | .Vector => { |
| 479 | 467 | const len = @typeInfo(T).Vector.len; |
| 480 | | try output(context, "{ "); |
| 468 | try out_stream.writeAll("{ "); |
| 481 | 469 | var i: usize = 0; |
| 482 | 470 | while (i < len) : (i += 1) { |
| 483 | | try formatValue(value[i], fmt, options, context, Errors, output); |
| 471 | try formatValue(value[i], fmt, options, out_stream); |
| 484 | 472 | if (i < len - 1) { |
| 485 | | try output(context, ", "); |
| 473 | try out_stream.writeAll(", "); |
| 486 | 474 | } |
| 487 | 475 | } |
| 488 | | try output(context, " }"); |
| 476 | try out_stream.writeAll(" }"); |
| 489 | 477 | }, |
| 490 | 478 | .Fn => { |
| 491 | | return format(context, Errors, output, "{}@{x}", .{ @typeName(T), @ptrToInt(value) }); |
| 479 | return format(out_stream, "{}@{x}", .{ @typeName(T), @ptrToInt(value) }); |
| 492 | 480 | }, |
| 493 | | .Type => return output(context, @typeName(T)), |
| 481 | .Type => return out_stream.writeAll(@typeName(T)), |
| 494 | 482 | .EnumLiteral => { |
| 495 | 483 | const buffer = [_]u8{'.'} ++ @tagName(value); |
| 496 | 484 | return formatType(buffer, fmt, options, context, Errors, output, max_depth); |
| ... | ... | @@ -503,21 +491,19 @@ fn formatValue( |
| 503 | 491 | value: var, |
| 504 | 492 | comptime fmt: []const u8, |
| 505 | 493 | options: FormatOptions, |
| 506 | | context: var, |
| 507 | | comptime Errors: type, |
| 508 | | comptime output: fn (@TypeOf(context), []const u8) Errors!void, |
| 509 | | ) Errors!void { |
| 494 | out_stream: var, |
| 495 | ) !void { |
| 510 | 496 | if (comptime std.mem.eql(u8, fmt, "B")) { |
| 511 | | return formatBytes(value, options, 1000, context, Errors, output); |
| 497 | return formatBytes(value, options, 1000, out_stream); |
| 512 | 498 | } else if (comptime std.mem.eql(u8, fmt, "Bi")) { |
| 513 | | return formatBytes(value, options, 1024, context, Errors, output); |
| 499 | return formatBytes(value, options, 1024, out_stream); |
| 514 | 500 | } |
| 515 | 501 | |
| 516 | 502 | const T = @TypeOf(value); |
| 517 | 503 | switch (@typeInfo(T)) { |
| 518 | | .Float => return formatFloatValue(value, fmt, options, context, Errors, output), |
| 519 | | .Int, .ComptimeInt => return formatIntValue(value, fmt, options, context, Errors, output), |
| 520 | | .Bool => return output(context, if (value) "true" else "false"), |
| 504 | .Float => return formatFloatValue(value, fmt, options, out_stream), |
| 505 | .Int, .ComptimeInt => return formatIntValue(value, fmt, options, out_stream), |
| 506 | .Bool => return out_stream.writeAll(if (value) "true" else "false"), |
| 521 | 507 | else => comptime unreachable, |
| 522 | 508 | } |
| 523 | 509 | } |
| ... | ... | @@ -526,10 +512,8 @@ pub fn formatIntValue( |
| 526 | 512 | value: var, |
| 527 | 513 | comptime fmt: []const u8, |
| 528 | 514 | options: FormatOptions, |
| 529 | | context: var, |
| 530 | | comptime Errors: type, |
| 531 | | comptime output: fn (@TypeOf(context), []const u8) Errors!void, |
| 532 | | ) Errors!void { |
| 515 | out_stream: var, |
| 516 | ) !void { |
| 533 | 517 | comptime var radix = 10; |
| 534 | 518 | comptime var uppercase = false; |
| 535 | 519 | |
| ... | ... | @@ -544,7 +528,7 @@ pub fn formatIntValue( |
| 544 | 528 | uppercase = false; |
| 545 | 529 | } else if (comptime std.mem.eql(u8, fmt, "c")) { |
| 546 | 530 | if (@TypeOf(int_value).bit_count <= 8) { |
| 547 | | return formatAsciiChar(@as(u8, int_value), options, context, Errors, output); |
| 531 | return formatAsciiChar(@as(u8, int_value), options, out_stream); |
| 548 | 532 | } else { |
| 549 | 533 | @compileError("Cannot print integer that is larger than 8 bits as a ascii"); |
| 550 | 534 | } |
| ... | ... | @@ -561,21 +545,19 @@ pub fn formatIntValue( |
| 561 | 545 | @compileError("Unknown format string: '" ++ fmt ++ "'"); |
| 562 | 546 | } |
| 563 | 547 | |
| 564 | | return formatInt(int_value, radix, uppercase, options, context, Errors, output); |
| 548 | return formatInt(int_value, radix, uppercase, options, out_stream); |
| 565 | 549 | } |
| 566 | 550 | |
| 567 | 551 | fn formatFloatValue( |
| 568 | 552 | value: var, |
| 569 | 553 | comptime fmt: []const u8, |
| 570 | 554 | options: FormatOptions, |
| 571 | | context: var, |
| 572 | | comptime Errors: type, |
| 573 | | comptime output: fn (@TypeOf(context), []const u8) Errors!void, |
| 574 | | ) Errors!void { |
| 555 | out_stream: var, |
| 556 | ) !void { |
| 575 | 557 | if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "e")) { |
| 576 | | return formatFloatScientific(value, options, context, Errors, output); |
| 558 | return formatFloatScientific(value, options, out_stream); |
| 577 | 559 | } else if (comptime std.mem.eql(u8, fmt, "d")) { |
| 578 | | return formatFloatDecimal(value, options, context, Errors, output); |
| 560 | return formatFloatDecimal(value, options, out_stream); |
| 579 | 561 | } else { |
| 580 | 562 | @compileError("Unknown format string: '" ++ fmt ++ "'"); |
| 581 | 563 | } |
| ... | ... | @@ -585,17 +567,15 @@ pub fn formatText( |
| 585 | 567 | bytes: []const u8, |
| 586 | 568 | comptime fmt: []const u8, |
| 587 | 569 | options: FormatOptions, |
| 588 | | context: var, |
| 589 | | comptime Errors: type, |
| 590 | | comptime output: fn (@TypeOf(context), []const u8) Errors!void, |
| 591 | | ) Errors!void { |
| 570 | out_stream: var, |
| 571 | ) !void { |
| 592 | 572 | if (fmt.len == 0) { |
| 593 | | return output(context, bytes); |
| 573 | return out_stream.writeAll(bytes); |
| 594 | 574 | } else if (comptime std.mem.eql(u8, fmt, "s")) { |
| 595 | | return formatBuf(bytes, options, context, Errors, output); |
| 575 | return formatBuf(bytes, options, out_stream); |
| 596 | 576 | } else if (comptime (std.mem.eql(u8, fmt, "x") or std.mem.eql(u8, fmt, "X"))) { |
| 597 | 577 | for (bytes) |c| { |
| 598 | | try formatInt(c, 16, fmt[0] == 'X', FormatOptions{ .width = 2, .fill = '0' }, context, Errors, output); |
| 578 | try formatInt(c, 16, fmt[0] == 'X', FormatOptions{ .width = 2, .fill = '0' }, out_stream); |
| 599 | 579 | } |
| 600 | 580 | return; |
| 601 | 581 | } else { |
| ... | ... | @@ -606,27 +586,23 @@ pub fn formatText( |
| 606 | 586 | pub fn formatAsciiChar( |
| 607 | 587 | c: u8, |
| 608 | 588 | options: FormatOptions, |
| 609 | | context: var, |
| 610 | | comptime Errors: type, |
| 611 | | comptime output: fn (@TypeOf(context), []const u8) Errors!void, |
| 612 | | ) Errors!void { |
| 613 | | return output(context, @as(*const [1]u8, &c)[0..]); |
| 589 | out_stream: var, |
| 590 | ) !void { |
| 591 | return out_stream.writeAll(@as(*const [1]u8, &c)[0..]); |
| 614 | 592 | } |
| 615 | 593 | |
| 616 | 594 | pub fn formatBuf( |
| 617 | 595 | buf: []const u8, |
| 618 | 596 | options: FormatOptions, |
| 619 | | context: var, |
| 620 | | comptime Errors: type, |
| 621 | | comptime output: fn (@TypeOf(context), []const u8) Errors!void, |
| 622 | | ) Errors!void { |
| 623 | | try output(context, buf); |
| 597 | out_stream: var, |
| 598 | ) !void { |
| 599 | try out_stream.writeAll(buf); |
| 624 | 600 | |
| 625 | 601 | const width = options.width orelse 0; |
| 626 | 602 | var leftover_padding = if (width > buf.len) (width - buf.len) else return; |
| 627 | 603 | const pad_byte: u8 = options.fill; |
| 628 | 604 | while (leftover_padding > 0) : (leftover_padding -= 1) { |
| 629 | | try output(context, @as(*const [1]u8, &pad_byte)[0..1]); |
| 605 | try out_stream.writeAll(@as(*const [1]u8, &pad_byte)[0..1]); |
| 630 | 606 | } |
| 631 | 607 | } |
| 632 | 608 | |
| ... | ... | @@ -636,40 +612,38 @@ pub fn formatBuf( |
| 636 | 612 | pub fn formatFloatScientific( |
| 637 | 613 | value: var, |
| 638 | 614 | options: FormatOptions, |
| 639 | | context: var, |
| 640 | | comptime Errors: type, |
| 641 | | comptime output: fn (@TypeOf(context), []const u8) Errors!void, |
| 642 | | ) Errors!void { |
| 615 | out_stream: var, |
| 616 | ) !void { |
| 643 | 617 | var x = @floatCast(f64, value); |
| 644 | 618 | |
| 645 | 619 | // Errol doesn't handle these special cases. |
| 646 | 620 | if (math.signbit(x)) { |
| 647 | | try output(context, "-"); |
| 621 | try out_stream.writeAll("-"); |
| 648 | 622 | x = -x; |
| 649 | 623 | } |
| 650 | 624 | |
| 651 | 625 | if (math.isNan(x)) { |
| 652 | | return output(context, "nan"); |
| 626 | return out_stream.writeAll("nan"); |
| 653 | 627 | } |
| 654 | 628 | if (math.isPositiveInf(x)) { |
| 655 | | return output(context, "inf"); |
| 629 | return out_stream.writeAll("inf"); |
| 656 | 630 | } |
| 657 | 631 | if (x == 0.0) { |
| 658 | | try output(context, "0"); |
| 632 | try out_stream.writeAll("0"); |
| 659 | 633 | |
| 660 | 634 | if (options.precision) |precision| { |
| 661 | 635 | if (precision != 0) { |
| 662 | | try output(context, "."); |
| 636 | try out_stream.writeAll("."); |
| 663 | 637 | var i: usize = 0; |
| 664 | 638 | while (i < precision) : (i += 1) { |
| 665 | | try output(context, "0"); |
| 639 | try out_stream.writeAll("0"); |
| 666 | 640 | } |
| 667 | 641 | } |
| 668 | 642 | } else { |
| 669 | | try output(context, ".0"); |
| 643 | try out_stream.writeAll(".0"); |
| 670 | 644 | } |
| 671 | 645 | |
| 672 | | try output(context, "e+00"); |
| 646 | try out_stream.writeAll("e+00"); |
| 673 | 647 | return; |
| 674 | 648 | } |
| 675 | 649 | |
| ... | ... | @@ -679,50 +653,50 @@ pub fn formatFloatScientific( |
| 679 | 653 | if (options.precision) |precision| { |
| 680 | 654 | errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Scientific); |
| 681 | 655 | |
| 682 | | try output(context, float_decimal.digits[0..1]); |
| 656 | try out_stream.writeAll(float_decimal.digits[0..1]); |
| 683 | 657 | |
| 684 | 658 | // {e0} case prints no `.` |
| 685 | 659 | if (precision != 0) { |
| 686 | | try output(context, "."); |
| 660 | try out_stream.writeAll("."); |
| 687 | 661 | |
| 688 | 662 | var printed: usize = 0; |
| 689 | 663 | if (float_decimal.digits.len > 1) { |
| 690 | 664 | const num_digits = math.min(float_decimal.digits.len, precision + 1); |
| 691 | | try output(context, float_decimal.digits[1..num_digits]); |
| 665 | try out_stream.writeAll(float_decimal.digits[1..num_digits]); |
| 692 | 666 | printed += num_digits - 1; |
| 693 | 667 | } |
| 694 | 668 | |
| 695 | 669 | while (printed < precision) : (printed += 1) { |
| 696 | | try output(context, "0"); |
| 670 | try out_stream.writeAll("0"); |
| 697 | 671 | } |
| 698 | 672 | } |
| 699 | 673 | } else { |
| 700 | | try output(context, float_decimal.digits[0..1]); |
| 701 | | try output(context, "."); |
| 674 | try out_stream.writeAll(float_decimal.digits[0..1]); |
| 675 | try out_stream.writeAll("."); |
| 702 | 676 | if (float_decimal.digits.len > 1) { |
| 703 | 677 | const num_digits = if (@TypeOf(value) == f32) math.min(@as(usize, 9), float_decimal.digits.len) else float_decimal.digits.len; |
| 704 | 678 | |
| 705 | | try output(context, float_decimal.digits[1..num_digits]); |
| 679 | try out_stream.writeAll(float_decimal.digits[1..num_digits]); |
| 706 | 680 | } else { |
| 707 | | try output(context, "0"); |
| 681 | try out_stream.writeAll("0"); |
| 708 | 682 | } |
| 709 | 683 | } |
| 710 | 684 | |
| 711 | | try output(context, "e"); |
| 685 | try out_stream.writeAll("e"); |
| 712 | 686 | const exp = float_decimal.exp - 1; |
| 713 | 687 | |
| 714 | 688 | if (exp >= 0) { |
| 715 | | try output(context, "+"); |
| 689 | try out_stream.writeAll("+"); |
| 716 | 690 | if (exp > -10 and exp < 10) { |
| 717 | | try output(context, "0"); |
| 691 | try out_stream.writeAll("0"); |
| 718 | 692 | } |
| 719 | | try formatInt(exp, 10, false, FormatOptions{ .width = 0 }, context, Errors, output); |
| 693 | try formatInt(exp, 10, false, FormatOptions{ .width = 0 }, out_stream); |
| 720 | 694 | } else { |
| 721 | | try output(context, "-"); |
| 695 | try out_stream.writeAll("-"); |
| 722 | 696 | if (exp > -10 and exp < 10) { |
| 723 | | try output(context, "0"); |
| 697 | try out_stream.writeAll("0"); |
| 724 | 698 | } |
| 725 | | try formatInt(-exp, 10, false, FormatOptions{ .width = 0 }, context, Errors, output); |
| 699 | try formatInt(-exp, 10, false, FormatOptions{ .width = 0 }, out_stream); |
| 726 | 700 | } |
| 727 | 701 | } |
| 728 | 702 | |
| ... | ... | @@ -731,36 +705,34 @@ pub fn formatFloatScientific( |
| 731 | 705 | pub fn formatFloatDecimal( |
| 732 | 706 | value: var, |
| 733 | 707 | options: FormatOptions, |
| 734 | | context: var, |
| 735 | | comptime Errors: type, |
| 736 | | comptime output: fn (@TypeOf(context), []const u8) Errors!void, |
| 737 | | ) Errors!void { |
| 708 | out_stream: var, |
| 709 | ) !void { |
| 738 | 710 | var x = @as(f64, value); |
| 739 | 711 | |
| 740 | 712 | // Errol doesn't handle these special cases. |
| 741 | 713 | if (math.signbit(x)) { |
| 742 | | try output(context, "-"); |
| 714 | try out_stream.writeAll("-"); |
| 743 | 715 | x = -x; |
| 744 | 716 | } |
| 745 | 717 | |
| 746 | 718 | if (math.isNan(x)) { |
| 747 | | return output(context, "nan"); |
| 719 | return out_stream.writeAll("nan"); |
| 748 | 720 | } |
| 749 | 721 | if (math.isPositiveInf(x)) { |
| 750 | | return output(context, "inf"); |
| 722 | return out_stream.writeAll("inf"); |
| 751 | 723 | } |
| 752 | 724 | if (x == 0.0) { |
| 753 | | try output(context, "0"); |
| 725 | try out_stream.writeAll("0"); |
| 754 | 726 | |
| 755 | 727 | if (options.precision) |precision| { |
| 756 | 728 | if (precision != 0) { |
| 757 | | try output(context, "."); |
| 729 | try out_stream.writeAll("."); |
| 758 | 730 | var i: usize = 0; |
| 759 | 731 | while (i < precision) : (i += 1) { |
| 760 | | try output(context, "0"); |
| 732 | try out_stream.writeAll("0"); |
| 761 | 733 | } |
| 762 | 734 | } else { |
| 763 | | try output(context, ".0"); |
| 735 | try out_stream.writeAll(".0"); |
| 764 | 736 | } |
| 765 | 737 | } |
| 766 | 738 | |
| ... | ... | @@ -782,14 +754,14 @@ pub fn formatFloatDecimal( |
| 782 | 754 | |
| 783 | 755 | if (num_digits_whole > 0) { |
| 784 | 756 | // We may have to zero pad, for instance 1e4 requires zero padding. |
| 785 | | try output(context, float_decimal.digits[0..num_digits_whole_no_pad]); |
| 757 | try out_stream.writeAll(float_decimal.digits[0..num_digits_whole_no_pad]); |
| 786 | 758 | |
| 787 | 759 | var i = num_digits_whole_no_pad; |
| 788 | 760 | while (i < num_digits_whole) : (i += 1) { |
| 789 | | try output(context, "0"); |
| 761 | try out_stream.writeAll("0"); |
| 790 | 762 | } |
| 791 | 763 | } else { |
| 792 | | try output(context, "0"); |
| 764 | try out_stream.writeAll("0"); |
| 793 | 765 | } |
| 794 | 766 | |
| 795 | 767 | // {.0} special case doesn't want a trailing '.' |
| ... | ... | @@ -797,7 +769,7 @@ pub fn formatFloatDecimal( |
| 797 | 769 | return; |
| 798 | 770 | } |
| 799 | 771 | |
| 800 | | try output(context, "."); |
| 772 | try out_stream.writeAll("."); |
| 801 | 773 | |
| 802 | 774 | // Keep track of fractional count printed for case where we pre-pad then post-pad with 0's. |
| 803 | 775 | var printed: usize = 0; |
| ... | ... | @@ -809,7 +781,7 @@ pub fn formatFloatDecimal( |
| 809 | 781 | |
| 810 | 782 | var i: usize = 0; |
| 811 | 783 | while (i < zeros_to_print) : (i += 1) { |
| 812 | | try output(context, "0"); |
| 784 | try out_stream.writeAll("0"); |
| 813 | 785 | printed += 1; |
| 814 | 786 | } |
| 815 | 787 | |
| ... | ... | @@ -821,14 +793,14 @@ pub fn formatFloatDecimal( |
| 821 | 793 | // Remaining fractional portion, zero-padding if insufficient. |
| 822 | 794 | assert(precision >= printed); |
| 823 | 795 | if (num_digits_whole_no_pad + precision - printed < float_decimal.digits.len) { |
| 824 | | try output(context, float_decimal.digits[num_digits_whole_no_pad .. num_digits_whole_no_pad + precision - printed]); |
| 796 | try out_stream.writeAll(float_decimal.digits[num_digits_whole_no_pad .. num_digits_whole_no_pad + precision - printed]); |
| 825 | 797 | return; |
| 826 | 798 | } else { |
| 827 | | try output(context, float_decimal.digits[num_digits_whole_no_pad..]); |
| 799 | try out_stream.writeAll(float_decimal.digits[num_digits_whole_no_pad..]); |
| 828 | 800 | printed += float_decimal.digits.len - num_digits_whole_no_pad; |
| 829 | 801 | |
| 830 | 802 | while (printed < precision) : (printed += 1) { |
| 831 | | try output(context, "0"); |
| 803 | try out_stream.writeAll("0"); |
| 832 | 804 | } |
| 833 | 805 | } |
| 834 | 806 | } else { |
| ... | ... | @@ -840,14 +812,14 @@ pub fn formatFloatDecimal( |
| 840 | 812 | |
| 841 | 813 | if (num_digits_whole > 0) { |
| 842 | 814 | // We may have to zero pad, for instance 1e4 requires zero padding. |
| 843 | | try output(context, float_decimal.digits[0..num_digits_whole_no_pad]); |
| 815 | try out_stream.writeAll(float_decimal.digits[0..num_digits_whole_no_pad]); |
| 844 | 816 | |
| 845 | 817 | var i = num_digits_whole_no_pad; |
| 846 | 818 | while (i < num_digits_whole) : (i += 1) { |
| 847 | | try output(context, "0"); |
| 819 | try out_stream.writeAll("0"); |
| 848 | 820 | } |
| 849 | 821 | } else { |
| 850 | | try output(context, "0"); |
| 822 | try out_stream.writeAll("0"); |
| 851 | 823 | } |
| 852 | 824 | |
| 853 | 825 | // Omit `.` if no fractional portion |
| ... | ... | @@ -855,7 +827,7 @@ pub fn formatFloatDecimal( |
| 855 | 827 | return; |
| 856 | 828 | } |
| 857 | 829 | |
| 858 | | try output(context, "."); |
| 830 | try out_stream.writeAll("."); |
| 859 | 831 | |
| 860 | 832 | // Zero-fill until we reach significant digits or run out of precision. |
| 861 | 833 | if (float_decimal.exp < 0) { |
| ... | ... | @@ -863,11 +835,11 @@ pub fn formatFloatDecimal( |
| 863 | 835 | |
| 864 | 836 | var i: usize = 0; |
| 865 | 837 | while (i < zero_digit_count) : (i += 1) { |
| 866 | | try output(context, "0"); |
| 838 | try out_stream.writeAll("0"); |
| 867 | 839 | } |
| 868 | 840 | } |
| 869 | 841 | |
| 870 | | try output(context, float_decimal.digits[num_digits_whole_no_pad..]); |
| 842 | try out_stream.writeAll(float_decimal.digits[num_digits_whole_no_pad..]); |
| 871 | 843 | } |
| 872 | 844 | } |
| 873 | 845 | |
| ... | ... | @@ -875,12 +847,10 @@ pub fn formatBytes( |
| 875 | 847 | value: var, |
| 876 | 848 | options: FormatOptions, |
| 877 | 849 | comptime radix: usize, |
| 878 | | context: var, |
| 879 | | comptime Errors: type, |
| 880 | | comptime output: fn (@TypeOf(context), []const u8) Errors!void, |
| 881 | | ) Errors!void { |
| 850 | out_stream: var, |
| 851 | ) !void { |
| 882 | 852 | if (value == 0) { |
| 883 | | return output(context, "0B"); |
| 853 | return out_stream.writeAll("0B"); |
| 884 | 854 | } |
| 885 | 855 | |
| 886 | 856 | const mags_si = " kMGTPEZY"; |
| ... | ... | @@ -897,10 +867,10 @@ pub fn formatBytes( |
| 897 | 867 | else => unreachable, |
| 898 | 868 | }; |
| 899 | 869 | |
| 900 | | try formatFloatDecimal(new_value, options, context, Errors, output); |
| 870 | try formatFloatDecimal(new_value, options, out_stream); |
| 901 | 871 | |
| 902 | 872 | if (suffix == ' ') { |
| 903 | | return output(context, "B"); |
| 873 | return out_stream.writeAll("B"); |
| 904 | 874 | } |
| 905 | 875 | |
| 906 | 876 | const buf = switch (radix) { |
| ... | ... | @@ -908,7 +878,7 @@ pub fn formatBytes( |
| 908 | 878 | 1024 => &[_]u8{ suffix, 'i', 'B' }, |
| 909 | 879 | else => unreachable, |
| 910 | 880 | }; |
| 911 | | return output(context, buf); |
| 881 | return out_stream.writeAll(buf); |
| 912 | 882 | } |
| 913 | 883 | |
| 914 | 884 | pub fn formatInt( |
| ... | ... | @@ -916,10 +886,8 @@ pub fn formatInt( |
| 916 | 886 | base: u8, |
| 917 | 887 | uppercase: bool, |
| 918 | 888 | options: FormatOptions, |
| 919 | | context: var, |
| 920 | | comptime Errors: type, |
| 921 | | comptime output: fn (@TypeOf(context), []const u8) Errors!void, |
| 922 | | ) Errors!void { |
| 889 | out_stream: var, |
| 890 | ) !void { |
| 923 | 891 | const int_value = if (@TypeOf(value) == comptime_int) blk: { |
| 924 | 892 | const Int = math.IntFittingRange(value, value); |
| 925 | 893 | break :blk @as(Int, value); |
| ... | ... | @@ -927,9 +895,9 @@ pub fn formatInt( |
| 927 | 895 | value; |
| 928 | 896 | |
| 929 | 897 | if (@TypeOf(int_value).is_signed) { |
| 930 | | return formatIntSigned(int_value, base, uppercase, options, context, Errors, output); |
| 898 | return formatIntSigned(int_value, base, uppercase, options, out_stream); |
| 931 | 899 | } else { |
| 932 | | return formatIntUnsigned(int_value, base, uppercase, options, context, Errors, output); |
| 900 | return formatIntUnsigned(int_value, base, uppercase, options, out_stream); |
| 933 | 901 | } |
| 934 | 902 | } |
| 935 | 903 | |
| ... | ... | @@ -938,10 +906,8 @@ fn formatIntSigned( |
| 938 | 906 | base: u8, |
| 939 | 907 | uppercase: bool, |
| 940 | 908 | options: FormatOptions, |
| 941 | | context: var, |
| 942 | | comptime Errors: type, |
| 943 | | comptime output: fn (@TypeOf(context), []const u8) Errors!void, |
| 944 | | ) Errors!void { |
| 909 | out_stream: var, |
| 910 | ) !void { |
| 945 | 911 | const new_options = FormatOptions{ |
| 946 | 912 | .width = if (options.width) |w| (if (w == 0) 0 else w - 1) else null, |
| 947 | 913 | .precision = options.precision, |
| ... | ... | @@ -950,15 +916,15 @@ fn formatIntSigned( |
| 950 | 916 | const bit_count = @typeInfo(@TypeOf(value)).Int.bits; |
| 951 | 917 | const Uint = std.meta.IntType(false, bit_count); |
| 952 | 918 | if (value < 0) { |
| 953 | | try output(context, "-"); |
| 919 | try out_stream.writeAll("-"); |
| 954 | 920 | const new_value = math.absCast(value); |
| 955 | | return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output); |
| 921 | return formatIntUnsigned(new_value, base, uppercase, new_options, out_stream); |
| 956 | 922 | } else if (options.width == null or options.width.? == 0) { |
| 957 | | return formatIntUnsigned(@intCast(Uint, value), base, uppercase, options, context, Errors, output); |
| 923 | return formatIntUnsigned(@intCast(Uint, value), base, uppercase, options, out_stream); |
| 958 | 924 | } else { |
| 959 | | try output(context, "+"); |
| 925 | try out_stream.writeAll("+"); |
| 960 | 926 | const new_value = @intCast(Uint, value); |
| 961 | | return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output); |
| 927 | return formatIntUnsigned(new_value, base, uppercase, new_options, out_stream); |
| 962 | 928 | } |
| 963 | 929 | } |
| 964 | 930 | |
| ... | ... | @@ -967,10 +933,8 @@ fn formatIntUnsigned( |
| 967 | 933 | base: u8, |
| 968 | 934 | uppercase: bool, |
| 969 | 935 | options: FormatOptions, |
| 970 | | context: var, |
| 971 | | comptime Errors: type, |
| 972 | | comptime output: fn (@TypeOf(context), []const u8) Errors!void, |
| 973 | | ) Errors!void { |
| 936 | out_stream: var, |
| 937 | ) !void { |
| 974 | 938 | assert(base >= 2); |
| 975 | 939 | var buf: [math.max(@TypeOf(value).bit_count, 1)]u8 = undefined; |
| 976 | 940 | const min_int_bits = comptime math.max(@TypeOf(value).bit_count, @TypeOf(base).bit_count); |
| ... | ... | @@ -994,16 +958,16 @@ fn formatIntUnsigned( |
| 994 | 958 | const zero_byte: u8 = options.fill; |
| 995 | 959 | var leftover_padding = padding - index; |
| 996 | 960 | while (true) { |
| 997 | | try output(context, @as(*const [1]u8, &zero_byte)[0..]); |
| 961 | try out_stream.writeAll(@as(*const [1]u8, &zero_byte)[0..]); |
| 998 | 962 | leftover_padding -= 1; |
| 999 | 963 | if (leftover_padding == 0) break; |
| 1000 | 964 | } |
| 1001 | 965 | mem.set(u8, buf[0..index], options.fill); |
| 1002 | | return output(context, &buf); |
| 966 | return out_stream.writeAll(&buf); |
| 1003 | 967 | } else { |
| 1004 | 968 | const padded_buf = buf[index - padding ..]; |
| 1005 | 969 | mem.set(u8, padded_buf[0..padding], options.fill); |
| 1006 | | return output(context, padded_buf); |
| 970 | return out_stream.writeAll(padded_buf); |
| 1007 | 971 | } |
| 1008 | 972 | } |
| 1009 | 973 | |
| ... | ... | @@ -1451,12 +1415,12 @@ test "custom" { |
| 1451 | 1415 | options: FormatOptions, |
| 1452 | 1416 | context: var, |
| 1453 | 1417 | comptime Errors: type, |
| 1454 | | comptime output: fn (@TypeOf(context), []const u8) Errors!void, |
| 1455 | | ) Errors!void { |
| 1418 | comptime output: fn (@TypeOf(context), []const u8) !void, |
| 1419 | ) !void { |
| 1456 | 1420 | if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "p")) { |
| 1457 | | return std.fmt.format(context, Errors, output, "({d:.3},{d:.3})", .{ self.x, self.y }); |
| 1421 | return std.fmtstream.format(out_stream, "({d:.3},{d:.3})", .{ self.x, self.y }); |
| 1458 | 1422 | } else if (comptime std.mem.eql(u8, fmt, "d")) { |
| 1459 | | return std.fmt.format(context, Errors, output, "{d:.3}x{d:.3}", .{ self.x, self.y }); |
| 1423 | return std.fmtstream.format(out_stream, "{d:.3}x{d:.3}", .{ self.x, self.y }); |
| 1460 | 1424 | } else { |
| 1461 | 1425 | @compileError("Unknown format character: '" ++ fmt ++ "'"); |
| 1462 | 1426 | } |
| ... | ... | @@ -1658,10 +1622,10 @@ test "formatType max_depth" { |
| 1658 | 1622 | options: FormatOptions, |
| 1659 | 1623 | context: var, |
| 1660 | 1624 | comptime Errors: type, |
| 1661 | | comptime output: fn (@TypeOf(context), []const u8) Errors!void, |
| 1662 | | ) Errors!void { |
| 1625 | comptime output: fn (@TypeOf(context), []const u8) !void, |
| 1626 | ) !void { |
| 1663 | 1627 | if (fmt.len == 0) { |
| 1664 | | return std.fmt.format(context, Errors, output, "({d:.3},{d:.3})", .{ self.x, self.y }); |
| 1628 | return std.fmtstream.format(out_stream, "({d:.3},{d:.3})", .{ self.x, self.y }); |
| 1665 | 1629 | } else { |
| 1666 | 1630 | @compileError("Unknown format string: '" ++ fmt ++ "'"); |
| 1667 | 1631 | } |