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