mirror of
https://github.com/ziglang/zig.git
synced 2026-02-12 20:37:54 +00:00
rename ryu128 -> format_float
Symmetry with parse_float and to hide the implementation from the user. Additionally, we expose the entire namespace and provide some aliases so everything is available to a user. Closes #19366
This commit is contained in:
parent
afdc41cfd6
commit
31791ae15b
@ -235,7 +235,7 @@ set(ZIG_STAGE2_SOURCES
|
||||
"${CMAKE_SOURCE_DIR}/lib/std/elf.zig"
|
||||
"${CMAKE_SOURCE_DIR}/lib/std/fifo.zig"
|
||||
"${CMAKE_SOURCE_DIR}/lib/std/fmt.zig"
|
||||
"${CMAKE_SOURCE_DIR}/lib/std/fmt/ryu128.zig"
|
||||
"${CMAKE_SOURCE_DIR}/lib/std/fmt/format_float.zig"
|
||||
"${CMAKE_SOURCE_DIR}/lib/std/fmt/parse_float.zig"
|
||||
"${CMAKE_SOURCE_DIR}/lib/std/fs.zig"
|
||||
"${CMAKE_SOURCE_DIR}/lib/std/fs/AtomicFile.zig"
|
||||
|
||||
@ -9,7 +9,6 @@ const assert = std.debug.assert;
|
||||
const mem = std.mem;
|
||||
const unicode = std.unicode;
|
||||
const meta = std.meta;
|
||||
const ryu128 = @import("fmt/ryu128.zig");
|
||||
const lossyCast = std.math.lossyCast;
|
||||
const expectFmt = std.testing.expectFmt;
|
||||
|
||||
@ -757,21 +756,25 @@ pub fn formatIntValue(
|
||||
return formatInt(int_value, base, case, options, writer);
|
||||
}
|
||||
|
||||
pub const format_float = @import("fmt/format_float.zig");
|
||||
pub const formatFloat = format_float.formatFloat;
|
||||
pub const FormatFloatError = format_float.FormatError;
|
||||
|
||||
fn formatFloatValue(
|
||||
value: anytype,
|
||||
comptime fmt: []const u8,
|
||||
options: FormatOptions,
|
||||
writer: anytype,
|
||||
) !void {
|
||||
var buf: [ryu128.bufferSize(.decimal, f64)]u8 = undefined;
|
||||
var buf: [format_float.bufferSize(.decimal, f64)]u8 = undefined;
|
||||
|
||||
if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "e")) {
|
||||
const s = ryu128.format(&buf, value, .{ .mode = .scientific, .precision = options.precision }) catch |err| switch (err) {
|
||||
const s = formatFloat(&buf, value, .{ .mode = .scientific, .precision = options.precision }) catch |err| switch (err) {
|
||||
error.BufferTooSmall => "(float)",
|
||||
};
|
||||
return formatBuf(s, options, writer);
|
||||
} else if (comptime std.mem.eql(u8, fmt, "d")) {
|
||||
const s = ryu128.format(&buf, value, .{ .mode = .decimal, .precision = options.precision }) catch |err| switch (err) {
|
||||
const s = formatFloat(&buf, value, .{ .mode = .decimal, .precision = options.precision }) catch |err| switch (err) {
|
||||
error.BufferTooSmall => "(float)",
|
||||
};
|
||||
return formatBuf(s, options, writer);
|
||||
@ -787,7 +790,7 @@ fn formatFloatValue(
|
||||
}
|
||||
|
||||
test {
|
||||
_ = &ryu128;
|
||||
_ = &format_float;
|
||||
}
|
||||
|
||||
pub const Case = enum { lower, upper };
|
||||
@ -890,7 +893,7 @@ fn formatSizeImpl(comptime base: comptime_int) type {
|
||||
return formatBuf("0B", options, writer);
|
||||
}
|
||||
// The worst case in terms of space needed is 32 bytes + 3 for the suffix.
|
||||
var buf: [ryu128.min_buffer_size + 3]u8 = undefined;
|
||||
var buf: [format_float.min_buffer_size + 3]u8 = undefined;
|
||||
|
||||
const mags_si = " kMGTPEZY";
|
||||
const mags_iec = " KMGTPEZY";
|
||||
@ -908,7 +911,7 @@ fn formatSizeImpl(comptime base: comptime_int) type {
|
||||
else => unreachable,
|
||||
};
|
||||
|
||||
const s = ryu128.format(&buf, new_value, .{ .mode = .decimal, .precision = options.precision }) catch |err| switch (err) {
|
||||
const s = formatFloat(&buf, new_value, .{ .mode = .decimal, .precision = options.precision }) catch |err| switch (err) {
|
||||
error.BufferTooSmall => unreachable,
|
||||
};
|
||||
|
||||
|
||||
@ -27,7 +27,7 @@ pub fn bufferSize(comptime mode: Format, comptime T: type) comptime_int {
|
||||
};
|
||||
}
|
||||
|
||||
pub const RyuError = error{
|
||||
pub const FormatError = error{
|
||||
BufferTooSmall,
|
||||
};
|
||||
|
||||
@ -52,7 +52,7 @@ pub const FormatOptions = struct {
|
||||
///
|
||||
/// When printing full precision decimals, use `bufferSize` to get the required space. It is
|
||||
/// recommended to bound decimal output with a fixed precision to reduce the required buffer size.
|
||||
pub fn format(buf: []u8, v_: anytype, options: FormatOptions) RyuError![]const u8 {
|
||||
pub fn formatFloat(buf: []u8, v_: anytype, options: FormatOptions) FormatError![]const u8 {
|
||||
const v = switch (@TypeOf(v_)) {
|
||||
// comptime_float internally is a f128; this preserves precision.
|
||||
comptime_float => @as(f128, v_),
|
||||
@ -182,7 +182,7 @@ fn round(f: FloatDecimal128, mode: RoundMode, precision: usize) FloatDecimal128
|
||||
/// will not fit.
|
||||
///
|
||||
/// It is recommended to bound decimal formatting with an exact precision.
|
||||
pub fn formatScientific(buf: []u8, f_: FloatDecimal128, precision: ?usize) RyuError![]const u8 {
|
||||
pub fn formatScientific(buf: []u8, f_: FloatDecimal128, precision: ?usize) FormatError![]const u8 {
|
||||
std.debug.assert(buf.len >= min_buffer_size);
|
||||
var f = f_;
|
||||
|
||||
@ -253,7 +253,7 @@ pub fn formatScientific(buf: []u8, f_: FloatDecimal128, precision: ?usize) RyuEr
|
||||
/// The buffer provided must be greater than `min_buffer_size` bytes in length. If no precision is
|
||||
/// specified, this may still return an error. If precision is specified, `2 + precision` bytes will
|
||||
/// always be written.
|
||||
pub fn formatDecimal(buf: []u8, f_: FloatDecimal128, precision: ?usize) RyuError![]const u8 {
|
||||
pub fn formatDecimal(buf: []u8, f_: FloatDecimal128, precision: ?usize) FormatError![]const u8 {
|
||||
std.debug.assert(buf.len >= min_buffer_size);
|
||||
var f = f_;
|
||||
|
||||
@ -964,7 +964,7 @@ fn check(comptime T: type, value: T, comptime expected: []const u8) !void {
|
||||
|
||||
var buf: [6000]u8 = undefined;
|
||||
const value_bits: I = @bitCast(value);
|
||||
const s = try format(&buf, value, .{});
|
||||
const s = try formatFloat(&buf, value, .{});
|
||||
try std.testing.expectEqualStrings(expected, s);
|
||||
|
||||
if (@bitSizeOf(T) != 80) {
|
||||
Loading…
x
Reference in New Issue
Block a user