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.
This commit is contained in:
Andrew Kelley
2023-11-21 21:22:48 -07:00
parent ddd8d59188
commit 9393a83323

View File

@@ -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);
try comp.generateFmt(prefix.constSlice(), w, ty);
try comp.generateSuffixMacro(prefix.constSlice(), w, ty);
const prefix = full[0 .. full.len - suffix.len]; // remove "_TYPE__"
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 {