authorgravatar for yujiri@disroot.orgEvin Yulo <yujiri@disroot.org> 2023-05-20 20:58:28-04:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-06-01 00:02:16+03:00
log6c2f3745564aefa669b336e249888bb7390b3a3f
treeecbd6f6ee28bc681bd935a979af22c704cc46528
parent3085e2af4197193f08ea95d80afb2cf982227334

Use the word 'base' consistently instead of 'radix'


8 files changed, 81 insertions(+), 78 deletions(-)

lib/std/fmt.zig+49-49
...@@ -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;
753753
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;
758758
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 }
789789
790 return formatInt(int_value, radix, case, options, writer);790 return formatInt(int_value, base, case, options, writer);
791}791}
792792
793fn formatFloatValue(793fn 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}
908908
909fn formatSizeImpl(comptime radix: comptime_int) type {909fn 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";
927927
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 {
944944
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}
17311731
1732/// Parses the string `buf` as signed or unsigned representation in the1732/// 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`.
1743pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) ParseIntError!T {1743pub 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}
17491749
1750test "parseInt" {1750test "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));
17791779
1780 // autodectect the radix1780 // 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" {
1804fn parseWithSign(1804fn 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;
18111811
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(
18451845
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);
18491849
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 }
18531853
1854 return x;1854 return x;
1855}1855}
18561856
1857/// Parses the string `buf` as unsigned representation in the specified radix1857/// 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`.
1868pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) ParseIntError!T {1868pub 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}
18711871
1872test "parseUnsigned" {1872test "parseUnsigned" {
...@@ -1889,7 +1889,7 @@ test "parseUnsigned" {...@@ -1889,7 +1889,7 @@ test "parseUnsigned" {
18891889
1890 try std.testing.expect((try parseUnsigned(u32, "NUMBER", 36)) == 1442151747);1890 try std.testing.expect((try parseUnsigned(u32, "NUMBER", 36)) == 1442151747);
18911891
1892 // these numbers should fit even though the radix itself doesn't fit in the destination type1892 // 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}
19071907
1908/// Parses a number like '2G', '2Gi', or '2GiB'.1908/// Parses a number like '2G', '2Gi', or '2GiB'.
1909pub fn parseIntSizeSuffix(buf: []const u8, radix: u8) ParseIntError!usize {1909pub 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}
19451945
...@@ -1962,7 +1962,7 @@ test {...@@ -1962,7 +1962,7 @@ test {
1962 _ = &parseFloat;1962 _ = &parseFloat;
1963}1963}
19641964
1965pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) {1965pub 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 };
19721972
1973 if (value >= radix) return error.InvalidCharacter;1973 if (value >= base) return error.InvalidCharacter;
19741974
1975 return value;1975 return value;
1976}1976}
lib/std/fmt/parse_float/decimal.zig+2-2
...@@ -34,13 +34,13 @@ pub fn Decimal(comptime T: type) type {...@@ -34,13 +34,13 @@ pub fn Decimal(comptime T: type) type {
34 /// For a double-precision IEEE-754 float, this required 767 digits,34 /// For a double-precision IEEE-754 float, this required 767 digits,
35 /// so we store the max digits + 1.35 /// so we store the max digits + 1.
36 ///36 ///
37 /// We can exactly represent a float in radix `b` from radix 2 if37 /// We can exactly represent a float in base `b` from base 2 if
38 /// `b` is divisible by 2. This function calculates the exact number of38 /// `b` is divisible by 2. This function calculates the exact number of
39 /// digits required to exactly represent that float.39 /// digits required to exactly represent that float.
40 ///40 ///
41 /// According to the "Handbook of Floating Point Arithmetic",41 /// According to the "Handbook of Floating Point Arithmetic",
42 /// for IEEE754, with emin being the min exponent, p2 being the42 /// for IEEE754, with emin being the min exponent, p2 being the
43 /// precision, and b being the radix, the number of digits follows as:43 /// precision, and b being the base, the number of digits follows as:
44 ///44 ///
45 /// `−emin + p2 + ⌊(emin + 1) log(2, b) − log(1 − 2^(−p2), b)⌋`45 /// `−emin + p2 + ⌊(emin + 1) log(2, b) − log(1 − 2^(−p2), b)⌋`
46 ///46 ///
lib/std/math/big/int.zig+8-8
...@@ -1627,7 +1627,7 @@ pub const Mutable = struct {...@@ -1627,7 +1627,7 @@ pub const Mutable = struct {
1627 // while x >= y * b^(n - t):1627 // while x >= y * b^(n - t):
1628 // x -= y * b^(n - t)1628 // x -= y * b^(n - t)
1629 // q[n - t] += 11629 // q[n - t] += 1
1630 // Note, this algorithm is performed only once if y[t] > radix/2 and y is even, which we1630 // Note, this algorithm is performed only once if y[t] > base/2 and y is even, which we
1631 // enforced in step 0. This means we can replace the while with an if.1631 // enforced in step 0. This means we can replace the while with an if.
1632 // Note, multiplication by b^(n - t) comes down to shifting to the right by n - t limbs.1632 // Note, multiplication by b^(n - t) comes down to shifting to the right by n - t limbs.
1633 // We can also replace x >= y * b^(n - t) by x/b^(n - t) >= y, and use shifts for that.1633 // We can also replace x >= y * b^(n - t) by x/b^(n - t) >= y, and use shifts for that.
...@@ -2206,20 +2206,20 @@ pub const Const = struct {...@@ -2206,20 +2206,20 @@ pub const Const = struct {
2206 out_stream: anytype,2206 out_stream: anytype,
2207 ) !void {2207 ) !void {
2208 _ = options;2208 _ = options;
2209 comptime var radix = 10;2209 comptime var base = 10;
2210 comptime var case: std.fmt.Case = .lower;2210 comptime var case: std.fmt.Case = .lower;
22112211
2212 if (fmt.len == 0 or comptime mem.eql(u8, fmt, "d")) {2212 if (fmt.len == 0 or comptime mem.eql(u8, fmt, "d")) {
2213 radix = 10;2213 base = 10;
2214 case = .lower;2214 case = .lower;
2215 } else if (comptime mem.eql(u8, fmt, "b")) {2215 } else if (comptime mem.eql(u8, fmt, "b")) {
2216 radix = 2;2216 base = 2;
2217 case = .lower;2217 case = .lower;
2218 } else if (comptime mem.eql(u8, fmt, "x")) {2218 } else if (comptime mem.eql(u8, fmt, "x")) {
2219 radix = 16;2219 base = 16;
2220 case = .lower;2220 case = .lower;
2221 } else if (comptime mem.eql(u8, fmt, "X")) {2221 } else if (comptime mem.eql(u8, fmt, "X")) {
2222 radix = 16;2222 base = 16;
2223 case = .upper;2223 case = .upper;
2224 } else {2224 } else {
2225 std.fmt.invalidFmtError(fmt, self);2225 std.fmt.invalidFmtError(fmt, self);
...@@ -2237,8 +2237,8 @@ pub const Const = struct {...@@ -2237,8 +2237,8 @@ pub const Const = struct {
2237 .limbs = &([1]Limb{comptime math.maxInt(Limb)} ** available_len),2237 .limbs = &([1]Limb{comptime math.maxInt(Limb)} ** available_len),
2238 .positive = false,2238 .positive = false,
2239 };2239 };
2240 var buf: [biggest.sizeInBaseUpperBound(radix)]u8 = undefined;2240 var buf: [biggest.sizeInBaseUpperBound(base)]u8 = undefined;
2241 const len = self.toString(&buf, radix, case, &limbs);2241 const len = self.toString(&buf, base, case, &limbs);
2242 return out_stream.writeAll(buf[0..len]);2242 return out_stream.writeAll(buf[0..len]);
2243 }2243 }
22442244
lib/std/math/scalbn.zig+2-2
...@@ -3,11 +3,11 @@ const expect = std.testing.expect;...@@ -3,11 +3,11 @@ const expect = std.testing.expect;
33
4/// Returns a * FLT_RADIX ^ exp.4/// Returns a * FLT_RADIX ^ exp.
5///5///
6/// Zig only supports binary radix IEEE-754 floats. Hence FLT_RADIX=2, and this is an alias for ldexp.6/// Zig only supports binary base IEEE-754 floats. Hence FLT_RADIX=2, and this is an alias for ldexp.
7pub const scalbn = @import("ldexp.zig").ldexp;7pub const scalbn = @import("ldexp.zig").ldexp;
88
9test "math.scalbn" {9test "math.scalbn" {
10 // Verify we are using radix 2.10 // Verify we are using base 2.
11 try expect(scalbn(@as(f16, 1.5), 4) == 24.0);11 try expect(scalbn(@as(f16, 1.5), 4) == 24.0);
12 try expect(scalbn(@as(f32, 1.5), 4) == 24.0);12 try expect(scalbn(@as(f32, 1.5), 4) == 24.0);
13 try expect(scalbn(@as(f64, 1.5), 4) == 24.0);13 try expect(scalbn(@as(f64, 1.5), 4) == 24.0);
lib/std/zig/c_translation.zig+8-5
...@@ -262,16 +262,19 @@ test "sizeof" {...@@ -262,16 +262,19 @@ test "sizeof" {
262 try testing.expect(sizeof(anyopaque) == 1);262 try testing.expect(sizeof(anyopaque) == 1);
263}263}
264264
265pub const CIntLiteralRadix = enum { decimal, octal, hexadecimal };265pub const CIntLiteralBase = enum { decimal, octal, hexadecimal };
266266
267fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime number: comptime_int, comptime radix: CIntLiteralRadix) type {267/// Deprecated: use `CIntLiteralBase`
268pub const CIntLiteralRadix = CIntLiteralBase;
269
270fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime number: comptime_int, comptime base: CIntLiteralBase) type {
268 const signed_decimal = [_]type{ c_int, c_long, c_longlong, c_ulonglong };271 const signed_decimal = [_]type{ c_int, c_long, c_longlong, c_ulonglong };
269 const signed_oct_hex = [_]type{ c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong };272 const signed_oct_hex = [_]type{ c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong };
270 const unsigned = [_]type{ c_uint, c_ulong, c_ulonglong };273 const unsigned = [_]type{ c_uint, c_ulong, c_ulonglong };
271274
272 const list: []const type = if (@typeInfo(SuffixType).Int.signedness == .unsigned)275 const list: []const type = if (@typeInfo(SuffixType).Int.signedness == .unsigned)
273 &unsigned276 &unsigned
274 else if (radix == .decimal)277 else if (base == .decimal)
275 &signed_decimal278 &signed_decimal
276 else279 else
277 &signed_oct_hex;280 &signed_oct_hex;
...@@ -290,8 +293,8 @@ fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime number: compt...@@ -290,8 +293,8 @@ fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime number: compt
290pub fn promoteIntLiteral(293pub fn promoteIntLiteral(
291 comptime SuffixType: type,294 comptime SuffixType: type,
292 comptime number: comptime_int,295 comptime number: comptime_int,
293 comptime radix: CIntLiteralRadix,296 comptime base: CIntLiteralBase,
294) PromoteIntLiteralReturnType(SuffixType, number, radix) {297) PromoteIntLiteralReturnType(SuffixType, number, base) {
295 return number;298 return number;
296}299}
297300
src/main.zig+4-4
...@@ -5786,12 +5786,12 @@ pub fn cmdChangelist(...@@ -5786,12 +5786,12 @@ pub fn cmdChangelist(
5786 try bw.flush();5786 try bw.flush();
5787}5787}
57885788
5789fn eatIntPrefix(arg: []const u8, radix: u8) []const u8 {5789fn eatIntPrefix(arg: []const u8, base: u8) []const u8 {
5790 if (arg.len > 2 and arg[0] == '0') {5790 if (arg.len > 2 and arg[0] == '0') {
5791 switch (std.ascii.toLower(arg[1])) {5791 switch (std.ascii.toLower(arg[1])) {
5792 'b' => if (radix == 2) return arg[2..],5792 'b' => if (base == 2) return arg[2..],
5793 'o' => if (radix == 8) return arg[2..],5793 'o' => if (base == 8) return arg[2..],
5794 'x' => if (radix == 16) return arg[2..],5794 'x' => if (base == 16) return arg[2..],
5795 else => {},5795 else => {},
5796 }5796 }
5797 }5797 }
src/translate_c.zig+5-5
...@@ -5735,21 +5735,21 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {...@@ -5735,21 +5735,21 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {
57355735
5736 switch (m.list[m.i].id) {5736 switch (m.list[m.i].id) {
5737 .IntegerLiteral => |suffix| {5737 .IntegerLiteral => |suffix| {
5738 var radix: []const u8 = "decimal";5738 var base: []const u8 = "decimal";
5739 if (lit_bytes.len >= 2 and lit_bytes[0] == '0') {5739 if (lit_bytes.len >= 2 and lit_bytes[0] == '0') {
5740 switch (lit_bytes[1]) {5740 switch (lit_bytes[1]) {
5741 '0'...'7' => {5741 '0'...'7' => {
5742 // Octal5742 // Octal
5743 lit_bytes = try std.fmt.allocPrint(c.arena, "0o{s}", .{lit_bytes[1..]});5743 lit_bytes = try std.fmt.allocPrint(c.arena, "0o{s}", .{lit_bytes[1..]});
5744 radix = "octal";5744 base = "octal";
5745 },5745 },
5746 'X' => {5746 'X' => {
5747 // Hexadecimal with capital X, valid in C but not in Zig5747 // Hexadecimal with capital X, valid in C but not in Zig
5748 lit_bytes = try std.fmt.allocPrint(c.arena, "0x{s}", .{lit_bytes[2..]});5748 lit_bytes = try std.fmt.allocPrint(c.arena, "0x{s}", .{lit_bytes[2..]});
5749 radix = "hexadecimal";5749 base = "hexadecimal";
5750 },5750 },
5751 'x' => {5751 'x' => {
5752 radix = "hexadecimal";5752 base = "hexadecimal";
5753 },5753 },
5754 else => {},5754 else => {},
5755 }5755 }
...@@ -5794,7 +5794,7 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {...@@ -5794,7 +5794,7 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {
5794 return Tag.helpers_promoteIntLiteral.create(c.arena, .{5794 return Tag.helpers_promoteIntLiteral.create(c.arena, .{
5795 .type = type_node,5795 .type = type_node,
5796 .value = literal_node,5796 .value = literal_node,
5797 .radix = try Tag.enum_literal.create(c.arena, radix),5797 .base = try Tag.enum_literal.create(c.arena, base),
5798 });5798 });
5799 }5799 }
5800 },5800 },
src/translate_c/ast.zig+3-3
...@@ -120,7 +120,7 @@ pub const Node = extern union {...@@ -120,7 +120,7 @@ pub const Node = extern union {
120 std_math_Log2Int,120 std_math_Log2Int,
121 /// @intCast(lhs, rhs)121 /// @intCast(lhs, rhs)
122 int_cast,122 int_cast,
123 /// @import("std").zig.c_translation.promoteIntLiteral(value, type, radix)123 /// @import("std").zig.c_translation.promoteIntLiteral(value, type, base)
124 helpers_promoteIntLiteral,124 helpers_promoteIntLiteral,
125 /// @import("std").meta.alignment(value)125 /// @import("std").meta.alignment(value)
126 std_meta_alignment,126 std_meta_alignment,
...@@ -699,7 +699,7 @@ pub const Payload = struct {...@@ -699,7 +699,7 @@ pub const Payload = struct {
699 data: struct {699 data: struct {
700 value: Node,700 value: Node,
701 type: Node,701 type: Node,
702 radix: Node,702 base: Node,
703 },703 },
704 };704 };
705705
...@@ -898,7 +898,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -898,7 +898,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
898 .helpers_promoteIntLiteral => {898 .helpers_promoteIntLiteral => {
899 const payload = node.castTag(.helpers_promoteIntLiteral).?.data;899 const payload = node.castTag(.helpers_promoteIntLiteral).?.data;
900 const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "promoteIntLiteral" });900 const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "promoteIntLiteral" });
901 return renderCall(c, import_node, &.{ payload.type, payload.value, payload.radix });901 return renderCall(c, import_node, &.{ payload.type, payload.value, payload.base });
902 },902 },
903 .std_meta_alignment => {903 .std_meta_alignment => {
904 const payload = node.castTag(.std_meta_alignment).?.data;904 const payload = node.castTag(.std_meta_alignment).?.data;