2019-03-02 21:46:04 +00:00
|
|
|
const std = @import("std.zig");
|
2017-12-24 03:08:53 +00:00
|
|
|
const math = std.math;
|
2019-11-20 01:29:08 +00:00
|
|
|
const assert = std.debug.assert;
|
2017-12-24 03:08:53 +00:00
|
|
|
const mem = std.mem;
|
2017-05-17 16:26:35 +00:00
|
|
|
const builtin = @import("builtin");
|
2019-03-02 21:46:04 +00:00
|
|
|
const errol = @import("fmt/errol.zig");
|
2018-06-17 06:57:07 +00:00
|
|
|
const lossyCast = std.math.lossyCast;
|
2017-03-10 00:12:15 +00:00
|
|
|
|
2019-04-27 09:36:48 +00:00
|
|
|
pub const default_max_depth = 3;
|
|
|
|
|
2019-06-25 05:11:04 +00:00
|
|
|
pub const Alignment = enum {
|
|
|
|
Left,
|
|
|
|
Center,
|
|
|
|
Right,
|
|
|
|
};
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
pub const FormatOptions = struct {
|
|
|
|
precision: ?usize = null,
|
|
|
|
width: ?usize = null,
|
2019-06-25 05:11:04 +00:00
|
|
|
alignment: ?Alignment = null,
|
|
|
|
fill: u8 = ' ',
|
2019-06-20 08:07:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
fn nextArg(comptime used_pos_args: *u32, comptime maybe_pos_arg: ?comptime_int, comptime next_arg: *comptime_int) comptime_int {
|
|
|
|
if (maybe_pos_arg) |pos_arg| {
|
|
|
|
used_pos_args.* |= 1 << pos_arg;
|
|
|
|
return pos_arg;
|
|
|
|
} else {
|
|
|
|
const arg = next_arg.*;
|
|
|
|
next_arg.* += 1;
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 05:11:04 +00:00
|
|
|
fn peekIsAlign(comptime fmt: []const u8) bool {
|
|
|
|
// Should only be called during a state transition to the format segment.
|
2019-11-20 01:29:08 +00:00
|
|
|
comptime assert(fmt[0] == ':');
|
2019-06-25 05:11:04 +00:00
|
|
|
|
|
|
|
inline for (([_]u8{ 1, 2 })[0..]) |i| {
|
|
|
|
if (fmt.len > i and (fmt[i] == '<' or fmt[i] == '^' or fmt[i] == '>')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-10 00:12:15 +00:00
|
|
|
/// Renders fmt string with args, calling output with slices of bytes.
|
2017-10-31 08:47:55 +00:00
|
|
|
/// If `output` returns an error, the error is returned from `format` and
|
|
|
|
/// `output` is not called again.
|
2019-10-22 18:58:27 +00:00
|
|
|
///
|
|
|
|
/// The format string must be comptime known and may contain placeholders following
|
|
|
|
/// this format:
|
|
|
|
/// `{[position][specifier]:[fill][alignment][width].[precision]}`
|
2019-10-30 02:59:30 +00:00
|
|
|
///
|
2019-10-22 18:58:27 +00:00
|
|
|
/// Each word between `[` and `]` is a parameter you have to replace with something:
|
|
|
|
///
|
|
|
|
/// - *position* is the index of the argument that should be inserted
|
|
|
|
/// - *specifier* is a type-dependent formatting option that determines how a type should formatted (see below)
|
|
|
|
/// - *fill* is a single character which is used to pad the formatted text
|
|
|
|
/// - *alignment* is one of the three characters `<`, `^` or `>`. they define if the text is *left*, *center*, or *right* aligned
|
|
|
|
/// - *width* is the total width of the field in characters
|
|
|
|
/// - *precision* specifies how many decimals a formatted number should have
|
|
|
|
///
|
|
|
|
/// Note that most of the parameters are optional and may be omitted. Also you can leave out separators like `:` and `.` when
|
|
|
|
/// all parameters after the separator are omitted.
|
|
|
|
/// Only exception is the *fill* parameter. If *fill* is required, one has to specify *alignment* as well, as otherwise
|
|
|
|
/// the digits after `:` is interpreted as *width*, not *fill*.
|
|
|
|
///
|
|
|
|
/// The *specifier* has several options for types:
|
|
|
|
/// - `x` and `X`:
|
|
|
|
/// - format the non-numeric value as a string of bytes in hexadecimal notation ("binary dump") in either lower case or upper case
|
|
|
|
/// - output numeric value in hexadecimal notation
|
|
|
|
/// - `s`: print a pointer-to-many as a c-string, use zero-termination
|
|
|
|
/// - `B` and `Bi`: output a memory size in either metric (1000) or power-of-two (1024) based notation. works for both float and integer values.
|
|
|
|
/// - `e`: output floating point value in scientific notation
|
|
|
|
/// - `d`: output numeric value in decimal notation
|
|
|
|
/// - `b`: output integer value in binary notation
|
|
|
|
/// - `c`: output integer as an ASCII character. Integer type must have 8 bits at max.
|
2019-10-30 02:59:30 +00:00
|
|
|
/// - `*`: output the address of the value instead of the value itself.
|
2019-10-22 18:58:27 +00:00
|
|
|
///
|
|
|
|
/// If a formatted user type contains a function of the type
|
|
|
|
/// ```
|
2019-12-09 20:59:12 +00:00
|
|
|
/// fn format(value: ?, comptime fmt: []const u8, options: std.fmt.FormatOptions, context: var, comptime Errors: type, output: fn (@TypeOf(context), []const u8) Errors!void) Errors!void
|
2019-10-22 18:58:27 +00:00
|
|
|
/// ```
|
|
|
|
/// with `?` being the type formatted, this function will be called instead of the default implementation.
|
|
|
|
/// This allows user types to be formatted in a logical manner instead of dumping all fields of the type.
|
|
|
|
///
|
|
|
|
/// A user type may be a `struct`, `union` or `enum` type.
|
2019-06-17 10:03:56 +00:00
|
|
|
pub fn format(
|
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2019-12-09 20:56:19 +00:00
|
|
|
output: fn (@TypeOf(context), []const u8) Errors!void,
|
2019-06-17 10:03:56 +00:00
|
|
|
comptime fmt: []const u8,
|
2019-12-09 03:53:51 +00:00
|
|
|
args: var,
|
2019-06-17 10:03:56 +00:00
|
|
|
) Errors!void {
|
2019-06-20 08:07:43 +00:00
|
|
|
const ArgSetType = @IntType(false, 32);
|
2019-12-09 19:55:51 +00:00
|
|
|
if (args.len > ArgSetType.bit_count) {
|
2019-06-20 08:07:43 +00:00
|
|
|
@compileError("32 arguments max are supported per format call");
|
|
|
|
}
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const State = enum {
|
2018-04-14 06:12:19 +00:00
|
|
|
Start,
|
2019-06-20 08:07:43 +00:00
|
|
|
Positional,
|
2018-04-14 06:12:19 +00:00
|
|
|
CloseBrace,
|
2019-06-20 08:07:43 +00:00
|
|
|
Specifier,
|
2019-06-25 05:11:04 +00:00
|
|
|
FormatFillAndAlign,
|
2019-06-20 08:07:43 +00:00
|
|
|
FormatWidth,
|
|
|
|
FormatPrecision,
|
2018-04-14 06:12:19 +00:00
|
|
|
};
|
|
|
|
|
2017-03-10 00:12:15 +00:00
|
|
|
comptime var start_index = 0;
|
|
|
|
comptime var state = State.Start;
|
|
|
|
comptime var next_arg = 0;
|
2019-06-20 08:07:43 +00:00
|
|
|
comptime var maybe_pos_arg: ?comptime_int = null;
|
|
|
|
comptime var used_pos_args: ArgSetType = 0;
|
|
|
|
comptime var specifier_start = 0;
|
|
|
|
comptime var specifier_end = 0;
|
|
|
|
comptime var options = FormatOptions{};
|
2017-03-10 00:12:15 +00:00
|
|
|
|
|
|
|
inline for (fmt) |c, i| {
|
|
|
|
switch (state) {
|
2019-06-17 10:03:56 +00:00
|
|
|
.Start => switch (c) {
|
2017-03-10 00:12:15 +00:00
|
|
|
'{' => {
|
|
|
|
if (start_index < i) {
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, fmt[start_index..i]);
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
2019-06-20 08:07:43 +00:00
|
|
|
|
2018-05-30 15:18:11 +00:00
|
|
|
start_index = i;
|
2019-06-20 08:07:43 +00:00
|
|
|
specifier_start = i + 1;
|
|
|
|
specifier_end = i + 1;
|
|
|
|
maybe_pos_arg = null;
|
|
|
|
state = .Positional;
|
|
|
|
options = FormatOptions{};
|
2017-03-10 00:12:15 +00:00
|
|
|
},
|
|
|
|
'}' => {
|
|
|
|
if (start_index < i) {
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, fmt[start_index..i]);
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
2019-06-20 08:07:43 +00:00
|
|
|
state = .CloseBrace;
|
2017-03-10 00:12:15 +00:00
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
},
|
2019-06-20 08:07:43 +00:00
|
|
|
.Positional => switch (c) {
|
2017-03-10 00:12:15 +00:00
|
|
|
'{' => {
|
2019-06-20 08:07:43 +00:00
|
|
|
state = .Start;
|
2017-03-10 00:12:15 +00:00
|
|
|
start_index = i;
|
|
|
|
},
|
2019-06-20 08:07:43 +00:00
|
|
|
':' => {
|
2019-06-25 05:11:04 +00:00
|
|
|
state = if (comptime peekIsAlign(fmt[i..])) State.FormatFillAndAlign else State.FormatWidth;
|
2019-06-20 08:07:43 +00:00
|
|
|
specifier_end = i;
|
|
|
|
},
|
|
|
|
'0'...'9' => {
|
|
|
|
if (maybe_pos_arg == null) {
|
|
|
|
maybe_pos_arg = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
maybe_pos_arg.? *= 10;
|
|
|
|
maybe_pos_arg.? += c - '0';
|
|
|
|
specifier_start = i + 1;
|
|
|
|
|
2019-12-09 19:55:51 +00:00
|
|
|
if (maybe_pos_arg.? >= args.len) {
|
2019-06-20 08:07:43 +00:00
|
|
|
@compileError("Positional value refers to non-existent argument");
|
|
|
|
}
|
|
|
|
},
|
2017-03-10 00:12:15 +00:00
|
|
|
'}' => {
|
2019-06-20 08:07:43 +00:00
|
|
|
const arg_to_print = comptime nextArg(&used_pos_args, maybe_pos_arg, &next_arg);
|
|
|
|
|
2019-12-09 19:55:51 +00:00
|
|
|
if (arg_to_print >= args.len) {
|
2019-10-31 17:23:02 +00:00
|
|
|
@compileError("Too few arguments");
|
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
try formatType(
|
|
|
|
args[arg_to_print],
|
|
|
|
fmt[0..0],
|
|
|
|
options,
|
|
|
|
context,
|
|
|
|
Errors,
|
|
|
|
output,
|
|
|
|
default_max_depth,
|
|
|
|
);
|
|
|
|
|
|
|
|
state = .Start;
|
2017-03-10 00:12:15 +00:00
|
|
|
start_index = i + 1;
|
|
|
|
},
|
2018-05-30 15:18:11 +00:00
|
|
|
else => {
|
2019-06-20 08:07:43 +00:00
|
|
|
state = .Specifier;
|
|
|
|
specifier_start = i;
|
2017-04-06 09:34:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-06-17 10:03:56 +00:00
|
|
|
.CloseBrace => switch (c) {
|
2017-03-10 00:12:15 +00:00
|
|
|
'}' => {
|
2019-06-20 08:07:43 +00:00
|
|
|
state = .Start;
|
2017-03-10 00:12:15 +00:00
|
|
|
start_index = i;
|
|
|
|
},
|
|
|
|
else => @compileError("Single '}' encountered in format string"),
|
|
|
|
},
|
2019-06-20 08:07:43 +00:00
|
|
|
.Specifier => switch (c) {
|
|
|
|
':' => {
|
|
|
|
specifier_end = i;
|
2019-06-25 05:11:04 +00:00
|
|
|
state = if (comptime peekIsAlign(fmt[i..])) State.FormatFillAndAlign else State.FormatWidth;
|
2019-06-20 08:07:43 +00:00
|
|
|
},
|
2017-03-10 00:12:15 +00:00
|
|
|
'}' => {
|
2019-06-20 08:07:43 +00:00
|
|
|
const arg_to_print = comptime nextArg(&used_pos_args, maybe_pos_arg, &next_arg);
|
|
|
|
|
|
|
|
try formatType(
|
|
|
|
args[arg_to_print],
|
|
|
|
fmt[specifier_start..i],
|
|
|
|
options,
|
|
|
|
context,
|
|
|
|
Errors,
|
|
|
|
output,
|
|
|
|
default_max_depth,
|
|
|
|
);
|
|
|
|
state = .Start;
|
2017-03-10 00:12:15 +00:00
|
|
|
start_index = i + 1;
|
|
|
|
},
|
2018-05-30 15:18:11 +00:00
|
|
|
else => {},
|
2018-05-16 02:11:03 +00:00
|
|
|
},
|
2019-06-25 05:11:04 +00:00
|
|
|
// Only entered if the format string contains a fill/align segment.
|
|
|
|
.FormatFillAndAlign => switch (c) {
|
|
|
|
'<' => {
|
|
|
|
options.alignment = Alignment.Left;
|
|
|
|
state = .FormatWidth;
|
|
|
|
},
|
|
|
|
'^' => {
|
|
|
|
options.alignment = Alignment.Center;
|
|
|
|
state = .FormatWidth;
|
|
|
|
},
|
|
|
|
'>' => {
|
|
|
|
options.alignment = Alignment.Right;
|
|
|
|
state = .FormatWidth;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
options.fill = c;
|
|
|
|
},
|
|
|
|
},
|
2019-06-20 08:07:43 +00:00
|
|
|
.FormatWidth => switch (c) {
|
|
|
|
'0'...'9' => {
|
|
|
|
if (options.width == null) {
|
|
|
|
options.width = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.width.? *= 10;
|
|
|
|
options.width.? += c - '0';
|
|
|
|
},
|
|
|
|
'.' => {
|
|
|
|
state = .FormatPrecision;
|
|
|
|
},
|
|
|
|
'}' => {
|
|
|
|
const arg_to_print = comptime nextArg(&used_pos_args, maybe_pos_arg, &next_arg);
|
|
|
|
|
|
|
|
try formatType(
|
|
|
|
args[arg_to_print],
|
|
|
|
fmt[specifier_start..specifier_end],
|
|
|
|
options,
|
|
|
|
context,
|
|
|
|
Errors,
|
|
|
|
output,
|
|
|
|
default_max_depth,
|
|
|
|
);
|
|
|
|
state = .Start;
|
|
|
|
start_index = i + 1;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
@compileError("Unexpected character in width value: " ++ [_]u8{c});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
.FormatPrecision => switch (c) {
|
|
|
|
'0'...'9' => {
|
|
|
|
if (options.precision == null) {
|
|
|
|
options.precision = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.precision.? *= 10;
|
|
|
|
options.precision.? += c - '0';
|
|
|
|
},
|
|
|
|
'}' => {
|
|
|
|
const arg_to_print = comptime nextArg(&used_pos_args, maybe_pos_arg, &next_arg);
|
|
|
|
|
|
|
|
try formatType(
|
|
|
|
args[arg_to_print],
|
|
|
|
fmt[specifier_start..specifier_end],
|
|
|
|
options,
|
|
|
|
context,
|
|
|
|
Errors,
|
|
|
|
output,
|
|
|
|
default_max_depth,
|
|
|
|
);
|
|
|
|
state = .Start;
|
|
|
|
start_index = i + 1;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
@compileError("Unexpected character in precision value: " ++ [_]u8{c});
|
|
|
|
},
|
|
|
|
},
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
comptime {
|
2019-06-20 08:07:43 +00:00
|
|
|
// All arguments must have been printed but we allow mixing positional and fixed to achieve this.
|
|
|
|
var i: usize = 0;
|
|
|
|
inline while (i < next_arg) : (i += 1) {
|
|
|
|
used_pos_args |= 1 << i;
|
|
|
|
}
|
|
|
|
|
2019-12-09 19:55:51 +00:00
|
|
|
if (@popCount(ArgSetType, used_pos_args) != args.len) {
|
2017-03-10 00:12:15 +00:00
|
|
|
@compileError("Unused arguments");
|
|
|
|
}
|
|
|
|
if (state != State.Start) {
|
|
|
|
@compileError("Incomplete format string: " ++ fmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (start_index < fmt.len) {
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, fmt[start_index..]);
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:06:55 +00:00
|
|
|
pub fn formatType(
|
|
|
|
value: var,
|
|
|
|
comptime fmt: []const u8,
|
2019-08-19 10:28:13 +00:00
|
|
|
options: FormatOptions,
|
2018-06-04 15:06:55 +00:00
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2019-12-09 20:56:19 +00:00
|
|
|
output: fn (@TypeOf(context), []const u8) Errors!void,
|
2019-04-27 09:36:48 +00:00
|
|
|
max_depth: usize,
|
2018-06-04 15:06:55 +00:00
|
|
|
) Errors!void {
|
2019-08-05 07:07:31 +00:00
|
|
|
if (comptime std.mem.eql(u8, fmt, "*")) {
|
2019-12-09 20:56:19 +00:00
|
|
|
try output(context, @typeName(@TypeOf(value).Child));
|
2019-08-05 07:07:31 +00:00
|
|
|
try output(context, "@");
|
2019-08-17 23:17:16 +00:00
|
|
|
try formatInt(@ptrToInt(value), 16, false, FormatOptions{}, context, Errors, output);
|
2019-08-05 07:07:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-09 20:56:19 +00:00
|
|
|
const T = @TypeOf(value);
|
2018-06-06 04:39:39 +00:00
|
|
|
switch (@typeInfo(T)) {
|
2019-06-17 10:03:56 +00:00
|
|
|
.ComptimeInt, .Int, .Float => {
|
2019-06-20 08:07:43 +00:00
|
|
|
return formatValue(value, fmt, options, context, Errors, output);
|
2017-05-17 16:26:35 +00:00
|
|
|
},
|
2019-06-17 10:03:56 +00:00
|
|
|
.Void => {
|
2017-05-17 16:26:35 +00:00
|
|
|
return output(context, "void");
|
|
|
|
},
|
2019-06-17 10:03:56 +00:00
|
|
|
.Bool => {
|
2017-05-17 16:26:35 +00:00
|
|
|
return output(context, if (value) "true" else "false");
|
|
|
|
},
|
2019-06-17 10:03:56 +00:00
|
|
|
.Optional => {
|
2017-05-23 22:38:41 +00:00
|
|
|
if (value) |payload| {
|
2019-06-20 08:07:43 +00:00
|
|
|
return formatType(payload, fmt, options, context, Errors, output, max_depth);
|
2017-05-23 22:38:41 +00:00
|
|
|
} else {
|
|
|
|
return output(context, "null");
|
|
|
|
}
|
|
|
|
},
|
2019-06-17 10:03:56 +00:00
|
|
|
.ErrorUnion => {
|
2017-05-23 22:38:41 +00:00
|
|
|
if (value) |payload| {
|
2019-06-20 08:07:43 +00:00
|
|
|
return formatType(payload, fmt, options, context, Errors, output, max_depth);
|
2017-05-23 22:38:41 +00:00
|
|
|
} else |err| {
|
2019-06-20 08:07:43 +00:00
|
|
|
return formatType(err, fmt, options, context, Errors, output, max_depth);
|
2017-05-23 22:38:41 +00:00
|
|
|
}
|
|
|
|
},
|
2019-06-17 10:03:56 +00:00
|
|
|
.ErrorSet => {
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, "error.");
|
2017-05-23 22:38:41 +00:00
|
|
|
return output(context, @errorName(value));
|
|
|
|
},
|
2019-06-17 10:03:56 +00:00
|
|
|
.Enum => {
|
|
|
|
if (comptime std.meta.trait.hasFn("format")(T)) {
|
2019-06-20 08:07:43 +00:00
|
|
|
return value.format(fmt, options, context, Errors, output);
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
2018-08-27 21:25:33 +00:00
|
|
|
|
2018-08-20 20:04:03 +00:00
|
|
|
try output(context, @typeName(T));
|
2019-06-17 10:03:56 +00:00
|
|
|
try output(context, ".");
|
2019-06-20 08:07:43 +00:00
|
|
|
return formatType(@tagName(value), "", options, context, Errors, output, max_depth);
|
2019-06-17 10:03:56 +00:00
|
|
|
},
|
|
|
|
.Union => {
|
|
|
|
if (comptime std.meta.trait.hasFn("format")(T)) {
|
2019-06-20 08:07:43 +00:00
|
|
|
return value.format(fmt, options, context, Errors, output);
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try output(context, @typeName(T));
|
|
|
|
if (max_depth == 0) {
|
|
|
|
return output(context, "{ ... }");
|
|
|
|
}
|
|
|
|
const info = @typeInfo(T).Union;
|
|
|
|
if (info.tag_type) |UnionTagType| {
|
|
|
|
try output(context, "{ .");
|
2019-11-07 23:52:09 +00:00
|
|
|
try output(context, @tagName(@as(UnionTagType, value)));
|
2019-06-17 10:03:56 +00:00
|
|
|
try output(context, " = ");
|
|
|
|
inline for (info.fields) |u_field| {
|
2019-11-07 23:52:09 +00:00
|
|
|
if (@enumToInt(@as(UnionTagType, value)) == u_field.enum_field.?.value) {
|
2019-06-20 08:07:43 +00:00
|
|
|
try formatType(@field(value, u_field.name), "", options, context, Errors, output, max_depth - 1);
|
2018-08-27 21:25:33 +00:00
|
|
|
}
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
try output(context, " }");
|
|
|
|
} else {
|
2019-12-09 03:53:51 +00:00
|
|
|
try format(context, Errors, output, "@{x}", .{@ptrToInt(&value)});
|
2018-08-20 20:04:03 +00:00
|
|
|
}
|
|
|
|
},
|
2019-06-17 10:03:56 +00:00
|
|
|
.Struct => {
|
|
|
|
if (comptime std.meta.trait.hasFn("format")(T)) {
|
2019-06-20 08:07:43 +00:00
|
|
|
return value.format(fmt, options, context, Errors, output);
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try output(context, @typeName(T));
|
|
|
|
if (max_depth == 0) {
|
|
|
|
return output(context, "{ ... }");
|
|
|
|
}
|
|
|
|
comptime var field_i = 0;
|
2019-07-29 01:37:35 +00:00
|
|
|
try output(context, "{");
|
2019-06-17 10:03:56 +00:00
|
|
|
inline while (field_i < @memberCount(T)) : (field_i += 1) {
|
|
|
|
if (field_i == 0) {
|
2019-07-29 01:37:35 +00:00
|
|
|
try output(context, " .");
|
2019-06-17 10:03:56 +00:00
|
|
|
} else {
|
|
|
|
try output(context, ", .");
|
|
|
|
}
|
|
|
|
try output(context, @memberName(T, field_i));
|
|
|
|
try output(context, " = ");
|
2019-06-20 08:07:43 +00:00
|
|
|
try formatType(@field(value, @memberName(T, field_i)), "", options, context, Errors, output, max_depth - 1);
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
try output(context, " }");
|
|
|
|
},
|
|
|
|
.Pointer => |ptr_info| switch (ptr_info.size) {
|
|
|
|
.One => switch (@typeInfo(ptr_info.child)) {
|
2018-06-06 04:39:39 +00:00
|
|
|
builtin.TypeId.Array => |info| {
|
|
|
|
if (info.child == u8) {
|
2019-06-20 08:07:43 +00:00
|
|
|
return formatText(value, fmt, options, context, Errors, output);
|
2018-05-30 15:18:11 +00:00
|
|
|
}
|
2019-12-09 03:53:51 +00:00
|
|
|
return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) });
|
2018-05-30 15:18:11 +00:00
|
|
|
},
|
2018-06-04 15:06:55 +00:00
|
|
|
builtin.TypeId.Enum, builtin.TypeId.Union, builtin.TypeId.Struct => {
|
2019-06-20 08:07:43 +00:00
|
|
|
return formatType(value.*, fmt, options, context, Errors, output, max_depth);
|
2018-05-30 15:18:11 +00:00
|
|
|
},
|
2019-12-09 03:53:51 +00:00
|
|
|
else => return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }),
|
2018-06-06 04:39:39 +00:00
|
|
|
},
|
2019-06-17 10:03:56 +00:00
|
|
|
.Many => {
|
2018-06-10 16:57:21 +00:00
|
|
|
if (ptr_info.child == u8) {
|
2018-08-14 17:56:41 +00:00
|
|
|
if (fmt.len > 0 and fmt[0] == 's') {
|
2019-05-26 17:17:34 +00:00
|
|
|
const len = mem.len(u8, value);
|
2019-06-20 08:07:43 +00:00
|
|
|
return formatText(value[0..len], fmt, options, context, Errors, output);
|
2018-06-10 16:57:21 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-09 03:53:51 +00:00
|
|
|
return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) });
|
2018-06-06 04:39:39 +00:00
|
|
|
},
|
2019-06-17 10:03:56 +00:00
|
|
|
.Slice => {
|
2018-11-14 16:12:10 +00:00
|
|
|
if (fmt.len > 0 and ((fmt[0] == 'x') or (fmt[0] == 'X'))) {
|
2019-06-20 08:07:43 +00:00
|
|
|
return formatText(value, fmt, options, context, Errors, output);
|
2018-11-14 16:12:10 +00:00
|
|
|
}
|
2019-05-29 15:23:21 +00:00
|
|
|
if (ptr_info.child == u8) {
|
2019-06-20 08:07:43 +00:00
|
|
|
return formatText(value, fmt, options, context, Errors, output);
|
2019-05-29 15:23:21 +00:00
|
|
|
}
|
2019-12-09 03:53:51 +00:00
|
|
|
return format(context, Errors, output, "{}@{x}", .{ @typeName(ptr_info.child), @ptrToInt(value.ptr) });
|
2018-06-06 04:39:39 +00:00
|
|
|
},
|
2019-06-17 10:03:56 +00:00
|
|
|
.C => {
|
2019-12-09 03:53:51 +00:00
|
|
|
return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) });
|
2019-02-11 21:07:40 +00:00
|
|
|
},
|
2017-05-17 16:26:35 +00:00
|
|
|
},
|
2019-06-17 10:03:56 +00:00
|
|
|
.Array => |info| {
|
2019-11-27 08:30:39 +00:00
|
|
|
const Slice = @Type(builtin.TypeInfo{
|
|
|
|
.Pointer = .{
|
|
|
|
.size = .Slice,
|
|
|
|
.is_const = true,
|
|
|
|
.is_volatile = false,
|
|
|
|
.is_allowzero = false,
|
|
|
|
.alignment = @alignOf(info.child),
|
|
|
|
.child = info.child,
|
|
|
|
.sentinel = null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return formatType(@as(Slice, &value), fmt, options, context, Errors, output, max_depth);
|
2018-06-15 17:49:39 +00:00
|
|
|
},
|
2019-06-17 10:03:56 +00:00
|
|
|
.Fn => {
|
2019-12-09 03:53:51 +00:00
|
|
|
return format(context, Errors, output, "{}@{x}", .{ @typeName(T), @ptrToInt(value) });
|
2018-12-19 15:07:35 +00:00
|
|
|
},
|
2019-12-09 18:30:53 +00:00
|
|
|
.Type => return output(context, @typeName(T)),
|
2018-06-06 04:39:39 +00:00
|
|
|
else => @compileError("Unable to format type '" ++ @typeName(T) ++ "'"),
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:06:55 +00:00
|
|
|
fn formatValue(
|
|
|
|
value: var,
|
|
|
|
comptime fmt: []const u8,
|
2019-08-19 10:28:13 +00:00
|
|
|
options: FormatOptions,
|
2018-06-04 15:06:55 +00:00
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2019-12-09 20:56:19 +00:00
|
|
|
output: fn (@TypeOf(context), []const u8) Errors!void,
|
2018-06-04 15:06:55 +00:00
|
|
|
) Errors!void {
|
2019-06-20 08:07:43 +00:00
|
|
|
if (comptime std.mem.eql(u8, fmt, "B")) {
|
2019-08-17 23:17:16 +00:00
|
|
|
return formatBytes(value, options, 1000, context, Errors, output);
|
2019-06-20 08:07:43 +00:00
|
|
|
} else if (comptime std.mem.eql(u8, fmt, "Bi")) {
|
2019-08-17 23:17:16 +00:00
|
|
|
return formatBytes(value, options, 1024, context, Errors, output);
|
2018-05-30 15:18:11 +00:00
|
|
|
}
|
2018-06-04 15:06:55 +00:00
|
|
|
|
2019-12-09 20:56:19 +00:00
|
|
|
const T = @TypeOf(value);
|
2018-05-30 15:18:11 +00:00
|
|
|
switch (@typeId(T)) {
|
2019-06-20 08:07:43 +00:00
|
|
|
.Float => return formatFloatValue(value, fmt, options, context, Errors, output),
|
|
|
|
.Int, .ComptimeInt => return formatIntValue(value, fmt, options, context, Errors, output),
|
2018-12-19 10:50:29 +00:00
|
|
|
else => comptime unreachable,
|
2018-05-30 15:18:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:06:55 +00:00
|
|
|
pub fn formatIntValue(
|
|
|
|
value: var,
|
|
|
|
comptime fmt: []const u8,
|
2019-08-19 10:28:13 +00:00
|
|
|
options: FormatOptions,
|
2018-06-04 15:06:55 +00:00
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2019-12-09 20:56:19 +00:00
|
|
|
output: fn (@TypeOf(context), []const u8) Errors!void,
|
2018-06-04 15:06:55 +00:00
|
|
|
) Errors!void {
|
2018-05-30 15:18:11 +00:00
|
|
|
comptime var radix = 10;
|
|
|
|
comptime var uppercase = false;
|
2019-04-05 01:36:31 +00:00
|
|
|
|
2019-12-09 20:56:19 +00:00
|
|
|
const int_value = if (@TypeOf(value) == comptime_int) blk: {
|
2019-04-05 01:36:31 +00:00
|
|
|
const Int = math.IntFittingRange(value, value);
|
2019-11-07 23:52:09 +00:00
|
|
|
break :blk @as(Int, value);
|
2019-04-05 01:36:31 +00:00
|
|
|
} else
|
|
|
|
value;
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "d")) {
|
|
|
|
radix = 10;
|
|
|
|
uppercase = false;
|
|
|
|
} else if (comptime std.mem.eql(u8, fmt, "c")) {
|
2019-12-09 20:56:19 +00:00
|
|
|
if (@TypeOf(int_value).bit_count <= 8) {
|
2019-11-07 04:25:57 +00:00
|
|
|
return formatAsciiChar(@as(u8, int_value), options, context, Errors, output);
|
2019-06-20 08:07:43 +00:00
|
|
|
} else {
|
|
|
|
@compileError("Cannot print integer that is larger than 8 bits as a ascii");
|
2018-05-30 15:18:11 +00:00
|
|
|
}
|
2019-06-20 08:07:43 +00:00
|
|
|
} else if (comptime std.mem.eql(u8, fmt, "b")) {
|
|
|
|
radix = 2;
|
|
|
|
uppercase = false;
|
|
|
|
} else if (comptime std.mem.eql(u8, fmt, "x")) {
|
|
|
|
radix = 16;
|
|
|
|
uppercase = false;
|
|
|
|
} else if (comptime std.mem.eql(u8, fmt, "X")) {
|
|
|
|
radix = 16;
|
|
|
|
uppercase = true;
|
|
|
|
} else {
|
|
|
|
@compileError("Unknown format string: '" ++ fmt ++ "'");
|
2018-05-30 15:18:11 +00:00
|
|
|
}
|
2019-06-20 08:07:43 +00:00
|
|
|
|
2019-08-17 23:17:16 +00:00
|
|
|
return formatInt(int_value, radix, uppercase, options, context, Errors, output);
|
2018-05-30 15:18:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:06:55 +00:00
|
|
|
fn formatFloatValue(
|
|
|
|
value: var,
|
|
|
|
comptime fmt: []const u8,
|
2019-08-19 10:28:13 +00:00
|
|
|
options: FormatOptions,
|
2018-06-04 15:06:55 +00:00
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2019-12-09 20:56:19 +00:00
|
|
|
output: fn (@TypeOf(context), []const u8) Errors!void,
|
2018-06-04 15:06:55 +00:00
|
|
|
) Errors!void {
|
2019-06-20 08:07:43 +00:00
|
|
|
if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "e")) {
|
2019-08-17 23:17:16 +00:00
|
|
|
return formatFloatScientific(value, options, context, Errors, output);
|
2019-06-20 08:07:43 +00:00
|
|
|
} else if (comptime std.mem.eql(u8, fmt, "d")) {
|
2019-08-17 23:17:16 +00:00
|
|
|
return formatFloatDecimal(value, options, context, Errors, output);
|
2019-06-20 08:07:43 +00:00
|
|
|
} else {
|
|
|
|
@compileError("Unknown format string: '" ++ fmt ++ "'");
|
2018-05-30 15:18:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:06:55 +00:00
|
|
|
pub fn formatText(
|
|
|
|
bytes: []const u8,
|
|
|
|
comptime fmt: []const u8,
|
2019-08-19 10:28:13 +00:00
|
|
|
options: FormatOptions,
|
2018-06-04 15:06:55 +00:00
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2019-12-09 20:56:19 +00:00
|
|
|
output: fn (@TypeOf(context), []const u8) Errors!void,
|
2018-06-04 15:06:55 +00:00
|
|
|
) Errors!void {
|
2019-06-20 08:07:43 +00:00
|
|
|
if (fmt.len == 0) {
|
|
|
|
return output(context, bytes);
|
|
|
|
} else if (comptime std.mem.eql(u8, fmt, "s")) {
|
2019-08-17 23:17:16 +00:00
|
|
|
return formatBuf(bytes, options, context, Errors, output);
|
2019-06-20 08:07:43 +00:00
|
|
|
} else if (comptime (std.mem.eql(u8, fmt, "x") or std.mem.eql(u8, fmt, "X"))) {
|
|
|
|
for (bytes) |c| {
|
2019-08-17 23:17:16 +00:00
|
|
|
try formatInt(c, 16, fmt[0] == 'X', FormatOptions{ .width = 2, .fill = '0' }, context, Errors, output);
|
2019-06-20 08:07:43 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
@compileError("Unknown format string: '" ++ fmt ++ "'");
|
2018-05-30 15:18:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:06:55 +00:00
|
|
|
pub fn formatAsciiChar(
|
|
|
|
c: u8,
|
2019-08-19 10:28:13 +00:00
|
|
|
options: FormatOptions,
|
2018-06-04 15:06:55 +00:00
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2019-12-09 20:56:19 +00:00
|
|
|
output: fn (@TypeOf(context), []const u8) Errors!void,
|
2018-06-04 15:06:55 +00:00
|
|
|
) Errors!void {
|
2019-12-17 23:04:01 +00:00
|
|
|
if (std.ascii.isPrint(c))
|
|
|
|
return output(context, @as(*const [1]u8, &c)[0..]);
|
|
|
|
return format(context, Errors, output, "\\x{x:0<2}", .{c});
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:06:55 +00:00
|
|
|
pub fn formatBuf(
|
|
|
|
buf: []const u8,
|
2019-08-19 10:28:13 +00:00
|
|
|
options: FormatOptions,
|
2018-06-04 15:06:55 +00:00
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2019-12-09 20:56:19 +00:00
|
|
|
output: fn (@TypeOf(context), []const u8) Errors!void,
|
2018-06-04 15:06:55 +00:00
|
|
|
) Errors!void {
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, buf);
|
2017-04-06 09:34:04 +00:00
|
|
|
|
2019-08-17 23:17:16 +00:00
|
|
|
const width = options.width orelse 0;
|
2017-10-31 08:47:55 +00:00
|
|
|
var leftover_padding = if (width > buf.len) (width - buf.len) else return;
|
2019-08-17 23:17:16 +00:00
|
|
|
const pad_byte: u8 = options.fill;
|
2017-05-03 22:12:07 +00:00
|
|
|
while (leftover_padding > 0) : (leftover_padding -= 1) {
|
2019-11-07 04:25:57 +00:00
|
|
|
try output(context, @as(*const [1]u8, &pad_byte)[0..1]);
|
2017-04-06 09:34:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 10:58:10 +00:00
|
|
|
// Print a float in scientific notation to the specified precision. Null uses full precision.
|
|
|
|
// It should be the case that every full precision, printed value can be re-parsed back to the
|
|
|
|
// same type unambiguously.
|
2018-06-04 15:06:55 +00:00
|
|
|
pub fn formatFloatScientific(
|
|
|
|
value: var,
|
2019-08-19 10:28:13 +00:00
|
|
|
options: FormatOptions,
|
2018-06-04 15:06:55 +00:00
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2019-12-09 20:56:19 +00:00
|
|
|
output: fn (@TypeOf(context), []const u8) Errors!void,
|
2018-06-04 15:06:55 +00:00
|
|
|
) Errors!void {
|
2018-06-30 08:15:02 +00:00
|
|
|
var x = @floatCast(f64, value);
|
2017-10-23 20:40:49 +00:00
|
|
|
|
|
|
|
// Errol doesn't handle these special cases.
|
2017-10-24 19:18:50 +00:00
|
|
|
if (math.signbit(x)) {
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, "-");
|
2017-10-24 19:18:50 +00:00
|
|
|
x = -x;
|
|
|
|
}
|
2018-04-17 10:58:10 +00:00
|
|
|
|
|
|
|
if (math.isNan(x)) {
|
|
|
|
return output(context, "nan");
|
|
|
|
}
|
2017-10-23 20:40:49 +00:00
|
|
|
if (math.isPositiveInf(x)) {
|
2018-04-17 10:58:10 +00:00
|
|
|
return output(context, "inf");
|
2017-10-23 20:40:49 +00:00
|
|
|
}
|
|
|
|
if (x == 0.0) {
|
2018-04-17 10:58:10 +00:00
|
|
|
try output(context, "0");
|
|
|
|
|
2019-08-17 23:17:16 +00:00
|
|
|
if (options.precision) |precision| {
|
2018-04-17 10:58:10 +00:00
|
|
|
if (precision != 0) {
|
|
|
|
try output(context, ".");
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < precision) : (i += 1) {
|
|
|
|
try output(context, "0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try output(context, ".0");
|
|
|
|
}
|
|
|
|
|
|
|
|
try output(context, "e+00");
|
|
|
|
return;
|
2017-10-23 20:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var buffer: [32]u8 = undefined;
|
2018-04-17 10:58:10 +00:00
|
|
|
var float_decimal = errol.errol3(x, buffer[0..]);
|
|
|
|
|
2019-08-17 23:17:16 +00:00
|
|
|
if (options.precision) |precision| {
|
2018-04-17 10:58:10 +00:00
|
|
|
errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Scientific);
|
|
|
|
|
|
|
|
try output(context, float_decimal.digits[0..1]);
|
|
|
|
|
|
|
|
// {e0} case prints no `.`
|
|
|
|
if (precision != 0) {
|
|
|
|
try output(context, ".");
|
|
|
|
|
|
|
|
var printed: usize = 0;
|
|
|
|
if (float_decimal.digits.len > 1) {
|
|
|
|
const num_digits = math.min(float_decimal.digits.len, precision + 1);
|
2018-05-01 05:53:04 +00:00
|
|
|
try output(context, float_decimal.digits[1..num_digits]);
|
2018-04-17 10:58:10 +00:00
|
|
|
printed += num_digits - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (printed < precision) : (printed += 1) {
|
|
|
|
try output(context, "0");
|
|
|
|
}
|
|
|
|
}
|
2017-08-18 21:54:42 +00:00
|
|
|
} else {
|
2018-04-17 10:58:10 +00:00
|
|
|
try output(context, float_decimal.digits[0..1]);
|
|
|
|
try output(context, ".");
|
|
|
|
if (float_decimal.digits.len > 1) {
|
2019-12-09 20:56:19 +00:00
|
|
|
const num_digits = if (@TypeOf(value) == f32) math.min(@as(usize, 9), float_decimal.digits.len) else float_decimal.digits.len;
|
2018-04-17 10:58:10 +00:00
|
|
|
|
2018-05-01 05:53:04 +00:00
|
|
|
try output(context, float_decimal.digits[1..num_digits]);
|
2018-04-17 10:58:10 +00:00
|
|
|
} else {
|
|
|
|
try output(context, "0");
|
|
|
|
}
|
2017-08-18 21:54:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 10:58:10 +00:00
|
|
|
try output(context, "e");
|
|
|
|
const exp = float_decimal.exp - 1;
|
|
|
|
|
|
|
|
if (exp >= 0) {
|
|
|
|
try output(context, "+");
|
|
|
|
if (exp > -10 and exp < 10) {
|
|
|
|
try output(context, "0");
|
|
|
|
}
|
2019-08-17 23:17:16 +00:00
|
|
|
try formatInt(exp, 10, false, FormatOptions{ .width = 0 }, context, Errors, output);
|
2018-04-17 10:58:10 +00:00
|
|
|
} else {
|
|
|
|
try output(context, "-");
|
|
|
|
if (exp > -10 and exp < 10) {
|
|
|
|
try output(context, "0");
|
|
|
|
}
|
2019-08-17 23:17:16 +00:00
|
|
|
try formatInt(-exp, 10, false, FormatOptions{ .width = 0 }, context, Errors, output);
|
2017-08-18 21:54:42 +00:00
|
|
|
}
|
2017-06-08 02:56:57 +00:00
|
|
|
}
|
2017-04-06 09:34:04 +00:00
|
|
|
|
2018-04-17 10:58:10 +00:00
|
|
|
// Print a float of the format x.yyyyy where the number of y is specified by the precision argument.
|
|
|
|
// By default floats are printed at full precision (no rounding).
|
2018-06-04 15:06:55 +00:00
|
|
|
pub fn formatFloatDecimal(
|
|
|
|
value: var,
|
2019-08-19 10:28:13 +00:00
|
|
|
options: FormatOptions,
|
2018-06-04 15:06:55 +00:00
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2019-12-09 20:56:19 +00:00
|
|
|
output: fn (@TypeOf(context), []const u8) Errors!void,
|
2018-06-04 15:06:55 +00:00
|
|
|
) Errors!void {
|
2019-11-07 04:25:57 +00:00
|
|
|
var x = @as(f64, value);
|
2017-12-30 23:27:58 +00:00
|
|
|
|
|
|
|
// Errol doesn't handle these special cases.
|
|
|
|
if (math.signbit(x)) {
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, "-");
|
2017-12-30 23:27:58 +00:00
|
|
|
x = -x;
|
|
|
|
}
|
2018-04-17 10:58:10 +00:00
|
|
|
|
|
|
|
if (math.isNan(x)) {
|
|
|
|
return output(context, "nan");
|
|
|
|
}
|
2017-12-30 23:27:58 +00:00
|
|
|
if (math.isPositiveInf(x)) {
|
2018-04-17 10:58:10 +00:00
|
|
|
return output(context, "inf");
|
2017-12-30 23:27:58 +00:00
|
|
|
}
|
|
|
|
if (x == 0.0) {
|
2018-04-17 10:58:10 +00:00
|
|
|
try output(context, "0");
|
|
|
|
|
2019-08-17 23:17:16 +00:00
|
|
|
if (options.precision) |precision| {
|
2018-04-17 10:58:10 +00:00
|
|
|
if (precision != 0) {
|
|
|
|
try output(context, ".");
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < precision) : (i += 1) {
|
|
|
|
try output(context, "0");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try output(context, ".0");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try output(context, "0");
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2017-12-30 23:27:58 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 10:58:10 +00:00
|
|
|
// non-special case, use errol3
|
2017-12-30 23:27:58 +00:00
|
|
|
var buffer: [32]u8 = undefined;
|
2018-04-17 10:58:10 +00:00
|
|
|
var float_decimal = errol.errol3(x, buffer[0..]);
|
|
|
|
|
2019-08-17 23:17:16 +00:00
|
|
|
if (options.precision) |precision| {
|
2018-04-17 10:58:10 +00:00
|
|
|
errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Decimal);
|
|
|
|
|
|
|
|
// exp < 0 means the leading is always 0 as errol result is normalized.
|
2018-06-17 06:57:07 +00:00
|
|
|
var num_digits_whole = if (float_decimal.exp > 0) @intCast(usize, float_decimal.exp) else 0;
|
2018-04-17 10:58:10 +00:00
|
|
|
|
|
|
|
// the actual slice into the buffer, we may need to zero-pad between num_digits_whole and this.
|
|
|
|
var num_digits_whole_no_pad = math.min(num_digits_whole, float_decimal.digits.len);
|
|
|
|
|
|
|
|
if (num_digits_whole > 0) {
|
|
|
|
// We may have to zero pad, for instance 1e4 requires zero padding.
|
2018-05-01 05:53:04 +00:00
|
|
|
try output(context, float_decimal.digits[0..num_digits_whole_no_pad]);
|
2018-04-17 10:58:10 +00:00
|
|
|
|
|
|
|
var i = num_digits_whole_no_pad;
|
|
|
|
while (i < num_digits_whole) : (i += 1) {
|
|
|
|
try output(context, "0");
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-01 05:53:04 +00:00
|
|
|
try output(context, "0");
|
2018-04-17 10:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// {.0} special case doesn't want a trailing '.'
|
|
|
|
if (precision == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try output(context, ".");
|
|
|
|
|
|
|
|
// Keep track of fractional count printed for case where we pre-pad then post-pad with 0's.
|
|
|
|
var printed: usize = 0;
|
|
|
|
|
|
|
|
// Zero-fill until we reach significant digits or run out of precision.
|
|
|
|
if (float_decimal.exp <= 0) {
|
2018-06-17 06:57:07 +00:00
|
|
|
const zero_digit_count = @intCast(usize, -float_decimal.exp);
|
2018-04-17 10:58:10 +00:00
|
|
|
const zeros_to_print = math.min(zero_digit_count, precision);
|
|
|
|
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < zeros_to_print) : (i += 1) {
|
|
|
|
try output(context, "0");
|
|
|
|
printed += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (printed >= precision) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remaining fractional portion, zero-padding if insufficient.
|
2019-02-08 23:18:47 +00:00
|
|
|
assert(precision >= printed);
|
2018-04-17 10:58:10 +00:00
|
|
|
if (num_digits_whole_no_pad + precision - printed < float_decimal.digits.len) {
|
2018-05-30 20:09:11 +00:00
|
|
|
try output(context, float_decimal.digits[num_digits_whole_no_pad .. num_digits_whole_no_pad + precision - printed]);
|
2018-04-17 10:58:10 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2018-05-01 05:53:04 +00:00
|
|
|
try output(context, float_decimal.digits[num_digits_whole_no_pad..]);
|
2018-04-17 10:58:10 +00:00
|
|
|
printed += float_decimal.digits.len - num_digits_whole_no_pad;
|
|
|
|
|
|
|
|
while (printed < precision) : (printed += 1) {
|
|
|
|
try output(context, "0");
|
|
|
|
}
|
|
|
|
}
|
2017-12-30 23:27:58 +00:00
|
|
|
} else {
|
2018-04-17 10:58:10 +00:00
|
|
|
// exp < 0 means the leading is always 0 as errol result is normalized.
|
2018-06-17 06:57:07 +00:00
|
|
|
var num_digits_whole = if (float_decimal.exp > 0) @intCast(usize, float_decimal.exp) else 0;
|
2018-04-17 10:58:10 +00:00
|
|
|
|
|
|
|
// the actual slice into the buffer, we may need to zero-pad between num_digits_whole and this.
|
|
|
|
var num_digits_whole_no_pad = math.min(num_digits_whole, float_decimal.digits.len);
|
|
|
|
|
|
|
|
if (num_digits_whole > 0) {
|
|
|
|
// We may have to zero pad, for instance 1e4 requires zero padding.
|
2018-05-01 05:53:04 +00:00
|
|
|
try output(context, float_decimal.digits[0..num_digits_whole_no_pad]);
|
2018-04-17 10:58:10 +00:00
|
|
|
|
|
|
|
var i = num_digits_whole_no_pad;
|
|
|
|
while (i < num_digits_whole) : (i += 1) {
|
|
|
|
try output(context, "0");
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-01 05:53:04 +00:00
|
|
|
try output(context, "0");
|
2018-04-17 10:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Omit `.` if no fractional portion
|
|
|
|
if (float_decimal.exp >= 0 and num_digits_whole_no_pad == float_decimal.digits.len) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try output(context, ".");
|
|
|
|
|
|
|
|
// Zero-fill until we reach significant digits or run out of precision.
|
|
|
|
if (float_decimal.exp < 0) {
|
2018-06-17 06:57:07 +00:00
|
|
|
const zero_digit_count = @intCast(usize, -float_decimal.exp);
|
2018-04-17 10:58:10 +00:00
|
|
|
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < zero_digit_count) : (i += 1) {
|
|
|
|
try output(context, "0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-01 05:53:04 +00:00
|
|
|
try output(context, float_decimal.digits[num_digits_whole_no_pad..]);
|
2017-12-30 23:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 00:23:55 +00:00
|
|
|
pub fn formatBytes(
|
|
|
|
value: var,
|
2019-08-19 10:28:13 +00:00
|
|
|
options: FormatOptions,
|
2018-05-29 00:23:55 +00:00
|
|
|
comptime radix: usize,
|
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2019-12-09 20:56:19 +00:00
|
|
|
output: fn (@TypeOf(context), []const u8) Errors!void,
|
2018-05-29 00:23:55 +00:00
|
|
|
) Errors!void {
|
2018-05-16 02:11:03 +00:00
|
|
|
if (value == 0) {
|
|
|
|
return output(context, "0B");
|
|
|
|
}
|
|
|
|
|
2018-06-09 16:05:58 +00:00
|
|
|
const mags_si = " kMGTPEZY";
|
|
|
|
const mags_iec = " KMGTPEZY";
|
2018-05-16 22:22:39 +00:00
|
|
|
const magnitude = switch (radix) {
|
2018-06-09 16:05:58 +00:00
|
|
|
1000 => math.min(math.log2(value) / comptime math.log2(1000), mags_si.len - 1),
|
|
|
|
1024 => math.min(math.log2(value) / 10, mags_iec.len - 1),
|
2018-05-16 22:22:39 +00:00
|
|
|
else => unreachable,
|
|
|
|
};
|
2018-06-17 06:57:07 +00:00
|
|
|
const new_value = lossyCast(f64, value) / math.pow(f64, lossyCast(f64, radix), lossyCast(f64, magnitude));
|
2018-06-09 16:05:58 +00:00
|
|
|
const suffix = switch (radix) {
|
|
|
|
1000 => mags_si[magnitude],
|
|
|
|
1024 => mags_iec[magnitude],
|
|
|
|
else => unreachable,
|
|
|
|
};
|
2018-05-16 02:11:03 +00:00
|
|
|
|
2019-08-17 23:17:16 +00:00
|
|
|
try formatFloatDecimal(new_value, options, context, Errors, output);
|
2018-05-16 02:11:03 +00:00
|
|
|
|
2018-05-16 22:22:39 +00:00
|
|
|
if (suffix == ' ') {
|
|
|
|
return output(context, "B");
|
2018-05-16 02:11:03 +00:00
|
|
|
}
|
2018-05-16 22:22:39 +00:00
|
|
|
|
|
|
|
const buf = switch (radix) {
|
2019-11-27 08:30:39 +00:00
|
|
|
1000 => &[_]u8{ suffix, 'B' },
|
|
|
|
1024 => &[_]u8{ suffix, 'i', 'B' },
|
2018-05-16 22:22:39 +00:00
|
|
|
else => unreachable,
|
|
|
|
};
|
|
|
|
return output(context, buf);
|
2018-05-16 02:11:03 +00:00
|
|
|
}
|
2017-12-30 23:27:58 +00:00
|
|
|
|
2018-05-29 00:23:55 +00:00
|
|
|
pub fn formatInt(
|
|
|
|
value: var,
|
|
|
|
base: u8,
|
|
|
|
uppercase: bool,
|
2019-08-19 10:28:13 +00:00
|
|
|
options: FormatOptions,
|
2018-05-29 00:23:55 +00:00
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2019-12-09 20:56:19 +00:00
|
|
|
output: fn (@TypeOf(context), []const u8) Errors!void,
|
2018-05-29 00:23:55 +00:00
|
|
|
) Errors!void {
|
2019-12-09 20:56:19 +00:00
|
|
|
const int_value = if (@TypeOf(value) == comptime_int) blk: {
|
2019-04-05 01:36:31 +00:00
|
|
|
const Int = math.IntFittingRange(value, value);
|
2019-11-10 23:08:24 +00:00
|
|
|
break :blk @as(Int, value);
|
2019-04-05 01:36:31 +00:00
|
|
|
} else
|
|
|
|
value;
|
|
|
|
|
2019-12-09 20:56:19 +00:00
|
|
|
if (@TypeOf(int_value).is_signed) {
|
2019-08-17 23:17:16 +00:00
|
|
|
return formatIntSigned(int_value, base, uppercase, options, context, Errors, output);
|
2017-03-10 00:12:15 +00:00
|
|
|
} else {
|
2019-08-17 23:17:16 +00:00
|
|
|
return formatIntUnsigned(int_value, base, uppercase, options, context, Errors, output);
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:06:55 +00:00
|
|
|
fn formatIntSigned(
|
|
|
|
value: var,
|
|
|
|
base: u8,
|
|
|
|
uppercase: bool,
|
2019-08-19 10:28:13 +00:00
|
|
|
options: FormatOptions,
|
2018-06-04 15:06:55 +00:00
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2019-12-09 20:56:19 +00:00
|
|
|
output: fn (@TypeOf(context), []const u8) Errors!void,
|
2018-06-04 15:06:55 +00:00
|
|
|
) Errors!void {
|
2019-08-17 23:17:16 +00:00
|
|
|
const new_options = FormatOptions{
|
|
|
|
.width = if (options.width) |w| (if (w == 0) 0 else w - 1) else null,
|
|
|
|
.precision = options.precision,
|
|
|
|
.fill = options.fill,
|
|
|
|
};
|
|
|
|
|
2019-12-09 20:56:19 +00:00
|
|
|
const uint = @IntType(false, @TypeOf(value).bit_count);
|
2017-03-10 00:12:15 +00:00
|
|
|
if (value < 0) {
|
|
|
|
const minus_sign: u8 = '-';
|
2019-11-07 04:25:57 +00:00
|
|
|
try output(context, @as(*const [1]u8, &minus_sign)[0..]);
|
2018-06-17 06:57:07 +00:00
|
|
|
const new_value = @intCast(uint, -(value + 1)) + 1;
|
2019-08-17 23:17:16 +00:00
|
|
|
return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output);
|
|
|
|
} else if (options.width == null or options.width.? == 0) {
|
|
|
|
return formatIntUnsigned(@intCast(uint, value), base, uppercase, options, context, Errors, output);
|
2017-03-10 00:12:15 +00:00
|
|
|
} else {
|
|
|
|
const plus_sign: u8 = '+';
|
2019-11-07 04:25:57 +00:00
|
|
|
try output(context, @as(*const [1]u8, &plus_sign)[0..]);
|
2018-06-17 06:57:07 +00:00
|
|
|
const new_value = @intCast(uint, value);
|
2019-08-17 23:17:16 +00:00
|
|
|
return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output);
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:06:55 +00:00
|
|
|
fn formatIntUnsigned(
|
|
|
|
value: var,
|
|
|
|
base: u8,
|
|
|
|
uppercase: bool,
|
2019-08-19 10:28:13 +00:00
|
|
|
options: FormatOptions,
|
2018-06-04 15:06:55 +00:00
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2019-12-09 20:56:19 +00:00
|
|
|
output: fn (@TypeOf(context), []const u8) Errors!void,
|
2018-06-04 15:06:55 +00:00
|
|
|
) Errors!void {
|
2019-03-19 14:11:28 +00:00
|
|
|
assert(base >= 2);
|
2019-12-09 20:56:19 +00:00
|
|
|
var buf: [math.max(@TypeOf(value).bit_count, 1)]u8 = undefined;
|
|
|
|
const min_int_bits = comptime math.max(@TypeOf(value).bit_count, @TypeOf(base).bit_count);
|
|
|
|
const MinInt = @IntType(@TypeOf(value).is_signed, min_int_bits);
|
2018-09-20 17:46:20 +00:00
|
|
|
var a: MinInt = value;
|
2017-03-10 00:12:15 +00:00
|
|
|
var index: usize = buf.len;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const digit = a % base;
|
|
|
|
index -= 1;
|
2018-06-17 06:57:07 +00:00
|
|
|
buf[index] = digitToChar(@intCast(u8, digit), uppercase);
|
2017-03-10 00:12:15 +00:00
|
|
|
a /= base;
|
2018-05-01 05:53:04 +00:00
|
|
|
if (a == 0) break;
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 14:39:59 +00:00
|
|
|
const digits_buf = buf[index..];
|
2019-08-17 23:17:16 +00:00
|
|
|
const width = options.width orelse 0;
|
2017-03-10 00:12:15 +00:00
|
|
|
const padding = if (width > digits_buf.len) (width - digits_buf.len) else 0;
|
|
|
|
|
|
|
|
if (padding > index) {
|
2019-08-17 23:17:16 +00:00
|
|
|
const zero_byte: u8 = options.fill;
|
2017-03-10 00:12:15 +00:00
|
|
|
var leftover_padding = padding - index;
|
|
|
|
while (true) {
|
2019-11-07 04:25:57 +00:00
|
|
|
try output(context, @as(*const [1]u8, &zero_byte)[0..]);
|
2017-03-10 00:12:15 +00:00
|
|
|
leftover_padding -= 1;
|
2018-05-01 05:53:04 +00:00
|
|
|
if (leftover_padding == 0) break;
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
2019-08-17 23:17:16 +00:00
|
|
|
mem.set(u8, buf[0..index], options.fill);
|
2019-11-27 08:30:39 +00:00
|
|
|
return output(context, &buf);
|
2017-03-10 00:12:15 +00:00
|
|
|
} else {
|
2018-05-30 20:09:11 +00:00
|
|
|
const padded_buf = buf[index - padding ..];
|
2019-08-17 23:17:16 +00:00
|
|
|
mem.set(u8, padded_buf[0..padding], options.fill);
|
2017-03-10 00:12:15 +00:00
|
|
|
return output(context, padded_buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 10:28:13 +00:00
|
|
|
pub fn formatIntBuf(out_buf: []u8, value: var, base: u8, uppercase: bool, options: FormatOptions) usize {
|
2018-11-13 13:08:37 +00:00
|
|
|
var context = FormatIntBuf{
|
2017-03-10 00:12:15 +00:00
|
|
|
.out_buf = out_buf,
|
|
|
|
.index = 0,
|
|
|
|
};
|
2019-08-17 23:17:16 +00:00
|
|
|
formatInt(value, base, uppercase, options, &context, error{}, formatIntCallback) catch unreachable;
|
2017-03-10 00:12:15 +00:00
|
|
|
return context.index;
|
|
|
|
}
|
2018-11-13 13:08:37 +00:00
|
|
|
const FormatIntBuf = struct {
|
2017-03-10 00:12:15 +00:00
|
|
|
out_buf: []u8,
|
|
|
|
index: usize,
|
|
|
|
};
|
2018-11-13 13:08:37 +00:00
|
|
|
fn formatIntCallback(context: *FormatIntBuf, bytes: []const u8) (error{}!void) {
|
2017-05-19 14:39:59 +00:00
|
|
|
mem.copy(u8, context.out_buf[context.index..], bytes);
|
2017-03-10 00:12:15 +00:00
|
|
|
context.index += bytes.len;
|
|
|
|
}
|
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) !T {
|
2018-05-01 05:53:04 +00:00
|
|
|
if (!T.is_signed) return parseUnsigned(T, buf, radix);
|
2019-11-07 23:52:09 +00:00
|
|
|
if (buf.len == 0) return @as(T, 0);
|
2017-09-26 23:40:51 +00:00
|
|
|
if (buf[0] == '-') {
|
2018-01-07 21:51:46 +00:00
|
|
|
return math.negate(try parseUnsigned(T, buf[1..], radix));
|
2017-09-26 23:40:51 +00:00
|
|
|
} else if (buf[0] == '+') {
|
|
|
|
return parseUnsigned(T, buf[1..], radix);
|
|
|
|
} else {
|
|
|
|
return parseUnsigned(T, buf, radix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 10:03:56 +00:00
|
|
|
test "parseInt" {
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect((parseInt(i32, "-10", 10) catch unreachable) == -10);
|
|
|
|
std.testing.expect((parseInt(i32, "+10", 10) catch unreachable) == 10);
|
|
|
|
std.testing.expect(if (parseInt(i32, " 10", 10)) |_| false else |err| err == error.InvalidCharacter);
|
|
|
|
std.testing.expect(if (parseInt(i32, "10 ", 10)) |_| false else |err| err == error.InvalidCharacter);
|
|
|
|
std.testing.expect(if (parseInt(u32, "-10", 10)) |_| false else |err| err == error.InvalidCharacter);
|
|
|
|
std.testing.expect((parseInt(u8, "255", 10) catch unreachable) == 255);
|
|
|
|
std.testing.expect(if (parseInt(u8, "256", 10)) |_| false else |err| err == error.Overflow);
|
2017-09-26 23:40:51 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 23:44:22 +00:00
|
|
|
pub const ParseUnsignedError = error{
|
2018-02-02 19:26:14 +00:00
|
|
|
/// The result cannot fit in the type specified
|
|
|
|
Overflow,
|
2018-05-01 05:53:04 +00:00
|
|
|
|
2018-02-02 19:26:14 +00:00
|
|
|
/// The input had a byte that was not a digit
|
|
|
|
InvalidCharacter,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) ParseUnsignedError!T {
|
2017-03-10 00:12:15 +00:00
|
|
|
var x: T = 0;
|
|
|
|
|
|
|
|
for (buf) |c| {
|
2018-01-07 21:51:46 +00:00
|
|
|
const digit = try charToDigit(c, radix);
|
2018-11-28 05:17:45 +00:00
|
|
|
|
|
|
|
if (x != 0) x = try math.mul(T, x, try math.cast(T, radix));
|
|
|
|
x = try math.add(T, x, try math.cast(T, digit));
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2019-06-17 10:03:56 +00:00
|
|
|
test "parseUnsigned" {
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124);
|
|
|
|
std.testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535);
|
|
|
|
std.testing.expectError(error.Overflow, parseUnsigned(u16, "65536", 10));
|
2018-11-28 05:17:45 +00:00
|
|
|
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect((try parseUnsigned(u64, "0ffffffffffffffff", 16)) == 0xffffffffffffffff);
|
|
|
|
std.testing.expectError(error.Overflow, parseUnsigned(u64, "10000000000000000", 16));
|
2018-11-28 05:17:45 +00:00
|
|
|
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect((try parseUnsigned(u32, "DeadBeef", 16)) == 0xDEADBEEF);
|
2018-11-28 05:17:45 +00:00
|
|
|
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect((try parseUnsigned(u7, "1", 10)) == 1);
|
|
|
|
std.testing.expect((try parseUnsigned(u7, "1000", 2)) == 8);
|
2018-11-28 05:17:45 +00:00
|
|
|
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expectError(error.InvalidCharacter, parseUnsigned(u32, "f", 10));
|
|
|
|
std.testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "109", 8));
|
2018-11-28 05:17:45 +00:00
|
|
|
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect((try parseUnsigned(u32, "NUMBER", 36)) == 1442151747);
|
2018-11-28 05:17:45 +00:00
|
|
|
|
|
|
|
// these numbers should fit even though the radix itself doesn't fit in the destination type
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect((try parseUnsigned(u1, "0", 10)) == 0);
|
|
|
|
std.testing.expect((try parseUnsigned(u1, "1", 10)) == 1);
|
|
|
|
std.testing.expectError(error.Overflow, parseUnsigned(u1, "2", 10));
|
|
|
|
std.testing.expect((try parseUnsigned(u1, "001", 16)) == 1);
|
|
|
|
std.testing.expect((try parseUnsigned(u2, "3", 16)) == 3);
|
|
|
|
std.testing.expectError(error.Overflow, parseUnsigned(u2, "4", 16));
|
2018-11-28 05:17:45 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 21:46:04 +00:00
|
|
|
pub const parseFloat = @import("fmt/parse_float.zig").parseFloat;
|
2019-02-13 11:06:32 +00:00
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "parseFloat" {
|
2019-03-02 21:46:04 +00:00
|
|
|
_ = @import("fmt/parse_float.zig");
|
2019-02-13 11:06:32 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) {
|
2017-03-10 00:12:15 +00:00
|
|
|
const value = switch (c) {
|
2018-05-29 00:23:55 +00:00
|
|
|
'0'...'9' => c - '0',
|
|
|
|
'A'...'Z' => c - 'A' + 10,
|
|
|
|
'a'...'z' => c - 'a' + 10,
|
2018-02-02 19:26:14 +00:00
|
|
|
else => return error.InvalidCharacter,
|
2017-03-10 00:12:15 +00:00
|
|
|
};
|
|
|
|
|
2018-05-01 05:53:04 +00:00
|
|
|
if (value >= radix) return error.InvalidCharacter;
|
2017-03-10 00:12:15 +00:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn digitToChar(digit: u8, uppercase: bool) u8 {
|
2017-03-10 00:12:15 +00:00
|
|
|
return switch (digit) {
|
2018-05-29 00:23:55 +00:00
|
|
|
0...9 => digit + '0',
|
2019-11-07 04:25:57 +00:00
|
|
|
10...35 => digit + ((if (uppercase) @as(u8, 'A') else @as(u8, 'a')) - 10),
|
2017-03-26 08:58:48 +00:00
|
|
|
else => unreachable,
|
2017-03-10 00:12:15 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const BufPrintContext = struct {
|
2017-04-06 09:34:04 +00:00
|
|
|
remaining: []u8,
|
|
|
|
};
|
|
|
|
|
2018-05-31 14:56:59 +00:00
|
|
|
fn bufPrintWrite(context: *BufPrintContext, bytes: []const u8) !void {
|
2019-10-18 00:20:22 +00:00
|
|
|
if (context.remaining.len < bytes.len) {
|
|
|
|
mem.copy(u8, context.remaining, bytes[0..context.remaining.len]);
|
|
|
|
return error.BufferTooSmall;
|
|
|
|
}
|
2017-04-06 09:34:04 +00:00
|
|
|
mem.copy(u8, context.remaining, bytes);
|
2017-05-19 14:39:59 +00:00
|
|
|
context.remaining = context.remaining[bytes.len..];
|
2017-04-06 09:34:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 00:20:22 +00:00
|
|
|
pub const BufPrintError = error{
|
|
|
|
/// As much as possible was written to the buffer, but it was too small to fit all the printed bytes.
|
|
|
|
BufferTooSmall,
|
|
|
|
};
|
2019-12-09 03:53:51 +00:00
|
|
|
pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: var) BufPrintError![]u8 {
|
2018-11-13 13:08:37 +00:00
|
|
|
var context = BufPrintContext{ .remaining = buf };
|
2019-10-18 00:20:22 +00:00
|
|
|
try format(&context, BufPrintError, bufPrintWrite, fmt, args);
|
2018-05-30 20:09:11 +00:00
|
|
|
return buf[0 .. buf.len - context.remaining.len];
|
2017-04-06 09:34:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
pub const AllocPrintError = error{OutOfMemory};
|
2018-07-17 22:36:47 +00:00
|
|
|
|
2019-12-09 03:53:51 +00:00
|
|
|
pub fn allocPrint(allocator: *mem.Allocator, comptime fmt: []const u8, args: var) AllocPrintError![]u8 {
|
2017-04-06 09:34:04 +00:00
|
|
|
var size: usize = 0;
|
2018-11-13 13:08:37 +00:00
|
|
|
format(&size, error{}, countSize, fmt, args) catch |err| switch (err) {};
|
2018-01-07 21:51:46 +00:00
|
|
|
const buf = try allocator.alloc(u8, size);
|
2018-07-17 22:36:47 +00:00
|
|
|
return bufPrint(buf, fmt, args) catch |err| switch (err) {
|
|
|
|
error.BufferTooSmall => unreachable, // we just counted the size above
|
|
|
|
};
|
2017-04-06 09:34:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
fn countSize(size: *usize, bytes: []const u8) (error{}!void) {
|
2018-05-01 05:53:04 +00:00
|
|
|
size.* += bytes.len;
|
2017-04-06 09:34:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 10:03:56 +00:00
|
|
|
test "bufPrintInt" {
|
2019-03-19 14:11:28 +00:00
|
|
|
var buffer: [100]u8 = undefined;
|
2017-05-19 14:39:59 +00:00
|
|
|
const buf = buffer[0..];
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -12345678), 2, false, FormatOptions{}), "-101111000110000101001110"));
|
|
|
|
std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -12345678), 10, false, FormatOptions{}), "-12345678"));
|
|
|
|
std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -12345678), 16, false, FormatOptions{}), "-bc614e"));
|
|
|
|
std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -12345678), 16, true, FormatOptions{}), "-BC614E"));
|
2017-03-10 00:12:15 +00:00
|
|
|
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(u32, 12345678), 10, true, FormatOptions{}), "12345678"));
|
2017-03-10 00:12:15 +00:00
|
|
|
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(u32, 666), 10, false, FormatOptions{ .width = 6 }), " 666"));
|
|
|
|
std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 6 }), " 1234"));
|
|
|
|
std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 1 }), "1234"));
|
2017-03-10 00:12:15 +00:00
|
|
|
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, 42), 10, false, FormatOptions{ .width = 3 }), "+42"));
|
|
|
|
std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -42), 10, false, FormatOptions{ .width = 3 }), "-42"));
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 10:28:13 +00:00
|
|
|
fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, options: FormatOptions) []u8 {
|
2019-08-17 23:17:16 +00:00
|
|
|
return buf[0..formatIntBuf(buf, value, base, uppercase, options)];
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 03:25:02 +00:00
|
|
|
test "parse u64 digit too big" {
|
2018-01-07 22:28:20 +00:00
|
|
|
_ = parseUnsigned(u64, "123a", 10) catch |err| {
|
2018-02-05 23:09:13 +00:00
|
|
|
if (err == error.InvalidCharacter) return;
|
2017-03-26 08:58:48 +00:00
|
|
|
unreachable;
|
2017-03-10 00:12:15 +00:00
|
|
|
};
|
2017-03-26 08:58:48 +00:00
|
|
|
unreachable;
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 03:25:02 +00:00
|
|
|
test "parse unsigned comptime" {
|
2017-03-10 00:12:15 +00:00
|
|
|
comptime {
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect((try parseUnsigned(usize, "2", 10)) == 2);
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-23 22:38:41 +00:00
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "optional" {
|
2017-05-23 22:38:41 +00:00
|
|
|
{
|
|
|
|
const value: ?i32 = 1234;
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("optional: 1234\n", "optional: {}\n", .{value});
|
2017-05-23 22:38:41 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const value: ?i32 = null;
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("optional: null\n", "optional: {}\n", .{value});
|
2017-05-23 22:38:41 +00:00
|
|
|
}
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "error" {
|
2017-05-23 22:38:41 +00:00
|
|
|
{
|
2018-11-13 13:08:37 +00:00
|
|
|
const value: anyerror!i32 = 1234;
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("error union: 1234\n", "error union: {}\n", .{value});
|
2017-05-23 22:38:41 +00:00
|
|
|
}
|
|
|
|
{
|
2018-11-13 13:08:37 +00:00
|
|
|
const value: anyerror!i32 = error.InvalidChar;
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("error union: error.InvalidChar\n", "error union: {}\n", .{value});
|
2017-05-23 22:38:41 +00:00
|
|
|
}
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "int.small" {
|
2017-10-21 17:03:08 +00:00
|
|
|
{
|
|
|
|
const value: u3 = 0b101;
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("u3: 5\n", "u3: {}\n", .{value});
|
2018-05-16 02:11:03 +00:00
|
|
|
}
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "int.specifier" {
|
2018-05-30 17:18:24 +00:00
|
|
|
{
|
|
|
|
const value: u8 = 'a';
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("u8: a\n", "u8: {c}\n", .{value});
|
2018-05-30 17:18:24 +00:00
|
|
|
}
|
2018-08-01 15:38:04 +00:00
|
|
|
{
|
|
|
|
const value: u8 = 0b1100;
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("u8: 0b1100\n", "u8: 0b{b}\n", .{value});
|
2018-08-01 15:38:04 +00:00
|
|
|
}
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "int.padded" {
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("u8: ' 1'", "u8: '{:4}'", .{@as(u8, 1)});
|
|
|
|
try testFmt("u8: 'xxx1'", "u8: '{:x<4}'", .{@as(u8, 1)});
|
2019-06-20 08:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test "buffer" {
|
2018-12-19 10:50:29 +00:00
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
var context = BufPrintContext{ .remaining = buf1[0..] };
|
2019-06-20 08:07:43 +00:00
|
|
|
try formatType(1234, "", FormatOptions{}, &context, error{BufferTooSmall}, bufPrintWrite, default_max_depth);
|
2018-12-19 10:50:29 +00:00
|
|
|
var res = buf1[0 .. buf1.len - context.remaining.len];
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect(mem.eql(u8, res, "1234"));
|
2018-12-19 10:50:29 +00:00
|
|
|
|
|
|
|
context = BufPrintContext{ .remaining = buf1[0..] };
|
2019-06-20 08:07:43 +00:00
|
|
|
try formatType('a', "c", FormatOptions{}, &context, error{BufferTooSmall}, bufPrintWrite, default_max_depth);
|
2018-12-19 10:50:29 +00:00
|
|
|
res = buf1[0 .. buf1.len - context.remaining.len];
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect(mem.eql(u8, res, "a"));
|
2018-12-19 10:50:29 +00:00
|
|
|
|
|
|
|
context = BufPrintContext{ .remaining = buf1[0..] };
|
2019-06-20 08:07:43 +00:00
|
|
|
try formatType(0b1100, "b", FormatOptions{}, &context, error{BufferTooSmall}, bufPrintWrite, default_max_depth);
|
2018-12-19 10:50:29 +00:00
|
|
|
res = buf1[0 .. buf1.len - context.remaining.len];
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect(mem.eql(u8, res, "1100"));
|
2018-12-19 10:50:29 +00:00
|
|
|
}
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "array" {
|
2018-07-31 15:34:42 +00:00
|
|
|
{
|
2019-11-20 01:29:08 +00:00
|
|
|
const value: [3]u8 = "abc".*;
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("array: abc\n", "array: {}\n", .{value});
|
|
|
|
try testFmt("array: abc\n", "array: {}\n", .{&value});
|
2018-07-31 15:34:42 +00:00
|
|
|
|
|
|
|
var buf: [100]u8 = undefined;
|
|
|
|
try testFmt(
|
2019-12-09 03:53:51 +00:00
|
|
|
try bufPrint(buf[0..], "array: [3]u8@{x}\n", .{@ptrToInt(&value)}),
|
2018-07-31 15:34:42 +00:00
|
|
|
"array: {*}\n",
|
2019-12-09 03:53:51 +00:00
|
|
|
.{&value},
|
2018-07-31 15:34:42 +00:00
|
|
|
);
|
|
|
|
}
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "slice" {
|
2018-07-31 15:34:42 +00:00
|
|
|
{
|
|
|
|
const value: []const u8 = "abc";
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("slice: abc\n", "slice: {}\n", .{value});
|
2018-07-31 15:34:42 +00:00
|
|
|
}
|
2019-05-29 15:23:21 +00:00
|
|
|
{
|
2019-12-15 11:48:35 +00:00
|
|
|
const value = @intToPtr([*]align(1) const []const u8, 0xdeadbeef)[0..0];
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("slice: []const u8@deadbeef\n", "slice: {}\n", .{value});
|
2019-05-29 15:23:21 +00:00
|
|
|
}
|
2019-06-17 10:03:56 +00:00
|
|
|
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("buf: Test \n", "buf: {s:5}\n", .{"Test"});
|
|
|
|
try testFmt("buf: Test\n Other text", "buf: {s}\n Other text", .{"Test"});
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "pointer" {
|
2018-07-31 15:34:42 +00:00
|
|
|
{
|
2019-12-15 11:48:35 +00:00
|
|
|
const value = @intToPtr(*align(1) i32, 0xdeadbeef);
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("pointer: i32@deadbeef\n", "pointer: {}\n", .{value});
|
|
|
|
try testFmt("pointer: i32@deadbeef\n", "pointer: {*}\n", .{value});
|
2018-07-31 15:34:42 +00:00
|
|
|
}
|
2018-12-19 10:50:29 +00:00
|
|
|
{
|
|
|
|
const value = @intToPtr(fn () void, 0xdeadbeef);
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("pointer: fn() void@deadbeef\n", "pointer: {}\n", .{value});
|
2018-12-19 10:50:29 +00:00
|
|
|
}
|
2018-12-19 15:07:35 +00:00
|
|
|
{
|
|
|
|
const value = @intToPtr(fn () void, 0xdeadbeef);
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("pointer: fn() void@deadbeef\n", "pointer: {}\n", .{value});
|
2018-12-19 15:07:35 +00:00
|
|
|
}
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "cstr" {
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("cstr: Test C\n", "cstr: {s}\n", .{"Test C"});
|
|
|
|
try testFmt("cstr: Test C \n", "cstr: {s:10}\n", .{"Test C"});
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "filesize" {
|
2019-09-25 19:57:13 +00:00
|
|
|
if (builtin.os == .linux and builtin.arch == .arm and builtin.abi == .musleabihf) {
|
|
|
|
// TODO https://github.com/ziglang/zig/issues/3289
|
|
|
|
return error.SkipZigTest;
|
|
|
|
}
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("file size: 63MiB\n", "file size: {Bi}\n", .{@as(usize, 63 * 1024 * 1024)});
|
|
|
|
try testFmt("file size: 66.06MB\n", "file size: {B:.2}\n", .{@as(usize, 63 * 1024 * 1024)});
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "struct" {
|
2018-02-07 21:18:48 +00:00
|
|
|
{
|
2018-11-13 13:08:37 +00:00
|
|
|
const Struct = struct {
|
2018-08-20 20:04:03 +00:00
|
|
|
field: u8,
|
2018-02-22 16:53:58 +00:00
|
|
|
};
|
2018-11-13 13:08:37 +00:00
|
|
|
const value = Struct{ .field = 42 };
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", .{value});
|
|
|
|
try testFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", .{&value});
|
2018-02-07 21:18:48 +00:00
|
|
|
}
|
2018-09-20 17:46:20 +00:00
|
|
|
{
|
2018-11-13 13:08:37 +00:00
|
|
|
const Struct = struct {
|
2018-09-20 17:46:20 +00:00
|
|
|
a: u0,
|
|
|
|
b: u1,
|
|
|
|
};
|
2018-11-13 13:08:37 +00:00
|
|
|
const value = Struct{ .a = 0, .b = 1 };
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("struct: Struct{ .a = 0, .b = 1 }\n", "struct: {}\n", .{value});
|
2018-09-20 17:46:20 +00:00
|
|
|
}
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "enum" {
|
2019-06-17 10:03:56 +00:00
|
|
|
const Enum = enum {
|
|
|
|
One,
|
|
|
|
Two,
|
|
|
|
};
|
|
|
|
const value = Enum.Two;
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("enum: Enum.Two\n", "enum: {}\n", .{value});
|
|
|
|
try testFmt("enum: Enum.Two\n", "enum: {}\n", .{&value});
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "float.scientific" {
|
2019-09-25 19:57:13 +00:00
|
|
|
if (builtin.os == .linux and builtin.arch == .arm and builtin.abi == .musleabihf) {
|
|
|
|
// TODO https://github.com/ziglang/zig/issues/3289
|
|
|
|
return error.SkipZigTest;
|
|
|
|
}
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("f32: 1.34000003e+00", "f32: {e}", .{@as(f32, 1.34)});
|
|
|
|
try testFmt("f32: 1.23400001e+01", "f32: {e}", .{@as(f32, 12.34)});
|
|
|
|
try testFmt("f64: -1.234e+11", "f64: {e}", .{@as(f64, -12.34e10)});
|
|
|
|
try testFmt("f64: 9.99996e-40", "f64: {e}", .{@as(f64, 9.999960e-40)});
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "float.scientific.precision" {
|
2019-09-25 19:57:13 +00:00
|
|
|
if (builtin.os == .linux and builtin.arch == .arm and builtin.abi == .musleabihf) {
|
|
|
|
// TODO https://github.com/ziglang/zig/issues/3289
|
|
|
|
return error.SkipZigTest;
|
|
|
|
}
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("f64: 1.40971e-42", "f64: {e:.5}", .{@as(f64, 1.409706e-42)});
|
|
|
|
try testFmt("f64: 1.00000e-09", "f64: {e:.5}", .{@as(f64, @bitCast(f32, @as(u32, 814313563)))});
|
|
|
|
try testFmt("f64: 7.81250e-03", "f64: {e:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1006632960)))});
|
2019-06-20 08:07:43 +00:00
|
|
|
// libc rounds 1.000005e+05 to 1.00000e+05 but zig does 1.00001e+05.
|
|
|
|
// In fact, libc doesn't round a lot of 5 cases up when one past the precision point.
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("f64: 1.00001e+05", "f64: {e:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1203982400)))});
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "float.special" {
|
2019-09-25 19:57:13 +00:00
|
|
|
if (builtin.os == .linux and builtin.arch == .arm and builtin.abi == .musleabihf) {
|
|
|
|
// TODO https://github.com/ziglang/zig/issues/3289
|
|
|
|
return error.SkipZigTest;
|
|
|
|
}
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("f64: nan", "f64: {}", .{math.nan_f64});
|
2019-06-20 08:07:43 +00:00
|
|
|
// negative nan is not defined by IEE 754,
|
|
|
|
// and ARM thus normalizes it to positive nan
|
2019-02-26 20:51:32 +00:00
|
|
|
if (builtin.arch != builtin.Arch.arm) {
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("f64: -nan", "f64: {}", .{-math.nan_f64});
|
2018-04-23 05:18:05 +00:00
|
|
|
}
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("f64: inf", "f64: {}", .{math.inf_f64});
|
|
|
|
try testFmt("f64: -inf", "f64: {}", .{-math.inf_f64});
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "float.decimal" {
|
2019-09-25 19:57:13 +00:00
|
|
|
if (builtin.os == .linux and builtin.arch == .arm and builtin.abi == .musleabihf) {
|
|
|
|
// TODO https://github.com/ziglang/zig/issues/3289
|
|
|
|
return error.SkipZigTest;
|
|
|
|
}
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("f64: 152314000000000000000000000000", "f64: {d}", .{@as(f64, 1.52314e+29)});
|
|
|
|
try testFmt("f32: 1.1", "f32: {d:.1}", .{@as(f32, 1.1234)});
|
|
|
|
try testFmt("f32: 1234.57", "f32: {d:.2}", .{@as(f32, 1234.567)});
|
2019-06-20 08:07:43 +00:00
|
|
|
// -11.1234 is converted to f64 -11.12339... internally (errol3() function takes f64).
|
|
|
|
// -11.12339... is rounded back up to -11.1234
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("f32: -11.1234", "f32: {d:.4}", .{@as(f32, -11.1234)});
|
|
|
|
try testFmt("f32: 91.12345", "f32: {d:.5}", .{@as(f32, 91.12345)});
|
|
|
|
try testFmt("f64: 91.1234567890", "f64: {d:.10}", .{@as(f64, 91.12345678901235)});
|
|
|
|
try testFmt("f64: 0.00000", "f64: {d:.5}", .{@as(f64, 0.0)});
|
|
|
|
try testFmt("f64: 6", "f64: {d:.0}", .{@as(f64, 5.700)});
|
|
|
|
try testFmt("f64: 10.0", "f64: {d:.1}", .{@as(f64, 9.999)});
|
|
|
|
try testFmt("f64: 1.000", "f64: {d:.3}", .{@as(f64, 1.0)});
|
|
|
|
try testFmt("f64: 0.00030000", "f64: {d:.8}", .{@as(f64, 0.0003)});
|
|
|
|
try testFmt("f64: 0.00000", "f64: {d:.5}", .{@as(f64, 1.40130e-45)});
|
|
|
|
try testFmt("f64: 0.00000", "f64: {d:.5}", .{@as(f64, 9.999960e-40)});
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "float.libc.sanity" {
|
2019-09-25 19:57:13 +00:00
|
|
|
if (builtin.os == .linux and builtin.arch == .arm and builtin.abi == .musleabihf) {
|
|
|
|
// TODO https://github.com/ziglang/zig/issues/3289
|
|
|
|
return error.SkipZigTest;
|
|
|
|
}
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("f64: 0.00001", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 916964781)))});
|
|
|
|
try testFmt("f64: 0.00001", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 925353389)))});
|
|
|
|
try testFmt("f64: 0.10000", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1036831278)))});
|
|
|
|
try testFmt("f64: 1.00000", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1065353133)))});
|
|
|
|
try testFmt("f64: 10.00000", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1092616192)))});
|
2019-06-20 08:07:43 +00:00
|
|
|
|
2018-04-23 05:18:05 +00:00
|
|
|
// libc differences
|
2019-06-20 08:07:43 +00:00
|
|
|
//
|
|
|
|
// This is 0.015625 exactly according to gdb. We thus round down,
|
|
|
|
// however glibc rounds up for some reason. This occurs for all
|
|
|
|
// floats of the form x.yyyy25 on a precision point.
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("f64: 0.01563", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1015021568)))});
|
2019-06-20 08:07:43 +00:00
|
|
|
// errol3 rounds to ... 630 but libc rounds to ...632. Grisu3
|
|
|
|
// also rounds to 630 so I'm inclined to believe libc is not
|
|
|
|
// optimal here.
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("f64: 18014400656965630.00000", "f64: {d:.5}", .{@as(f64, @bitCast(f32, @as(u32, 1518338049)))});
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "custom" {
|
2019-06-17 10:03:56 +00:00
|
|
|
const Vec2 = struct {
|
|
|
|
const SelfType = @This();
|
|
|
|
x: f32,
|
|
|
|
y: f32,
|
|
|
|
|
|
|
|
pub fn format(
|
|
|
|
self: SelfType,
|
|
|
|
comptime fmt: []const u8,
|
2019-08-19 10:28:13 +00:00
|
|
|
options: FormatOptions,
|
2019-06-17 10:03:56 +00:00
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2019-12-09 20:56:19 +00:00
|
|
|
output: fn (@TypeOf(context), []const u8) Errors!void,
|
2019-06-17 10:03:56 +00:00
|
|
|
) Errors!void {
|
2019-06-20 08:07:43 +00:00
|
|
|
if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "p")) {
|
2019-12-09 03:53:51 +00:00
|
|
|
return std.fmt.format(context, Errors, output, "({d:.3},{d:.3})", .{ self.x, self.y });
|
2019-06-20 08:07:43 +00:00
|
|
|
} else if (comptime std.mem.eql(u8, fmt, "d")) {
|
2019-12-09 03:53:51 +00:00
|
|
|
return std.fmt.format(context, Errors, output, "{d:.3}x{d:.3}", .{ self.x, self.y });
|
2019-06-20 08:07:43 +00:00
|
|
|
} else {
|
|
|
|
@compileError("Unknown format character: '" ++ fmt ++ "'");
|
2018-05-30 15:18:11 +00:00
|
|
|
}
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
};
|
2018-06-04 15:06:55 +00:00
|
|
|
|
2019-06-17 10:03:56 +00:00
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
var value = Vec2{
|
|
|
|
.x = 10.2,
|
|
|
|
.y = 2.22,
|
|
|
|
};
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("point: (10.200,2.220)\n", "point: {}\n", .{&value});
|
|
|
|
try testFmt("dim: 10.200x2.220\n", "dim: {d}\n", .{&value});
|
2018-08-20 20:04:03 +00:00
|
|
|
|
2019-06-17 10:03:56 +00:00
|
|
|
// same thing but not passing a pointer
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("point: (10.200,2.220)\n", "point: {}\n", .{value});
|
|
|
|
try testFmt("dim: 10.200x2.220\n", "dim: {d}\n", .{value});
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
2018-08-27 23:25:18 +00:00
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "struct" {
|
2019-06-17 10:03:56 +00:00
|
|
|
const S = struct {
|
|
|
|
a: u32,
|
|
|
|
b: anyerror,
|
|
|
|
};
|
2018-08-27 23:25:18 +00:00
|
|
|
|
2019-06-17 10:03:56 +00:00
|
|
|
const inst = S{
|
|
|
|
.a = 456,
|
|
|
|
.b = error.Unused,
|
|
|
|
};
|
2018-08-27 23:25:18 +00:00
|
|
|
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("S{ .a = 456, .b = error.Unused }", "{}", .{inst});
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
2018-08-27 23:25:18 +00:00
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "union" {
|
2019-06-17 10:03:56 +00:00
|
|
|
const TU = union(enum) {
|
|
|
|
float: f32,
|
|
|
|
int: u32,
|
|
|
|
};
|
2018-08-27 23:25:18 +00:00
|
|
|
|
2019-06-17 10:03:56 +00:00
|
|
|
const UU = union {
|
|
|
|
float: f32,
|
|
|
|
int: u32,
|
|
|
|
};
|
2018-08-27 23:25:18 +00:00
|
|
|
|
2019-06-17 10:03:56 +00:00
|
|
|
const EU = extern union {
|
|
|
|
float: f32,
|
|
|
|
int: u32,
|
|
|
|
};
|
2018-08-27 23:25:18 +00:00
|
|
|
|
2019-06-17 10:03:56 +00:00
|
|
|
const tu_inst = TU{ .int = 123 };
|
|
|
|
const uu_inst = UU{ .int = 456 };
|
|
|
|
const eu_inst = EU{ .float = 321.123 };
|
2018-08-27 23:25:18 +00:00
|
|
|
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("TU{ .int = 123 }", "{}", .{tu_inst});
|
2018-08-27 23:25:18 +00:00
|
|
|
|
2019-06-17 10:03:56 +00:00
|
|
|
var buf: [100]u8 = undefined;
|
2019-12-09 03:53:51 +00:00
|
|
|
const uu_result = try bufPrint(buf[0..], "{}", .{uu_inst});
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect(mem.eql(u8, uu_result[0..3], "UU@"));
|
2018-08-27 23:25:18 +00:00
|
|
|
|
2019-12-09 03:53:51 +00:00
|
|
|
const eu_result = try bufPrint(buf[0..], "{}", .{eu_inst});
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect(mem.eql(u8, uu_result[0..3], "EU@"));
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
2019-04-27 09:36:48 +00:00
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "enum" {
|
2019-06-17 10:03:56 +00:00
|
|
|
const E = enum {
|
|
|
|
One,
|
|
|
|
Two,
|
|
|
|
Three,
|
|
|
|
};
|
2019-04-27 09:36:48 +00:00
|
|
|
|
2019-06-17 10:03:56 +00:00
|
|
|
const inst = E.Two;
|
|
|
|
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("E.Two", "{}", .{inst});
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "struct.self-referential" {
|
2019-06-17 10:03:56 +00:00
|
|
|
const S = struct {
|
|
|
|
const SelfType = @This();
|
|
|
|
a: ?*SelfType,
|
|
|
|
};
|
|
|
|
|
|
|
|
var inst = S{
|
|
|
|
.a = null,
|
|
|
|
};
|
|
|
|
inst.a = &inst;
|
|
|
|
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("S{ .a = S{ .a = S{ .a = S{ ... } } } }", "{}", .{inst});
|
2019-06-17 10:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 01:37:35 +00:00
|
|
|
test "struct.zero-size" {
|
|
|
|
const A = struct {
|
|
|
|
fn foo() void {}
|
|
|
|
};
|
|
|
|
const B = struct {
|
|
|
|
a: A,
|
|
|
|
c: i32,
|
|
|
|
};
|
|
|
|
|
|
|
|
const a = A{};
|
|
|
|
const b = B{ .a = a, .c = 0 };
|
|
|
|
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("B{ .a = A{ }, .c = 0 }", "{}", .{b});
|
2019-07-29 01:37:35 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "bytes.hex" {
|
2019-06-17 10:03:56 +00:00
|
|
|
const some_bytes = "\xCA\xFE\xBA\xBE";
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", .{some_bytes});
|
|
|
|
try testFmt("uppercase: CAFEBABE\n", "uppercase: {X}\n", .{some_bytes});
|
2019-06-17 10:03:56 +00:00
|
|
|
//Test Slices
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("uppercase: CAFE\n", "uppercase: {X}\n", .{some_bytes[0..2]});
|
|
|
|
try testFmt("lowercase: babe\n", "lowercase: {x}\n", .{some_bytes[2..]});
|
2019-06-17 10:03:56 +00:00
|
|
|
const bytes_with_zeros = "\x00\x0E\xBA\xBE";
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("lowercase: 000ebabe\n", "lowercase: {x}\n", .{bytes_with_zeros});
|
2017-05-23 22:38:41 +00:00
|
|
|
}
|
2017-09-26 07:03:12 +00:00
|
|
|
|
2019-12-09 03:53:51 +00:00
|
|
|
fn testFmt(expected: []const u8, comptime template: []const u8, args: var) !void {
|
2018-05-16 22:22:39 +00:00
|
|
|
var buf: [100]u8 = undefined;
|
|
|
|
const result = try bufPrint(buf[0..], template, args);
|
2018-05-29 00:23:55 +00:00
|
|
|
if (mem.eql(u8, result, expected)) return;
|
2018-05-16 22:22:39 +00:00
|
|
|
|
2019-12-09 03:53:51 +00:00
|
|
|
std.debug.warn("\n====== expected this output: =========\n", .{});
|
|
|
|
std.debug.warn("{}", .{expected});
|
|
|
|
std.debug.warn("\n======== instead found this: =========\n", .{});
|
|
|
|
std.debug.warn("{}", .{result});
|
|
|
|
std.debug.warn("\n======================================\n", .{});
|
2018-05-16 22:22:39 +00:00
|
|
|
return error.TestFailed;
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn trim(buf: []const u8) []const u8 {
|
2017-09-26 07:03:12 +00:00
|
|
|
var start: usize = 0;
|
2018-05-01 05:53:04 +00:00
|
|
|
while (start < buf.len and isWhiteSpace(buf[start])) : (start += 1) {}
|
2017-09-26 07:03:12 +00:00
|
|
|
|
|
|
|
var end: usize = buf.len;
|
|
|
|
while (true) {
|
|
|
|
if (end > start) {
|
|
|
|
const new_end = end - 1;
|
|
|
|
if (isWhiteSpace(buf[new_end])) {
|
|
|
|
end = new_end;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return buf[start..end];
|
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "trim" {
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect(mem.eql(u8, "abc", trim("\n abc \t")));
|
|
|
|
std.testing.expect(mem.eql(u8, "", trim(" ")));
|
|
|
|
std.testing.expect(mem.eql(u8, "", trim("")));
|
|
|
|
std.testing.expect(mem.eql(u8, "abc", trim(" abc")));
|
|
|
|
std.testing.expect(mem.eql(u8, "abc", trim("abc ")));
|
2017-09-26 07:03:12 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn isWhiteSpace(byte: u8) bool {
|
2017-09-26 07:03:12 +00:00
|
|
|
return switch (byte) {
|
2018-05-29 00:23:55 +00:00
|
|
|
' ', '\t', '\n', '\r' => true,
|
2017-09-26 07:03:12 +00:00
|
|
|
else => false,
|
|
|
|
};
|
|
|
|
}
|
2018-09-02 09:02:24 +00:00
|
|
|
|
2018-09-02 23:23:30 +00:00
|
|
|
pub fn hexToBytes(out: []u8, input: []const u8) !void {
|
|
|
|
if (out.len * 2 < input.len)
|
|
|
|
return error.InvalidLength;
|
2018-09-02 09:02:24 +00:00
|
|
|
|
2018-09-02 23:23:30 +00:00
|
|
|
var in_i: usize = 0;
|
|
|
|
while (in_i != input.len) : (in_i += 2) {
|
|
|
|
const hi = try charToDigit(input[in_i], 16);
|
|
|
|
const lo = try charToDigit(input[in_i + 1], 16);
|
|
|
|
out[in_i / 2] = (hi << 4) | lo;
|
|
|
|
}
|
2018-09-02 09:02:24 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "hexToBytes" {
|
2018-09-02 23:23:30 +00:00
|
|
|
const test_hex_str = "909A312BB12ED1F819B3521AC4C1E896F2160507FFC1C8381E3B07BB16BD1706";
|
|
|
|
var pb: [32]u8 = undefined;
|
|
|
|
try hexToBytes(pb[0..], test_hex_str);
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt(test_hex_str, "{X}", .{pb});
|
2018-09-02 09:02:24 +00:00
|
|
|
}
|
2019-04-05 01:36:31 +00:00
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "formatIntValue with comptime_int" {
|
2019-04-05 01:36:31 +00:00
|
|
|
const value: comptime_int = 123456789123456789;
|
|
|
|
|
|
|
|
var buf = try std.Buffer.init(std.debug.global_allocator, "");
|
2019-12-09 20:56:19 +00:00
|
|
|
try formatIntValue(value, "", FormatOptions{}, &buf, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append);
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect(mem.eql(u8, buf.toSlice(), "123456789123456789"));
|
2019-04-05 01:36:31 +00:00
|
|
|
}
|
2019-04-27 09:36:48 +00:00
|
|
|
|
2019-06-20 08:07:43 +00:00
|
|
|
test "formatType max_depth" {
|
2019-04-27 09:36:48 +00:00
|
|
|
const Vec2 = struct {
|
|
|
|
const SelfType = @This();
|
|
|
|
x: f32,
|
|
|
|
y: f32,
|
|
|
|
|
|
|
|
pub fn format(
|
|
|
|
self: SelfType,
|
|
|
|
comptime fmt: []const u8,
|
2019-08-19 10:28:13 +00:00
|
|
|
options: FormatOptions,
|
2019-04-27 09:36:48 +00:00
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2019-12-09 20:56:19 +00:00
|
|
|
output: fn (@TypeOf(context), []const u8) Errors!void,
|
2019-04-27 09:36:48 +00:00
|
|
|
) Errors!void {
|
2019-06-20 08:07:43 +00:00
|
|
|
if (fmt.len == 0) {
|
2019-12-09 03:53:51 +00:00
|
|
|
return std.fmt.format(context, Errors, output, "({d:.3},{d:.3})", .{ self.x, self.y });
|
2019-06-20 08:07:43 +00:00
|
|
|
} else {
|
|
|
|
@compileError("Unknown format string: '" ++ fmt ++ "'");
|
|
|
|
}
|
2019-04-27 09:36:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
const E = enum {
|
|
|
|
One,
|
|
|
|
Two,
|
|
|
|
Three,
|
|
|
|
};
|
|
|
|
const TU = union(enum) {
|
|
|
|
const SelfType = @This();
|
|
|
|
float: f32,
|
|
|
|
int: u32,
|
|
|
|
ptr: ?*SelfType,
|
|
|
|
};
|
|
|
|
const S = struct {
|
|
|
|
const SelfType = @This();
|
|
|
|
a: ?*SelfType,
|
|
|
|
tu: TU,
|
|
|
|
e: E,
|
|
|
|
vec: Vec2,
|
|
|
|
};
|
|
|
|
|
|
|
|
var inst = S{
|
|
|
|
.a = null,
|
|
|
|
.tu = TU{ .ptr = null },
|
|
|
|
.e = E.Two,
|
|
|
|
.vec = Vec2{ .x = 10.2, .y = 2.22 },
|
|
|
|
};
|
|
|
|
inst.a = &inst;
|
|
|
|
inst.tu.ptr = &inst.tu;
|
|
|
|
|
|
|
|
var buf0 = try std.Buffer.init(std.debug.global_allocator, "");
|
2019-12-09 20:56:19 +00:00
|
|
|
try formatType(inst, "", FormatOptions{}, &buf0, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 0);
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect(mem.eql(u8, buf0.toSlice(), "S{ ... }"));
|
2019-04-27 09:36:48 +00:00
|
|
|
|
|
|
|
var buf1 = try std.Buffer.init(std.debug.global_allocator, "");
|
2019-12-09 20:56:19 +00:00
|
|
|
try formatType(inst, "", FormatOptions{}, &buf1, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 1);
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect(mem.eql(u8, buf1.toSlice(), "S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }"));
|
2019-04-27 09:36:48 +00:00
|
|
|
|
|
|
|
var buf2 = try std.Buffer.init(std.debug.global_allocator, "");
|
2019-12-09 20:56:19 +00:00
|
|
|
try formatType(inst, "", FormatOptions{}, &buf2, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 2);
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect(mem.eql(u8, buf2.toSlice(), "S{ .a = S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ ... } }, .e = E.Two, .vec = (10.200,2.220) }"));
|
2019-04-27 09:36:48 +00:00
|
|
|
|
|
|
|
var buf3 = try std.Buffer.init(std.debug.global_allocator, "");
|
2019-12-09 20:56:19 +00:00
|
|
|
try formatType(inst, "", FormatOptions{}, &buf3, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 3);
|
2019-11-20 01:29:08 +00:00
|
|
|
std.testing.expect(mem.eql(u8, buf3.toSlice(), "S{ .a = S{ .a = S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ ... } }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ .ptr = TU{ ... } } }, .e = E.Two, .vec = (10.200,2.220) }"));
|
2019-04-27 09:36:48 +00:00
|
|
|
}
|
2019-06-20 08:07:43 +00:00
|
|
|
|
|
|
|
test "positional" {
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("2 1 0", "{2} {1} {0}", .{ @as(usize, 0), @as(usize, 1), @as(usize, 2) });
|
|
|
|
try testFmt("2 1 0", "{2} {1} {}", .{ @as(usize, 0), @as(usize, 1), @as(usize, 2) });
|
|
|
|
try testFmt("0 0", "{0} {0}", .{@as(usize, 0)});
|
|
|
|
try testFmt("0 1", "{} {1}", .{ @as(usize, 0), @as(usize, 1) });
|
|
|
|
try testFmt("1 0 0 1", "{1} {} {0} {}", .{ @as(usize, 0), @as(usize, 1) });
|
2019-06-20 08:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test "positional with specifier" {
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("10.0", "{0d:.1}", .{@as(f64, 9.999)});
|
2019-06-20 08:07:43 +00:00
|
|
|
}
|
2019-06-25 05:11:04 +00:00
|
|
|
|
|
|
|
test "positional/alignment/width/precision" {
|
2019-12-09 03:53:51 +00:00
|
|
|
try testFmt("10.0", "{0d: >3.1}", .{@as(f64, 9.999)});
|
2019-06-25 05:11:04 +00:00
|
|
|
}
|