| ... | ... | @@ -748,7 +748,7 @@ pub fn formatIntValue( |
| 748 | 748 | options: FormatOptions, |
| 749 | 749 | writer: anytype, |
| 750 | 750 | ) !void { |
| 751 | | comptime var radix = 10; |
| 751 | comptime var base = 10; |
| 752 | 752 | comptime var case: Case = .lower; |
| 753 | 753 | |
| 754 | 754 | const int_value = if (@TypeOf(value) == comptime_int) blk: { |
| ... | ... | @@ -757,7 +757,7 @@ pub fn formatIntValue( |
| 757 | 757 | } else value; |
| 758 | 758 | |
| 759 | 759 | if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "d")) { |
| 760 | | radix = 10; |
| 760 | base = 10; |
| 761 | 761 | case = .lower; |
| 762 | 762 | } else if (comptime std.mem.eql(u8, fmt, "c")) { |
| 763 | 763 | if (@typeInfo(@TypeOf(int_value)).Int.bits <= 8) { |
| ... | ... | @@ -772,22 +772,22 @@ pub fn formatIntValue( |
| 772 | 772 | @compileError("cannot print integer that is larger than 21 bits as an UTF-8 sequence"); |
| 773 | 773 | } |
| 774 | 774 | } else if (comptime std.mem.eql(u8, fmt, "b")) { |
| 775 | | radix = 2; |
| 775 | base = 2; |
| 776 | 776 | case = .lower; |
| 777 | 777 | } else if (comptime std.mem.eql(u8, fmt, "x")) { |
| 778 | | radix = 16; |
| 778 | base = 16; |
| 779 | 779 | case = .lower; |
| 780 | 780 | } else if (comptime std.mem.eql(u8, fmt, "X")) { |
| 781 | | radix = 16; |
| 781 | base = 16; |
| 782 | 782 | case = .upper; |
| 783 | 783 | } else if (comptime std.mem.eql(u8, fmt, "o")) { |
| 784 | | radix = 8; |
| 784 | base = 8; |
| 785 | 785 | case = .lower; |
| 786 | 786 | } else { |
| 787 | 787 | invalidFmtError(fmt, value); |
| 788 | 788 | } |
| 789 | 789 | |
| 790 | | return formatInt(int_value, radix, case, options, writer); |
| 790 | return formatInt(int_value, base, case, options, writer); |
| 791 | 791 | } |
| 792 | 792 | |
| 793 | 793 | fn formatFloatValue( |
| ... | ... | @@ -906,7 +906,7 @@ pub fn fmtSliceEscapeUpper(bytes: []const u8) std.fmt.Formatter(formatSliceEscap |
| 906 | 906 | return .{ .data = bytes }; |
| 907 | 907 | } |
| 908 | 908 | |
| 909 | | fn formatSizeImpl(comptime radix: comptime_int) type { |
| 909 | fn formatSizeImpl(comptime base: comptime_int) type { |
| 910 | 910 | return struct { |
| 911 | 911 | fn formatSizeImpl( |
| 912 | 912 | value: u64, |
| ... | ... | @@ -926,13 +926,13 @@ fn formatSizeImpl(comptime radix: comptime_int) type { |
| 926 | 926 | const mags_iec = " KMGTPEZY"; |
| 927 | 927 | |
| 928 | 928 | const log2 = math.log2(value); |
| 929 | | const magnitude = switch (radix) { |
| 929 | const magnitude = switch (base) { |
| 930 | 930 | 1000 => math.min(log2 / comptime math.log2(1000), mags_si.len - 1), |
| 931 | 931 | 1024 => math.min(log2 / 10, mags_iec.len - 1), |
| 932 | 932 | else => unreachable, |
| 933 | 933 | }; |
| 934 | | const new_value = lossyCast(f64, value) / math.pow(f64, lossyCast(f64, radix), lossyCast(f64, magnitude)); |
| 935 | | const suffix = switch (radix) { |
| 934 | const new_value = lossyCast(f64, value) / math.pow(f64, lossyCast(f64, base), lossyCast(f64, magnitude)); |
| 935 | const suffix = switch (base) { |
| 936 | 936 | 1000 => mags_si[magnitude], |
| 937 | 937 | 1024 => mags_iec[magnitude], |
| 938 | 938 | else => unreachable, |
| ... | ... | @@ -944,7 +944,7 @@ fn formatSizeImpl(comptime radix: comptime_int) type { |
| 944 | 944 | |
| 945 | 945 | bufstream.writer().writeAll(if (suffix == ' ') |
| 946 | 946 | "B" |
| 947 | | else switch (radix) { |
| 947 | else switch (base) { |
| 948 | 948 | 1000 => &[_]u8{ suffix, 'B' }, |
| 949 | 949 | 1024 => &[_]u8{ suffix, 'i', 'B' }, |
| 950 | 950 | else => unreachable, |
| ... | ... | @@ -1730,21 +1730,21 @@ pub fn Formatter(comptime format_fn: anytype) type { |
| 1730 | 1730 | } |
| 1731 | 1731 | |
| 1732 | 1732 | /// Parses the string `buf` as signed or unsigned representation in the |
| 1733 | | /// specified radix of an integral value of type `T`. |
| 1733 | /// specified base of an integral value of type `T`. |
| 1734 | 1734 | /// |
| 1735 | | /// When `radix` is zero the string prefix is examined to detect the true radix: |
| 1736 | | /// * A prefix of "0b" implies radix=2, |
| 1737 | | /// * A prefix of "0o" implies radix=8, |
| 1738 | | /// * A prefix of "0x" implies radix=16, |
| 1739 | | /// * Otherwise radix=10 is assumed. |
| 1735 | /// When `base` is zero the string prefix is examined to detect the true base: |
| 1736 | /// * A prefix of "0b" implies base=2, |
| 1737 | /// * A prefix of "0o" implies base=8, |
| 1738 | /// * A prefix of "0x" implies base=16, |
| 1739 | /// * Otherwise base=10 is assumed. |
| 1740 | 1740 | /// |
| 1741 | 1741 | /// Ignores '_' character in `buf`. |
| 1742 | 1742 | /// See also `parseUnsigned`. |
| 1743 | | pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) ParseIntError!T { |
| 1743 | pub fn parseInt(comptime T: type, buf: []const u8, base: u8) ParseIntError!T { |
| 1744 | 1744 | if (buf.len == 0) return error.InvalidCharacter; |
| 1745 | | if (buf[0] == '+') return parseWithSign(T, buf[1..], radix, .pos); |
| 1746 | | if (buf[0] == '-') return parseWithSign(T, buf[1..], radix, .neg); |
| 1747 | | return parseWithSign(T, buf, radix, .pos); |
| 1745 | if (buf[0] == '+') return parseWithSign(T, buf[1..], base, .pos); |
| 1746 | if (buf[0] == '-') return parseWithSign(T, buf[1..], base, .neg); |
| 1747 | return parseWithSign(T, buf, base, .pos); |
| 1748 | 1748 | } |
| 1749 | 1749 | |
| 1750 | 1750 | test "parseInt" { |
| ... | ... | @@ -1777,7 +1777,7 @@ test "parseInt" { |
| 1777 | 1777 | try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "-", 10)); |
| 1778 | 1778 | try std.testing.expectError(error.InvalidCharacter, parseInt(i32, "-", 10)); |
| 1779 | 1779 | |
| 1780 | | // autodectect the radix |
| 1780 | // autodectect the base |
| 1781 | 1781 | try std.testing.expect((try parseInt(i32, "111", 0)) == 111); |
| 1782 | 1782 | try std.testing.expect((try parseInt(i32, "1_1_1", 0)) == 111); |
| 1783 | 1783 | try std.testing.expect((try parseInt(i32, "1_1_1", 0)) == 111); |
| ... | ... | @@ -1804,29 +1804,29 @@ test "parseInt" { |
| 1804 | 1804 | fn parseWithSign( |
| 1805 | 1805 | comptime T: type, |
| 1806 | 1806 | buf: []const u8, |
| 1807 | | radix: u8, |
| 1807 | base: u8, |
| 1808 | 1808 | comptime sign: enum { pos, neg }, |
| 1809 | 1809 | ) ParseIntError!T { |
| 1810 | 1810 | if (buf.len == 0) return error.InvalidCharacter; |
| 1811 | 1811 | |
| 1812 | | var buf_radix = radix; |
| 1812 | var buf_base = base; |
| 1813 | 1813 | var buf_start = buf; |
| 1814 | | if (radix == 0) { |
| 1814 | if (base == 0) { |
| 1815 | 1815 | // Treat is as a decimal number by default. |
| 1816 | | buf_radix = 10; |
| 1817 | | // Detect the radix by looking at buf prefix. |
| 1816 | buf_base = 10; |
| 1817 | // Detect the base by looking at buf prefix. |
| 1818 | 1818 | if (buf.len > 2 and buf[0] == '0') { |
| 1819 | 1819 | switch (std.ascii.toLower(buf[1])) { |
| 1820 | 1820 | 'b' => { |
| 1821 | | buf_radix = 2; |
| 1821 | buf_base = 2; |
| 1822 | 1822 | buf_start = buf[2..]; |
| 1823 | 1823 | }, |
| 1824 | 1824 | 'o' => { |
| 1825 | | buf_radix = 8; |
| 1825 | buf_base = 8; |
| 1826 | 1826 | buf_start = buf[2..]; |
| 1827 | 1827 | }, |
| 1828 | 1828 | 'x' => { |
| 1829 | | buf_radix = 16; |
| 1829 | buf_base = 16; |
| 1830 | 1830 | buf_start = buf[2..]; |
| 1831 | 1831 | }, |
| 1832 | 1832 | else => {}, |
| ... | ... | @@ -1845,28 +1845,28 @@ fn parseWithSign( |
| 1845 | 1845 | |
| 1846 | 1846 | for (buf_start) |c| { |
| 1847 | 1847 | if (c == '_') continue; |
| 1848 | | const digit = try charToDigit(c, buf_radix); |
| 1848 | const digit = try charToDigit(c, buf_base); |
| 1849 | 1849 | |
| 1850 | | if (x != 0) x = try math.mul(T, x, math.cast(T, buf_radix) orelse return error.Overflow); |
| 1850 | if (x != 0) x = try math.mul(T, x, math.cast(T, buf_base) orelse return error.Overflow); |
| 1851 | 1851 | x = try add(T, x, math.cast(T, digit) orelse return error.Overflow); |
| 1852 | 1852 | } |
| 1853 | 1853 | |
| 1854 | 1854 | return x; |
| 1855 | 1855 | } |
| 1856 | 1856 | |
| 1857 | | /// Parses the string `buf` as unsigned representation in the specified radix |
| 1857 | /// Parses the string `buf` as unsigned representation in the specified base |
| 1858 | 1858 | /// of an integral value of type `T`. |
| 1859 | 1859 | /// |
| 1860 | | /// When `radix` is zero the string prefix is examined to detect the true radix: |
| 1861 | | /// * A prefix of "0b" implies radix=2, |
| 1862 | | /// * A prefix of "0o" implies radix=8, |
| 1863 | | /// * A prefix of "0x" implies radix=16, |
| 1864 | | /// * Otherwise radix=10 is assumed. |
| 1860 | /// When `base` is zero the string prefix is examined to detect the true base: |
| 1861 | /// * A prefix of "0b" implies base=2, |
| 1862 | /// * A prefix of "0o" implies base=8, |
| 1863 | /// * A prefix of "0x" implies base=16, |
| 1864 | /// * Otherwise base=10 is assumed. |
| 1865 | 1865 | /// |
| 1866 | 1866 | /// Ignores '_' character in `buf`. |
| 1867 | 1867 | /// See also `parseInt`. |
| 1868 | | pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) ParseIntError!T { |
| 1869 | | return parseWithSign(T, buf, radix, .pos); |
| 1868 | pub fn parseUnsigned(comptime T: type, buf: []const u8, base: u8) ParseIntError!T { |
| 1869 | return parseWithSign(T, buf, base, .pos); |
| 1870 | 1870 | } |
| 1871 | 1871 | |
| 1872 | 1872 | test "parseUnsigned" { |
| ... | ... | @@ -1889,7 +1889,7 @@ test "parseUnsigned" { |
| 1889 | 1889 | |
| 1890 | 1890 | try std.testing.expect((try parseUnsigned(u32, "NUMBER", 36)) == 1442151747); |
| 1891 | 1891 | |
| 1892 | | // these numbers should fit even though the radix itself doesn't fit in the destination type |
| 1892 | // these numbers should fit even though the base itself doesn't fit in the destination type |
| 1893 | 1893 | try std.testing.expect((try parseUnsigned(u1, "0", 10)) == 0); |
| 1894 | 1894 | try std.testing.expect((try parseUnsigned(u1, "1", 10)) == 1); |
| 1895 | 1895 | try std.testing.expectError(error.Overflow, parseUnsigned(u1, "2", 10)); |
| ... | ... | @@ -1906,14 +1906,14 @@ test "parseUnsigned" { |
| 1906 | 1906 | } |
| 1907 | 1907 | |
| 1908 | 1908 | /// Parses a number like '2G', '2Gi', or '2GiB'. |
| 1909 | | pub fn parseIntSizeSuffix(buf: []const u8, radix: u8) ParseIntError!usize { |
| 1909 | pub fn parseIntSizeSuffix(buf: []const u8, digit_base: u8) ParseIntError!usize { |
| 1910 | 1910 | var without_B = buf; |
| 1911 | 1911 | if (mem.endsWith(u8, buf, "B")) without_B.len -= 1; |
| 1912 | 1912 | var without_i = without_B; |
| 1913 | | var base: usize = 1000; |
| 1913 | var magnitude_base: usize = 1000; |
| 1914 | 1914 | if (mem.endsWith(u8, without_B, "i")) { |
| 1915 | 1915 | without_i.len -= 1; |
| 1916 | | base = 1024; |
| 1916 | magnitude_base = 1024; |
| 1917 | 1917 | } |
| 1918 | 1918 | if (without_i.len == 0) return error.InvalidCharacter; |
| 1919 | 1919 | const orders_of_magnitude: usize = switch (without_i[without_i.len - 1]) { |
| ... | ... | @@ -1935,11 +1935,11 @@ pub fn parseIntSizeSuffix(buf: []const u8, radix: u8) ParseIntError!usize { |
| 1935 | 1935 | } else if (without_i.len != without_B.len) { |
| 1936 | 1936 | return error.InvalidCharacter; |
| 1937 | 1937 | } |
| 1938 | | const multiplier = math.powi(usize, base, orders_of_magnitude) catch |err| switch (err) { |
| 1938 | const multiplier = math.powi(usize, magnitude_base, orders_of_magnitude) catch |err| switch (err) { |
| 1939 | 1939 | error.Underflow => unreachable, |
| 1940 | 1940 | error.Overflow => return error.Overflow, |
| 1941 | 1941 | }; |
| 1942 | | const number = try std.fmt.parseInt(usize, without_suffix, radix); |
| 1942 | const number = try std.fmt.parseInt(usize, without_suffix, digit_base); |
| 1943 | 1943 | return math.mul(usize, number, multiplier); |
| 1944 | 1944 | } |
| 1945 | 1945 | |
| ... | ... | @@ -1962,7 +1962,7 @@ test { |
| 1962 | 1962 | _ = &parseFloat; |
| 1963 | 1963 | } |
| 1964 | 1964 | |
| 1965 | | pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) { |
| 1965 | pub fn charToDigit(c: u8, base: u8) (error{InvalidCharacter}!u8) { |
| 1966 | 1966 | const value = switch (c) { |
| 1967 | 1967 | '0'...'9' => c - '0', |
| 1968 | 1968 | 'A'...'Z' => c - 'A' + 10, |
| ... | ... | @@ -1970,7 +1970,7 @@ pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) { |
| 1970 | 1970 | else => return error.InvalidCharacter, |
| 1971 | 1971 | }; |
| 1972 | 1972 | |
| 1973 | | if (value >= radix) return error.InvalidCharacter; |
| 1973 | if (value >= base) return error.InvalidCharacter; |
| 1974 | 1974 | |
| 1975 | 1975 | return value; |
| 1976 | 1976 | } |