2017-12-24 03:08:53 +00:00
|
|
|
const std = @import("../index.zig");
|
|
|
|
const math = std.math;
|
|
|
|
const debug = std.debug;
|
2017-03-10 00:12:15 +00:00
|
|
|
const assert = 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");
|
2017-06-08 02:56:57 +00:00
|
|
|
const errol3 = @import("errol/index.zig").errol3;
|
2017-03-10 00:12:15 +00:00
|
|
|
|
|
|
|
const max_int_digits = 65;
|
|
|
|
|
|
|
|
const State = enum { // TODO put inside format function and make sure the name and debug info is correct
|
|
|
|
Start,
|
|
|
|
OpenBrace,
|
|
|
|
CloseBrace,
|
|
|
|
Integer,
|
|
|
|
IntegerWidth,
|
2017-12-30 23:27:58 +00:00
|
|
|
Float,
|
|
|
|
FloatWidth,
|
2017-03-10 00:12:15 +00:00
|
|
|
Character,
|
2017-04-06 09:34:04 +00:00
|
|
|
Buf,
|
|
|
|
BufWidth,
|
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.
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void,
|
|
|
|
comptime fmt: []const u8, args: ...) Errors!void
|
2017-03-10 00:12:15 +00:00
|
|
|
{
|
|
|
|
comptime var start_index = 0;
|
|
|
|
comptime var state = State.Start;
|
|
|
|
comptime var next_arg = 0;
|
|
|
|
comptime var radix = 0;
|
|
|
|
comptime var uppercase = false;
|
|
|
|
comptime var width = 0;
|
|
|
|
comptime var width_start = 0;
|
|
|
|
|
|
|
|
inline for (fmt) |c, i| {
|
|
|
|
switch (state) {
|
|
|
|
State.Start => switch (c) {
|
|
|
|
'{' => {
|
|
|
|
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
|
|
|
}
|
|
|
|
state = State.OpenBrace;
|
|
|
|
},
|
|
|
|
'}' => {
|
|
|
|
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
|
|
|
}
|
|
|
|
state = State.CloseBrace;
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
},
|
|
|
|
State.OpenBrace => switch (c) {
|
|
|
|
'{' => {
|
|
|
|
state = State.Start;
|
|
|
|
start_index = i;
|
|
|
|
},
|
|
|
|
'}' => {
|
2018-02-01 03:48:40 +00:00
|
|
|
try formatValue(args[next_arg], context, Errors, output);
|
2017-03-10 00:12:15 +00:00
|
|
|
next_arg += 1;
|
|
|
|
state = State.Start;
|
|
|
|
start_index = i + 1;
|
|
|
|
},
|
|
|
|
'd' => {
|
|
|
|
radix = 10;
|
|
|
|
uppercase = false;
|
|
|
|
width = 0;
|
|
|
|
state = State.Integer;
|
|
|
|
},
|
|
|
|
'x' => {
|
|
|
|
radix = 16;
|
|
|
|
uppercase = false;
|
|
|
|
width = 0;
|
|
|
|
state = State.Integer;
|
|
|
|
},
|
|
|
|
'X' => {
|
|
|
|
radix = 16;
|
|
|
|
uppercase = true;
|
|
|
|
width = 0;
|
|
|
|
state = State.Integer;
|
|
|
|
},
|
|
|
|
'c' => {
|
|
|
|
state = State.Character;
|
|
|
|
},
|
2017-04-06 09:34:04 +00:00
|
|
|
's' => {
|
|
|
|
state = State.Buf;
|
2017-12-30 23:27:58 +00:00
|
|
|
},'.' => {
|
|
|
|
state = State.Float;
|
2017-04-06 09:34:04 +00:00
|
|
|
},
|
2017-03-10 00:12:15 +00:00
|
|
|
else => @compileError("Unknown format character: " ++ []u8{c}),
|
|
|
|
},
|
2017-04-06 09:34:04 +00:00
|
|
|
State.Buf => switch (c) {
|
|
|
|
'}' => {
|
|
|
|
return output(context, args[next_arg]);
|
|
|
|
},
|
|
|
|
'0' ... '9' => {
|
|
|
|
width_start = i;
|
|
|
|
state = State.BufWidth;
|
|
|
|
},
|
|
|
|
else => @compileError("Unexpected character in format string: " ++ []u8{c}),
|
|
|
|
},
|
2017-03-10 00:12:15 +00:00
|
|
|
State.CloseBrace => switch (c) {
|
|
|
|
'}' => {
|
|
|
|
state = State.Start;
|
|
|
|
start_index = i;
|
|
|
|
},
|
|
|
|
else => @compileError("Single '}' encountered in format string"),
|
|
|
|
},
|
|
|
|
State.Integer => switch (c) {
|
|
|
|
'}' => {
|
2018-02-01 03:48:40 +00:00
|
|
|
try formatInt(args[next_arg], radix, uppercase, width, context, Errors, output);
|
2017-03-10 00:12:15 +00:00
|
|
|
next_arg += 1;
|
|
|
|
state = State.Start;
|
|
|
|
start_index = i + 1;
|
|
|
|
},
|
|
|
|
'0' ... '9' => {
|
|
|
|
width_start = i;
|
|
|
|
state = State.IntegerWidth;
|
|
|
|
},
|
|
|
|
else => @compileError("Unexpected character in format string: " ++ []u8{c}),
|
|
|
|
},
|
|
|
|
State.IntegerWidth => switch (c) {
|
|
|
|
'}' => {
|
2018-01-09 05:07:01 +00:00
|
|
|
width = comptime (parseUnsigned(usize, fmt[width_start..i], 10) catch unreachable);
|
2018-02-01 03:48:40 +00:00
|
|
|
try formatInt(args[next_arg], radix, uppercase, width, context, Errors, output);
|
2017-03-10 00:12:15 +00:00
|
|
|
next_arg += 1;
|
|
|
|
state = State.Start;
|
|
|
|
start_index = i + 1;
|
|
|
|
},
|
|
|
|
'0' ... '9' => {},
|
|
|
|
else => @compileError("Unexpected character in format string: " ++ []u8{c}),
|
|
|
|
},
|
2017-12-30 23:27:58 +00:00
|
|
|
State.Float => switch (c) {
|
|
|
|
'}' => {
|
2018-02-01 03:48:40 +00:00
|
|
|
try formatFloatDecimal(args[next_arg], 0, context, Errors, output);
|
2017-12-30 23:27:58 +00:00
|
|
|
next_arg += 1;
|
|
|
|
state = State.Start;
|
|
|
|
start_index = i + 1;
|
|
|
|
},
|
|
|
|
'0' ... '9' => {
|
|
|
|
width_start = i;
|
|
|
|
state = State.FloatWidth;
|
|
|
|
},
|
|
|
|
else => @compileError("Unexpected character in format string: " ++ []u8{c}),
|
|
|
|
},
|
|
|
|
State.FloatWidth => switch (c) {
|
|
|
|
'}' => {
|
2018-01-09 05:07:01 +00:00
|
|
|
width = comptime (parseUnsigned(usize, fmt[width_start..i], 10) catch unreachable);
|
2018-02-01 03:48:40 +00:00
|
|
|
try formatFloatDecimal(args[next_arg], width, context, Errors, output);
|
2017-12-30 23:27:58 +00:00
|
|
|
next_arg += 1;
|
|
|
|
state = State.Start;
|
|
|
|
start_index = i + 1;
|
|
|
|
},
|
|
|
|
'0' ... '9' => {},
|
|
|
|
else => @compileError("Unexpected character in format string: " ++ []u8{c}),
|
|
|
|
},
|
2017-04-06 09:34:04 +00:00
|
|
|
State.BufWidth => switch (c) {
|
|
|
|
'}' => {
|
2018-01-09 05:07:01 +00:00
|
|
|
width = comptime (parseUnsigned(usize, fmt[width_start..i], 10) catch unreachable);
|
2018-02-01 03:48:40 +00:00
|
|
|
try formatBuf(args[next_arg], width, context, Errors, output);
|
2017-04-06 09:34:04 +00:00
|
|
|
next_arg += 1;
|
|
|
|
state = State.Start;
|
|
|
|
start_index = i + 1;
|
|
|
|
},
|
|
|
|
'0' ... '9' => {},
|
|
|
|
else => @compileError("Unexpected character in format string: " ++ []u8{c}),
|
|
|
|
},
|
2017-03-10 00:12:15 +00:00
|
|
|
State.Character => switch (c) {
|
|
|
|
'}' => {
|
2018-02-01 03:48:40 +00:00
|
|
|
try formatAsciiChar(args[next_arg], context, Errors, output);
|
2017-03-10 00:12:15 +00:00
|
|
|
next_arg += 1;
|
|
|
|
state = State.Start;
|
|
|
|
start_index = i + 1;
|
|
|
|
},
|
|
|
|
else => @compileError("Unexpected character in format string: " ++ []u8{c}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
comptime {
|
|
|
|
if (args.len != next_arg) {
|
|
|
|
@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-02-01 03:48:40 +00:00
|
|
|
pub fn formatValue(value: var, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void {
|
2017-03-10 00:12:15 +00:00
|
|
|
const T = @typeOf(value);
|
2017-05-17 16:26:35 +00:00
|
|
|
switch (@typeId(T)) {
|
|
|
|
builtin.TypeId.Int => {
|
2018-02-02 19:26:14 +00:00
|
|
|
return formatInt(value, 10, false, 0, context, Errors, output);
|
2017-05-17 16:26:35 +00:00
|
|
|
},
|
|
|
|
builtin.TypeId.Float => {
|
2018-02-03 16:51:29 +00:00
|
|
|
return formatFloat(value, context, Errors, output);
|
2017-05-17 16:26:35 +00:00
|
|
|
},
|
|
|
|
builtin.TypeId.Void => {
|
|
|
|
return output(context, "void");
|
|
|
|
},
|
|
|
|
builtin.TypeId.Bool => {
|
|
|
|
return output(context, if (value) "true" else "false");
|
|
|
|
},
|
2017-05-23 22:38:41 +00:00
|
|
|
builtin.TypeId.Nullable => {
|
|
|
|
if (value) |payload| {
|
2018-02-01 03:48:40 +00:00
|
|
|
return formatValue(payload, context, Errors, output);
|
2017-05-23 22:38:41 +00:00
|
|
|
} else {
|
|
|
|
return output(context, "null");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
builtin.TypeId.ErrorUnion => {
|
|
|
|
if (value) |payload| {
|
2018-02-01 03:48:40 +00:00
|
|
|
return formatValue(payload, context, Errors, output);
|
2017-05-23 22:38:41 +00:00
|
|
|
} else |err| {
|
2018-02-01 03:48:40 +00:00
|
|
|
return formatValue(err, context, Errors, output);
|
2017-05-23 22:38:41 +00:00
|
|
|
}
|
|
|
|
},
|
2018-02-01 15:23:25 +00:00
|
|
|
builtin.TypeId.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));
|
|
|
|
},
|
2017-09-09 23:42:31 +00:00
|
|
|
builtin.TypeId.Pointer => {
|
2017-09-13 18:30:57 +00:00
|
|
|
if (@typeId(T.Child) == builtin.TypeId.Array and T.Child.Child == u8) {
|
2017-09-09 23:42:31 +00:00
|
|
|
return output(context, (*value)[0..]);
|
|
|
|
} else {
|
|
|
|
@compileError("Unable to format type '" ++ @typeName(T) ++ "'");
|
|
|
|
}
|
|
|
|
},
|
2017-05-17 16:26:35 +00:00
|
|
|
else => if (@canImplicitCast([]const u8, value)) {
|
|
|
|
const casted_value = ([]const u8)(value);
|
|
|
|
return output(context, casted_value);
|
|
|
|
} else {
|
|
|
|
@compileError("Unable to format type '" ++ @typeName(T) ++ "'");
|
|
|
|
},
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn formatAsciiChar(c: u8, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void {
|
2017-05-19 14:39:59 +00:00
|
|
|
return output(context, (&c)[0..1]);
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 09:34:04 +00:00
|
|
|
pub fn formatBuf(buf: []const u8, width: usize,
|
2018-02-01 03:48:40 +00:00
|
|
|
context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void
|
2017-04-06 09:34:04 +00:00
|
|
|
{
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, buf);
|
2017-04-06 09:34:04 +00:00
|
|
|
|
2017-10-31 08:47:55 +00:00
|
|
|
var leftover_padding = if (width > buf.len) (width - buf.len) else return;
|
2017-04-06 09:34:04 +00:00
|
|
|
const pad_byte: u8 = ' ';
|
2017-05-03 22:12:07 +00:00
|
|
|
while (leftover_padding > 0) : (leftover_padding -= 1) {
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, (&pad_byte)[0..1]);
|
2017-04-06 09:34:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn formatFloat(value: var, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void {
|
2017-10-23 20:40:49 +00:00
|
|
|
var x = f64(value);
|
|
|
|
|
|
|
|
// Errol doesn't handle these special cases.
|
|
|
|
if (math.isNan(x)) {
|
|
|
|
return output(context, "NaN");
|
|
|
|
}
|
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;
|
|
|
|
}
|
2017-10-23 20:40:49 +00:00
|
|
|
if (math.isPositiveInf(x)) {
|
|
|
|
return output(context, "Infinity");
|
|
|
|
}
|
|
|
|
if (x == 0.0) {
|
|
|
|
return output(context, "0.0");
|
|
|
|
}
|
|
|
|
|
|
|
|
var buffer: [32]u8 = undefined;
|
|
|
|
const float_decimal = errol3(x, buffer[0..]);
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, float_decimal.digits[0..1]);
|
|
|
|
try output(context, ".");
|
2017-08-18 21:54:42 +00:00
|
|
|
if (float_decimal.digits.len > 1) {
|
2017-12-22 05:50:30 +00:00
|
|
|
const num_digits = if (@typeOf(value) == f32)
|
2017-10-24 19:18:50 +00:00
|
|
|
math.min(usize(9), float_decimal.digits.len)
|
2017-12-22 05:50:30 +00:00
|
|
|
else
|
|
|
|
float_decimal.digits.len;
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, float_decimal.digits[1 .. num_digits]);
|
2017-08-18 21:54:42 +00:00
|
|
|
} else {
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, "0");
|
2017-08-18 21:54:42 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 20:40:49 +00:00
|
|
|
if (float_decimal.exp != 1) {
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, "e");
|
2018-02-02 19:26:14 +00:00
|
|
|
try formatInt(float_decimal.exp - 1, 10, false, 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-02-01 03:48:40 +00:00
|
|
|
pub fn formatFloatDecimal(value: var, precision: usize, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void {
|
2017-12-30 23:27:58 +00:00
|
|
|
var x = f64(value);
|
|
|
|
|
|
|
|
// Errol doesn't handle these special cases.
|
|
|
|
if (math.isNan(x)) {
|
|
|
|
return output(context, "NaN");
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (math.isPositiveInf(x)) {
|
|
|
|
return output(context, "Infinity");
|
|
|
|
}
|
|
|
|
if (x == 0.0) {
|
|
|
|
return output(context, "0.0");
|
|
|
|
}
|
|
|
|
|
|
|
|
var buffer: [32]u8 = undefined;
|
|
|
|
const float_decimal = errol3(x, buffer[0..]);
|
|
|
|
|
|
|
|
const num_left_digits = if (float_decimal.exp > 0) usize(float_decimal.exp) else 1;
|
|
|
|
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, float_decimal.digits[0 .. num_left_digits]);
|
|
|
|
try output(context, ".");
|
2017-12-30 23:27:58 +00:00
|
|
|
if (float_decimal.digits.len > 1) {
|
|
|
|
const num_valid_digtis = if (@typeOf(value) == f32) math.min(usize(7), float_decimal.digits.len)
|
|
|
|
else
|
|
|
|
float_decimal.digits.len;
|
|
|
|
|
|
|
|
const num_right_digits = if (precision != 0)
|
|
|
|
math.min(precision, (num_valid_digtis-num_left_digits))
|
|
|
|
else
|
|
|
|
num_valid_digtis - num_left_digits;
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, float_decimal.digits[num_left_digits .. (num_left_digits + num_right_digits)]);
|
2017-12-30 23:27:58 +00:00
|
|
|
} else {
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, "0");
|
2017-12-30 23:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-10 00:12:15 +00:00
|
|
|
pub fn formatInt(value: var, base: u8, uppercase: bool, width: usize,
|
2018-02-02 19:26:14 +00:00
|
|
|
context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void
|
2017-03-10 00:12:15 +00:00
|
|
|
{
|
|
|
|
if (@typeOf(value).is_signed) {
|
2018-02-02 19:26:14 +00:00
|
|
|
return formatIntSigned(value, base, uppercase, width, context, Errors, output);
|
2017-03-10 00:12:15 +00:00
|
|
|
} else {
|
2018-02-02 19:26:14 +00:00
|
|
|
return formatIntUnsigned(value, base, uppercase, width, context, Errors, output);
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize,
|
2018-02-01 03:48:40 +00:00
|
|
|
context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void
|
2017-03-10 00:12:15 +00:00
|
|
|
{
|
2017-04-18 04:05:09 +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 = '-';
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, (&minus_sign)[0..1]);
|
2017-03-10 00:12:15 +00:00
|
|
|
const new_value = uint(-(value + 1)) + 1;
|
|
|
|
const new_width = if (width == 0) 0 else (width - 1);
|
2018-02-02 19:26:14 +00:00
|
|
|
return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output);
|
2017-03-10 00:12:15 +00:00
|
|
|
} else if (width == 0) {
|
2018-02-02 19:26:14 +00:00
|
|
|
return formatIntUnsigned(uint(value), base, uppercase, width, context, Errors, output);
|
2017-03-10 00:12:15 +00:00
|
|
|
} else {
|
|
|
|
const plus_sign: u8 = '+';
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, (&plus_sign)[0..1]);
|
2017-03-10 00:12:15 +00:00
|
|
|
const new_value = uint(value);
|
|
|
|
const new_width = if (width == 0) 0 else (width - 1);
|
2018-02-02 19:26:14 +00:00
|
|
|
return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output);
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn formatIntUnsigned(value: var, base: u8, uppercase: bool, width: usize,
|
2018-02-01 03:48:40 +00:00
|
|
|
context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void
|
2017-03-10 00:12:15 +00:00
|
|
|
{
|
|
|
|
// max_int_digits accounts for the minus sign. when printing an unsigned
|
|
|
|
// number we don't need to do that.
|
|
|
|
var buf: [max_int_digits - 1]u8 = undefined;
|
2017-10-21 17:03:08 +00:00
|
|
|
var a = if (@sizeOf(@typeOf(value)) == 1) u8(value) else value;
|
2017-03-10 00:12:15 +00:00
|
|
|
var index: usize = buf.len;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const digit = a % base;
|
|
|
|
index -= 1;
|
|
|
|
buf[index] = digitToChar(u8(digit), uppercase);
|
|
|
|
a /= base;
|
|
|
|
if (a == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-05-19 14:39:59 +00:00
|
|
|
const digits_buf = buf[index..];
|
2017-03-10 00:12:15 +00:00
|
|
|
const padding = if (width > digits_buf.len) (width - digits_buf.len) else 0;
|
|
|
|
|
|
|
|
if (padding > index) {
|
|
|
|
const zero_byte: u8 = '0';
|
|
|
|
var leftover_padding = padding - index;
|
|
|
|
while (true) {
|
2018-01-07 21:51:46 +00:00
|
|
|
try output(context, (&zero_byte)[0..1]);
|
2017-03-10 00:12:15 +00:00
|
|
|
leftover_padding -= 1;
|
|
|
|
if (leftover_padding == 0)
|
|
|
|
break;
|
|
|
|
}
|
2017-05-19 14:39:59 +00:00
|
|
|
mem.set(u8, buf[0..index], '0');
|
2017-03-10 00:12:15 +00:00
|
|
|
return output(context, buf);
|
|
|
|
} else {
|
2017-05-19 14:39:59 +00:00
|
|
|
const padded_buf = buf[index - padding..];
|
|
|
|
mem.set(u8, padded_buf[0..padding], '0');
|
2017-03-10 00:12:15 +00:00
|
|
|
return output(context, padded_buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn formatIntBuf(out_buf: []u8, value: var, base: u8, uppercase: bool, width: usize) usize {
|
2017-03-10 00:12:15 +00:00
|
|
|
var context = FormatIntBuf {
|
|
|
|
.out_buf = out_buf,
|
|
|
|
.index = 0,
|
|
|
|
};
|
2018-02-02 19:26:14 +00:00
|
|
|
formatInt(value, base, uppercase, width, &context, error{}, formatIntCallback) catch unreachable;
|
2017-03-10 00:12:15 +00:00
|
|
|
return context.index;
|
|
|
|
}
|
|
|
|
const FormatIntBuf = struct {
|
|
|
|
out_buf: []u8,
|
|
|
|
index: usize,
|
|
|
|
};
|
2018-02-03 16:51:29 +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 {
|
2017-09-26 23:40:51 +00:00
|
|
|
if (!T.is_signed)
|
|
|
|
return parseUnsigned(T, buf, radix);
|
|
|
|
if (buf.len == 0)
|
|
|
|
return T(0);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test "fmt.parseInt" {
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((parseInt(i32, "-10", 10) catch unreachable) == -10);
|
|
|
|
assert((parseInt(i32, "+10", 10) catch unreachable) == 10);
|
2018-02-05 23:09:13 +00:00
|
|
|
assert(if (parseInt(i32, " 10", 10)) |_| false else |err| err == error.InvalidCharacter);
|
|
|
|
assert(if (parseInt(i32, "10 ", 10)) |_| false else |err| err == error.InvalidCharacter);
|
|
|
|
assert(if (parseInt(u32, "-10", 10)) |_| false else |err| err == error.InvalidCharacter);
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((parseInt(u8, "255", 10) catch unreachable) == 255);
|
2017-12-22 07:49:17 +00:00
|
|
|
assert(if (parseInt(u8, "256", 10)) |_| false else |err| err == error.Overflow);
|
2017-09-26 23:40:51 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 19:26:14 +00:00
|
|
|
const ParseUnsignedError = error {
|
|
|
|
/// The result cannot fit in the type specified
|
|
|
|
Overflow,
|
|
|
|
/// 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);
|
|
|
|
x = try math.mul(T, x, radix);
|
|
|
|
x = try math.add(T, x, digit);
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:26:14 +00:00
|
|
|
fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) {
|
2017-03-10 00:12:15 +00:00
|
|
|
const value = switch (c) {
|
|
|
|
'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
|
|
|
};
|
|
|
|
|
|
|
|
if (value >= radix)
|
2018-02-02 19:26:14 +00:00
|
|
|
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) {
|
|
|
|
0 ... 9 => digit + '0',
|
|
|
|
10 ... 35 => digit + ((if (uppercase) u8('A') else u8('a')) - 10),
|
2017-03-26 08:58:48 +00:00
|
|
|
else => unreachable,
|
2017-03-10 00:12:15 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-04-06 09:34:04 +00:00
|
|
|
const BufPrintContext = struct {
|
|
|
|
remaining: []u8,
|
|
|
|
};
|
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
fn bufPrintWrite(context: &BufPrintContext, bytes: []const u8) !void {
|
2017-12-22 07:49:17 +00:00
|
|
|
if (context.remaining.len < bytes.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
|
|
|
}
|
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: ...) ![]u8 {
|
2017-04-06 09:34:04 +00:00
|
|
|
var context = BufPrintContext { .remaining = buf, };
|
2018-02-03 16:51:29 +00:00
|
|
|
try format(&context, error{BufferTooSmall}, bufPrintWrite, fmt, args);
|
2017-05-19 14:39:59 +00:00
|
|
|
return buf[0..buf.len - context.remaining.len];
|
2017-04-06 09:34:04 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn allocPrint(allocator: &mem.Allocator, comptime fmt: []const u8, args: ...) ![]u8 {
|
2017-04-06 09:34:04 +00:00
|
|
|
var size: usize = 0;
|
2018-02-02 04:32:09 +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);
|
2017-04-30 22:56:24 +00:00
|
|
|
return bufPrint(buf, fmt, args);
|
2017-04-06 09:34:04 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 07:08:45 +00:00
|
|
|
fn countSize(size: &usize, bytes: []const u8) (error{}!void) {
|
2017-04-06 09:34:04 +00:00
|
|
|
*size += bytes.len;
|
|
|
|
}
|
|
|
|
|
2017-05-08 03:25:02 +00:00
|
|
|
test "buf print int" {
|
2017-03-10 00:12:15 +00:00
|
|
|
var buffer: [max_int_digits]u8 = undefined;
|
2017-05-19 14:39:59 +00:00
|
|
|
const buf = buffer[0..];
|
2017-03-10 00:12:15 +00:00
|
|
|
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 2, false, 0), "-101111000110000101001110"));
|
|
|
|
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 10, false, 0), "-12345678"));
|
|
|
|
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, false, 0), "-bc614e"));
|
|
|
|
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, true, 0), "-BC614E"));
|
|
|
|
|
|
|
|
assert(mem.eql(u8, bufPrintIntToSlice(buf, u32(12345678), 10, true, 0), "12345678"));
|
|
|
|
|
|
|
|
assert(mem.eql(u8, bufPrintIntToSlice(buf, u32(666), 10, false, 6), "000666"));
|
|
|
|
assert(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, 6), "001234"));
|
|
|
|
assert(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, 1), "1234"));
|
|
|
|
|
|
|
|
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(42), 10, false, 3), "+42"));
|
|
|
|
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-42), 10, false, 3), "-42"));
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, width: usize) []u8 {
|
2017-05-19 14:39:59 +00:00
|
|
|
return buf[0..formatIntBuf(buf, value, base, uppercase, width)];
|
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 {
|
2018-01-09 05:07:01 +00:00
|
|
|
assert((try parseUnsigned(usize, "2", 10)) == 2);
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-23 22:38:41 +00:00
|
|
|
|
|
|
|
test "fmt.format" {
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: ?i32 = 1234;
|
2018-01-09 05:07:01 +00:00
|
|
|
const result = try bufPrint(buf1[0..], "nullable: {}\n", value);
|
2017-05-23 22:38:41 +00:00
|
|
|
assert(mem.eql(u8, result, "nullable: 1234\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: ?i32 = null;
|
2018-01-09 05:07:01 +00:00
|
|
|
const result = try bufPrint(buf1[0..], "nullable: {}\n", value);
|
2017-05-23 22:38:41 +00:00
|
|
|
assert(mem.eql(u8, result, "nullable: null\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
2018-02-01 03:48:40 +00:00
|
|
|
const value: error!i32 = 1234;
|
2018-01-09 05:07:01 +00:00
|
|
|
const result = try bufPrint(buf1[0..], "error union: {}\n", value);
|
2017-05-23 22:38:41 +00:00
|
|
|
assert(mem.eql(u8, result, "error union: 1234\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
2018-02-01 03:48:40 +00:00
|
|
|
const value: error!i32 = error.InvalidChar;
|
2018-01-09 05:07:01 +00:00
|
|
|
const result = try bufPrint(buf1[0..], "error union: {}\n", value);
|
2017-05-23 22:38:41 +00:00
|
|
|
assert(mem.eql(u8, result, "error union: error.InvalidChar\n"));
|
|
|
|
}
|
2017-10-21 17:03:08 +00:00
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: u3 = 0b101;
|
2018-01-09 05:07:01 +00:00
|
|
|
const result = try bufPrint(buf1[0..], "u3: {}\n", value);
|
2017-10-21 17:03:08 +00:00
|
|
|
assert(mem.eql(u8, result, "u3: 5\n"));
|
|
|
|
}
|
2017-10-25 01:45:00 +00:00
|
|
|
|
|
|
|
// TODO get these tests passing in release modes
|
|
|
|
// https://github.com/zig-lang/zig/issues/564
|
|
|
|
if (builtin.mode == builtin.Mode.Debug) {
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f32 = 12.34;
|
2018-01-09 05:07:01 +00:00
|
|
|
const result = try bufPrint(buf1[0..], "f32: {}\n", value);
|
2017-10-25 01:45:00 +00:00
|
|
|
assert(mem.eql(u8, result, "f32: 1.23400001e1\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = -12.34e10;
|
2018-01-09 05:07:01 +00:00
|
|
|
const result = try bufPrint(buf1[0..], "f64: {}\n", value);
|
2017-10-25 01:45:00 +00:00
|
|
|
assert(mem.eql(u8, result, "f64: -1.234e11\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
2018-01-09 05:07:01 +00:00
|
|
|
const result = try bufPrint(buf1[0..], "f64: {}\n", math.nan_f64);
|
2017-10-25 01:45:00 +00:00
|
|
|
assert(mem.eql(u8, result, "f64: NaN\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
2018-01-09 05:07:01 +00:00
|
|
|
const result = try bufPrint(buf1[0..], "f64: {}\n", math.inf_f64);
|
2017-10-25 01:45:00 +00:00
|
|
|
assert(mem.eql(u8, result, "f64: Infinity\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
2018-01-09 05:07:01 +00:00
|
|
|
const result = try bufPrint(buf1[0..], "f64: {}\n", -math.inf_f64);
|
2017-10-25 01:45:00 +00:00
|
|
|
assert(mem.eql(u8, result, "f64: -Infinity\n"));
|
|
|
|
}
|
2017-12-30 23:27:58 +00:00
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f32 = 1.1234;
|
2018-01-09 05:07:01 +00:00
|
|
|
const result = try bufPrint(buf1[0..], "f32: {.1}\n", value);
|
2017-12-30 23:27:58 +00:00
|
|
|
assert(mem.eql(u8, result, "f32: 1.1\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f32 = 1234.567;
|
2018-01-09 05:07:01 +00:00
|
|
|
const result = try bufPrint(buf1[0..], "f32: {.2}\n", value);
|
2017-12-30 23:27:58 +00:00
|
|
|
assert(mem.eql(u8, result, "f32: 1234.56\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f32 = -11.1234;
|
2018-01-09 05:07:01 +00:00
|
|
|
const result = try bufPrint(buf1[0..], "f32: {.4}\n", value);
|
2017-12-30 23:27:58 +00:00
|
|
|
// -11.1234 is converted to f64 -11.12339... internally (errol3() function takes f64).
|
|
|
|
// -11.12339... is truncated to -11.1233
|
|
|
|
assert(mem.eql(u8, result, "f32: -11.1233\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f32 = 91.12345;
|
2018-01-09 05:07:01 +00:00
|
|
|
const result = try bufPrint(buf1[0..], "f32: {.}\n", value);
|
2017-12-30 23:27:58 +00:00
|
|
|
assert(mem.eql(u8, result, "f32: 91.12345\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = 91.12345678901235;
|
2018-01-09 05:07:01 +00:00
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.10}\n", value);
|
2017-12-30 23:27:58 +00:00
|
|
|
assert(mem.eql(u8, result, "f64: 91.1234567890\n"));
|
|
|
|
}
|
|
|
|
|
2017-10-25 01:45:00 +00:00
|
|
|
}
|
2017-05-23 22:38:41 +00:00
|
|
|
}
|
2017-09-26 07:03:12 +00:00
|
|
|
|
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;
|
|
|
|
while (start < buf.len and isWhiteSpace(buf[start])) : (start += 1) { }
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
test "fmt.trim" {
|
|
|
|
assert(mem.eql(u8, "abc", trim("\n abc \t")));
|
|
|
|
assert(mem.eql(u8, "", trim(" ")));
|
|
|
|
assert(mem.eql(u8, "", trim("")));
|
|
|
|
assert(mem.eql(u8, "abc", trim(" abc")));
|
|
|
|
assert(mem.eql(u8, "abc", trim("abc ")));
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
' ', '\t', '\n', '\r' => true,
|
|
|
|
else => false,
|
|
|
|
};
|
|
|
|
}
|