Compare commits

...

3 Commits

Author SHA1 Message Date
fkobi
9082b004b6 README: use HTTPS for releases.llvm.org 2025-11-23 20:54:20 +01:00
meowjesty
755a3d957c
langref: convert to unmanaged ArrayList in example 2025-11-23 18:32:05 +00:00
Justus Klausecker
e5c2df9f17 std.math.big.int: fix format functions 2025-11-23 09:52:43 -08:00
3 changed files with 40 additions and 5 deletions

View File

@ -238,7 +238,7 @@ Note: In case you get the error "llvm-config not found" (or similar), make sure
Install [CMake](https://cmake.org/), version 3.20.0 or newer.
[Download LLVM, Clang, and LLD sources](http://releases.llvm.org/download.html#21.0.0)
[Download LLVM, Clang, and LLD sources](https://releases.llvm.org/download.html#21.0.0)
The downloads from llvm lead to the github release pages, where the source's
will be listed as : `llvm-21.X.X.src.tar.xz`, `clang-21.X.X.src.tar.xz`,
`lld-21.X.X.src.tar.xz`. Unzip each to their own directory. Ensure no

View File

@ -1,9 +1,10 @@
const std = @import("std");
test "detect leak" {
var list = std.array_list.Managed(u21).init(std.testing.allocator);
// missing `defer list.deinit();`
try list.append('☔');
const gpa = std.testing.allocator;
var list: std.ArrayList(u21) = .empty;
// missing `defer list.deinit(gpa);`
try list.append(gpa, '☔');
try std.testing.expect(list.items.len == 1);
}

View File

@ -2032,7 +2032,11 @@ pub const Mutable = struct {
return formatNumber(self, w, .{});
}
pub fn formatNumber(self: Const, w: *std.Io.Writer, n: std.fmt.Number) std.Io.Writer.Error!void {
/// If the absolute value of integer is greater than or equal to `pow(2, 64 * @sizeOf(usize) * 8)`,
/// this function will fail to print the string, printing "(BigInt)" instead of a number.
/// This is because the rendering algorithm requires reversing a string, which requires O(N) memory.
/// See `Const.toString` and `Const.toStringAlloc` for a way to print big integers without failure.
pub fn formatNumber(self: Mutable, w: *std.Io.Writer, n: std.fmt.Number) std.Io.Writer.Error!void {
return self.toConst().formatNumber(w, n);
}
};
@ -2321,6 +2325,10 @@ pub const Const = struct {
return .{ normalized_res.reconstruct(if (self.positive) .positive else .negative), exactness };
}
pub fn format(self: Const, w: *std.Io.Writer) std.Io.Writer.Error!void {
return self.formatNumber(w, .{});
}
/// If the absolute value of integer is greater than or equal to `pow(2, 64 * @sizeOf(usize) * 8)`,
/// this function will fail to print the string, printing "(BigInt)" instead of a number.
/// This is because the rendering algorithm requires reversing a string, which requires O(N) memory.
@ -4625,3 +4633,29 @@ fn testOneShiftCaseAliasing(func: fn ([]Limb, []const Limb, usize) usize, case:
try std.testing.expectEqualSlices(Limb, expected, r[base .. base + len]);
}
}
test "format" {
var a: Managed = try .init(std.testing.allocator);
defer a.deinit();
try a.set(123);
try testFormat(a, "123");
try a.set(-123);
try testFormat(a, "-123");
try a.set(20000000000000000000); // > maxInt(u64)
try testFormat(a, "20000000000000000000");
try a.set(1 << 64 * @sizeOf(usize) * 8);
try testFormat(a, "(BigInt)");
try a.set(-(1 << 64 * @sizeOf(usize) * 8));
try testFormat(a, "(BigInt)");
}
fn testFormat(a: Managed, expected: []const u8) !void {
try std.testing.expectFmt(expected, "{f}", .{a});
try std.testing.expectFmt(expected, "{f}", .{a.toMutable()});
try std.testing.expectFmt(expected, "{f}", .{a.toConst()});
}