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(
748748 options: FormatOptions,
749749 writer: anytype,
750750) !void {
751 comptime var radix = 10;
751 comptime var base = 10;
752752 comptime var case: Case = .lower;
753753
754754 const int_value = if (@TypeOf(value) == comptime_int) blk: {
......@@ -757,7 +757,7 @@ pub fn formatIntValue(
757757 } else value;
758758
759759 if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "d")) {
760 radix = 10;
760 base = 10;
761761 case = .lower;
762762 } else if (comptime std.mem.eql(u8, fmt, "c")) {
763763 if (@typeInfo(@TypeOf(int_value)).Int.bits <= 8) {
......@@ -772,22 +772,22 @@ pub fn formatIntValue(
772772 @compileError("cannot print integer that is larger than 21 bits as an UTF-8 sequence");
773773 }
774774 } else if (comptime std.mem.eql(u8, fmt, "b")) {
775 radix = 2;
775 base = 2;
776776 case = .lower;
777777 } else if (comptime std.mem.eql(u8, fmt, "x")) {
778 radix = 16;
778 base = 16;
779779 case = .lower;
780780 } else if (comptime std.mem.eql(u8, fmt, "X")) {
781 radix = 16;
781 base = 16;
782782 case = .upper;
783783 } else if (comptime std.mem.eql(u8, fmt, "o")) {
784 radix = 8;
784 base = 8;
785785 case = .lower;
786786 } else {
787787 invalidFmtError(fmt, value);
788788 }
789789
790 return formatInt(int_value, radix, case, options, writer);
790 return formatInt(int_value, base, case, options, writer);
791791}
792792
793793fn formatFloatValue(
......@@ -906,7 +906,7 @@ pub fn fmtSliceEscapeUpper(bytes: []const u8) std.fmt.Formatter(formatSliceEscap
906906 return .{ .data = bytes };
907907}
908908
909fn formatSizeImpl(comptime radix: comptime_int) type {
909fn formatSizeImpl(comptime base: comptime_int) type {
910910 return struct {
911911 fn formatSizeImpl(
912912 value: u64,
......@@ -926,13 +926,13 @@ fn formatSizeImpl(comptime radix: comptime_int) type {
926926 const mags_iec = " KMGTPEZY";
927927
928928 const log2 = math.log2(value);
929 const magnitude = switch (radix) {
929 const magnitude = switch (base) {
930930 1000 => math.min(log2 / comptime math.log2(1000), mags_si.len - 1),
931931 1024 => math.min(log2 / 10, mags_iec.len - 1),
932932 else => unreachable,
933933 };
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) {
936936 1000 => mags_si[magnitude],
937937 1024 => mags_iec[magnitude],
938938 else => unreachable,
......@@ -944,7 +944,7 @@ fn formatSizeImpl(comptime radix: comptime_int) type {
944944
945945 bufstream.writer().writeAll(if (suffix == ' ')
946946 "B"
947 else switch (radix) {
947 else switch (base) {
948948 1000 => &[_]u8{ suffix, 'B' },
949949 1024 => &[_]u8{ suffix, 'i', 'B' },
950950 else => unreachable,
......@@ -1730,21 +1730,21 @@ pub fn Formatter(comptime format_fn: anytype) type {
17301730}
17311731
17321732/// 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`.
17341734///
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.
17401740///
17411741/// Ignores '_' character in `buf`.
17421742/// 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 {
17441744 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);
17481748}
17491749
17501750test "parseInt" {
......@@ -1777,7 +1777,7 @@ test "parseInt" {
17771777 try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "-", 10));
17781778 try std.testing.expectError(error.InvalidCharacter, parseInt(i32, "-", 10));
17791779
1780 // autodectect the radix
1780 // autodectect the base
17811781 try std.testing.expect((try parseInt(i32, "111", 0)) == 111);
17821782 try std.testing.expect((try parseInt(i32, "1_1_1", 0)) == 111);
17831783 try std.testing.expect((try parseInt(i32, "1_1_1", 0)) == 111);
......@@ -1804,29 +1804,29 @@ test "parseInt" {
18041804fn parseWithSign(
18051805 comptime T: type,
18061806 buf: []const u8,
1807 radix: u8,
1807 base: u8,
18081808 comptime sign: enum { pos, neg },
18091809) ParseIntError!T {
18101810 if (buf.len == 0) return error.InvalidCharacter;
18111811
1812 var buf_radix = radix;
1812 var buf_base = base;
18131813 var buf_start = buf;
1814 if (radix == 0) {
1814 if (base == 0) {
18151815 // 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.
18181818 if (buf.len > 2 and buf[0] == '0') {
18191819 switch (std.ascii.toLower(buf[1])) {
18201820 'b' => {
1821 buf_radix = 2;
1821 buf_base = 2;
18221822 buf_start = buf[2..];
18231823 },
18241824 'o' => {
1825 buf_radix = 8;
1825 buf_base = 8;
18261826 buf_start = buf[2..];
18271827 },
18281828 'x' => {
1829 buf_radix = 16;
1829 buf_base = 16;
18301830 buf_start = buf[2..];
18311831 },
18321832 else => {},
......@@ -1845,28 +1845,28 @@ fn parseWithSign(
18451845
18461846 for (buf_start) |c| {
18471847 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);
18511851 x = try add(T, x, math.cast(T, digit) orelse return error.Overflow);
18521852 }
18531853
18541854 return x;
18551855}
18561856
1857/// Parses the string `buf` as unsigned representation in the specified radix
1857/// Parses the string `buf` as unsigned representation in the specified base
18581858/// of an integral value of type `T`.
18591859///
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.
18651865///
18661866/// Ignores '_' character in `buf`.
18671867/// See also `parseInt`.
1868pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) ParseIntError!T {
1869 return parseWithSign(T, buf, radix, .pos);
1868pub fn parseUnsigned(comptime T: type, buf: []const u8, base: u8) ParseIntError!T {
1869 return parseWithSign(T, buf, base, .pos);
18701870}
18711871
18721872test "parseUnsigned" {
......@@ -1889,7 +1889,7 @@ test "parseUnsigned" {
18891889
18901890 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 type
1892 // these numbers should fit even though the base itself doesn't fit in the destination type
18931893 try std.testing.expect((try parseUnsigned(u1, "0", 10)) == 0);
18941894 try std.testing.expect((try parseUnsigned(u1, "1", 10)) == 1);
18951895 try std.testing.expectError(error.Overflow, parseUnsigned(u1, "2", 10));
......@@ -1906,14 +1906,14 @@ test "parseUnsigned" {
19061906}
19071907
19081908/// 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 {
19101910 var without_B = buf;
19111911 if (mem.endsWith(u8, buf, "B")) without_B.len -= 1;
19121912 var without_i = without_B;
1913 var base: usize = 1000;
1913 var magnitude_base: usize = 1000;
19141914 if (mem.endsWith(u8, without_B, "i")) {
19151915 without_i.len -= 1;
1916 base = 1024;
1916 magnitude_base = 1024;
19171917 }
19181918 if (without_i.len == 0) return error.InvalidCharacter;
19191919 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 {
19351935 } else if (without_i.len != without_B.len) {
19361936 return error.InvalidCharacter;
19371937 }
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) {
19391939 error.Underflow => unreachable,
19401940 error.Overflow => return error.Overflow,
19411941 };
1942 const number = try std.fmt.parseInt(usize, without_suffix, radix);
1942 const number = try std.fmt.parseInt(usize, without_suffix, digit_base);
19431943 return math.mul(usize, number, multiplier);
19441944}
19451945
......@@ -1962,7 +1962,7 @@ test {
19621962 _ = &parseFloat;
19631963}
19641964
1965pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) {
1965pub fn charToDigit(c: u8, base: u8) (error{InvalidCharacter}!u8) {
19661966 const value = switch (c) {
19671967 '0'...'9' => c - '0',
19681968 'A'...'Z' => c - 'A' + 10,
......@@ -1970,7 +1970,7 @@ pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) {
19701970 else => return error.InvalidCharacter,
19711971 };
19721972
1973 if (value >= radix) return error.InvalidCharacter;
1973 if (value >= base) return error.InvalidCharacter;
19741974
19751975 return value;
19761976}
lib/std/fmt/parse_float/decimal.zig+2-2
......@@ -34,13 +34,13 @@ pub fn Decimal(comptime T: type) type {
3434 /// For a double-precision IEEE-754 float, this required 767 digits,
3535 /// so we store the max digits + 1.
3636 ///
37 /// We can exactly represent a float in radix `b` from radix 2 if
37 /// We can exactly represent a float in base `b` from base 2 if
3838 /// `b` is divisible by 2. This function calculates the exact number of
3939 /// digits required to exactly represent that float.
4040 ///
4141 /// According to the "Handbook of Floating Point Arithmetic",
4242 /// 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:
4444 ///
4545 /// `−emin + p2 + ⌊(emin + 1) log(2, b) − log(1 − 2^(−p2), b)⌋`
4646 ///
lib/std/math/big/int.zig+8-8
......@@ -1627,7 +1627,7 @@ pub const Mutable = struct {
16271627 // while x >= y * b^(n - t):
16281628 // x -= y * b^(n - t)
16291629 // q[n - t] += 1
1630 // Note, this algorithm is performed only once if y[t] > radix/2 and y is even, which we
1630 // Note, this algorithm is performed only once if y[t] > base/2 and y is even, which we
16311631 // enforced in step 0. This means we can replace the while with an if.
16321632 // Note, multiplication by b^(n - t) comes down to shifting to the right by n - t limbs.
16331633 // 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 {
22062206 out_stream: anytype,
22072207 ) !void {
22082208 _ = options;
2209 comptime var radix = 10;
2209 comptime var base = 10;
22102210 comptime var case: std.fmt.Case = .lower;
22112211
22122212 if (fmt.len == 0 or comptime mem.eql(u8, fmt, "d")) {
2213 radix = 10;
2213 base = 10;
22142214 case = .lower;
22152215 } else if (comptime mem.eql(u8, fmt, "b")) {
2216 radix = 2;
2216 base = 2;
22172217 case = .lower;
22182218 } else if (comptime mem.eql(u8, fmt, "x")) {
2219 radix = 16;
2219 base = 16;
22202220 case = .lower;
22212221 } else if (comptime mem.eql(u8, fmt, "X")) {
2222 radix = 16;
2222 base = 16;
22232223 case = .upper;
22242224 } else {
22252225 std.fmt.invalidFmtError(fmt, self);
......@@ -2237,8 +2237,8 @@ pub const Const = struct {
22372237 .limbs = &([1]Limb{comptime math.maxInt(Limb)} ** available_len),
22382238 .positive = false,
22392239 };
2240 var buf: [biggest.sizeInBaseUpperBound(radix)]u8 = undefined;
2241 const len = self.toString(&buf, radix, case, &limbs);
2240 var buf: [biggest.sizeInBaseUpperBound(base)]u8 = undefined;
2241 const len = self.toString(&buf, base, case, &limbs);
22422242 return out_stream.writeAll(buf[0..len]);
22432243 }
22442244
lib/std/math/scalbn.zig+2-2
......@@ -3,11 +3,11 @@ const expect = std.testing.expect;
33
44/// Returns a * FLT_RADIX ^ exp.
55///
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.
77pub const scalbn = @import("ldexp.zig").ldexp;
88
99test "math.scalbn" {
10 // Verify we are using radix 2.
10 // Verify we are using base 2.
1111 try expect(scalbn(@as(f16, 1.5), 4) == 24.0);
1212 try expect(scalbn(@as(f32, 1.5), 4) == 24.0);
1313 try expect(scalbn(@as(f64, 1.5), 4) == 24.0);
lib/std/zig/c_translation.zig+8-5
......@@ -262,16 +262,19 @@ test "sizeof" {
262262 try testing.expect(sizeof(anyopaque) == 1);
263263}
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 {
268271 const signed_decimal = [_]type{ c_int, c_long, c_longlong, c_ulonglong };
269272 const signed_oct_hex = [_]type{ c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong };
270273 const unsigned = [_]type{ c_uint, c_ulong, c_ulonglong };
271274
272275 const list: []const type = if (@typeInfo(SuffixType).Int.signedness == .unsigned)
273276 &unsigned
274 else if (radix == .decimal)
277 else if (base == .decimal)
275278 &signed_decimal
276279 else
277280 &signed_oct_hex;
......@@ -290,8 +293,8 @@ fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime number: compt
290293pub fn promoteIntLiteral(
291294 comptime SuffixType: type,
292295 comptime number: comptime_int,
293 comptime radix: CIntLiteralRadix,
294) PromoteIntLiteralReturnType(SuffixType, number, radix) {
296 comptime base: CIntLiteralBase,
297) PromoteIntLiteralReturnType(SuffixType, number, base) {
295298 return number;
296299}
297300
src/main.zig+4-4
......@@ -5786,12 +5786,12 @@ pub fn cmdChangelist(
57865786 try bw.flush();
57875787}
57885788
5789fn eatIntPrefix(arg: []const u8, radix: u8) []const u8 {
5789fn eatIntPrefix(arg: []const u8, base: u8) []const u8 {
57905790 if (arg.len > 2 and arg[0] == '0') {
57915791 switch (std.ascii.toLower(arg[1])) {
5792 'b' => if (radix == 2) return arg[2..],
5793 'o' => if (radix == 8) return arg[2..],
5794 'x' => if (radix == 16) return arg[2..],
5792 'b' => if (base == 2) return arg[2..],
5793 'o' => if (base == 8) return arg[2..],
5794 'x' => if (base == 16) return arg[2..],
57955795 else => {},
57965796 }
57975797 }
src/translate_c.zig+5-5
......@@ -5735,21 +5735,21 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {
57355735
57365736 switch (m.list[m.i].id) {
57375737 .IntegerLiteral => |suffix| {
5738 var radix: []const u8 = "decimal";
5738 var base: []const u8 = "decimal";
57395739 if (lit_bytes.len >= 2 and lit_bytes[0] == '0') {
57405740 switch (lit_bytes[1]) {
57415741 '0'...'7' => {
57425742 // Octal
57435743 lit_bytes = try std.fmt.allocPrint(c.arena, "0o{s}", .{lit_bytes[1..]});
5744 radix = "octal";
5744 base = "octal";
57455745 },
57465746 'X' => {
57475747 // Hexadecimal with capital X, valid in C but not in Zig
57485748 lit_bytes = try std.fmt.allocPrint(c.arena, "0x{s}", .{lit_bytes[2..]});
5749 radix = "hexadecimal";
5749 base = "hexadecimal";
57505750 },
57515751 'x' => {
5752 radix = "hexadecimal";
5752 base = "hexadecimal";
57535753 },
57545754 else => {},
57555755 }
......@@ -5794,7 +5794,7 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {
57945794 return Tag.helpers_promoteIntLiteral.create(c.arena, .{
57955795 .type = type_node,
57965796 .value = literal_node,
5797 .radix = try Tag.enum_literal.create(c.arena, radix),
5797 .base = try Tag.enum_literal.create(c.arena, base),
57985798 });
57995799 }
58005800 },
src/translate_c/ast.zig+3-3
......@@ -120,7 +120,7 @@ pub const Node = extern union {
120120 std_math_Log2Int,
121121 /// @intCast(lhs, rhs)
122122 int_cast,
123 /// @import("std").zig.c_translation.promoteIntLiteral(value, type, radix)
123 /// @import("std").zig.c_translation.promoteIntLiteral(value, type, base)
124124 helpers_promoteIntLiteral,
125125 /// @import("std").meta.alignment(value)
126126 std_meta_alignment,
......@@ -699,7 +699,7 @@ pub const Payload = struct {
699699 data: struct {
700700 value: Node,
701701 type: Node,
702 radix: Node,
702 base: Node,
703703 },
704704 };
705705
......@@ -898,7 +898,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
898898 .helpers_promoteIntLiteral => {
899899 const payload = node.castTag(.helpers_promoteIntLiteral).?.data;
900900 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 });
902902 },
903903 .std_meta_alignment => {
904904 const payload = node.castTag(.std_meta_alignment).?.data;