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");
|
2018-04-17 10:58:10 +00:00
|
|
|
const errol = @import("errol/index.zig");
|
2018-06-17 06:57:07 +00:00
|
|
|
const lossyCast = std.math.lossyCast;
|
2017-03-10 00:12:15 +00:00
|
|
|
|
|
|
|
const max_int_digits = 65;
|
|
|
|
|
|
|
|
/// 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-05-30 20:09:11 +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 {
|
2018-04-14 06:12:19 +00:00
|
|
|
const State = enum {
|
|
|
|
Start,
|
|
|
|
OpenBrace,
|
|
|
|
CloseBrace,
|
2018-05-30 15:18:11 +00:00
|
|
|
FormatString,
|
2018-07-31 15:34:42 +00:00
|
|
|
Pointer,
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2018-05-30 15:18:11 +00:00
|
|
|
start_index = i;
|
2017-03-10 00:12:15 +00:00
|
|
|
state = State.OpenBrace;
|
|
|
|
},
|
2018-06-04 15:06:55 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
state = State.CloseBrace;
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
},
|
|
|
|
State.OpenBrace => switch (c) {
|
|
|
|
'{' => {
|
|
|
|
state = State.Start;
|
|
|
|
start_index = i;
|
|
|
|
},
|
|
|
|
'}' => {
|
2018-05-30 15:18:11 +00:00
|
|
|
try formatType(args[next_arg], fmt[0..0], context, Errors, output);
|
2017-03-10 00:12:15 +00:00
|
|
|
next_arg += 1;
|
|
|
|
state = State.Start;
|
|
|
|
start_index = i + 1;
|
|
|
|
},
|
2018-07-31 15:34:42 +00:00
|
|
|
'*' => state = State.Pointer,
|
2018-05-30 15:18:11 +00:00
|
|
|
else => {
|
|
|
|
state = State.FormatString;
|
2017-04-06 09:34:04 +00:00
|
|
|
},
|
|
|
|
},
|
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"),
|
|
|
|
},
|
2018-05-30 15:24:27 +00:00
|
|
|
State.FormatString => switch (c) {
|
2017-03-10 00:12:15 +00:00
|
|
|
'}' => {
|
2018-05-30 15:18:11 +00:00
|
|
|
const s = start_index + 1;
|
|
|
|
try formatType(args[next_arg], fmt[s..i], context, Errors, output);
|
2017-03-10 00:12:15 +00:00
|
|
|
next_arg += 1;
|
|
|
|
state = State.Start;
|
|
|
|
start_index = i + 1;
|
|
|
|
},
|
2018-05-30 15:18:11 +00:00
|
|
|
else => {},
|
2018-05-16 02:11:03 +00:00
|
|
|
},
|
2018-07-31 15:34:42 +00:00
|
|
|
State.Pointer => switch (c) {
|
|
|
|
'}' => {
|
|
|
|
try output(context, @typeName(@typeOf(args[next_arg]).Child));
|
|
|
|
try output(context, "@");
|
|
|
|
try formatInt(@ptrToInt(args[next_arg]), 16, false, 0, context, Errors, output);
|
|
|
|
next_arg += 1;
|
|
|
|
state = State.Start;
|
|
|
|
start_index = i + 1;
|
|
|
|
},
|
|
|
|
else => @compileError("Unexpected format character after '*'"),
|
|
|
|
},
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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-06-04 15:06:55 +00:00
|
|
|
pub fn formatType(
|
|
|
|
value: var,
|
|
|
|
comptime fmt: []const u8,
|
|
|
|
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);
|
2018-06-06 04:39:39 +00:00
|
|
|
if (T == error) {
|
|
|
|
try output(context, "error.");
|
|
|
|
return output(context, @errorName(value));
|
|
|
|
}
|
|
|
|
switch (@typeInfo(T)) {
|
2018-06-04 15:06:55 +00:00
|
|
|
builtin.TypeId.Int, builtin.TypeId.Float => {
|
2018-05-30 15:18:11 +00:00
|
|
|
return formatValue(value, fmt, 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");
|
|
|
|
},
|
2018-06-10 03:42:14 +00:00
|
|
|
builtin.TypeId.Optional => {
|
2017-05-23 22:38:41 +00:00
|
|
|
if (value) |payload| {
|
2018-05-30 15:18:11 +00:00
|
|
|
return formatType(payload, fmt, context, Errors, output);
|
2017-05-23 22:38:41 +00:00
|
|
|
} else {
|
|
|
|
return output(context, "null");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
builtin.TypeId.ErrorUnion => {
|
|
|
|
if (value) |payload| {
|
2018-05-30 15:18:11 +00:00
|
|
|
return formatType(payload, fmt, context, Errors, output);
|
2017-05-23 22:38:41 +00:00
|
|
|
} else |err| {
|
2018-05-30 15:18:11 +00:00
|
|
|
return formatType(err, fmt, 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));
|
|
|
|
},
|
2018-06-29 19:39:55 +00:00
|
|
|
builtin.TypeId.Promise => {
|
|
|
|
return format(context, Errors, output, "promise@{x}", @ptrToInt(value));
|
|
|
|
},
|
2018-08-20 20:04:03 +00:00
|
|
|
builtin.TypeId.Enum, builtin.TypeId.Union, builtin.TypeId.Struct => {
|
|
|
|
const has_cust_fmt = comptime cf: {
|
|
|
|
const info = @typeInfo(T);
|
|
|
|
const defs = switch (info) {
|
|
|
|
builtin.TypeId.Struct => |s| s.defs,
|
|
|
|
builtin.TypeId.Union => |u| u.defs,
|
|
|
|
builtin.TypeId.Enum => |e| e.defs,
|
|
|
|
else => unreachable,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (defs) |def| {
|
|
|
|
if (mem.eql(u8, def.name, "format")) {
|
|
|
|
break :cf true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break :cf false;
|
|
|
|
};
|
|
|
|
if (has_cust_fmt) return value.format(fmt, context, Errors, output);
|
2018-08-27 21:25:33 +00:00
|
|
|
|
2018-08-20 20:04:03 +00:00
|
|
|
try output(context, @typeName(T));
|
2018-08-27 21:25:33 +00:00
|
|
|
switch (comptime @typeId(T)) {
|
|
|
|
builtin.TypeId.Enum => {
|
|
|
|
try output(context, ".");
|
|
|
|
try formatType(@tagName(value), "", context, Errors, output);
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
builtin.TypeId.Struct => {
|
|
|
|
comptime var field_i = 0;
|
|
|
|
inline while (field_i < @memberCount(T)) : (field_i += 1) {
|
|
|
|
if (field_i == 0) {
|
|
|
|
try output(context, "{ .");
|
|
|
|
} else {
|
|
|
|
try output(context, ", .");
|
|
|
|
}
|
|
|
|
try output(context, @memberName(T, field_i));
|
|
|
|
try output(context, " = ");
|
|
|
|
try formatType(@field(value, @memberName(T, field_i)), "", context, Errors, output);
|
|
|
|
}
|
|
|
|
try output(context, " }");
|
|
|
|
},
|
|
|
|
builtin.TypeId.Union => {
|
|
|
|
const info = @typeInfo(T).Union;
|
2018-08-27 23:25:18 +00:00
|
|
|
if (info.tag_type) |UnionTagType| {
|
2018-08-27 21:25:33 +00:00
|
|
|
try output(context, "{ .");
|
|
|
|
try output(context, @tagName(UnionTagType(value)));
|
|
|
|
try output(context, " = ");
|
2018-08-27 23:25:18 +00:00
|
|
|
inline for (info.fields) |u_field| {
|
|
|
|
if (@enumToInt(UnionTagType(value)) == u_field.enum_field.?.value) {
|
2018-08-27 21:25:33 +00:00
|
|
|
try formatType(@field(value, u_field.name), "", context, Errors, output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try output(context, " }");
|
|
|
|
} else {
|
|
|
|
try format(context, Errors, output, "@{x}", @ptrToInt(&value));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => unreachable,
|
2018-08-20 20:04:03 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
},
|
2018-06-06 04:39:39 +00:00
|
|
|
builtin.TypeId.Pointer => |ptr_info| switch (ptr_info.size) {
|
|
|
|
builtin.TypeInfo.Pointer.Size.One => switch (@typeInfo(ptr_info.child)) {
|
|
|
|
builtin.TypeId.Array => |info| {
|
|
|
|
if (info.child == u8) {
|
2018-05-30 15:18:11 +00:00
|
|
|
return formatText(value, fmt, context, Errors, output);
|
|
|
|
}
|
2018-06-06 04:39:39 +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 => {
|
2018-08-20 20:04:03 +00:00
|
|
|
return formatType(value.*, fmt, context, Errors, output);
|
2018-05-30 15:18:11 +00:00
|
|
|
},
|
2018-06-04 15:06:55 +00:00
|
|
|
else => return format(context, Errors, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value)),
|
2018-06-06 04:39:39 +00:00
|
|
|
},
|
|
|
|
builtin.TypeInfo.Pointer.Size.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') {
|
2018-06-10 16:57:21 +00:00
|
|
|
const len = std.cstr.len(value);
|
|
|
|
return formatText(value[0..len], fmt, context, Errors, output);
|
|
|
|
}
|
|
|
|
}
|
2018-06-06 04:39:39 +00:00
|
|
|
return format(context, Errors, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value));
|
|
|
|
},
|
|
|
|
builtin.TypeInfo.Pointer.Size.Slice => {
|
|
|
|
const casted_value = ([]const u8)(value);
|
|
|
|
return output(context, casted_value);
|
|
|
|
},
|
2017-05-17 16:26:35 +00:00
|
|
|
},
|
2018-06-15 17:49:39 +00:00
|
|
|
builtin.TypeId.Array => |info| {
|
|
|
|
if (info.child == u8) {
|
|
|
|
return formatText(value, fmt, context, Errors, output);
|
|
|
|
}
|
|
|
|
return format(context, Errors, output, "{}@{x}", @typeName(T.Child), @ptrToInt(&value));
|
|
|
|
},
|
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,
|
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
|
|
|
output: fn (@typeOf(context), []const u8) Errors!void,
|
|
|
|
) Errors!void {
|
2018-05-30 15:18:11 +00:00
|
|
|
if (fmt.len > 0) {
|
|
|
|
if (fmt[0] == 'B') {
|
|
|
|
comptime var width: ?usize = null;
|
|
|
|
if (fmt.len > 1) {
|
|
|
|
if (fmt[1] == 'i') {
|
|
|
|
if (fmt.len > 2) width = comptime (parseUnsigned(usize, fmt[2..], 10) catch unreachable);
|
|
|
|
return formatBytes(value, width, 1024, context, Errors, output);
|
|
|
|
}
|
|
|
|
width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable);
|
|
|
|
}
|
|
|
|
return formatBytes(value, width, 1000, context, Errors, output);
|
|
|
|
}
|
|
|
|
}
|
2018-06-04 15:06:55 +00:00
|
|
|
|
2018-05-30 15:18:11 +00:00
|
|
|
comptime var T = @typeOf(value);
|
|
|
|
switch (@typeId(T)) {
|
|
|
|
builtin.TypeId.Float => return formatFloatValue(value, fmt, context, Errors, output),
|
|
|
|
builtin.TypeId.Int => return formatIntValue(value, fmt, context, Errors, output),
|
|
|
|
else => unreachable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:06:55 +00:00
|
|
|
pub fn formatIntValue(
|
|
|
|
value: var,
|
|
|
|
comptime fmt: []const u8,
|
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
|
|
|
output: fn (@typeOf(context), []const u8) Errors!void,
|
|
|
|
) Errors!void {
|
2018-05-30 15:18:11 +00:00
|
|
|
comptime var radix = 10;
|
|
|
|
comptime var uppercase = false;
|
|
|
|
comptime var width = 0;
|
|
|
|
if (fmt.len > 0) {
|
|
|
|
switch (fmt[0]) {
|
|
|
|
'c' => {
|
2018-06-04 15:06:55 +00:00
|
|
|
if (@typeOf(value) == u8) {
|
|
|
|
if (fmt.len > 1) @compileError("Unknown format character: " ++ []u8{fmt[1]});
|
2018-05-30 17:18:24 +00:00
|
|
|
return formatAsciiChar(value, context, Errors, output);
|
2018-05-30 15:18:11 +00:00
|
|
|
}
|
|
|
|
},
|
2018-08-01 15:38:04 +00:00
|
|
|
'b' => {
|
|
|
|
radix = 2;
|
|
|
|
uppercase = false;
|
|
|
|
width = 0;
|
|
|
|
},
|
2018-05-30 15:18:11 +00:00
|
|
|
'd' => {
|
|
|
|
radix = 10;
|
|
|
|
uppercase = false;
|
|
|
|
width = 0;
|
|
|
|
},
|
|
|
|
'x' => {
|
|
|
|
radix = 16;
|
|
|
|
uppercase = false;
|
|
|
|
width = 0;
|
|
|
|
},
|
|
|
|
'X' => {
|
|
|
|
radix = 16;
|
|
|
|
uppercase = true;
|
|
|
|
width = 0;
|
|
|
|
},
|
|
|
|
else => @compileError("Unknown format character: " ++ []u8{fmt[0]}),
|
|
|
|
}
|
|
|
|
if (fmt.len > 1) width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable);
|
|
|
|
}
|
|
|
|
return formatInt(value, radix, uppercase, width, context, Errors, output);
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:06:55 +00:00
|
|
|
fn formatFloatValue(
|
|
|
|
value: var,
|
|
|
|
comptime fmt: []const u8,
|
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
|
|
|
output: fn (@typeOf(context), []const u8) Errors!void,
|
|
|
|
) Errors!void {
|
2018-05-30 15:18:11 +00:00
|
|
|
comptime var width: ?usize = null;
|
|
|
|
comptime var float_fmt = 'e';
|
|
|
|
if (fmt.len > 0) {
|
|
|
|
float_fmt = fmt[0];
|
2018-06-04 15:06:55 +00:00
|
|
|
if (fmt.len > 1) width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable);
|
2018-05-30 15:18:11 +00:00
|
|
|
}
|
2018-06-04 15:06:55 +00:00
|
|
|
|
2018-05-30 15:18:11 +00:00
|
|
|
switch (float_fmt) {
|
|
|
|
'e' => try formatFloatScientific(value, width, context, Errors, output),
|
|
|
|
'.' => try formatFloatDecimal(value, width, context, Errors, output),
|
|
|
|
else => @compileError("Unknown format character: " ++ []u8{float_fmt}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:06:55 +00:00
|
|
|
pub fn formatText(
|
|
|
|
bytes: []const u8,
|
|
|
|
comptime fmt: []const u8,
|
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
|
|
|
output: fn (@typeOf(context), []const u8) Errors!void,
|
|
|
|
) Errors!void {
|
2018-05-30 15:18:11 +00:00
|
|
|
if (fmt.len > 0) {
|
|
|
|
if (fmt[0] == 's') {
|
|
|
|
comptime var width = 0;
|
2018-06-04 15:06:55 +00:00
|
|
|
if (fmt.len > 1) width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable);
|
2018-05-30 15:18:11 +00:00
|
|
|
return formatBuf(bytes, width, context, Errors, output);
|
2018-09-01 10:40:05 +00:00
|
|
|
} else if ((fmt[0] == 'x') or (fmt[0] == 'X') ) {
|
|
|
|
for (bytes) |c| {
|
2018-09-02 06:04:20 +00:00
|
|
|
try formatInt(c, 16, fmt[0] == 'X', 2, context, Errors, output);
|
2018-09-01 10:40:05 +00:00
|
|
|
}
|
|
|
|
return;
|
2018-06-04 15:06:55 +00:00
|
|
|
} else @compileError("Unknown format character: " ++ []u8{fmt[0]});
|
2018-05-30 15:18:11 +00:00
|
|
|
}
|
|
|
|
return output(context, bytes);
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:06:55 +00:00
|
|
|
pub fn formatAsciiChar(
|
|
|
|
c: u8,
|
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
|
|
|
output: fn (@typeOf(context), []const u8) Errors!void,
|
|
|
|
) Errors!void {
|
2018-06-05 02:11:14 +00:00
|
|
|
return output(context, (*[1]u8)(&c)[0..]);
|
2017-03-10 00:12:15 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:06:55 +00:00
|
|
|
pub fn formatBuf(
|
|
|
|
buf: []const u8,
|
|
|
|
width: usize,
|
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
|
|
|
output: fn (@typeOf(context), []const u8) Errors!void,
|
|
|
|
) Errors!void {
|
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-06-10 16:57:21 +00:00
|
|
|
try output(context, (*[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,
|
|
|
|
maybe_precision: ?usize,
|
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
|
|
|
output: fn (@typeOf(context), []const u8) Errors!void,
|
|
|
|
) 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");
|
|
|
|
|
|
|
|
if (maybe_precision) |precision| {
|
|
|
|
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..]);
|
|
|
|
|
|
|
|
if (maybe_precision) |precision| {
|
|
|
|
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) {
|
2018-05-01 05:53:04 +00:00
|
|
|
const num_digits = if (@typeOf(value) == f32) math.min(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");
|
|
|
|
}
|
|
|
|
try formatInt(exp, 10, false, 0, context, Errors, output);
|
|
|
|
} else {
|
|
|
|
try output(context, "-");
|
|
|
|
if (exp > -10 and exp < 10) {
|
|
|
|
try output(context, "0");
|
|
|
|
}
|
|
|
|
try formatInt(-exp, 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-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,
|
|
|
|
maybe_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.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");
|
|
|
|
|
|
|
|
if (maybe_precision) |precision| {
|
|
|
|
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..]);
|
|
|
|
|
|
|
|
if (maybe_precision) |precision| {
|
|
|
|
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.
|
|
|
|
debug.assert(precision >= printed);
|
|
|
|
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,
|
|
|
|
width: ?usize,
|
|
|
|
comptime radix: usize,
|
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2018-05-30 20:09:11 +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
|
|
|
|
|
|
|
try formatFloatDecimal(new_value, width, context, Errors, output);
|
|
|
|
|
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) {
|
2018-05-29 00:23:55 +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,
|
|
|
|
width: usize,
|
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
2018-05-30 20:09:11 +00:00
|
|
|
output: fn (@typeOf(context), []const u8) Errors!void,
|
2018-05-29 00:23:55 +00:00
|
|
|
) 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:06:55 +00:00
|
|
|
fn formatIntSigned(
|
|
|
|
value: var,
|
|
|
|
base: u8,
|
|
|
|
uppercase: bool,
|
|
|
|
width: usize,
|
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
|
|
|
output: fn (@typeOf(context), []const u8) Errors!void,
|
|
|
|
) Errors!void {
|
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-06-05 02:11:14 +00:00
|
|
|
try output(context, (*[1]u8)(&minus_sign)[0..]);
|
2018-06-17 06:57:07 +00:00
|
|
|
const new_value = @intCast(uint, -(value + 1)) + 1;
|
2017-03-10 00:12:15 +00:00
|
|
|
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-06-17 06:57:07 +00:00
|
|
|
return formatIntUnsigned(@intCast(uint, value), base, uppercase, width, context, Errors, output);
|
2017-03-10 00:12:15 +00:00
|
|
|
} else {
|
|
|
|
const plus_sign: u8 = '+';
|
2018-06-05 02:11:14 +00:00
|
|
|
try output(context, (*[1]u8)(&plus_sign)[0..]);
|
2018-06-17 06:57:07 +00:00
|
|
|
const new_value = @intCast(uint, value);
|
2017-03-10 00:12:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:06:55 +00:00
|
|
|
fn formatIntUnsigned(
|
|
|
|
value: var,
|
|
|
|
base: u8,
|
|
|
|
uppercase: bool,
|
|
|
|
width: usize,
|
|
|
|
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;
|
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..];
|
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-06-05 02:11:14 +00:00
|
|
|
try output(context, (*[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
|
|
|
}
|
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 {
|
2018-05-30 20:09:11 +00:00
|
|
|
const padded_buf = buf[index - padding ..];
|
2017-05-19 14:39:59 +00:00
|
|
|
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 {
|
2018-05-01 05:53:04 +00:00
|
|
|
var context = FormatIntBuf{
|
2017-03-10 00:12:15 +00:00
|
|
|
.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-05-31 14:56:59 +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);
|
|
|
|
if (buf.len == 0) return 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-01 05:53:04 +00:00
|
|
|
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);
|
|
|
|
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-03-07 08:55:52 +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',
|
|
|
|
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-05-31 14:56:59 +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 {
|
2018-05-01 05:53:04 +00:00
|
|
|
var context = BufPrintContext{ .remaining = buf };
|
2018-02-03 16:51:29 +00:00
|
|
|
try format(&context, error{BufferTooSmall}, 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-07-17 22:36:47 +00:00
|
|
|
pub const AllocPrintError = error{OutOfMemory};
|
|
|
|
|
|
|
|
pub fn allocPrint(allocator: *mem.Allocator, comptime fmt: []const u8, args: ...) AllocPrintError![]u8 {
|
2017-04-06 09:34:04 +00:00
|
|
|
var size: usize = 0;
|
2018-05-29 00:23:55 +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-05-31 14:56:59 +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
|
|
|
}
|
|
|
|
|
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" {
|
|
|
|
{
|
|
|
|
const value: ?i32 = 1234;
|
2018-06-10 03:42:14 +00:00
|
|
|
try testFmt("optional: 1234\n", "optional: {}\n", value);
|
2017-05-23 22:38:41 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const value: ?i32 = null;
|
2018-06-10 03:42:14 +00:00
|
|
|
try testFmt("optional: null\n", "optional: {}\n", value);
|
2017-05-23 22:38:41 +00:00
|
|
|
}
|
|
|
|
{
|
2018-02-01 03:48:40 +00:00
|
|
|
const value: error!i32 = 1234;
|
2018-05-16 22:22:39 +00:00
|
|
|
try testFmt("error union: 1234\n", "error union: {}\n", value);
|
2017-05-23 22:38:41 +00:00
|
|
|
}
|
|
|
|
{
|
2018-02-01 03:48:40 +00:00
|
|
|
const value: error!i32 = error.InvalidChar;
|
2018-05-16 22:22:39 +00:00
|
|
|
try testFmt("error union: error.InvalidChar\n", "error union: {}\n", value);
|
2017-05-23 22:38:41 +00:00
|
|
|
}
|
2017-10-21 17:03:08 +00:00
|
|
|
{
|
|
|
|
const value: u3 = 0b101;
|
2018-05-16 22:22:39 +00:00
|
|
|
try testFmt("u3: 5\n", "u3: {}\n", value);
|
2018-05-16 02:11:03 +00:00
|
|
|
}
|
2018-05-30 17:18:24 +00:00
|
|
|
{
|
|
|
|
const value: u8 = 'a';
|
|
|
|
try testFmt("u8: a\n", "u8: {c}\n", value);
|
|
|
|
}
|
2018-08-01 15:38:04 +00:00
|
|
|
{
|
|
|
|
const value: u8 = 0b1100;
|
|
|
|
try testFmt("u8: 0b1100\n", "u8: 0b{b}\n", value);
|
|
|
|
}
|
2018-07-31 15:34:42 +00:00
|
|
|
{
|
|
|
|
const value: [3]u8 = "abc";
|
|
|
|
try testFmt("array: abc\n", "array: {}\n", value);
|
|
|
|
try testFmt("array: abc\n", "array: {}\n", &value);
|
|
|
|
|
|
|
|
var buf: [100]u8 = undefined;
|
|
|
|
try testFmt(
|
|
|
|
try bufPrint(buf[0..], "array: [3]u8@{x}\n", @ptrToInt(&value)),
|
|
|
|
"array: {*}\n",
|
|
|
|
&value,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const value: []const u8 = "abc";
|
|
|
|
try testFmt("slice: abc\n", "slice: {}\n", value);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const value = @intToPtr(*i32, 0xdeadbeef);
|
|
|
|
try testFmt("pointer: i32@deadbeef\n", "pointer: {}\n", value);
|
|
|
|
try testFmt("pointer: i32@deadbeef\n", "pointer: {*}\n", value);
|
|
|
|
}
|
2018-06-10 16:57:21 +00:00
|
|
|
try testFmt("buf: Test \n", "buf: {s5}\n", "Test");
|
|
|
|
try testFmt("buf: Test\n Other text", "buf: {s}\n Other text", "Test");
|
|
|
|
try testFmt("cstr: Test C\n", "cstr: {s}\n", c"Test C");
|
|
|
|
try testFmt("cstr: Test C \n", "cstr: {s10}\n", c"Test C");
|
2018-05-16 22:22:39 +00:00
|
|
|
try testFmt("file size: 63MiB\n", "file size: {Bi}\n", usize(63 * 1024 * 1024));
|
|
|
|
try testFmt("file size: 66.06MB\n", "file size: {B2}\n", usize(63 * 1024 * 1024));
|
2018-02-07 21:18:48 +00:00
|
|
|
{
|
2018-02-22 16:53:58 +00:00
|
|
|
const Struct = struct {
|
2018-08-20 20:04:03 +00:00
|
|
|
field: u8,
|
2018-02-22 16:53:58 +00:00
|
|
|
};
|
2018-08-20 20:04:03 +00:00
|
|
|
const value = Struct{ .field = 42 };
|
|
|
|
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-08-25 21:28:30 +00:00
|
|
|
{
|
|
|
|
const Enum = enum {
|
|
|
|
One,
|
|
|
|
Two,
|
|
|
|
};
|
|
|
|
const value = Enum.Two;
|
|
|
|
try testFmt("enum: Enum.Two\n", "enum: {}\n", value);
|
|
|
|
try testFmt("enum: Enum.Two\n", "enum: {}\n", &value);
|
|
|
|
}
|
2018-04-23 05:18:05 +00:00
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f32 = 1.34;
|
|
|
|
const result = try bufPrint(buf1[0..], "f32: {e}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f32: 1.34000003e+00\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f32 = 12.34;
|
|
|
|
const result = try bufPrint(buf1[0..], "f32: {e}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f32: 1.23400001e+01\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = -12.34e10;
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {e}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: -1.234e+11\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// This fails on release due to a minor rounding difference.
|
|
|
|
// --release-fast outputs 9.999960000000001e-40 vs. the expected.
|
2018-08-20 20:04:03 +00:00
|
|
|
// TODO fix this, it should be the same in Debug and ReleaseFast
|
2018-04-23 05:18:05 +00:00
|
|
|
if (builtin.mode == builtin.Mode.Debug) {
|
2018-04-17 10:58:10 +00:00
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = 9.999960e-40;
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {e}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 9.99996e-40\n"));
|
|
|
|
}
|
2018-04-23 05:18:05 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = 1.409706e-42;
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {e5}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 1.40971e-42\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = @bitCast(f32, u32(814313563));
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {e5}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 1.00000e-09\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = @bitCast(f32, u32(1006632960));
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {e5}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 7.81250e-03\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = @bitCast(f32, u32(1203982400));
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {e5}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 1.00001e+05\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {}\n", math.nan_f64);
|
|
|
|
assert(mem.eql(u8, result, "f64: nan\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {}\n", -math.nan_f64);
|
|
|
|
assert(mem.eql(u8, result, "f64: -nan\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {}\n", math.inf_f64);
|
|
|
|
assert(mem.eql(u8, result, "f64: inf\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {}\n", -math.inf_f64);
|
|
|
|
assert(mem.eql(u8, result, "f64: -inf\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [64]u8 = undefined;
|
|
|
|
const value: f64 = 1.52314e+29;
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 152314000000000000000000000000\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f32 = 1.1234;
|
|
|
|
const result = try bufPrint(buf1[0..], "f32: {.1}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f32: 1.1\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f32 = 1234.567;
|
|
|
|
const result = try bufPrint(buf1[0..], "f32: {.2}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f32: 1234.57\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f32 = -11.1234;
|
|
|
|
const result = try bufPrint(buf1[0..], "f32: {.4}\n", value);
|
|
|
|
// -11.1234 is converted to f64 -11.12339... internally (errol3() function takes f64).
|
|
|
|
// -11.12339... is rounded back up to -11.1234
|
|
|
|
assert(mem.eql(u8, result, "f32: -11.1234\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f32 = 91.12345;
|
|
|
|
const result = try bufPrint(buf1[0..], "f32: {.5}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f32: 91.12345\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = 91.12345678901235;
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.10}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 91.1234567890\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = 0.0;
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 0.00000\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = 5.700;
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.0}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 6\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = 9.999;
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.1}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 10.0\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = 1.0;
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.3}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 1.000\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = 0.0003;
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.8}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 0.00030000\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = 1.40130e-45;
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 0.00000\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = 9.999960e-40;
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 0.00000\n"));
|
|
|
|
}
|
|
|
|
// libc checks
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = f64(@bitCast(f32, u32(916964781)));
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 0.00001\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = f64(@bitCast(f32, u32(925353389)));
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 0.00001\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = f64(@bitCast(f32, u32(1036831278)));
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 0.10000\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = f64(@bitCast(f32, u32(1065353133)));
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 1.00000\n"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = f64(@bitCast(f32, u32(1092616192)));
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 10.00000\n"));
|
|
|
|
}
|
|
|
|
// libc differences
|
|
|
|
{
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
// 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.
|
|
|
|
const value: f64 = f64(@bitCast(f32, u32(1015021568)));
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 0.01563\n"));
|
|
|
|
}
|
|
|
|
// std-windows-x86_64-Debug-bare test case fails
|
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
var buf1: [32]u8 = undefined;
|
|
|
|
const value: f64 = f64(@bitCast(f32, u32(1518338049)));
|
|
|
|
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
|
|
|
|
assert(mem.eql(u8, result, "f64: 18014400656965630.00000\n"));
|
2017-10-25 01:45:00 +00:00
|
|
|
}
|
2018-05-30 15:18:11 +00:00
|
|
|
//custom type format
|
|
|
|
{
|
|
|
|
const Vec2 = struct {
|
|
|
|
const SelfType = this;
|
|
|
|
x: f32,
|
|
|
|
y: f32,
|
2018-06-04 15:06:55 +00:00
|
|
|
|
|
|
|
pub fn format(
|
2018-08-20 20:04:03 +00:00
|
|
|
self: SelfType,
|
2018-06-04 15:06:55 +00:00
|
|
|
comptime fmt: []const u8,
|
|
|
|
context: var,
|
|
|
|
comptime Errors: type,
|
|
|
|
output: fn (@typeOf(context), []const u8) Errors!void,
|
|
|
|
) Errors!void {
|
2018-08-20 20:04:03 +00:00
|
|
|
switch (fmt.len) {
|
|
|
|
0 => return std.fmt.format(context, Errors, output, "({.3},{.3})", self.x, self.y),
|
|
|
|
1 => switch (fmt[0]) {
|
2018-05-30 15:18:11 +00:00
|
|
|
//point format
|
|
|
|
'p' => return std.fmt.format(context, Errors, output, "({.3},{.3})", self.x, self.y),
|
|
|
|
//dimension format
|
|
|
|
'd' => return std.fmt.format(context, Errors, output, "{.3}x{.3}", self.x, self.y),
|
|
|
|
else => unreachable,
|
2018-08-20 20:04:03 +00:00
|
|
|
},
|
|
|
|
else => unreachable,
|
2018-05-30 15:18:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-06-04 15:06:55 +00:00
|
|
|
|
2018-05-30 15:18:11 +00:00
|
|
|
var buf1: [32]u8 = undefined;
|
2018-06-04 15:06:55 +00:00
|
|
|
var value = Vec2{
|
|
|
|
.x = 10.2,
|
|
|
|
.y = 2.22,
|
|
|
|
};
|
2018-05-30 17:18:24 +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
|
|
|
|
|
|
|
// same thing but not passing a pointer
|
|
|
|
try testFmt("point: (10.200,2.220)\n", "point: {}\n", value);
|
|
|
|
try testFmt("dim: 10.200x2.220\n", "dim: {d}\n", value);
|
2018-05-30 15:18:11 +00:00
|
|
|
}
|
2018-08-27 21:25:33 +00:00
|
|
|
//struct format
|
|
|
|
{
|
|
|
|
const S = struct {
|
|
|
|
a: u32,
|
|
|
|
b: error,
|
|
|
|
};
|
2018-08-27 23:25:18 +00:00
|
|
|
|
|
|
|
const inst = S{
|
2018-08-27 21:25:33 +00:00
|
|
|
.a = 456,
|
|
|
|
.b = error.Unused,
|
|
|
|
};
|
2018-08-27 23:25:18 +00:00
|
|
|
|
2018-08-27 21:25:33 +00:00
|
|
|
try testFmt("S{ .a = 456, .b = error.Unused }", "{}", inst);
|
|
|
|
}
|
|
|
|
//union format
|
|
|
|
{
|
2018-08-27 23:25:18 +00:00
|
|
|
const TU = union(enum) {
|
2018-08-27 21:25:33 +00:00
|
|
|
float: f32,
|
|
|
|
int: u32,
|
|
|
|
};
|
2018-08-27 23:25:18 +00:00
|
|
|
|
|
|
|
const UU = union {
|
2018-08-27 21:25:33 +00:00
|
|
|
float: f32,
|
|
|
|
int: u32,
|
|
|
|
};
|
2018-08-27 23:25:18 +00:00
|
|
|
|
|
|
|
const EU = extern union {
|
2018-08-27 21:25:33 +00:00
|
|
|
float: f32,
|
|
|
|
int: u32,
|
|
|
|
};
|
2018-08-27 23:25:18 +00:00
|
|
|
|
|
|
|
const tu_inst = TU{ .int = 123 };
|
|
|
|
const uu_inst = UU{ .int = 456 };
|
|
|
|
const eu_inst = EU{ .float = 321.123 };
|
|
|
|
|
2018-08-27 21:25:33 +00:00
|
|
|
try testFmt("TU{ .int = 123 }", "{}", tu_inst);
|
2018-08-27 23:25:18 +00:00
|
|
|
|
2018-08-27 21:25:33 +00:00
|
|
|
var buf: [100]u8 = undefined;
|
|
|
|
const uu_result = try bufPrint(buf[0..], "{}", uu_inst);
|
|
|
|
debug.assert(mem.eql(u8, uu_result[0..3], "UU@"));
|
2018-08-27 23:25:18 +00:00
|
|
|
|
2018-08-27 21:25:33 +00:00
|
|
|
const eu_result = try bufPrint(buf[0..], "{}", eu_inst);
|
|
|
|
debug.assert(mem.eql(u8, uu_result[0..3], "EU@"));
|
|
|
|
}
|
|
|
|
//enum format
|
|
|
|
{
|
2018-08-27 23:25:18 +00:00
|
|
|
const E = enum {
|
2018-08-27 21:25:33 +00:00
|
|
|
One,
|
|
|
|
Two,
|
|
|
|
Three,
|
|
|
|
};
|
2018-08-27 23:25:18 +00:00
|
|
|
|
2018-08-27 21:25:33 +00:00
|
|
|
const inst = E.Two;
|
2018-08-27 23:25:18 +00:00
|
|
|
|
2018-08-27 21:25:33 +00:00
|
|
|
try testFmt("E.Two", "{}", inst);
|
|
|
|
}
|
2018-09-01 10:53:11 +00:00
|
|
|
//print bytes as hex
|
|
|
|
{
|
|
|
|
const some_bytes = "\xCA\xFE\xBA\xBE";
|
|
|
|
try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", some_bytes);
|
|
|
|
try testFmt("uppercase: CAFEBABE\n", "uppercase: {X}\n", some_bytes);
|
2018-09-02 06:04:57 +00:00
|
|
|
const bytes_with_zeros = "\x00\x0E\xBA\xBE";
|
|
|
|
try testFmt("lowercase: 000ebabe\n", "lowercase: {x}\n", bytes_with_zeros);
|
2018-09-01 10:53:11 +00:00
|
|
|
}
|
2017-05-23 22:38:41 +00:00
|
|
|
}
|
2017-09-26 07:03:12 +00:00
|
|
|
|
2018-05-16 22:22:39 +00:00
|
|
|
fn testFmt(expected: []const u8, comptime template: []const u8, args: ...) !void {
|
|
|
|
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
|
|
|
|
|
|
|
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");
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
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
|
|
|
|
|
|
|
// depends on https://github.com/ziglang/zig/pull/1454
|
|
|
|
pub fn hexToBytes(input: []const u8, out: []u8) !void {
|
|
|
|
if (out.len * 2 < input.len)
|
|
|
|
return error.InvalidLength;
|
|
|
|
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < input.len) : (i += 2) {
|
|
|
|
out[i/2] = (try charToDigit(input[i], 36)) << 4;
|
|
|
|
out[i/2] += try charToDigit(input[i+1], 36);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test "fmt.hexToBytes" {
|
|
|
|
const test_hex_str = "909A312BB12ED1F819B3521AC4C1E896F2160507FFC1C8381E3B07BB16BD1706";
|
|
|
|
var pb: [32]u8 = undefined;
|
|
|
|
try hexToBytes(test_hex_str, pb[0..]);
|
|
|
|
try testFmt(test_hex_str, "{X}", pb);
|
|
|
|
}
|
|
|
|
|