-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmt.zig
463 lines (406 loc) · 17.3 KB
/
fmt.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
const Format_Bytes_Data = struct {
bytes: u64,
negative: bool = false,
use_iec_suffixes: bool = false,
limit: f64 = 1023.5,
};
fn format_bytes(data: Format_Bytes_Data, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
const max_exponent = 6;
const limit: f64 = data.limit;
var value: f64 = @floatFromInt(data.bytes);
var exponent: u16 = 0;
while (exponent < max_exponent and value >= limit) {
exponent += 1;
value /= 1024;
}
if (data.negative) {
value = -value;
}
const suffix = if (data.use_iec_suffixes) switch (exponent) {
0 => " B",
1 => " KiB",
2 => " MiB",
3 => " GiB",
4 => " TiB",
5 => " PiB",
6 => " EiB",
else => unreachable,
} else switch (exponent) {
0 => " B",
1 => " KB",
2 => " MB",
3 => " GB",
4 => " TB",
5 => " PB",
6 => " EB",
else => unreachable,
};
var buf: [std.fmt.format_float.bufferSize(.decimal, f64) + 4]u8 = undefined;
const float_buf = buf[0 .. buf.len - 4];
var out: []const u8 = buf[0..0];
if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "e")) {
out = std.fmt.formatFloat(float_buf, value, .{ .mode = .scientific, .precision = options.precision }) catch |err| switch (err) {
error.BufferTooSmall => "(float)",
};
} else if (comptime std.mem.eql(u8, fmt, "d")) {
out = std.fmt.formatFloat(float_buf, value, .{ .mode = .decimal, .precision = options.precision }) catch |err| switch (err) {
error.BufferTooSmall => "(float)",
};
} else if (comptime std.mem.eql(u8, fmt, "x")) {
var buf_stream = std.io.fixedBufferStream(float_buf);
std.fmt.formatFloatHexadecimal(value, options, buf_stream.writer()) catch |err| switch (err) {
error.NoSpaceLeft => unreachable,
};
out = buf_stream.getWritten();
} else {
std.fmt.invalidFmtError(fmt, value);
}
@memcpy(buf[out.len..][0..suffix.len], suffix);
out = buf[0 .. out.len + suffix.len];
return std.fmt.formatBuf(out, options, writer);
}
pub fn fmtBytes(bytes: u64) std.fmt.Formatter(format_bytes) {
return .{
.data = .{
.bytes = bytes,
},
};
}
pub fn fmtBytesSigned(bytes: i64) std.fmt.Formatter(format_bytes) {
return .{
.data = .{
.bytes = @abs(bytes),
.negative = bytes < 0,
},
};
}
test fmtBytes {
var buf: [24]u8 = undefined;
inline for (.{
.{ .fmt = "{d}", .s = "0 B", .b = 0 },
.{ .fmt = "{d}", .s = "1 B", .b = 1 },
.{ .fmt = "{d}", .s = "1023 B", .b = 1023 },
.{ .fmt = "{d}", .s = "1 KB", .b = 1024 },
.{ .fmt = "{d}", .s = "1 MB", .b = 1024 * 1024 },
.{ .fmt = "{d}", .s = "1 GB", .b = 1024 * 1024 * 1024 },
.{ .fmt = "{d}", .s = "1 TB", .b = 1024 * 1024 * 1024 * 1024 },
.{ .fmt = "{d}", .s = "1.5 KB", .b = 1536 },
.{ .fmt = "{d:.1}", .s = "1.0 MB", .b = 1024 * 1024 - 1 },
.{ .fmt = "{d:.1}", .s = "1.0 GB", .b = 1024 * 1024 * 1024 - 1 },
.{ .fmt = "{d:.1}", .s = "1.0 TB", .b = 1024 * 1024 * 1024 * 1024 - 1 },
.{ .fmt = "{d:.3}", .s = "1023.023 KB", .b = 1024 * 1024 - 1000 },
.{ .fmt = "{d:.3}", .s = "1023.046 MB", .b = 1024 * 1024 * 1024 - 1000 * 1000 },
.{ .fmt = "{d:.3}", .s = "1023.069 GB", .b = 1024 * 1024 * 1024 * 1024 - 1000 * 1000 * 1000 },
.{ .fmt = "{d}", .s = "0.9999990463256836 MB", .b = 1024 * 1024 - 1 },
.{ .fmt = "{d}", .s = "0.9999999990686774 GB", .b = 1024 * 1024 * 1024 - 1 },
.{ .fmt = "{d}", .s = "0.9999999999990905 TB", .b = 1024 * 1024 * 1024 * 1024 - 1 },
.{ .fmt = "{d}", .s = "16 EB", .b = std.math.maxInt(u64) },
.{ .fmt = "{d:=>10}", .s = "=======0 B", .b = 0 },
.{ .fmt = "{d:=<10}", .s = "1 B=======", .b = 1 },
.{ .fmt = "{d:^10}", .s = " 100 KB ", .b = 102400 },
}) |tc| {
const slice = try std.fmt.bufPrint(&buf, tc.fmt, .{ fmtBytes(tc.b) });
try std.testing.expectEqualStrings(tc.s, slice);
}
}
const Format_Bytes_Floor_Data = struct {
bytes: u64,
negative: bool = false,
use_iec_suffixes: bool = false,
limit: u64 = 1024,
};
fn format_bytes_floor(data: Format_Bytes_Floor_Data, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
_ = fmt;
const max_exponent = 6;
var value: u64 = data.bytes;
var exponent: u16 = 0;
while (exponent < max_exponent and value >= data.limit) {
exponent += 1;
value = @divFloor(value, 1024);
}
const signed_value: i64 = @intCast(value);
const final_value = if (data.negative) -signed_value else signed_value;
const suffix = if (data.use_iec_suffixes) switch (exponent) {
0 => " B",
1 => " KiB",
2 => " MiB",
3 => " GiB",
4 => " TiB",
5 => " PiB",
6 => " EiB",
else => unreachable,
} else switch (exponent) {
0 => " B",
1 => " KB",
2 => " MB",
3 => " GB",
4 => " TB",
5 => " PB",
6 => " EB",
else => unreachable,
};
var buf: [25]u8 = undefined;
const int_buf = buf[0 .. buf.len - 4];
var buf_stream = std.io.fixedBufferStream(int_buf);
std.fmt.formatInt(final_value, 10, .lower, .{}, buf_stream.writer()) catch unreachable;
var out = buf_stream.getWritten();
@memcpy(buf[out.len..][0..suffix.len], suffix);
out = buf[0 .. out.len + suffix.len];
return std.fmt.formatBuf(out, options, writer);
}
/// Like fmtBytes, but always truncates towards zero instead of rounding, and avoids floating point computation entirely.
pub fn fmtBytesFloor(bytes: u64) std.fmt.Formatter(format_bytes_floor) {
const data = Format_Bytes_Floor_Data{ .bytes = bytes };
return .{ .data = data };
}
test fmtBytesFloor {
var buf: [24]u8 = undefined;
inline for (.{
.{ .fmt = "{d}", .s = "0 B", .b = 0 },
.{ .fmt = "{d}", .s = "1 B", .b = 1 },
.{ .fmt = "{d}", .s = "1023 B", .b = 1023 },
.{ .fmt = "{d}", .s = "1 KB", .b = 1024 },
.{ .fmt = "{d}", .s = "1 MB", .b = 1024 * 1024 },
.{ .fmt = "{d}", .s = "1 GB", .b = 1024 * 1024 * 1024 },
.{ .fmt = "{d}", .s = "1 TB", .b = 1024 * 1024 * 1024 * 1024 },
.{ .fmt = "{d}", .s = "1 KB", .b = 1536 },
.{ .fmt = "{d}", .s = "1023 KB", .b = 1024 * 1024 - 1 },
.{ .fmt = "{d}", .s = "1023 MB", .b = 1024 * 1024 * 1024 - 1 },
.{ .fmt = "{d}", .s = "1023 GB", .b = 1024 * 1024 * 1024 * 1024 - 1 },
.{ .fmt = "{d}", .s = "1023 KB", .b = 1024 * 1024 - 1000 },
.{ .fmt = "{d}", .s = "1023 MB", .b = 1024 * 1024 * 1024 - 1000 * 1000 },
.{ .fmt = "{d}", .s = "1023 GB", .b = 1024 * 1024 * 1024 * 1024 - 1000 * 1000 * 1000 },
.{ .fmt = "{d}", .s = "15 EB", .b = std.math.maxInt(u64) },
.{ .fmt = "{d:=>10}", .s = "=======0 B", .b = 0 },
.{ .fmt = "{d:=<10}", .s = "1 B=======", .b = 1 },
.{ .fmt = "{d:^10}", .s = " 100 KB ", .b = 102400 },
}) |tc| {
const slice = try std.fmt.bufPrint(&buf, tc.fmt, .{ fmtBytesFloor(tc.b) });
try std.testing.expectEqualStrings(tc.s, slice);
}
}
const Format_SI_Float_Data = struct {
value: f64,
limit: f64 = 999.5,
unit: []const u8,
use_utf8: bool = true,
};
fn format_si_float(data: Format_SI_Float_Data, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
if (data.unit.len > 32) return error.InvalidUnit;
const min_exponent = -10;
const max_exponent = 10;
var value = @abs(data.value);
var exponent: i16 = 0;
while (exponent < max_exponent and value >= data.limit) {
exponent += 1;
value /= 1000;
}
const limit = data.limit / 1000;
while (exponent > min_exponent and value < limit and value != 0) {
exponent -= 1;
value *= 1000;
}
if (data.value < 0) {
value = -value;
}
const suffix: []const u8 = switch (exponent) {
-10 => " q",
-9 => " r",
-8 => " y",
-7 => " z",
-6 => " a",
-5 => " f",
-4 => " p",
-3 => " n",
-2 => if (data.use_utf8) " \u{b5}" else " u",
-1 => " m",
0 => " ",
1 => " k",
2 => " M",
3 => " G",
4 => " T",
5 => " P",
6 => " E",
7 => " Z",
8 => " Y",
9 => " R",
10 => " Q",
else => unreachable,
};
var buf: [std.fmt.format_float.bufferSize(.decimal, f64) + 35]u8 = undefined;
const float_buf = buf[0 .. buf.len - 35];
var out: []const u8 = buf[0..0];
if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "e")) {
out = std.fmt.formatFloat(float_buf, value, .{ .mode = .scientific, .precision = options.precision }) catch |err| switch (err) {
error.BufferTooSmall => "(float)",
};
} else if (comptime std.mem.eql(u8, fmt, "d")) {
out = std.fmt.formatFloat(float_buf, value, .{ .mode = .decimal, .precision = options.precision }) catch |err| switch (err) {
error.BufferTooSmall => "(float)",
};
} else if (comptime std.mem.eql(u8, fmt, "x")) {
var buf_stream = std.io.fixedBufferStream(float_buf);
std.fmt.formatFloatHexadecimal(value, options, buf_stream.writer()) catch |err| switch (err) {
error.NoSpaceLeft => unreachable,
};
out = buf_stream.getWritten();
} else {
std.fmt.invalidFmtError(fmt, value);
}
@memcpy(buf[out.len..][0..suffix.len], suffix);
@memcpy(buf[out.len + suffix.len ..][0..data.unit.len], data.unit);
out = buf[0 .. out.len + suffix.len + data.unit.len];
return std.fmt.formatBuf(out, options, writer);
}
pub const Format_SI_Int_Options = struct {
unit: []const u8,
exponent_offset: i16 = 0,
use_utf8: bool = true,
limit: comptime_int = 1000,
};
fn Format_SI_Int(comptime T: type, comptime si_options: Format_SI_Int_Options) type {
return struct {
value: T,
pub const Formatter = std.fmt.Formatter(format);
pub fn format(data: @This(), comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
_ = fmt;
const min_exponent = -10;
const max_exponent = 10;
const precision = options.precision orelse 0;
if (precision > 32) return error.InvalidPrecision;
var precision_buf: [32]u8 = .{ 0 } ** 32;
var precision_slice = precision_buf[0..precision];
var exponent = si_options.exponent_offset;
var value = @abs(data.value);
while (exponent > max_exponent) {
value *= 1000;
exponent -= 1;
}
while (exponent < min_exponent or (exponent < max_exponent and value >= si_options.limit)) {
for (0..3) |_| {
if (precision > 0) {
const remainder: u8 = @intCast(value % 10);
std.mem.copyBackwards(u8, precision_slice[1..], precision_slice[0 .. precision_slice.len - 1]);
precision_slice[0] = '0' + remainder;
}
value = @divTrunc(value, 10);
}
exponent += 1;
}
const suffix: []const u8 = switch (exponent) {
-10 => "q",
-9 => "r",
-8 => "y",
-7 => "z",
-6 => "a",
-5 => "f",
-4 => " p",
-3 => " n",
-2 => if (si_options.use_utf8) " \u{b5}" else " u",
-1 => " m",
0 => " ",
1 => " k",
2 => " M",
3 => " G",
4 => " T",
5 => " P",
6 => " E",
7 => " Z",
8 => " Y",
9 => " R",
10 => " Q",
else => unreachable,
};
var buf: [@bitSizeOf(T) / 3 + 36 + si_options.unit.len]u8 = undefined;
var buf_stream = std.io.fixedBufferStream(&buf);
var w = buf_stream.writer();
if (data.value < 0) {
w.writeByte('-') catch unreachable;
}
std.fmt.formatInt(value, 10, .lower, .{}, w) catch unreachable;
if (options.precision) |_| {
w.writeByte('.') catch unreachable;
w.writeAll(precision_slice) catch unreachable;
}
w.writeAll(suffix) catch unreachable;
w.writeAll(si_options.unit) catch unreachable;
return std.fmt.formatBuf(buf_stream.getWritten(), options, writer);
}
};
}
fn Format_SI_Formatter(comptime T: type, comptime unit: []const u8) type {
return switch (@typeInfo(T)) {
.float, .comptime_float => std.fmt.Formatter(format_si_float),
.int => Format_SI_Int(T, .{ .unit = unit }).Formatter,
.comptime_int => Format_SI_Int(i64, .{ .unit = unit }).Formatter,
else => @compileError("Expected float or int value"),
};
}
pub fn fmtSI(value: anytype, comptime unit: []const u8) Format_SI_Formatter(@TypeOf(value), unit) {
switch (@typeInfo(@TypeOf(value))) {
.float, .comptime_float => {
const data = Format_SI_Float_Data{ .value = value, .unit = unit };
return .{ .data = data };
},
.int => {
const data = Format_SI_Int(@TypeOf(value), .{ .unit = unit }) { .value = value };
return .{ .data = data };
},
.comptime_int => {
const data = Format_SI_Int(i64, .{ .unit = unit }) { .value = @intCast(value) };
return .{ .data = data };
},
else => @compileError("Expected float or int value"),
}
}
/// N.B this is mainly useful for small floating point durations; consider using std.fmt.fmtDuration for longer periods
pub inline fn fmtSeconds(value: anytype) Format_SI_Formatter(@TypeOf(value), "s") { return fmtSI(value, "s"); }
pub inline fn fmtGrams(value: anytype) Format_SI_Formatter(@TypeOf(value), "g") { return fmtSI(value, "g"); }
pub inline fn fmtMeters(value: anytype) Format_SI_Formatter(@TypeOf(value), "m") { return fmtSI(value, "m"); }
pub inline fn fmtLiters(value: anytype) Format_SI_Formatter(@TypeOf(value), "L") { return fmtSI(value, "L"); }
pub inline fn fmtKelvins(value: anytype) Format_SI_Formatter(@TypeOf(value), "K") { return fmtSI(value, "K"); }
pub inline fn fmtRadians(value: anytype) Format_SI_Formatter(@TypeOf(value), "rad") { return fmtSI(value, "rad"); }
pub inline fn fmtHertz(value: anytype) Format_SI_Formatter(@TypeOf(value), "Hz") { return fmtSI(value, "Hz"); }
pub inline fn fmtVolts(value: anytype) Format_SI_Formatter(@TypeOf(value), "V") { return fmtSI(value, "V"); }
pub inline fn fmtAmps(value: anytype) Format_SI_Formatter(@TypeOf(value), "A") { return fmtSI(value, "A"); }
pub inline fn fmtWatts(value: anytype) Format_SI_Formatter(@TypeOf(value), "W") { return fmtSI(value, "W"); }
pub inline fn fmtJoules(value: anytype) Format_SI_Formatter(@TypeOf(value), "J") { return fmtSI(value, "J"); }
pub inline fn fmtOhms(value: anytype) Format_SI_Formatter(@TypeOf(value), "\u{3A9}") { return fmtSI(value, "\u{3A9}"); }
pub inline fn fmtFarads(value: anytype) Format_SI_Formatter(@TypeOf(value), "F") { return fmtSI(value, "F"); }
pub inline fn fmtHenries(value: anytype) Format_SI_Formatter(@TypeOf(value), "H") { return fmtSI(value, "H"); }
test fmtSI {
var buf: [24]u8 = undefined;
inline for (.{
.{ .u = "m", .fmt = "{d}", .s = "0 m", .b = 0 },
.{ .u = "m", .fmt = "{d}", .s = "1 m", .b = 1 },
.{ .u = "m", .fmt = "{d}", .s = "999 m", .b = 999 },
.{ .u = "m", .fmt = "{d}", .s = "1 km", .b = 1000 },
.{ .u = "m", .fmt = "{d}", .s = "1 km", .b = 1423 },
.{ .u = "m", .fmt = "{d}", .s = "1 km", .b = 1999 },
.{ .u = "m", .fmt = "{d:.3}", .s = "1.023 km", .b = 1023 },
.{ .u = "m", .fmt = "{d:.3}", .s = "1.023 Mm", .b = 1023456 },
.{ .u = "m", .fmt = "{d:=>10}", .s = "=======0 m", .b = 0 },
.{ .u = "m", .fmt = "{d:=<10}", .s = "1 m=======", .b = 1 },
.{ .u = "m", .fmt = "{d:^10}", .s = " 102 km ", .b = 102400 },
}) |tc| {
const slice = try std.fmt.bufPrint(&buf, tc.fmt, .{ fmtSI(tc.b, tc.u) });
try std.testing.expectEqualStrings(tc.s, slice);
}
inline for (.{
.{ .u = "m", .fmt = "{d}", .s = "0 m", .b = 0 },
.{ .u = "m", .fmt = "{d}", .s = "1 m", .b = 1 },
.{ .u = "m", .fmt = "{d}", .s = "999 m", .b = 999 },
.{ .u = "m", .fmt = "{d}", .s = "1 km", .b = 1000 },
.{ .u = "m", .fmt = "{d}", .s = "1.423 km", .b = 1423 },
.{ .u = "m", .fmt = "{d}", .s = "1.999 km", .b = 1999 },
.{ .u = "m", .fmt = "{d:.3}", .s = "1.023 km", .b = 1023 },
.{ .u = "m", .fmt = "{d:.3}", .s = "1.023 Mm", .b = 1023456 },
.{ .u = "m", .fmt = "{d:=>10}", .s = "=======0 m", .b = 0 },
.{ .u = "m", .fmt = "{d:=<10}", .s = "1 m=======", .b = 1 },
.{ .u = "m", .fmt = "{d:^10}", .s = " 102.4 km ", .b = 102400 },
}) |tc| {
const slice = try std.fmt.bufPrint(&buf, tc.fmt, .{ fmtSI(@as(f64, tc.b), tc.u) });
try std.testing.expectEqualStrings(tc.s, slice);
}
}
const std = @import("std");