authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-21 21:22:48-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-22 11:33:34-07:00
log9393a83323bfdfa37cda703997f4e0faf1ac2732
treef513b02b03f1632119cca73963ea2307366c69b0
parentddd8d5918890177e0d6e5477c1be3701d5973908

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.

1 files changed, 18 insertions(+), 17 deletions(-)

deps/aro/aro/Compilation.zig+18-17
......@@ -582,10 +582,9 @@ fn generateFloatMacros(w: anytype, prefix: []const u8, semantics: target_util.FP
582582 },
583583 );
584584
585 var defPrefix = std.BoundedArray(u8, 32).init(0) catch unreachable;
586 defPrefix.writer().print("__{s}_", .{prefix}) catch return error.OutOfMemory;
587
588 const prefix_slice = defPrefix.constSlice();
585 var def_prefix_buf: [32]u8 = undefined;
586 const prefix_slice = std.fmt.bufPrint(&def_prefix_buf, "__{s}_", .{prefix}) catch
587 return error.OutOfMemory;
589588
590589 try w.print("#define {s}DENORM_MIN__ {s}{s}\n", .{ prefix_slice, denormMin, ext });
591590 try w.print("#define {s}HAS_DENORM__\n", .{prefix_slice});
......@@ -770,18 +769,18 @@ fn generateExactWidthType(comp: *const Compilation, w: anytype, mapper: StrInt.T
770769 ty = if (unsigned) comp.types.int64.makeIntegerUnsigned() else comp.types.int64;
771770 }
772771
773 var prefix = std.BoundedArray(u8, 16).init(0) catch unreachable;
774 prefix.writer().print("{s}{d}", .{ if (unsigned) "__UINT" else "__INT", width }) catch return error.OutOfMemory;
772 var buffer: [16]u8 = undefined;
773 const suffix = "_TYPE__";
774 const full = std.fmt.bufPrint(&buffer, "{s}{d}{s}", .{
775 if (unsigned) "__UINT" else "__INT", width, suffix,
776 }) catch return error.OutOfMemory;
775777
776 {
777 const len = prefix.len;
778 defer prefix.resize(len) catch unreachable; // restoring previous size
779 prefix.appendSliceAssumeCapacity("_TYPE__");
780 try generateTypeMacro(w, mapper, prefix.constSlice(), ty, comp.langopts);
781 }
778 try generateTypeMacro(w, mapper, full, ty, comp.langopts);
779
780 const prefix = full[0 .. full.len - suffix.len]; // remove "_TYPE__"
782781
783 try comp.generateFmt(prefix.constSlice(), w, ty);
784 try comp.generateSuffixMacro(prefix.constSlice(), w, ty);
782 try comp.generateFmt(prefix, w, ty);
783 try comp.generateSuffixMacro(prefix, w, ty);
785784}
786785
787786pub fn hasFloat128(comp: *const Compilation) bool {
......@@ -908,10 +907,12 @@ fn generateExactWidthIntMax(comp: *const Compilation, w: anytype, specifier: Typ
908907 ty = if (unsigned) comp.types.int64.makeIntegerUnsigned() else comp.types.int64;
909908 }
910909
911 var name = std.BoundedArray(u8, 6).init(0) catch unreachable;
912 name.writer().print("{s}{d}", .{ if (unsigned) "UINT" else "INT", bit_count }) catch return error.OutOfMemory;
910 var name_buffer: [6]u8 = undefined;
911 const name = std.fmt.bufPrint(&name_buffer, "{s}{d}", .{
912 if (unsigned) "UINT" else "INT", bit_count,
913 }) catch return error.OutOfMemory;
913914
914 return comp.generateIntMax(w, name.constSlice(), ty);
915 return comp.generateIntMax(w, name, ty);
915916}
916917
917918fn generateIntWidth(comp: *Compilation, w: anytype, name: []const u8, ty: Type) !void {