| author | |
| committer | |
| log | 916b645fc1a17f46bab35b54832e037fcf2ab92b |
| tree | 7ebdf361125b2d13d347713418d3ad08e29ef1f5 |
| parent | b82d6422ac5c0a68b812319f689b66b52b0eaf59 |
9 files changed, 88 insertions(+), 86 deletions(-)
lib/std/fmt.zig+48-46| ... | ... | @@ -370,15 +370,15 @@ pub fn formatAddress(value: anytype, options: FormatOptions, writer: anytype) @T |
| 370 | 370 | .Pointer => |info| { |
| 371 | 371 | try writer.writeAll(@typeName(info.child) ++ "@"); |
| 372 | 372 | if (info.size == .Slice) |
| 373 | try formatInt(@ptrToInt(value.ptr), 16, false, FormatOptions{}, writer) | |
| 373 | try formatInt(@ptrToInt(value.ptr), 16, .lower, FormatOptions{}, writer) | |
| 374 | 374 | else |
| 375 | try formatInt(@ptrToInt(value), 16, false, FormatOptions{}, writer); | |
| 375 | try formatInt(@ptrToInt(value), 16, .lower, FormatOptions{}, writer); | |
| 376 | 376 | return; |
| 377 | 377 | }, |
| 378 | 378 | .Optional => |info| { |
| 379 | 379 | if (@typeInfo(info.child) == .Pointer) { |
| 380 | 380 | try writer.writeAll(@typeName(info.child) ++ "@"); |
| 381 | try formatInt(@ptrToInt(value), 16, false, FormatOptions{}, writer); | |
| 381 | try formatInt(@ptrToInt(value), 16, .lower, FormatOptions{}, writer); | |
| 382 | 382 | return; |
| 383 | 383 | } |
| 384 | 384 | }, |
| ... | ... | @@ -651,7 +651,7 @@ pub fn formatIntValue( |
| 651 | 651 | writer: anytype, |
| 652 | 652 | ) !void { |
| 653 | 653 | comptime var radix = 10; |
| 654 | comptime var uppercase = false; | |
| 654 | comptime var case: Case = .lower; | |
| 655 | 655 | |
| 656 | 656 | const int_value = if (@TypeOf(value) == comptime_int) blk: { |
| 657 | 657 | const Int = math.IntFittingRange(value, value); |
| ... | ... | @@ -660,7 +660,7 @@ pub fn formatIntValue( |
| 660 | 660 | |
| 661 | 661 | if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "d")) { |
| 662 | 662 | radix = 10; |
| 663 | uppercase = false; | |
| 663 | case = .lower; | |
| 664 | 664 | } else if (comptime std.mem.eql(u8, fmt, "c")) { |
| 665 | 665 | if (@typeInfo(@TypeOf(int_value)).Int.bits <= 8) { |
| 666 | 666 | return formatAsciiChar(@as(u8, int_value), options, writer); |
| ... | ... | @@ -675,21 +675,21 @@ pub fn formatIntValue( |
| 675 | 675 | } |
| 676 | 676 | } else if (comptime std.mem.eql(u8, fmt, "b")) { |
| 677 | 677 | radix = 2; |
| 678 | uppercase = false; | |
| 678 | case = .lower; | |
| 679 | 679 | } else if (comptime std.mem.eql(u8, fmt, "x")) { |
| 680 | 680 | radix = 16; |
| 681 | uppercase = false; | |
| 681 | case = .lower; | |
| 682 | 682 | } else if (comptime std.mem.eql(u8, fmt, "X")) { |
| 683 | 683 | radix = 16; |
| 684 | uppercase = true; | |
| 684 | case = .upper; | |
| 685 | 685 | } else if (comptime std.mem.eql(u8, fmt, "o")) { |
| 686 | 686 | radix = 8; |
| 687 | uppercase = false; | |
| 687 | case = .lower; | |
| 688 | 688 | } else { |
| 689 | 689 | @compileError("Unsupported format string '" ++ fmt ++ "' for type '" ++ @typeName(@TypeOf(value)) ++ "'"); |
| 690 | 690 | } |
| 691 | 691 | |
| 692 | return formatInt(int_value, radix, uppercase, options, writer); | |
| 692 | return formatInt(int_value, radix, case, options, writer); | |
| 693 | 693 | } |
| 694 | 694 | |
| 695 | 695 | fn formatFloatValue( |
| ... | ... | @@ -724,8 +724,10 @@ fn formatFloatValue( |
| 724 | 724 | return formatBuf(buf_stream.getWritten(), options, writer); |
| 725 | 725 | } |
| 726 | 726 | |
| 727 | fn formatSliceHexImpl(comptime uppercase: bool) type { | |
| 728 | const charset = "0123456789" ++ if (uppercase) "ABCDEF" else "abcdef"; | |
| 727 | pub const Case = enum { lower, upper }; | |
| 728 | ||
| 729 | fn formatSliceHexImpl(comptime case: Case) type { | |
| 730 | const charset = "0123456789" ++ if (case == .upper) "ABCDEF" else "abcdef"; | |
| 729 | 731 | |
| 730 | 732 | return struct { |
| 731 | 733 | pub fn f( |
| ... | ... | @@ -745,8 +747,8 @@ fn formatSliceHexImpl(comptime uppercase: bool) type { |
| 745 | 747 | }; |
| 746 | 748 | } |
| 747 | 749 | |
| 748 | const formatSliceHexLower = formatSliceHexImpl(false).f; | |
| 749 | const formatSliceHexUpper = formatSliceHexImpl(true).f; | |
| 750 | const formatSliceHexLower = formatSliceHexImpl(.lower).f; | |
| 751 | const formatSliceHexUpper = formatSliceHexImpl(.upper).f; | |
| 750 | 752 | |
| 751 | 753 | /// Return a Formatter for a []const u8 where every byte is formatted as a pair |
| 752 | 754 | /// of lowercase hexadecimal digits. |
| ... | ... | @@ -754,14 +756,14 @@ pub fn fmtSliceHexLower(bytes: []const u8) std.fmt.Formatter(formatSliceHexLower |
| 754 | 756 | return .{ .data = bytes }; |
| 755 | 757 | } |
| 756 | 758 | |
| 757 | /// Return a Formatter for a []const u8 where every byte is formatted as a pair | |
| 759 | /// Return a Formatter for a []const u8 where every byte is formatted as pair | |
| 758 | 760 | /// of uppercase hexadecimal digits. |
| 759 | 761 | pub fn fmtSliceHexUpper(bytes: []const u8) std.fmt.Formatter(formatSliceHexUpper) { |
| 760 | 762 | return .{ .data = bytes }; |
| 761 | 763 | } |
| 762 | 764 | |
| 763 | fn formatSliceEscapeImpl(comptime uppercase: bool) type { | |
| 764 | const charset = "0123456789" ++ if (uppercase) "ABCDEF" else "abcdef"; | |
| 765 | fn formatSliceEscapeImpl(comptime case: Case) type { | |
| 766 | const charset = "0123456789" ++ if (case == .upper) "ABCDEF" else "abcdef"; | |
| 765 | 767 | |
| 766 | 768 | return struct { |
| 767 | 769 | pub fn f( |
| ... | ... | @@ -788,8 +790,8 @@ fn formatSliceEscapeImpl(comptime uppercase: bool) type { |
| 788 | 790 | }; |
| 789 | 791 | } |
| 790 | 792 | |
| 791 | const formatSliceEscapeLower = formatSliceEscapeImpl(false).f; | |
| 792 | const formatSliceEscapeUpper = formatSliceEscapeImpl(true).f; | |
| 793 | const formatSliceEscapeLower = formatSliceEscapeImpl(.lower).f; | |
| 794 | const formatSliceEscapeUpper = formatSliceEscapeImpl(.upper).f; | |
| 793 | 795 | |
| 794 | 796 | /// Return a Formatter for a []const u8 where every non-printable ASCII |
| 795 | 797 | /// character is escaped as \xNN, where NN is the character in lowercase |
| ... | ... | @@ -1034,13 +1036,13 @@ pub fn formatFloatScientific( |
| 1034 | 1036 | if (exp > -10 and exp < 10) { |
| 1035 | 1037 | try writer.writeAll("0"); |
| 1036 | 1038 | } |
| 1037 | try formatInt(exp, 10, false, FormatOptions{ .width = 0 }, writer); | |
| 1039 | try formatInt(exp, 10, .lower, FormatOptions{ .width = 0 }, writer); | |
| 1038 | 1040 | } else { |
| 1039 | 1041 | try writer.writeAll("-"); |
| 1040 | 1042 | if (exp > -10 and exp < 10) { |
| 1041 | 1043 | try writer.writeAll("0"); |
| 1042 | 1044 | } |
| 1043 | try formatInt(-exp, 10, false, FormatOptions{ .width = 0 }, writer); | |
| 1045 | try formatInt(-exp, 10, .lower, FormatOptions{ .width = 0 }, writer); | |
| 1044 | 1046 | } |
| 1045 | 1047 | } |
| 1046 | 1048 | |
| ... | ... | @@ -1133,7 +1135,7 @@ pub fn formatFloatHexadecimal( |
| 1133 | 1135 | |
| 1134 | 1136 | // +1 for the decimal part. |
| 1135 | 1137 | var buf: [1 + mantissa_digits]u8 = undefined; |
| 1136 | const N = formatIntBuf(&buf, mantissa, 16, false, .{ .fill = '0', .width = 1 + mantissa_digits }); | |
| 1138 | const N = formatIntBuf(&buf, mantissa, 16, .lower, .{ .fill = '0', .width = 1 + mantissa_digits }); | |
| 1137 | 1139 | |
| 1138 | 1140 | try writer.writeAll("0x"); |
| 1139 | 1141 | try writer.writeByte(buf[0]); |
| ... | ... | @@ -1150,7 +1152,7 @@ pub fn formatFloatHexadecimal( |
| 1150 | 1152 | try writer.writeByteNTimes('0', precision - trimmed.len); |
| 1151 | 1153 | }; |
| 1152 | 1154 | try writer.writeAll("p"); |
| 1153 | try formatInt(exponent - exponent_bias, 10, false, .{}, writer); | |
| 1155 | try formatInt(exponent - exponent_bias, 10, .lower, .{}, writer); | |
| 1154 | 1156 | } |
| 1155 | 1157 | |
| 1156 | 1158 | /// Print a float of the format x.yyyyy where the number of y is specified by the precision argument. |
| ... | ... | @@ -1299,7 +1301,7 @@ pub fn formatFloatDecimal( |
| 1299 | 1301 | pub fn formatInt( |
| 1300 | 1302 | value: anytype, |
| 1301 | 1303 | base: u8, |
| 1302 | uppercase: bool, | |
| 1304 | case: Case, | |
| 1303 | 1305 | options: FormatOptions, |
| 1304 | 1306 | writer: anytype, |
| 1305 | 1307 | ) !void { |
| ... | ... | @@ -1326,7 +1328,7 @@ pub fn formatInt( |
| 1326 | 1328 | while (true) { |
| 1327 | 1329 | const digit = a % base; |
| 1328 | 1330 | index -= 1; |
| 1329 | buf[index] = digitToChar(@intCast(u8, digit), uppercase); | |
| 1331 | buf[index] = digitToChar(@intCast(u8, digit), case); | |
| 1330 | 1332 | a /= base; |
| 1331 | 1333 | if (a == 0) break; |
| 1332 | 1334 | } |
| ... | ... | @@ -1348,9 +1350,9 @@ pub fn formatInt( |
| 1348 | 1350 | return formatBuf(buf[index..], options, writer); |
| 1349 | 1351 | } |
| 1350 | 1352 | |
| 1351 | pub fn formatIntBuf(out_buf: []u8, value: anytype, base: u8, uppercase: bool, options: FormatOptions) usize { | |
| 1353 | pub fn formatIntBuf(out_buf: []u8, value: anytype, base: u8, case: Case, options: FormatOptions) usize { | |
| 1352 | 1354 | var fbs = std.io.fixedBufferStream(out_buf); |
| 1353 | formatInt(value, base, uppercase, options, fbs.writer()) catch unreachable; | |
| 1355 | formatInt(value, base, case, options, fbs.writer()) catch unreachable; | |
| 1354 | 1356 | return fbs.pos; |
| 1355 | 1357 | } |
| 1356 | 1358 | |
| ... | ... | @@ -1365,7 +1367,7 @@ fn formatDuration(ns: u64, comptime fmt: []const u8, options: std.fmt.FormatOpti |
| 1365 | 1367 | }) |unit| { |
| 1366 | 1368 | if (ns_remaining >= unit.ns) { |
| 1367 | 1369 | const units = ns_remaining / unit.ns; |
| 1368 | try formatInt(units, 10, false, .{}, writer); | |
| 1370 | try formatInt(units, 10, .lower, .{}, writer); | |
| 1369 | 1371 | try writer.writeByte(unit.sep); |
| 1370 | 1372 | ns_remaining -= units * unit.ns; |
| 1371 | 1373 | if (ns_remaining == 0) return; |
| ... | ... | @@ -1379,12 +1381,12 @@ fn formatDuration(ns: u64, comptime fmt: []const u8, options: std.fmt.FormatOpti |
| 1379 | 1381 | }) |unit| { |
| 1380 | 1382 | const kunits = ns_remaining * 1000 / unit.ns; |
| 1381 | 1383 | if (kunits >= 1000) { |
| 1382 | try formatInt(kunits / 1000, 10, false, .{}, writer); | |
| 1384 | try formatInt(kunits / 1000, 10, .lower, .{}, writer); | |
| 1383 | 1385 | const frac = kunits % 1000; |
| 1384 | 1386 | if (frac > 0) { |
| 1385 | 1387 | // Write up to 3 decimal places |
| 1386 | 1388 | var buf = [_]u8{ '.', 0, 0, 0 }; |
| 1387 | _ = formatIntBuf(buf[1..], frac, 10, false, .{ .fill = '0', .width = 3 }); | |
| 1389 | _ = formatIntBuf(buf[1..], frac, 10, .lower, .{ .fill = '0', .width = 3 }); | |
| 1388 | 1390 | var end: usize = 4; |
| 1389 | 1391 | while (end > 1) : (end -= 1) { |
| 1390 | 1392 | if (buf[end - 1] != '0') break; |
| ... | ... | @@ -1396,7 +1398,7 @@ fn formatDuration(ns: u64, comptime fmt: []const u8, options: std.fmt.FormatOpti |
| 1396 | 1398 | } |
| 1397 | 1399 | } |
| 1398 | 1400 | |
| 1399 | try formatInt(ns_remaining, 10, false, .{}, writer); | |
| 1401 | try formatInt(ns_remaining, 10, .lower, .{}, writer); | |
| 1400 | 1402 | try writer.writeAll("ns"); |
| 1401 | 1403 | return; |
| 1402 | 1404 | } |
| ... | ... | @@ -1673,10 +1675,10 @@ pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) { |
| 1673 | 1675 | return value; |
| 1674 | 1676 | } |
| 1675 | 1677 | |
| 1676 | pub fn digitToChar(digit: u8, uppercase: bool) u8 { | |
| 1678 | pub fn digitToChar(digit: u8, case: Case) u8 { | |
| 1677 | 1679 | return switch (digit) { |
| 1678 | 1680 | 0...9 => digit + '0', |
| 1679 | 10...35 => digit + ((if (uppercase) @as(u8, 'A') else @as(u8, 'a')) - 10), | |
| 1681 | 10...35 => digit + ((if (case == .upper) @as(u8, 'A') else @as(u8, 'a')) - 10), | |
| 1680 | 1682 | else => unreachable, |
| 1681 | 1683 | }; |
| 1682 | 1684 | } |
| ... | ... | @@ -1728,25 +1730,25 @@ test "bufPrintInt" { |
| 1728 | 1730 | var buffer: [100]u8 = undefined; |
| 1729 | 1731 | const buf = buffer[0..]; |
| 1730 | 1732 | |
| 1731 | try std.testing.expectEqualSlices(u8, "-1", bufPrintIntToSlice(buf, @as(i1, -1), 10, false, FormatOptions{})); | |
| 1733 | try std.testing.expectEqualSlices(u8, "-1", bufPrintIntToSlice(buf, @as(i1, -1), 10, .lower, FormatOptions{})); | |
| 1732 | 1734 | |
| 1733 | try std.testing.expectEqualSlices(u8, "-101111000110000101001110", bufPrintIntToSlice(buf, @as(i32, -12345678), 2, false, FormatOptions{})); | |
| 1734 | try std.testing.expectEqualSlices(u8, "-12345678", bufPrintIntToSlice(buf, @as(i32, -12345678), 10, false, FormatOptions{})); | |
| 1735 | try std.testing.expectEqualSlices(u8, "-bc614e", bufPrintIntToSlice(buf, @as(i32, -12345678), 16, false, FormatOptions{})); | |
| 1736 | try std.testing.expectEqualSlices(u8, "-BC614E", bufPrintIntToSlice(buf, @as(i32, -12345678), 16, true, FormatOptions{})); | |
| 1735 | try std.testing.expectEqualSlices(u8, "-101111000110000101001110", bufPrintIntToSlice(buf, @as(i32, -12345678), 2, .lower, FormatOptions{})); | |
| 1736 | try std.testing.expectEqualSlices(u8, "-12345678", bufPrintIntToSlice(buf, @as(i32, -12345678), 10, .lower, FormatOptions{})); | |
| 1737 | try std.testing.expectEqualSlices(u8, "-bc614e", bufPrintIntToSlice(buf, @as(i32, -12345678), 16, .lower, FormatOptions{})); | |
| 1738 | try std.testing.expectEqualSlices(u8, "-BC614E", bufPrintIntToSlice(buf, @as(i32, -12345678), 16, .upper, FormatOptions{})); | |
| 1737 | 1739 | |
| 1738 | try std.testing.expectEqualSlices(u8, "12345678", bufPrintIntToSlice(buf, @as(u32, 12345678), 10, true, FormatOptions{})); | |
| 1740 | try std.testing.expectEqualSlices(u8, "12345678", bufPrintIntToSlice(buf, @as(u32, 12345678), 10, .upper, FormatOptions{})); | |
| 1739 | 1741 | |
| 1740 | try std.testing.expectEqualSlices(u8, " 666", bufPrintIntToSlice(buf, @as(u32, 666), 10, false, FormatOptions{ .width = 6 })); | |
| 1741 | try std.testing.expectEqualSlices(u8, " 1234", bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 6 })); | |
| 1742 | try std.testing.expectEqualSlices(u8, "1234", bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 1 })); | |
| 1742 | try std.testing.expectEqualSlices(u8, " 666", bufPrintIntToSlice(buf, @as(u32, 666), 10, .lower, FormatOptions{ .width = 6 })); | |
| 1743 | try std.testing.expectEqualSlices(u8, " 1234", bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, .lower, FormatOptions{ .width = 6 })); | |
| 1744 | try std.testing.expectEqualSlices(u8, "1234", bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, .lower, FormatOptions{ .width = 1 })); | |
| 1743 | 1745 | |
| 1744 | try std.testing.expectEqualSlices(u8, "+42", bufPrintIntToSlice(buf, @as(i32, 42), 10, false, FormatOptions{ .width = 3 })); | |
| 1745 | try std.testing.expectEqualSlices(u8, "-42", bufPrintIntToSlice(buf, @as(i32, -42), 10, false, FormatOptions{ .width = 3 })); | |
| 1746 | try std.testing.expectEqualSlices(u8, "+42", bufPrintIntToSlice(buf, @as(i32, 42), 10, .lower, FormatOptions{ .width = 3 })); | |
| 1747 | try std.testing.expectEqualSlices(u8, "-42", bufPrintIntToSlice(buf, @as(i32, -42), 10, .lower, FormatOptions{ .width = 3 })); | |
| 1746 | 1748 | } |
| 1747 | 1749 | |
| 1748 | pub fn bufPrintIntToSlice(buf: []u8, value: anytype, base: u8, uppercase: bool, options: FormatOptions) []u8 { | |
| 1749 | return buf[0..formatIntBuf(buf, value, base, uppercase, options)]; | |
| 1750 | pub fn bufPrintIntToSlice(buf: []u8, value: anytype, base: u8, case: Case, options: FormatOptions) []u8 { | |
| 1751 | return buf[0..formatIntBuf(buf, value, base, case, options)]; | |
| 1750 | 1752 | } |
| 1751 | 1753 | |
| 1752 | 1754 | pub fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args):0]u8 { |
lib/std/math/big/int.zig+14-14| ... | ... | @@ -1140,20 +1140,20 @@ pub const Const = struct { |
| 1140 | 1140 | out_stream: anytype, |
| 1141 | 1141 | ) !void { |
| 1142 | 1142 | comptime var radix = 10; |
| 1143 | comptime var uppercase = false; | |
| 1143 | comptime var case: std.fmt.Case = .lower; | |
| 1144 | 1144 | |
| 1145 | 1145 | if (fmt.len == 0 or comptime mem.eql(u8, fmt, "d")) { |
| 1146 | 1146 | radix = 10; |
| 1147 | uppercase = false; | |
| 1147 | case = .lower; | |
| 1148 | 1148 | } else if (comptime mem.eql(u8, fmt, "b")) { |
| 1149 | 1149 | radix = 2; |
| 1150 | uppercase = false; | |
| 1150 | case = .lower; | |
| 1151 | 1151 | } else if (comptime mem.eql(u8, fmt, "x")) { |
| 1152 | 1152 | radix = 16; |
| 1153 | uppercase = false; | |
| 1153 | case = .lower; | |
| 1154 | 1154 | } else if (comptime mem.eql(u8, fmt, "X")) { |
| 1155 | 1155 | radix = 16; |
| 1156 | uppercase = true; | |
| 1156 | case = .upper; | |
| 1157 | 1157 | } else { |
| 1158 | 1158 | @compileError("Unknown format string: '" ++ fmt ++ "'"); |
| 1159 | 1159 | } |
| ... | ... | @@ -1171,7 +1171,7 @@ pub const Const = struct { |
| 1171 | 1171 | .positive = false, |
| 1172 | 1172 | }; |
| 1173 | 1173 | var buf: [biggest.sizeInBaseUpperBound(radix)]u8 = undefined; |
| 1174 | const len = self.toString(&buf, radix, uppercase, &limbs); | |
| 1174 | const len = self.toString(&buf, radix, case, &limbs); | |
| 1175 | 1175 | return out_stream.writeAll(buf[0..len]); |
| 1176 | 1176 | } |
| 1177 | 1177 | |
| ... | ... | @@ -1179,7 +1179,7 @@ pub const Const = struct { |
| 1179 | 1179 | /// Caller owns returned memory. |
| 1180 | 1180 | /// Asserts that `base` is in the range [2, 16]. |
| 1181 | 1181 | /// See also `toString`, a lower level function than this. |
| 1182 | pub fn toStringAlloc(self: Const, allocator: *Allocator, base: u8, uppercase: bool) Allocator.Error![]u8 { | |
| 1182 | pub fn toStringAlloc(self: Const, allocator: *Allocator, base: u8, case: std.fmt.Case) Allocator.Error![]u8 { | |
| 1183 | 1183 | assert(base >= 2); |
| 1184 | 1184 | assert(base <= 16); |
| 1185 | 1185 | |
| ... | ... | @@ -1192,7 +1192,7 @@ pub const Const = struct { |
| 1192 | 1192 | const limbs = try allocator.alloc(Limb, calcToStringLimbsBufferLen(self.limbs.len, base)); |
| 1193 | 1193 | defer allocator.free(limbs); |
| 1194 | 1194 | |
| 1195 | return allocator.shrink(string, self.toString(string, base, uppercase, limbs)); | |
| 1195 | return allocator.shrink(string, self.toString(string, base, case, limbs)); | |
| 1196 | 1196 | } |
| 1197 | 1197 | |
| 1198 | 1198 | /// Converts self to a string in the requested base. |
| ... | ... | @@ -1204,7 +1204,7 @@ pub const Const = struct { |
| 1204 | 1204 | /// length of at least `calcToStringLimbsBufferLen`. |
| 1205 | 1205 | /// In the case of power-of-two base, `limbs_buffer` is ignored. |
| 1206 | 1206 | /// See also `toStringAlloc`, a higher level function than this. |
| 1207 | pub fn toString(self: Const, string: []u8, base: u8, uppercase: bool, limbs_buffer: []Limb) usize { | |
| 1207 | pub fn toString(self: Const, string: []u8, base: u8, case: std.fmt.Case, limbs_buffer: []Limb) usize { | |
| 1208 | 1208 | assert(base >= 2); |
| 1209 | 1209 | assert(base <= 16); |
| 1210 | 1210 | |
| ... | ... | @@ -1223,7 +1223,7 @@ pub const Const = struct { |
| 1223 | 1223 | var shift: usize = 0; |
| 1224 | 1224 | while (shift < limb_bits) : (shift += base_shift) { |
| 1225 | 1225 | const r = @intCast(u8, (limb >> @intCast(Log2Limb, shift)) & @as(Limb, base - 1)); |
| 1226 | const ch = std.fmt.digitToChar(r, uppercase); | |
| 1226 | const ch = std.fmt.digitToChar(r, case); | |
| 1227 | 1227 | string[digits_len] = ch; |
| 1228 | 1228 | digits_len += 1; |
| 1229 | 1229 | // If we hit the end, it must be all zeroes from here. |
| ... | ... | @@ -1269,7 +1269,7 @@ pub const Const = struct { |
| 1269 | 1269 | var r_word = r.limbs[0]; |
| 1270 | 1270 | var i: usize = 0; |
| 1271 | 1271 | while (i < digits_per_limb) : (i += 1) { |
| 1272 | const ch = std.fmt.digitToChar(@intCast(u8, r_word % base), uppercase); | |
| 1272 | const ch = std.fmt.digitToChar(@intCast(u8, r_word % base), case); | |
| 1273 | 1273 | r_word /= base; |
| 1274 | 1274 | string[digits_len] = ch; |
| 1275 | 1275 | digits_len += 1; |
| ... | ... | @@ -1281,7 +1281,7 @@ pub const Const = struct { |
| 1281 | 1281 | |
| 1282 | 1282 | var r_word = q.limbs[0]; |
| 1283 | 1283 | while (r_word != 0) { |
| 1284 | const ch = std.fmt.digitToChar(@intCast(u8, r_word % base), uppercase); | |
| 1284 | const ch = std.fmt.digitToChar(@intCast(u8, r_word % base), case); | |
| 1285 | 1285 | r_word /= base; |
| 1286 | 1286 | string[digits_len] = ch; |
| 1287 | 1287 | digits_len += 1; |
| ... | ... | @@ -1615,9 +1615,9 @@ pub const Managed = struct { |
| 1615 | 1615 | |
| 1616 | 1616 | /// Converts self to a string in the requested base. Memory is allocated from the provided |
| 1617 | 1617 | /// allocator and not the one present in self. |
| 1618 | pub fn toString(self: Managed, allocator: *Allocator, base: u8, uppercase: bool) ![]u8 { | |
| 1618 | pub fn toString(self: Managed, allocator: *Allocator, base: u8, case: std.fmt.Case) ![]u8 { | |
| 1619 | 1619 | if (base < 2 or base > 16) return error.InvalidBase; |
| 1620 | return self.toConst().toStringAlloc(self.allocator, base, uppercase); | |
| 1620 | return self.toConst().toStringAlloc(self.allocator, base, case); | |
| 1621 | 1621 | } |
| 1622 | 1622 | |
| 1623 | 1623 | /// To allow `std.fmt.format` to work with `Managed`. |
lib/std/math/big/int_test.zig+15-15| ... | ... | @@ -277,7 +277,7 @@ test "big.int string to" { |
| 277 | 277 | var a = try Managed.initSet(testing.allocator, 120317241209124781241290847124); |
| 278 | 278 | defer a.deinit(); |
| 279 | 279 | |
| 280 | const as = try a.toString(testing.allocator, 10, false); | |
| 280 | const as = try a.toString(testing.allocator, 10, .lower); | |
| 281 | 281 | defer testing.allocator.free(as); |
| 282 | 282 | const es = "120317241209124781241290847124"; |
| 283 | 283 | |
| ... | ... | @@ -288,14 +288,14 @@ test "big.int string to base base error" { |
| 288 | 288 | var a = try Managed.initSet(testing.allocator, 0xffffffff); |
| 289 | 289 | defer a.deinit(); |
| 290 | 290 | |
| 291 | try testing.expectError(error.InvalidBase, a.toString(testing.allocator, 45, false)); | |
| 291 | try testing.expectError(error.InvalidBase, a.toString(testing.allocator, 45, .lower)); | |
| 292 | 292 | } |
| 293 | 293 | |
| 294 | 294 | test "big.int string to base 2" { |
| 295 | 295 | var a = try Managed.initSet(testing.allocator, -0b1011); |
| 296 | 296 | defer a.deinit(); |
| 297 | 297 | |
| 298 | const as = try a.toString(testing.allocator, 2, false); | |
| 298 | const as = try a.toString(testing.allocator, 2, .lower); | |
| 299 | 299 | defer testing.allocator.free(as); |
| 300 | 300 | const es = "-1011"; |
| 301 | 301 | |
| ... | ... | @@ -306,7 +306,7 @@ test "big.int string to base 16" { |
| 306 | 306 | var a = try Managed.initSet(testing.allocator, 0xefffffff00000001eeeeeeefaaaaaaab); |
| 307 | 307 | defer a.deinit(); |
| 308 | 308 | |
| 309 | const as = try a.toString(testing.allocator, 16, false); | |
| 309 | const as = try a.toString(testing.allocator, 16, .lower); | |
| 310 | 310 | defer testing.allocator.free(as); |
| 311 | 311 | const es = "efffffff00000001eeeeeeefaaaaaaab"; |
| 312 | 312 | |
| ... | ... | @@ -317,7 +317,7 @@ test "big.int neg string to" { |
| 317 | 317 | var a = try Managed.initSet(testing.allocator, -123907434); |
| 318 | 318 | defer a.deinit(); |
| 319 | 319 | |
| 320 | const as = try a.toString(testing.allocator, 10, false); | |
| 320 | const as = try a.toString(testing.allocator, 10, .lower); | |
| 321 | 321 | defer testing.allocator.free(as); |
| 322 | 322 | const es = "-123907434"; |
| 323 | 323 | |
| ... | ... | @@ -328,7 +328,7 @@ test "big.int zero string to" { |
| 328 | 328 | var a = try Managed.initSet(testing.allocator, 0); |
| 329 | 329 | defer a.deinit(); |
| 330 | 330 | |
| 331 | const as = try a.toString(testing.allocator, 10, false); | |
| 331 | const as = try a.toString(testing.allocator, 10, .lower); | |
| 332 | 332 | defer testing.allocator.free(as); |
| 333 | 333 | const es = "0"; |
| 334 | 334 | |
| ... | ... | @@ -1180,7 +1180,7 @@ test "big.int div multi-multi zero-limb trailing (with rem)" { |
| 1180 | 1180 | |
| 1181 | 1181 | try testing.expect((try q.to(u128)) == 0x10000000000000000); |
| 1182 | 1182 | |
| 1183 | const rs = try r.toString(testing.allocator, 16, false); | |
| 1183 | const rs = try r.toString(testing.allocator, 16, .lower); | |
| 1184 | 1184 | defer testing.allocator.free(rs); |
| 1185 | 1185 | try testing.expect(std.mem.eql(u8, rs, "4444444344444443111111111111111100000000000000000000000000000000")); |
| 1186 | 1186 | } |
| ... | ... | @@ -1199,7 +1199,7 @@ test "big.int div multi-multi zero-limb trailing (with rem) and dividend zero-li |
| 1199 | 1199 | |
| 1200 | 1200 | try testing.expect((try q.to(u128)) == 0x1); |
| 1201 | 1201 | |
| 1202 | const rs = try r.toString(testing.allocator, 16, false); | |
| 1202 | const rs = try r.toString(testing.allocator, 16, .lower); | |
| 1203 | 1203 | defer testing.allocator.free(rs); |
| 1204 | 1204 | try testing.expect(std.mem.eql(u8, rs, "444444434444444311111111111111110000000000000000")); |
| 1205 | 1205 | } |
| ... | ... | @@ -1216,11 +1216,11 @@ test "big.int div multi-multi zero-limb trailing (with rem) and dividend zero-li |
| 1216 | 1216 | defer r.deinit(); |
| 1217 | 1217 | try Managed.divTrunc(&q, &r, a.toConst(), b.toConst()); |
| 1218 | 1218 | |
| 1219 | const qs = try q.toString(testing.allocator, 16, false); | |
| 1219 | const qs = try q.toString(testing.allocator, 16, .lower); | |
| 1220 | 1220 | defer testing.allocator.free(qs); |
| 1221 | 1221 | try testing.expect(std.mem.eql(u8, qs, "10000000000000000820820803105186f")); |
| 1222 | 1222 | |
| 1223 | const rs = try r.toString(testing.allocator, 16, false); | |
| 1223 | const rs = try r.toString(testing.allocator, 16, .lower); | |
| 1224 | 1224 | defer testing.allocator.free(rs); |
| 1225 | 1225 | try testing.expect(std.mem.eql(u8, rs, "4e11f2baa5896a321d463b543d0104e30000000000000000")); |
| 1226 | 1226 | } |
| ... | ... | @@ -1240,11 +1240,11 @@ test "big.int div multi-multi fuzz case #1" { |
| 1240 | 1240 | defer r.deinit(); |
| 1241 | 1241 | try Managed.divTrunc(&q, &r, a.toConst(), b.toConst()); |
| 1242 | 1242 | |
| 1243 | const qs = try q.toString(testing.allocator, 16, false); | |
| 1243 | const qs = try q.toString(testing.allocator, 16, .lower); | |
| 1244 | 1244 | defer testing.allocator.free(qs); |
| 1245 | 1245 | try testing.expect(std.mem.eql(u8, qs, "3ffffffffffffffffffffffffffff0000000000000000000000000000000000001ffffffffffffffffffffffffffff7fffffffe000000000000000000000000000180000000000000000000003fffffbfffffffdfffffffffffffeffff800000100101000000100000000020003fffffdfbfffffe3ffffffffffffeffff7fffc00800a100000017ffe000002000400007efbfff7fe9f00000037ffff3fff7fffa004006100000009ffe00000190038200bf7d2ff7fefe80400060000f7d7f8fbf9401fe38e0403ffc0bdffffa51102c300d7be5ef9df4e5060007b0127ad3fa69f97d0f820b6605ff617ddf7f32ad7a05c0d03f2e7bc78a6000e087a8bbcdc59e07a5a079128a7861f553ddebed7e8e56701756f9ead39b48cd1b0831889ea6ec1fddf643d0565b075ff07e6caea4e2854ec9227fd635ed60a2f5eef2893052ffd54718fa08604acbf6a15e78a467c4a3c53c0278af06c4416573f925491b195e8fd79302cb1aaf7caf4ecfc9aec1254cc969786363ac729f914c6ddcc26738d6b0facd54eba026580aba2eb6482a088b0d224a8852420b91ec1")); |
| 1246 | 1246 | |
| 1247 | const rs = try r.toString(testing.allocator, 16, false); | |
| 1247 | const rs = try r.toString(testing.allocator, 16, .lower); | |
| 1248 | 1248 | defer testing.allocator.free(rs); |
| 1249 | 1249 | try testing.expect(std.mem.eql(u8, rs, "310d1d4c414426b4836c2635bad1df3a424e50cbdd167ffccb4dfff57d36b4aae0d6ca0910698220171a0f3373c1060a046c2812f0027e321f72979daa5e7973214170d49e885de0c0ecc167837d44502430674a82522e5df6a0759548052420b91ec1")); |
| 1250 | 1250 | } |
| ... | ... | @@ -1264,11 +1264,11 @@ test "big.int div multi-multi fuzz case #2" { |
| 1264 | 1264 | defer r.deinit(); |
| 1265 | 1265 | try Managed.divTrunc(&q, &r, a.toConst(), b.toConst()); |
| 1266 | 1266 | |
| 1267 | const qs = try q.toString(testing.allocator, 16, false); | |
| 1267 | const qs = try q.toString(testing.allocator, 16, .lower); | |
| 1268 | 1268 | defer testing.allocator.free(qs); |
| 1269 | 1269 | try testing.expect(std.mem.eql(u8, qs, "40100400fe3f8fe3f8fe3f8fe3f8fe3f8fe4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f91e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4992649926499264991e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4792e4b92e4b92e4b92e4b92a4a92a4a92a4")); |
| 1270 | 1270 | |
| 1271 | const rs = try r.toString(testing.allocator, 16, false); | |
| 1271 | const rs = try r.toString(testing.allocator, 16, .lower); | |
| 1272 | 1272 | defer testing.allocator.free(rs); |
| 1273 | 1273 | try testing.expect(std.mem.eql(u8, rs, "a900000000000000000000000000000000000000000000000000")); |
| 1274 | 1274 | } |
| ... | ... | @@ -1533,7 +1533,7 @@ test "big.int pow" { |
| 1533 | 1533 | |
| 1534 | 1534 | try testing.expect(a.eq(y)); |
| 1535 | 1535 | |
| 1536 | const ys = try y.toString(testing.allocator, 16, false); | |
| 1536 | const ys = try y.toString(testing.allocator, 16, .lower); | |
| 1537 | 1537 | defer testing.allocator.free(ys); |
| 1538 | 1538 | try testing.expectEqualSlices( |
| 1539 | 1539 | u8, |
lib/std/zig/fmt.zig+1-1| ... | ... | @@ -68,7 +68,7 @@ pub fn formatEscapes( |
| 68 | 68 | // Use hex escapes for rest any unprintable characters. |
| 69 | 69 | else => { |
| 70 | 70 | try writer.writeAll("\\x"); |
| 71 | try std.fmt.formatInt(byte, 16, false, .{ .width = 2, .fill = '0' }, writer); | |
| 71 | try std.fmt.formatInt(byte, 16, .lower, .{ .width = 2, .fill = '0' }, writer); | |
| 72 | 72 | }, |
| 73 | 73 | }; |
| 74 | 74 | } |
src/Compilation.zig+1-1| ... | ... | @@ -4009,7 +4009,7 @@ fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node |
| 4009 | 4009 | }); |
| 4010 | 4010 | var digest_plus_flags: [digest.len + 2]u8 = undefined; |
| 4011 | 4011 | digest_plus_flags[0..digest.len].* = digest; |
| 4012 | assert(std.fmt.formatIntBuf(digest_plus_flags[digest.len..], stage1_flags_byte, 16, false, .{ | |
| 4012 | assert(std.fmt.formatIntBuf(digest_plus_flags[digest.len..], stage1_flags_byte, 16, .lower, .{ | |
| 4013 | 4013 | .width = 2, |
| 4014 | 4014 | .fill = '0', |
| 4015 | 4015 | }) == 2); |
src/DepTokenizer.zig+2-2| ... | ... | @@ -1026,13 +1026,13 @@ fn hexDump16(out: anytype, offset: usize, bytes: []const u8) !void { |
| 1026 | 1026 | |
| 1027 | 1027 | fn printDecValue(out: anytype, value: u64, width: u8) !void { |
| 1028 | 1028 | var buffer: [20]u8 = undefined; |
| 1029 | const len = std.fmt.formatIntBuf(buffer[0..], value, 10, false, .{ .width = width, .fill = '0' }); | |
| 1029 | const len = std.fmt.formatIntBuf(buffer[0..], value, 10, .lower, .{ .width = width, .fill = '0' }); | |
| 1030 | 1030 | try out.writeAll(buffer[0..len]); |
| 1031 | 1031 | } |
| 1032 | 1032 | |
| 1033 | 1033 | fn printHexValue(out: anytype, value: u64, width: u8) !void { |
| 1034 | 1034 | var buffer: [16]u8 = undefined; |
| 1035 | const len = std.fmt.formatIntBuf(buffer[0..], value, 16, false, .{ .width = width, .fill = '0' }); | |
| 1035 | const len = std.fmt.formatIntBuf(buffer[0..], value, 16, .lower, .{ .width = width, .fill = '0' }); | |
| 1036 | 1036 | try out.writeAll(buffer[0..len]); |
| 1037 | 1037 | } |
| 1038 | 1038 |
src/Zir.zig+1-1| ... | ... | @@ -3231,7 +3231,7 @@ const Writer = struct { |
| 3231 | 3231 | .limbs = limbs, |
| 3232 | 3232 | .positive = true, |
| 3233 | 3233 | }; |
| 3234 | const as_string = try big_int.toStringAlloc(self.gpa, 10, false); | |
| 3234 | const as_string = try big_int.toStringAlloc(self.gpa, 10, .lower); | |
| 3235 | 3235 | defer self.gpa.free(as_string); |
| 3236 | 3236 | try stream.print("{s})", .{as_string}); |
| 3237 | 3237 | } |
src/translate_c.zig+4-4| ... | ... | @@ -4146,7 +4146,7 @@ fn transCreateNodeAPInt(c: *Context, int: *const clang.APSInt) !Node { |
| 4146 | 4146 | } |
| 4147 | 4147 | |
| 4148 | 4148 | const big: math.big.int.Const = .{ .limbs = limbs, .positive = true }; |
| 4149 | const str = big.toStringAlloc(c.arena, 10, false) catch |err| switch (err) { | |
| 4149 | const str = big.toStringAlloc(c.arena, 10, .lower) catch |err| switch (err) { | |
| 4150 | 4150 | error.OutOfMemory => return error.OutOfMemory, |
| 4151 | 4151 | }; |
| 4152 | 4152 | const res = try Tag.integer_literal.create(c.arena, str); |
| ... | ... | @@ -5083,7 +5083,7 @@ fn zigifyEscapeSequences(ctx: *Context, m: *MacroCtx) ![]const u8 { |
| 5083 | 5083 | num += c - 'A' + 10; |
| 5084 | 5084 | }, |
| 5085 | 5085 | else => { |
| 5086 | i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{ .fill = '0', .width = 2 }); | |
| 5086 | i += std.fmt.formatIntBuf(bytes[i..], num, 16, .lower, std.fmt.FormatOptions{ .fill = '0', .width = 2 }); | |
| 5087 | 5087 | num = 0; |
| 5088 | 5088 | if (c == '\\') |
| 5089 | 5089 | state = .Escape |
| ... | ... | @@ -5109,7 +5109,7 @@ fn zigifyEscapeSequences(ctx: *Context, m: *MacroCtx) ![]const u8 { |
| 5109 | 5109 | }; |
| 5110 | 5110 | num += c - '0'; |
| 5111 | 5111 | } else { |
| 5112 | i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{ .fill = '0', .width = 2 }); | |
| 5112 | i += std.fmt.formatIntBuf(bytes[i..], num, 16, .lower, std.fmt.FormatOptions{ .fill = '0', .width = 2 }); | |
| 5113 | 5113 | num = 0; |
| 5114 | 5114 | count = 0; |
| 5115 | 5115 | if (c == '\\') |
| ... | ... | @@ -5123,7 +5123,7 @@ fn zigifyEscapeSequences(ctx: *Context, m: *MacroCtx) ![]const u8 { |
| 5123 | 5123 | } |
| 5124 | 5124 | } |
| 5125 | 5125 | if (state == .Hex or state == .Octal) |
| 5126 | i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{ .fill = '0', .width = 2 }); | |
| 5126 | i += std.fmt.formatIntBuf(bytes[i..], num, 16, .lower, std.fmt.FormatOptions{ .fill = '0', .width = 2 }); | |
| 5127 | 5127 | return bytes[0..i]; |
| 5128 | 5128 | } |
| 5129 | 5129 |
test/behavior/enum_with_members.zig+2-2| ... | ... | @@ -8,8 +8,8 @@ const ET = union(enum) { |
| 8 | 8 | |
| 9 | 9 | pub fn print(a: *const ET, buf: []u8) anyerror!usize { |
| 10 | 10 | return switch (a.*) { |
| 11 | ET.SINT => |x| fmt.formatIntBuf(buf, x, 10, false, fmt.FormatOptions{}), | |
| 12 | ET.UINT => |x| fmt.formatIntBuf(buf, x, 10, false, fmt.FormatOptions{}), | |
| 11 | ET.SINT => |x| fmt.formatIntBuf(buf, x, 10, .lower, fmt.FormatOptions{}), | |
| 12 | ET.UINT => |x| fmt.formatIntBuf(buf, x, 10, .lower, fmt.FormatOptions{}), | |
| 13 | 13 | }; |
| 14 | 14 | } |
| 15 | 15 | }; |