zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 9393a83323bfdfa37cda703997f4e0faf1ac2732 (tree)
parent ddd8d5918890177e0d6e5477c1be3701d5973908
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Tue, 21 Nov 2023 21:22:48 -0700

aro: use std.fmt.bufPrint rather than BoundedArray

This simplifies the logic. For example, in generateExactWidthType, it no
longer has to save a previous length and then use defer to reset the
length after mutating it.

Diffstat:
Mdeps/aro/aro/Compilation.zig | 35++++++++++++++++++-----------------
1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/deps/aro/aro/Compilation.zig b/deps/aro/aro/Compilation.zig @@ -582,10 +582,9 @@ fn generateFloatMacros(w: anytype, prefix: []const u8, semantics: target_util.FP }, ); - var defPrefix = std.BoundedArray(u8, 32).init(0) catch unreachable; - defPrefix.writer().print("__{s}_", .{prefix}) catch return error.OutOfMemory; - - const prefix_slice = defPrefix.constSlice(); + var def_prefix_buf: [32]u8 = undefined; + const prefix_slice = std.fmt.bufPrint(&def_prefix_buf, "__{s}_", .{prefix}) catch + return error.OutOfMemory; try w.print("#define {s}DENORM_MIN__ {s}{s}\n", .{ prefix_slice, denormMin, ext }); try w.print("#define {s}HAS_DENORM__\n", .{prefix_slice}); @@ -770,18 +769,18 @@ fn generateExactWidthType(comp: *const Compilation, w: anytype, mapper: StrInt.T ty = if (unsigned) comp.types.int64.makeIntegerUnsigned() else comp.types.int64; } - var prefix = std.BoundedArray(u8, 16).init(0) catch unreachable; - prefix.writer().print("{s}{d}", .{ if (unsigned) "__UINT" else "__INT", width }) catch return error.OutOfMemory; + var buffer: [16]u8 = undefined; + const suffix = "_TYPE__"; + const full = std.fmt.bufPrint(&buffer, "{s}{d}{s}", .{ + if (unsigned) "__UINT" else "__INT", width, suffix, + }) catch return error.OutOfMemory; - { - const len = prefix.len; - defer prefix.resize(len) catch unreachable; // restoring previous size - prefix.appendSliceAssumeCapacity("_TYPE__"); - try generateTypeMacro(w, mapper, prefix.constSlice(), ty, comp.langopts); - } + try generateTypeMacro(w, mapper, full, ty, comp.langopts); + + const prefix = full[0 .. full.len - suffix.len]; // remove "_TYPE__" - try comp.generateFmt(prefix.constSlice(), w, ty); - try comp.generateSuffixMacro(prefix.constSlice(), w, ty); + try comp.generateFmt(prefix, w, ty); + try comp.generateSuffixMacro(prefix, w, ty); } pub fn hasFloat128(comp: *const Compilation) bool { @@ -908,10 +907,12 @@ fn generateExactWidthIntMax(comp: *const Compilation, w: anytype, specifier: Typ ty = if (unsigned) comp.types.int64.makeIntegerUnsigned() else comp.types.int64; } - var name = std.BoundedArray(u8, 6).init(0) catch unreachable; - name.writer().print("{s}{d}", .{ if (unsigned) "UINT" else "INT", bit_count }) catch return error.OutOfMemory; + var name_buffer: [6]u8 = undefined; + const name = std.fmt.bufPrint(&name_buffer, "{s}{d}", .{ + if (unsigned) "UINT" else "INT", bit_count, + }) catch return error.OutOfMemory; - return comp.generateIntMax(w, name.constSlice(), ty); + return comp.generateIntMax(w, name, ty); } fn generateIntWidth(comp: *Compilation, w: anytype, name: []const u8, ty: Type) !void {