| ... | @@ -672,25 +672,34 @@ pub fn formatBuf( | ... | @@ -672,25 +672,34 @@ pub fn formatBuf( |
| 672 | options: FormatOptions, | 672 | options: FormatOptions, |
| 673 | writer: anytype, | 673 | writer: anytype, |
| 674 | ) !void { | 674 | ) !void { |
| 675 | const width = options.width orelse buf.len; | 675 | if (options.width) |min_width| { |
| 676 | const padding = if (width > buf.len) (width - buf.len) else 0; | 676 | // In case of error assume the buffer content is ASCII-encoded |
| 677 | | 677 | const width = unicode.utf8CountCodepoints(buf) catch |_| buf.len; |
| 678 | switch (options.alignment) { | 678 | const padding = if (width < min_width) min_width - width else 0; |
| 679 | .Left => { | 679 | |
| 680 | try writer.writeAll(buf); | 680 | if (padding == 0) |
| 681 | try writer.writeByteNTimes(options.fill, padding); | 681 | return writer.writeAll(buf); |
| 682 | }, | 682 | |
| 683 | .Center => { | 683 | switch (options.alignment) { |
| 684 | const left_padding = padding / 2; | 684 | .Left => { |
| 685 | const right_padding = (padding + 1) / 2; | 685 | try writer.writeAll(buf); |
| 686 | try writer.writeByteNTimes(options.fill, left_padding); | 686 | try writer.writeByteNTimes(options.fill, padding); |
| 687 | try writer.writeAll(buf); | 687 | }, |
| 688 | try writer.writeByteNTimes(options.fill, right_padding); | 688 | .Center => { |
| 689 | }, | 689 | const left_padding = padding / 2; |
| 690 | .Right => { | 690 | const right_padding = (padding + 1) / 2; |
| 691 | try writer.writeByteNTimes(options.fill, padding); | 691 | try writer.writeByteNTimes(options.fill, left_padding); |
| 692 | try writer.writeAll(buf); | 692 | try writer.writeAll(buf); |
| 693 | }, | 693 | try writer.writeByteNTimes(options.fill, right_padding); |
| | 694 | }, |
| | 695 | .Right => { |
| | 696 | try writer.writeByteNTimes(options.fill, padding); |
| | 697 | try writer.writeAll(buf); |
| | 698 | }, |
| | 699 | } |
| | 700 | } else { |
| | 701 | // Fast path, avoid counting the number of codepoints |
| | 702 | try writer.writeAll(buf); |
| 694 | } | 703 | } |
| 695 | } | 704 | } |
| 696 | | 705 | |
| ... | @@ -1442,6 +1451,10 @@ test "int.padded" { | ... | @@ -1442,6 +1451,10 @@ test "int.padded" { |
| 1442 | try testFmt("i16: '-12345'", "i16: '{:4}'", .{@as(i16, -12345)}); | 1451 | try testFmt("i16: '-12345'", "i16: '{:4}'", .{@as(i16, -12345)}); |
| 1443 | try testFmt("i16: '+12345'", "i16: '{:4}'", .{@as(i16, 12345)}); | 1452 | try testFmt("i16: '+12345'", "i16: '{:4}'", .{@as(i16, 12345)}); |
| 1444 | try testFmt("u16: '12345'", "u16: '{:4}'", .{@as(u16, 12345)}); | 1453 | try testFmt("u16: '12345'", "u16: '{:4}'", .{@as(u16, 12345)}); |
| | 1454 | |
| | 1455 | try testFmt("UTF-8: 'ü '", "UTF-8: '{u:<4}'", .{'ü'}); |
| | 1456 | try testFmt("UTF-8: ' ü'", "UTF-8: '{u:>4}'", .{'ü'}); |
| | 1457 | try testFmt("UTF-8: ' ü '", "UTF-8: '{u:^4}'", .{'ü'}); |
| 1445 | } | 1458 | } |
| 1446 | | 1459 | |
| 1447 | test "buffer" { | 1460 | test "buffer" { |
| ... | @@ -1971,6 +1984,9 @@ test "padding" { | ... | @@ -1971,6 +1984,9 @@ test "padding" { |
| 1971 | try testFmt("==================Filled", "{:=>24}", .{"Filled"}); | 1984 | try testFmt("==================Filled", "{:=>24}", .{"Filled"}); |
| 1972 | try testFmt(" Centered ", "{:^24}", .{"Centered"}); | 1985 | try testFmt(" Centered ", "{:^24}", .{"Centered"}); |
| 1973 | try testFmt("-", "{:-^1}", .{""}); | 1986 | try testFmt("-", "{:-^1}", .{""}); |
| | 1987 | try testFmt("==crêpe===", "{:=^10}", .{"crêpe"}); |
| | 1988 | try testFmt("=====crêpe", "{:=>10}", .{"crêpe"}); |
| | 1989 | try testFmt("crêpe=====", "{:=<10}", .{"crêpe"}); |
| 1974 | } | 1990 | } |
| 1975 | | 1991 | |
| 1976 | test "decimal float padding" { | 1992 | test "decimal float padding" { |