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...@@ -582,10 +582,9 @@ fn generateFloatMacros(w: anytype, prefix: []const u8, semantics: target_util.FP
582 },582 },
583 );583 );
584584
585 var defPrefix = std.BoundedArray(u8, 32).init(0) catch unreachable;585 var def_prefix_buf: [32]u8 = undefined;
586 defPrefix.writer().print("__{s}_", .{prefix}) catch return error.OutOfMemory;586 const prefix_slice = std.fmt.bufPrint(&def_prefix_buf, "__{s}_", .{prefix}) catch
587587 return error.OutOfMemory;
588 const prefix_slice = defPrefix.constSlice();
589588
590 try w.print("#define {s}DENORM_MIN__ {s}{s}\n", .{ prefix_slice, denormMin, ext });589 try w.print("#define {s}DENORM_MIN__ {s}{s}\n", .{ prefix_slice, denormMin, ext });
591 try w.print("#define {s}HAS_DENORM__\n", .{prefix_slice});590 try w.print("#define {s}HAS_DENORM__\n", .{prefix_slice});
...@@ -770,18 +769,18 @@ fn generateExactWidthType(comp: *const Compilation, w: anytype, mapper: StrInt.T...@@ -770,18 +769,18 @@ fn generateExactWidthType(comp: *const Compilation, w: anytype, mapper: StrInt.T
770 ty = if (unsigned) comp.types.int64.makeIntegerUnsigned() else comp.types.int64;769 ty = if (unsigned) comp.types.int64.makeIntegerUnsigned() else comp.types.int64;
771 }770 }
772771
773 var prefix = std.BoundedArray(u8, 16).init(0) catch unreachable;772 var buffer: [16]u8 = undefined;
774 prefix.writer().print("{s}{d}", .{ if (unsigned) "__UINT" else "__INT", width }) catch return error.OutOfMemory;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 {778 try generateTypeMacro(w, mapper, full, ty, comp.langopts);
777 const len = prefix.len;779
778 defer prefix.resize(len) catch unreachable; // restoring previous size780 const prefix = full[0 .. full.len - suffix.len]; // remove "_TYPE__"
779 prefix.appendSliceAssumeCapacity("_TYPE__");
780 try generateTypeMacro(w, mapper, prefix.constSlice(), ty, comp.langopts);
781 }
782781
783 try comp.generateFmt(prefix.constSlice(), w, ty);782 try comp.generateFmt(prefix, w, ty);
784 try comp.generateSuffixMacro(prefix.constSlice(), w, ty);783 try comp.generateSuffixMacro(prefix, w, ty);
785}784}
786785
787pub fn hasFloat128(comp: *const Compilation) bool {786pub fn hasFloat128(comp: *const Compilation) bool {
...@@ -908,10 +907,12 @@ fn generateExactWidthIntMax(comp: *const Compilation, w: anytype, specifier: Typ...@@ -908,10 +907,12 @@ fn generateExactWidthIntMax(comp: *const Compilation, w: anytype, specifier: Typ
908 ty = if (unsigned) comp.types.int64.makeIntegerUnsigned() else comp.types.int64;907 ty = if (unsigned) comp.types.int64.makeIntegerUnsigned() else comp.types.int64;
909 }908 }
910909
911 var name = std.BoundedArray(u8, 6).init(0) catch unreachable;910 var name_buffer: [6]u8 = undefined;
912 name.writer().print("{s}{d}", .{ if (unsigned) "UINT" else "INT", bit_count }) catch return error.OutOfMemory;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);
915}916}
916917
917fn generateIntWidth(comp: *Compilation, w: anytype, name: []const u8, ty: Type) !void {918fn generateIntWidth(comp: *Compilation, w: anytype, name: []const u8, ty: Type) !void {