From 94e30a756edc4c2182168dabd97d481b8aec0ff2 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 30 Apr 2023 18:02:08 +0100 Subject: std: fix a bunch of typos The majority of these are in comments, some in doc comments which might affect the generated documentation, and a few in parameter names - nothing that should be breaking, however. --- lib/std/fmt/parse_float/convert_fast.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/std/fmt/parse_float') diff --git a/lib/std/fmt/parse_float/convert_fast.zig b/lib/std/fmt/parse_float/convert_fast.zig index 75875156fd..74beb745de 100644 --- a/lib/std/fmt/parse_float/convert_fast.zig +++ b/lib/std/fmt/parse_float/convert_fast.zig @@ -1,4 +1,4 @@ -//! Representation of a float as the signficant digits and exponent. +//! Representation of a float as the significant digits and exponent. //! The fast path algorithm using machine-sized integers and floats. //! //! This only works if both the mantissa and the exponent can be exactly -- cgit v1.2.3 From b18b4db7095f58b1e8b3f5ac4993bda38231c2af Mon Sep 17 00:00:00 2001 From: Jayden Date: Mon, 8 May 2023 06:42:24 -0400 Subject: parse_float: Error when a float is attempted to be parsed into an invalid type Co-authored-by: Ryan Liptak --- lib/std/fmt/parse_float/parse_float.zig | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'lib/std/fmt/parse_float') diff --git a/lib/std/fmt/parse_float/parse_float.zig b/lib/std/fmt/parse_float/parse_float.zig index 6d77346e9b..08d1c55862 100644 --- a/lib/std/fmt/parse_float/parse_float.zig +++ b/lib/std/fmt/parse_float/parse_float.zig @@ -12,6 +12,14 @@ pub const ParseFloatError = error{ }; pub fn parseFloat(comptime T: type, s: []const u8) ParseFloatError!T { + if (@typeInfo(T) != .Float) { + @compileError("Cannot parse a float into a non-floating point type."); + } + + if (T == f80) { + @compileError("TODO support parsing float to f80"); + } + if (s.len == 0) { return error.InvalidCharacter; } -- cgit v1.2.3 From 6c2f3745564aefa669b336e249888bb7390b3a3f Mon Sep 17 00:00:00 2001 From: Evin Yulo Date: Sat, 20 May 2023 20:58:28 -0400 Subject: Use the word 'base' consistently instead of 'radix' --- lib/std/fmt.zig | 98 ++++++++++++++++++------------------- lib/std/fmt/parse_float/decimal.zig | 4 +- lib/std/math/big/int.zig | 16 +++--- lib/std/math/scalbn.zig | 4 +- lib/std/zig/c_translation.zig | 13 +++-- src/main.zig | 8 +-- src/translate_c.zig | 10 ++-- src/translate_c/ast.zig | 6 +-- 8 files changed, 81 insertions(+), 78 deletions(-) (limited to 'lib/std/fmt/parse_float') diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index be6ebf20ac..92e15f06ba 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -748,7 +748,7 @@ pub fn formatIntValue( options: FormatOptions, writer: anytype, ) !void { - comptime var radix = 10; + comptime var base = 10; comptime var case: Case = .lower; const int_value = if (@TypeOf(value) == comptime_int) blk: { @@ -757,7 +757,7 @@ pub fn formatIntValue( } else value; if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "d")) { - radix = 10; + base = 10; case = .lower; } else if (comptime std.mem.eql(u8, fmt, "c")) { if (@typeInfo(@TypeOf(int_value)).Int.bits <= 8) { @@ -772,22 +772,22 @@ pub fn formatIntValue( @compileError("cannot print integer that is larger than 21 bits as an UTF-8 sequence"); } } else if (comptime std.mem.eql(u8, fmt, "b")) { - radix = 2; + base = 2; case = .lower; } else if (comptime std.mem.eql(u8, fmt, "x")) { - radix = 16; + base = 16; case = .lower; } else if (comptime std.mem.eql(u8, fmt, "X")) { - radix = 16; + base = 16; case = .upper; } else if (comptime std.mem.eql(u8, fmt, "o")) { - radix = 8; + base = 8; case = .lower; } else { invalidFmtError(fmt, value); } - return formatInt(int_value, radix, case, options, writer); + return formatInt(int_value, base, case, options, writer); } fn formatFloatValue( @@ -906,7 +906,7 @@ pub fn fmtSliceEscapeUpper(bytes: []const u8) std.fmt.Formatter(formatSliceEscap return .{ .data = bytes }; } -fn formatSizeImpl(comptime radix: comptime_int) type { +fn formatSizeImpl(comptime base: comptime_int) type { return struct { fn formatSizeImpl( value: u64, @@ -926,13 +926,13 @@ fn formatSizeImpl(comptime radix: comptime_int) type { const mags_iec = " KMGTPEZY"; const log2 = math.log2(value); - const magnitude = switch (radix) { + const magnitude = switch (base) { 1000 => math.min(log2 / comptime math.log2(1000), mags_si.len - 1), 1024 => math.min(log2 / 10, mags_iec.len - 1), else => unreachable, }; - const new_value = lossyCast(f64, value) / math.pow(f64, lossyCast(f64, radix), lossyCast(f64, magnitude)); - const suffix = switch (radix) { + const new_value = lossyCast(f64, value) / math.pow(f64, lossyCast(f64, base), lossyCast(f64, magnitude)); + const suffix = switch (base) { 1000 => mags_si[magnitude], 1024 => mags_iec[magnitude], else => unreachable, @@ -944,7 +944,7 @@ fn formatSizeImpl(comptime radix: comptime_int) type { bufstream.writer().writeAll(if (suffix == ' ') "B" - else switch (radix) { + else switch (base) { 1000 => &[_]u8{ suffix, 'B' }, 1024 => &[_]u8{ suffix, 'i', 'B' }, else => unreachable, @@ -1730,21 +1730,21 @@ pub fn Formatter(comptime format_fn: anytype) type { } /// Parses the string `buf` as signed or unsigned representation in the -/// specified radix of an integral value of type `T`. +/// specified base of an integral value of type `T`. /// -/// When `radix` is zero the string prefix is examined to detect the true radix: -/// * A prefix of "0b" implies radix=2, -/// * A prefix of "0o" implies radix=8, -/// * A prefix of "0x" implies radix=16, -/// * Otherwise radix=10 is assumed. +/// When `base` is zero the string prefix is examined to detect the true base: +/// * A prefix of "0b" implies base=2, +/// * A prefix of "0o" implies base=8, +/// * A prefix of "0x" implies base=16, +/// * Otherwise base=10 is assumed. /// /// Ignores '_' character in `buf`. /// See also `parseUnsigned`. -pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) ParseIntError!T { +pub fn parseInt(comptime T: type, buf: []const u8, base: u8) ParseIntError!T { if (buf.len == 0) return error.InvalidCharacter; - if (buf[0] == '+') return parseWithSign(T, buf[1..], radix, .pos); - if (buf[0] == '-') return parseWithSign(T, buf[1..], radix, .neg); - return parseWithSign(T, buf, radix, .pos); + if (buf[0] == '+') return parseWithSign(T, buf[1..], base, .pos); + if (buf[0] == '-') return parseWithSign(T, buf[1..], base, .neg); + return parseWithSign(T, buf, base, .pos); } test "parseInt" { @@ -1777,7 +1777,7 @@ test "parseInt" { try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "-", 10)); try std.testing.expectError(error.InvalidCharacter, parseInt(i32, "-", 10)); - // autodectect the radix + // autodectect the base try std.testing.expect((try parseInt(i32, "111", 0)) == 111); try std.testing.expect((try parseInt(i32, "1_1_1", 0)) == 111); try std.testing.expect((try parseInt(i32, "1_1_1", 0)) == 111); @@ -1804,29 +1804,29 @@ test "parseInt" { fn parseWithSign( comptime T: type, buf: []const u8, - radix: u8, + base: u8, comptime sign: enum { pos, neg }, ) ParseIntError!T { if (buf.len == 0) return error.InvalidCharacter; - var buf_radix = radix; + var buf_base = base; var buf_start = buf; - if (radix == 0) { + if (base == 0) { // Treat is as a decimal number by default. - buf_radix = 10; - // Detect the radix by looking at buf prefix. + buf_base = 10; + // Detect the base by looking at buf prefix. if (buf.len > 2 and buf[0] == '0') { switch (std.ascii.toLower(buf[1])) { 'b' => { - buf_radix = 2; + buf_base = 2; buf_start = buf[2..]; }, 'o' => { - buf_radix = 8; + buf_base = 8; buf_start = buf[2..]; }, 'x' => { - buf_radix = 16; + buf_base = 16; buf_start = buf[2..]; }, else => {}, @@ -1845,28 +1845,28 @@ fn parseWithSign( for (buf_start) |c| { if (c == '_') continue; - const digit = try charToDigit(c, buf_radix); + const digit = try charToDigit(c, buf_base); - if (x != 0) x = try math.mul(T, x, math.cast(T, buf_radix) orelse return error.Overflow); + if (x != 0) x = try math.mul(T, x, math.cast(T, buf_base) orelse return error.Overflow); x = try add(T, x, math.cast(T, digit) orelse return error.Overflow); } return x; } -/// Parses the string `buf` as unsigned representation in the specified radix +/// Parses the string `buf` as unsigned representation in the specified base /// of an integral value of type `T`. /// -/// When `radix` is zero the string prefix is examined to detect the true radix: -/// * A prefix of "0b" implies radix=2, -/// * A prefix of "0o" implies radix=8, -/// * A prefix of "0x" implies radix=16, -/// * Otherwise radix=10 is assumed. +/// When `base` is zero the string prefix is examined to detect the true base: +/// * A prefix of "0b" implies base=2, +/// * A prefix of "0o" implies base=8, +/// * A prefix of "0x" implies base=16, +/// * Otherwise base=10 is assumed. /// /// Ignores '_' character in `buf`. /// See also `parseInt`. -pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) ParseIntError!T { - return parseWithSign(T, buf, radix, .pos); +pub fn parseUnsigned(comptime T: type, buf: []const u8, base: u8) ParseIntError!T { + return parseWithSign(T, buf, base, .pos); } test "parseUnsigned" { @@ -1889,7 +1889,7 @@ test "parseUnsigned" { try std.testing.expect((try parseUnsigned(u32, "NUMBER", 36)) == 1442151747); - // these numbers should fit even though the radix itself doesn't fit in the destination type + // these numbers should fit even though the base itself doesn't fit in the destination type try std.testing.expect((try parseUnsigned(u1, "0", 10)) == 0); try std.testing.expect((try parseUnsigned(u1, "1", 10)) == 1); try std.testing.expectError(error.Overflow, parseUnsigned(u1, "2", 10)); @@ -1906,14 +1906,14 @@ test "parseUnsigned" { } /// Parses a number like '2G', '2Gi', or '2GiB'. -pub fn parseIntSizeSuffix(buf: []const u8, radix: u8) ParseIntError!usize { +pub fn parseIntSizeSuffix(buf: []const u8, digit_base: u8) ParseIntError!usize { var without_B = buf; if (mem.endsWith(u8, buf, "B")) without_B.len -= 1; var without_i = without_B; - var base: usize = 1000; + var magnitude_base: usize = 1000; if (mem.endsWith(u8, without_B, "i")) { without_i.len -= 1; - base = 1024; + magnitude_base = 1024; } if (without_i.len == 0) return error.InvalidCharacter; 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 { } else if (without_i.len != without_B.len) { return error.InvalidCharacter; } - const multiplier = math.powi(usize, base, orders_of_magnitude) catch |err| switch (err) { + const multiplier = math.powi(usize, magnitude_base, orders_of_magnitude) catch |err| switch (err) { error.Underflow => unreachable, error.Overflow => return error.Overflow, }; - const number = try std.fmt.parseInt(usize, without_suffix, radix); + const number = try std.fmt.parseInt(usize, without_suffix, digit_base); return math.mul(usize, number, multiplier); } @@ -1962,7 +1962,7 @@ test { _ = &parseFloat; } -pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) { +pub fn charToDigit(c: u8, base: u8) (error{InvalidCharacter}!u8) { const value = switch (c) { '0'...'9' => c - '0', 'A'...'Z' => c - 'A' + 10, @@ -1970,7 +1970,7 @@ pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) { else => return error.InvalidCharacter, }; - if (value >= radix) return error.InvalidCharacter; + if (value >= base) return error.InvalidCharacter; return value; } diff --git a/lib/std/fmt/parse_float/decimal.zig b/lib/std/fmt/parse_float/decimal.zig index 9dbe7095ac..5bb5fa8d5e 100644 --- a/lib/std/fmt/parse_float/decimal.zig +++ b/lib/std/fmt/parse_float/decimal.zig @@ -34,13 +34,13 @@ pub fn Decimal(comptime T: type) type { /// For a double-precision IEEE-754 float, this required 767 digits, /// so we store the max digits + 1. /// - /// We can exactly represent a float in radix `b` from radix 2 if + /// We can exactly represent a float in base `b` from base 2 if /// `b` is divisible by 2. This function calculates the exact number of /// digits required to exactly represent that float. /// /// According to the "Handbook of Floating Point Arithmetic", /// for IEEE754, with emin being the min exponent, p2 being the - /// precision, and b being the radix, the number of digits follows as: + /// precision, and b being the base, the number of digits follows as: /// /// `−emin + p2 + ⌊(emin + 1) log(2, b) − log(1 − 2^(−p2), b)⌋` /// diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig index fdee6b6190..c4d3ccf077 100644 --- a/lib/std/math/big/int.zig +++ b/lib/std/math/big/int.zig @@ -1627,7 +1627,7 @@ pub const Mutable = struct { // while x >= y * b^(n - t): // x -= y * b^(n - t) // q[n - t] += 1 - // Note, this algorithm is performed only once if y[t] > radix/2 and y is even, which we + // Note, this algorithm is performed only once if y[t] > base/2 and y is even, which we // enforced in step 0. This means we can replace the while with an if. // Note, multiplication by b^(n - t) comes down to shifting to the right by n - t limbs. // 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 { out_stream: anytype, ) !void { _ = options; - comptime var radix = 10; + comptime var base = 10; comptime var case: std.fmt.Case = .lower; if (fmt.len == 0 or comptime mem.eql(u8, fmt, "d")) { - radix = 10; + base = 10; case = .lower; } else if (comptime mem.eql(u8, fmt, "b")) { - radix = 2; + base = 2; case = .lower; } else if (comptime mem.eql(u8, fmt, "x")) { - radix = 16; + base = 16; case = .lower; } else if (comptime mem.eql(u8, fmt, "X")) { - radix = 16; + base = 16; case = .upper; } else { std.fmt.invalidFmtError(fmt, self); @@ -2237,8 +2237,8 @@ pub const Const = struct { .limbs = &([1]Limb{comptime math.maxInt(Limb)} ** available_len), .positive = false, }; - var buf: [biggest.sizeInBaseUpperBound(radix)]u8 = undefined; - const len = self.toString(&buf, radix, case, &limbs); + var buf: [biggest.sizeInBaseUpperBound(base)]u8 = undefined; + const len = self.toString(&buf, base, case, &limbs); return out_stream.writeAll(buf[0..len]); } diff --git a/lib/std/math/scalbn.zig b/lib/std/math/scalbn.zig index 294ee4abf0..2c8c3733fa 100644 --- a/lib/std/math/scalbn.zig +++ b/lib/std/math/scalbn.zig @@ -3,11 +3,11 @@ const expect = std.testing.expect; /// Returns a * FLT_RADIX ^ exp. /// -/// Zig only supports binary radix IEEE-754 floats. Hence FLT_RADIX=2, and this is an alias for ldexp. +/// Zig only supports binary base IEEE-754 floats. Hence FLT_RADIX=2, and this is an alias for ldexp. pub const scalbn = @import("ldexp.zig").ldexp; test "math.scalbn" { - // Verify we are using radix 2. + // Verify we are using base 2. try expect(scalbn(@as(f16, 1.5), 4) == 24.0); try expect(scalbn(@as(f32, 1.5), 4) == 24.0); try expect(scalbn(@as(f64, 1.5), 4) == 24.0); diff --git a/lib/std/zig/c_translation.zig b/lib/std/zig/c_translation.zig index 6e95ab53ab..1273527358 100644 --- a/lib/std/zig/c_translation.zig +++ b/lib/std/zig/c_translation.zig @@ -262,16 +262,19 @@ test "sizeof" { try testing.expect(sizeof(anyopaque) == 1); } -pub const CIntLiteralRadix = enum { decimal, octal, hexadecimal }; +pub const CIntLiteralBase = enum { decimal, octal, hexadecimal }; -fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime number: comptime_int, comptime radix: CIntLiteralRadix) type { +/// Deprecated: use `CIntLiteralBase` +pub const CIntLiteralRadix = CIntLiteralBase; + +fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime number: comptime_int, comptime base: CIntLiteralBase) type { const signed_decimal = [_]type{ c_int, c_long, c_longlong, c_ulonglong }; const signed_oct_hex = [_]type{ c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong }; const unsigned = [_]type{ c_uint, c_ulong, c_ulonglong }; const list: []const type = if (@typeInfo(SuffixType).Int.signedness == .unsigned) &unsigned - else if (radix == .decimal) + else if (base == .decimal) &signed_decimal else &signed_oct_hex; @@ -290,8 +293,8 @@ fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime number: compt pub fn promoteIntLiteral( comptime SuffixType: type, comptime number: comptime_int, - comptime radix: CIntLiteralRadix, -) PromoteIntLiteralReturnType(SuffixType, number, radix) { + comptime base: CIntLiteralBase, +) PromoteIntLiteralReturnType(SuffixType, number, base) { return number; } diff --git a/src/main.zig b/src/main.zig index b4778c1b00..bc40dbc431 100644 --- a/src/main.zig +++ b/src/main.zig @@ -5786,12 +5786,12 @@ pub fn cmdChangelist( try bw.flush(); } -fn eatIntPrefix(arg: []const u8, radix: u8) []const u8 { +fn eatIntPrefix(arg: []const u8, base: u8) []const u8 { if (arg.len > 2 and arg[0] == '0') { switch (std.ascii.toLower(arg[1])) { - 'b' => if (radix == 2) return arg[2..], - 'o' => if (radix == 8) return arg[2..], - 'x' => if (radix == 16) return arg[2..], + 'b' => if (base == 2) return arg[2..], + 'o' => if (base == 8) return arg[2..], + 'x' => if (base == 16) return arg[2..], else => {}, } } diff --git a/src/translate_c.zig b/src/translate_c.zig index becb2779b2..8cc2d1856c 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -5735,21 +5735,21 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node { switch (m.list[m.i].id) { .IntegerLiteral => |suffix| { - var radix: []const u8 = "decimal"; + var base: []const u8 = "decimal"; if (lit_bytes.len >= 2 and lit_bytes[0] == '0') { switch (lit_bytes[1]) { '0'...'7' => { // Octal lit_bytes = try std.fmt.allocPrint(c.arena, "0o{s}", .{lit_bytes[1..]}); - radix = "octal"; + base = "octal"; }, 'X' => { // Hexadecimal with capital X, valid in C but not in Zig lit_bytes = try std.fmt.allocPrint(c.arena, "0x{s}", .{lit_bytes[2..]}); - radix = "hexadecimal"; + base = "hexadecimal"; }, 'x' => { - radix = "hexadecimal"; + base = "hexadecimal"; }, else => {}, } @@ -5794,7 +5794,7 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node { return Tag.helpers_promoteIntLiteral.create(c.arena, .{ .type = type_node, .value = literal_node, - .radix = try Tag.enum_literal.create(c.arena, radix), + .base = try Tag.enum_literal.create(c.arena, base), }); } }, diff --git a/src/translate_c/ast.zig b/src/translate_c/ast.zig index 328feb989a..6c6bbf28bd 100644 --- a/src/translate_c/ast.zig +++ b/src/translate_c/ast.zig @@ -120,7 +120,7 @@ pub const Node = extern union { std_math_Log2Int, /// @intCast(lhs, rhs) int_cast, - /// @import("std").zig.c_translation.promoteIntLiteral(value, type, radix) + /// @import("std").zig.c_translation.promoteIntLiteral(value, type, base) helpers_promoteIntLiteral, /// @import("std").meta.alignment(value) std_meta_alignment, @@ -699,7 +699,7 @@ pub const Payload = struct { data: struct { value: Node, type: Node, - radix: Node, + base: Node, }, }; @@ -898,7 +898,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex { .helpers_promoteIntLiteral => { const payload = node.castTag(.helpers_promoteIntLiteral).?.data; const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "promoteIntLiteral" }); - return renderCall(c, import_node, &.{ payload.type, payload.value, payload.radix }); + return renderCall(c, import_node, &.{ payload.type, payload.value, payload.base }); }, .std_meta_alignment => { const payload = node.castTag(.std_meta_alignment).?.data; -- cgit v1.2.3