字符串格式化和解析。
builtinstdmathassertmemexpectFmttestingAllocatorWriterfloatdefault_max_depthAlignmentCasedefault_alignmentdefault_fill_charOptionsNumberPlaceholderSpecifierParserArgSetTypeArgStateprintIntdigits2AltaltotherParseIntErrorparseIntparseIntWithGenericCharacterparseIntWithSignparseUnsignedparseIntSizeSuffixparseFloatParseFloatErrorcharToDigitdigitToCharBufPrintErrorbufPrintbufPrintSentinelcountallocPrintallocPrintSentinelcomptimePrintexpectArrayFmtformatSelfTypefoobytesToHexhexToBytesRformathex_charsethex
std.fmt.Alignment
容器
字段
left
center
right
std.fmt.Allocator
别名
别名目标 std.mem.Allocator
std.fmt.allocPrint
函数
已弃用,请改用 Allocator.print。
pub fn allocPrint(gpa: Allocator, comptime fmt: []const u8, args: anytype) Allocator.Error![]u8
参数
gpa: Allocator
fmt: []const u8
错误
- OutOfMemory
std.fmt.allocPrintSentinel
函数
已弃用,请改用 Allocator.printSentinel。
pub fn allocPrintSentinel( gpa: Allocator, comptime fmt: []const u8, args: anytype, comptime sentinel: u8, ) Allocator.Error![:sentinel]u8
参数
错误
- OutOfMemory
std.fmt.alt
函数
用于调用名称不是“format”的其他格式化方法的辅助函数。
pub fn alt( context: anytype, comptime func_name: @EnumLiteral(), ) Alt(@TypeOf(context), @field(@TypeOf(context), @tagName(func_name)))
参数
func_name: @EnumLiteral()
用法示例
test alt {
const Example = struct {
number: u8,
pub fn other(ex: @This(), w: *Writer) Writer.Error!void {
try w.writeByte(ex.number);
}
};
const ex: Example = .{ .number = 'a' };
try expectFmt("a", "{f}", .{alt(ex, .other)});
}
std.fmt.Alt
类型函数
创建适合实例化并传给 "{f}" 占位符的类型。
pub fn Alt( comptime Data: type, comptime formatFn: fn (data: Data, writer: *Writer) Writer.Error!void, ) type
参数
字段
data: Data
std.fmt.Alt.format
函数
pub inline fn format(self: @This(), writer: *Writer) Writer.Error!void
参数
self: @This()
writer: *Writer
错误
- WriteFailed
详细诊断信息见
Writer实现。
std.fmt.ArgSetType
类型
std.fmt.ArgState
容器
字段
std.fmt.ArgState.hasUnusedArgs
函数
pub fn hasUnusedArgs(self: *@This()) bool
参数
self: *@This()
std.fmt.ArgState.nextArg
函数
pub fn nextArg(self: *@This(), arg_index: ?usize) ?usize
参数
self: *@This()
arg_index: ?usize
std.fmt.assert
别名
别名目标 std.debug.assert
std.fmt.bufPrint
函数
已弃用,改用 mem.print。
pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintError![]u8
参数
buf: []u8
fmt: []const u8
错误
- NoSpaceLeft PrintError
已尽量向缓冲区写入数据,但缓冲区太小, 无法容纳所有打印字节。
std.fmt.BufPrintError
别名
已弃用,改用 mem.PrintError。
别名目标 std.mem.PrintError
std.fmt.bufPrintSentinel
函数
已弃用,改用 mem.printSentinel。
pub fn bufPrintSentinel( buf: []u8, comptime fmt: []const u8, args: anytype, comptime sentinel: u8, ) BufPrintError![:sentinel]u8
参数
buf: []u8
fmt: []const u8
sentinel: u8
错误
- NoSpaceLeft PrintError
已尽量向缓冲区写入数据,但缓冲区太小, 无法容纳所有打印字节。
std.fmt.builtin
常量
std.fmt.bytesToHex
函数
将字节序列编码为十六进制数字。 返回包含编码后字节的数组。
pub fn bytesToHex(input: anytype, case: Case) [input.len * 2]u8
参数
case: Case
用法示例
test bytesToHex {
const input = "input slice";
const encoded = bytesToHex(input, .lower);
var decoded: [input.len]u8 = undefined;
try testing.expectEqualSlices(u8, input, try hexToBytes(&decoded, &encoded));
}
std.fmt.Case
容器
字段
lower
upper
std.fmt.charToDigit
函数
pub fn charToDigit(c: u8, base: u8) (error{InvalidCharacter}!u8)
参数
c: u8
base: u8
std.fmt.comptimePrint
函数
pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args):0]u8
参数
fmt: []const u8
用法示例
test comptimePrint {
@setEvalBranchQuota(2000);
try testing.expectEqual(*const [3:0]u8, @TypeOf(comptimePrint("{}", .{100})));
try testing.expectEqualSlices(u8, "100", comptimePrint("{}", .{100}));
try testing.expectEqualStrings("30", comptimePrint("{d}", .{30.0}));
try testing.expectEqualStrings("30.0", comptimePrint("{d:3.1}", .{30.0}));
try testing.expectEqualStrings("0.05", comptimePrint("{d}", .{0.05}));
try testing.expectEqualStrings("5e-2", comptimePrint("{e}", .{0.05}));
}
std.fmt.count
函数
计算格式化所需的字符数。
pub fn count(comptime fmt: []const u8, args: anytype) usize
参数
fmt: []const u8
std.fmt.default_alignment
常量
std.fmt.default_fill_char
常量
std.fmt.default_max_depth
常量
std.fmt.digits2
函数
将 [0, 100) 范围内的值转换为十进制字符串。
pub fn digits2(value: u8) [2]u8
参数
value: u8
std.fmt.digitToChar
函数
pub fn digitToChar(digit: u8, case: Case) u8
参数
digit: u8
case: Case
std.fmt.expectArrayFmt
函数
fn expectArrayFmt(expected: []const u8, comptime template: []const u8, comptime array_value: anytype) !void
参数
expected: []const u8
template: []const u8
std.fmt.expectFmt
别名
std.fmt.float
别名
别名目标 std.fmt.float
std.fmt.foo
函数
fn foo() void
std.fmt.format
函数
pub fn format(s: @This(), writer: *Writer) Writer.Error!void
参数
s: @This()
writer: *Writer
错误
- WriteFailed
详细诊断信息见
Writer实现。
std.fmt.format
函数
pub fn format(self: R, writer: *Writer) Writer.Error!void
参数
错误
- WriteFailed
详细诊断信息见
Writer实现。
std.fmt.hex
函数
将位宽为 u8 整数倍的无符号整数转换为小端序的 小写十六进制字节数组。
pub fn hex(x: anytype) [@typeInfo(@TypeOf(x)).int.bits / 4]u8
用法示例
test hex {
{
const x = hex(@as(u32, 0xdeadbeef));
try testing.expect(x.len == 8);
try testing.expectEqualStrings("efbeadde", &x);
}
{
const s = "[" ++ hex(@as(u48, 0x12345678_abcd)) ++ "]";
try testing.expect(s.len == 14);
try testing.expectEqualStrings("[cdab78563412]", s);
}
{
const s = "[" ++ hex(@as(u64, 0x12345678_abcdef00)) ++ "]";
try testing.expect(s.len == 18);
try testing.expectEqualStrings("[00efcdab78563412]", s);
}
}
std.fmt.hex_charset
常量
std.fmt.hexToBytes
函数
解码指定十六进制字符字符串表示的字节序列。 返回包含解码字节的输出缓冲区切片。
pub fn hexToBytes(out: []u8, input: []const u8) ![]u8
参数
out: []u8
input: []const u8
用法示例
test hexToBytes {
const repeated: []const u8 = repeated: {
const buf: [32][2]u8 = @splat("90".*);
break :repeated @ptrCast(&buf);
};
var buf: [32]u8 = undefined;
try expectFmt(repeated, "{X}", .{try hexToBytes(&buf, repeated)});
try expectFmt("ABCD", "{X}", .{try hexToBytes(&buf, "ABCD")});
try expectFmt("", "{X}", .{try hexToBytes(&buf, "")});
try testing.expectError(error.InvalidCharacter, hexToBytes(&buf, "012Z"));
try testing.expectError(error.InvalidLength, hexToBytes(&buf, "AAA"));
try testing.expectError(error.NoSpaceLeft, hexToBytes(buf[0..1], "ABAB"));
}
std.fmt.math
别名
别名目标 std.math
std.fmt.mem
别名
别名目标 std.mem
std.fmt.Number
容器
字段
mode: Mode = .decimal
case: Case = .lower同时影响十六进制数字以及浮点数的 "inf"/"INF"。
precision: ?usize = null
width: ?usize = null
alignment: Alignment = default_alignment
fill: u8 = default_fill_char
std.fmt.Number.Mode
容器
字段
std.fmt.Number.Mode.base
函数
pub fn base(mode: Mode) ?u8
参数
mode: Mode
std.fmt.Options
容器
字段
precision: ?usize = null
width: ?usize = null
alignment: Alignment = default_alignment
fill: u8 = default_fill_char
std.fmt.Options.toNumber
函数
pub fn toNumber(o: Options, mode: Number.Mode, case: Case) Number
参数
std.fmt.other
函数
pub fn other(ex: @This(), w: *Writer) Writer.Error!void
参数
ex: @This()
w: *Writer
错误
- WriteFailed
详细诊断信息见
Writer实现。
std.fmt.parseFloat
别名
std.fmt.ParseFloatError
别名
std.fmt.parseInt
函数
将字符串 buf 解析为以指定进制表示的有符号或无符号
T 类型整数值。
当 base 为零时,检查字符串前缀以确定实际进制:
- 前缀 "0b" 表示 base=2;
- 前缀 "0o" 表示 base=8;
- 前缀 "0x" 表示 base=16;
- 否则假定 base=10。
忽略 buf 中的字符 '_'。
另请参见 parseUnsigned。
pub fn parseInt(comptime T: type, buf: []const u8, base: u8) ParseIntError!T
参数
T: type
buf: []const u8
base: u8
错误
- InvalidCharacter
输入为空或包含无效字符。
- Overflow
结果无法由指定类型容纳。
用法示例
test parseInt {
try testing.expectEqual(-10, try parseInt(i32, "-10", 10));
try testing.expectEqual(10, try parseInt(i32, "+10", 10));
try testing.expectEqual(10, try parseInt(u32, "+10", 10));
try testing.expectError(error.Overflow, parseInt(u32, "-10", 10));
try testing.expectError(error.InvalidCharacter, parseInt(u32, " 10", 10));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "10 ", 10));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "_10_", 10));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "0x_10_", 10));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "0x10_", 10));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "0x_10", 10));
try testing.expectEqual(255, try parseInt(u8, "255", 10));
try testing.expectError(error.Overflow, parseInt(u8, "256", 10));
// +0 和 -0 应适用于无符号类型
try testing.expectEqual(0, try parseInt(u8, "-0", 10));
try testing.expectEqual(0, try parseInt(u8, "+0", 10));
// 确保正确解析 minInt
try testing.expectEqual(math.minInt(i1), try parseInt(i1, "-1", 10));
try testing.expectEqual(math.minInt(i8), try parseInt(i8, "-128", 10));
try testing.expectEqual(math.minInt(i43), try parseInt(i43, "-4398046511104", 10));
// 空字符串或单独的 +- 无效
try testing.expectError(error.InvalidCharacter, parseInt(u32, "", 10));
try testing.expectError(error.InvalidCharacter, parseInt(i32, "", 10));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "+", 10));
try testing.expectError(error.InvalidCharacter, parseInt(i32, "+", 10));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "-", 10));
try testing.expectError(error.InvalidCharacter, parseInt(i32, "-", 10));
// 自动检测基数
try testing.expectEqual(111, try parseInt(i32, "111", 0));
try testing.expectEqual(111, try parseInt(i32, "1_1_1", 0));
try testing.expectEqual(111, try parseInt(i32, "1_1_1", 0));
try testing.expectEqual(7, try parseInt(i32, "+0b111", 0));
try testing.expectEqual(7, try parseInt(i32, "+0B111", 0));
try testing.expectEqual(7, try parseInt(i32, "+0b1_11", 0));
try testing.expectEqual(73, try parseInt(i32, "+0o111", 0));
try testing.expectEqual(73, try parseInt(i32, "+0O111", 0));
try testing.expectEqual(73, try parseInt(i32, "+0o11_1", 0));
try testing.expectEqual(273, try parseInt(i32, "+0x111", 0));
try testing.expectEqual(-7, try parseInt(i32, "-0b111", 0));
try testing.expectEqual(-7, try parseInt(i32, "-0b11_1", 0));
try testing.expectEqual(-73, try parseInt(i32, "-0o111", 0));
try testing.expectEqual(-273, try parseInt(i32, "-0x111", 0));
try testing.expectEqual(-273, try parseInt(i32, "-0X111", 0));
try testing.expectEqual(-273, try parseInt(i32, "-0x1_11", 0));
// 仅有二进制、八进制或十进制前缀无效
try testing.expectError(error.InvalidCharacter, parseInt(u32, "0b", 0));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "0o", 0));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "0x", 0));
// 此前因基数使 T 溢出而报错的边界情况
try testing.expectEqual(@as(i2, -2), try std.fmt.parseInt(i2, "-10", 2));
try testing.expectEqual(@as(i4, -8), try std.fmt.parseInt(i4, "-10", 8));
try testing.expectEqual(@as(i5, -16), try std.fmt.parseInt(i5, "-10", 16));
}
std.fmt.ParseIntError
错误集合
错误
- InvalidCharacter
输入为空或包含无效字符。
- Overflow
结果无法由指定类型容纳。
std.fmt.parseIntSizeSuffix
函数
解析形如 '2G'、'2Gi' 或 '2GiB' 的数字。
pub fn parseIntSizeSuffix(buf: []const u8, digit_base: u8) ParseIntError!usize
参数
buf: []const u8
digit_base: u8
错误
- InvalidCharacter
输入为空或包含无效字符。
- Overflow
结果无法由指定类型容纳。
用法示例
test parseIntSizeSuffix {
try testing.expectEqual(2, try parseIntSizeSuffix("2", 10));
try testing.expectEqual(2, try parseIntSizeSuffix("2B", 10));
try testing.expectEqual(2000, try parseIntSizeSuffix("2kB", 10));
try testing.expectEqual(2000, try parseIntSizeSuffix("2k", 10));
try testing.expectEqual(2048, try parseIntSizeSuffix("2KiB", 10));
try testing.expectEqual(2048, try parseIntSizeSuffix("2Ki", 10));
try testing.expectEqual(10240, try parseIntSizeSuffix("aKiB", 16));
try testing.expectError(error.InvalidCharacter, parseIntSizeSuffix("", 10));
try testing.expectError(error.InvalidCharacter, parseIntSizeSuffix("2iB", 10));
}
std.fmt.parseIntWithGenericCharacter
函数
类似于 parseInt,但使用泛型 Character 类型。
pub fn parseIntWithGenericCharacter( comptime Result: type, comptime Character: type, buf: []const Character, base: u8, ) ParseIntError!Result
参数
Result: type
Character: type
buf: []const Character
base: u8
错误
- InvalidCharacter
输入为空或包含无效字符。
- Overflow
结果无法由指定类型容纳。
std.fmt.parseIntWithSign
函数
fn parseIntWithSign( comptime Result: type, comptime Character: type, buf: []const Character, base: u8, comptime sign: enum { pos, neg }, ) ParseIntError!Result
参数
Result: type
Character: type
buf: []const Character
base: u8
sign: enum { pos, neg }
错误
- InvalidCharacter
输入为空或包含无效字符。
- Overflow
结果无法由指定类型容纳。
std.fmt.Parser
容器
基于流的格式字符串解析器。
可用于实现兼容 std.fmt 的格式化器,而无需重复 实现标准库的行为。
字段
bytes: []const u8
i: usize
std.fmt.Parser.char
函数
pub fn char(self: *@This()) ?u8
参数
self: *@This()
std.fmt.Parser.maybe
函数
pub fn maybe(self: *@This(), byte: u8) bool
参数
self: *@This()
byte: u8
std.fmt.Parser.number
函数
pub fn number(self: *@This()) ?usize
参数
self: *@This()
std.fmt.Parser.peek
函数
pub fn peek(self: *@This(), i: usize) ?u8
参数
self: *@This()
i: usize
std.fmt.Parser.specifier
函数
pub fn specifier(self: *@This()) !Specifier
参数
self: *@This()
std.fmt.Parser.until
函数
pub fn until(self: *@This(), delimiter: u8) []const u8
参数
self: *@This()
delimiter: u8
std.fmt.parseUnsigned
函数
按指定进制,将字符串 buf 作为无符号表示解析为 T 类型的整数值。
base 为零时,检查字符串前缀以确定实际进制:
- 前缀“0b”表示 base=2。
- 前缀“0o”表示 base=8。
- 前缀“0x”表示 base=16。
- 否则假定 base=10。
忽略 buf 中的“_”字符。
另见 parseInt。
pub fn parseUnsigned(comptime T: type, buf: []const u8, base: u8) ParseIntError!T
参数
T: type
buf: []const u8
base: u8
错误
- InvalidCharacter
输入为空或包含无效字符。
- Overflow
结果无法由指定类型容纳。
用法示例
test parseUnsigned {
try testing.expectEqual(50124, try parseUnsigned(u16, "050124", 10));
try testing.expectEqual(65535, try parseUnsigned(u16, "65535", 10));
try testing.expectEqual(65535, try parseUnsigned(u16, "65_535", 10));
try testing.expectError(error.Overflow, parseUnsigned(u16, "65536", 10));
try testing.expectEqual(0xffffffffffffffff, try parseUnsigned(u64, "0ffffffffffffffff", 16));
try testing.expectEqual(0xffffffffffffffff, try parseUnsigned(u64, "0f_fff_fff_fff_fff_fff", 16));
try testing.expectError(error.Overflow, parseUnsigned(u64, "10000000000000000", 16));
try testing.expectEqual(0xDEADBEEF, try parseUnsigned(u32, "DeadBeef", 16));
try testing.expectEqual(1, try parseUnsigned(u7, "1", 10));
try testing.expectEqual(8, try parseUnsigned(u7, "1000", 2));
try testing.expectError(error.InvalidCharacter, parseUnsigned(u32, "f", 10));
try testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "109", 8));
try testing.expectEqual(1442151747, try parseUnsigned(u32, "NUMBER", 36));
// 尽管基数本身无法容纳于目标类型,这些数值仍应能够容纳
try testing.expectEqual(0, try parseUnsigned(u1, "0", 10));
try testing.expectEqual(1, try parseUnsigned(u1, "1", 10));
try testing.expectError(error.Overflow, parseUnsigned(u1, "2", 10));
try testing.expectEqual(1, try parseUnsigned(u1, "001", 16));
try testing.expectEqual(3, try parseUnsigned(u2, "3", 16));
try testing.expectError(error.Overflow, parseUnsigned(u2, "4", 16));
// parseUnsigned 不接受符号
try testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "+0", 10));
try testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "-0", 10));
// 测试空字符串错误
try testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "", 10));
}
std.fmt.Placeholder
容器
字段
std.fmt.Placeholder.parse
函数
pub fn parse(comptime bytes: []const u8) Placeholder
参数
bytes: []const u8
std.fmt.printInt
函数
断言渲染后的整数值能放入 buffer。
返回其在 buffer 内的结束索引。
pub fn printInt(buffer: []u8, value: anytype, base: u8, case: Case, options: Options) usize
参数
用法示例
test printInt {
const x: i32 = -3;
var buffer: [64]u8 = undefined;
const s = buffer[0..printInt(&buffer, x, 10, .lower, .{})];
try testing.expectEqualStrings("-3", s);
}
std.fmt.R
别名
别名目标 std.fmt
std.fmt.SelfType
别名
别名目标 std.fmt
std.fmt.Specifier
容器
字段
none
number: usize
named: []const u8
std.fmt.std
别名
别名目标 std
std.fmt.testing
别名
别名目标 std.testing
std.fmt.Writer
别名
别名目标 std.Io.Writer
源码
std/fmt.zig
//! 字符串格式化和解析。
const builtin = @import("builtin");
const std = @import("std.zig");
const math = std.math;
const assert = std.debug.assert;
const mem = std.mem;
const expectFmt = std.testing.expectFmt;
const testing = std.testing;
const Allocator = std.mem.Allocator;
const Writer = std.Io.Writer;
pub const float = @import("fmt/float.zig");
pub const default_max_depth = 3;
pub const Alignment = enum {
left,
center,
right,
};
pub const Case = enum { lower, upper };
const default_alignment = .right;
const default_fill_char = ' ';
pub const Options = struct {
precision: ?usize = null,
width: ?usize = null,
alignment: Alignment = default_alignment,
fill: u8 = default_fill_char,
pub fn toNumber(o: Options, mode: Number.Mode, case: Case) Number {
return .{
.mode = mode,
.case = case,
.precision = o.precision,
.width = o.width,
.alignment = o.alignment,
.fill = o.fill,
};
}
};
pub const Number = struct {
mode: Mode = .decimal,
/// 同时影响十六进制数字以及浮点数的 "inf"/"INF"。
case: Case = .lower,
precision: ?usize = null,
width: ?usize = null,
alignment: Alignment = default_alignment,
fill: u8 = default_fill_char,
pub const Mode = enum {
decimal,
binary,
octal,
hex,
scientific,
pub fn base(mode: Mode) ?u8 {
return switch (mode) {
.decimal => 10,
.binary => 2,
.octal => 8,
.hex => 16,
.scientific => null,
};
}
};
};
pub const Placeholder = struct {
specifier_arg: []const u8,
fill: u8,
alignment: Alignment,
arg: Specifier,
width: Specifier,
precision: Specifier,
pub fn parse(comptime bytes: []const u8) Placeholder {
var parser: Parser = .{ .bytes = bytes, .i = 0 };
const arg = parser.specifier() catch |err| @compileError(@errorName(err));
const specifier_arg = parser.until(':');
if (parser.char()) |b| {
if (b != ':') @compileError("expected : or }, found '" ++ &[1]u8{b} ++ "'");
}
// 解析填充字节(如果存在)。
//
// 若同时指定宽度字段,填充字节后必须
// 跟随对齐说明符;唯一例外为 '0'(零),
// 它将作为宽度说明符的一部分处理。
var fill: ?u8 = if (parser.peek(1)) |b|
switch (b) {
'<', '^', '>' => parser.char(),
else => null,
}
else
null;
// 解析对齐参数
const alignment: ?Alignment = if (parser.peek(0)) |b| init: {
switch (b) {
'<', '^', '>' => {
// 消耗该字符
break :init switch (parser.char().?) {
'<' => .left,
'^' => .center,
else => .right,
};
},
else => break :init null,
}
} else null;
// 未提供填充字符和对齐说明符时,
// 检查宽度是否以零开头。
if (fill == null and alignment == null) {
fill = if (parser.peek(0) == '0') '0' else null;
}
// 解析 width 参数
const width = parser.specifier() catch |err| @compileError(@errorName(err));
// 若存在点号,则跳过
if (parser.char()) |b| {
if (b != '.') @compileError("expected . or }, found '" ++ &[1]u8{b} ++ "'");
}
// 解析精度参数
const precision = parser.specifier() catch |err| @compileError(@errorName(err));
if (parser.char()) |b| @compileError("extraneous trailing character '" ++ &[1]u8{b} ++ "'");
const specifier_array = specifier_arg[0..specifier_arg.len].*;
return .{
.specifier_arg = &specifier_array,
.fill = fill orelse default_fill_char,
.alignment = alignment orelse default_alignment,
.arg = arg,
.width = width,
.precision = precision,
};
}
};
pub const Specifier = union(enum) {
none,
number: usize,
named: []const u8,
};
/// 基于流的格式字符串解析器。
///
/// 可用于实现兼容 std.fmt 的格式化器,而无需重复
/// 实现标准库的行为。
pub const Parser = struct {
bytes: []const u8,
i: usize,
pub fn number(self: *@This()) ?usize {
var r: ?usize = null;
while (self.peek(0)) |byte| {
switch (byte) {
'0'...'9' => {
if (r == null) r = 0;
r.? *= 10;
r.? += byte - '0';
},
else => break,
}
self.i += 1;
}
return r;
}
pub fn until(self: *@This(), delimiter: u8) []const u8 {
const start = self.i;
self.i = std.mem.findScalarPos(u8, self.bytes, self.i, delimiter) orelse self.bytes.len;
return self.bytes[start..self.i];
}
pub fn char(self: *@This()) ?u8 {
const i = self.i;
if (self.bytes.len - i == 0) return null;
self.i = i + 1;
return self.bytes[i];
}
pub fn maybe(self: *@This(), byte: u8) bool {
if (self.peek(0) == byte) {
self.i += 1;
return true;
}
return false;
}
pub fn specifier(self: *@This()) !Specifier {
if (self.maybe('[')) {
const arg_name = self.until(']');
if (!self.maybe(']')) return error.@"Expected closing ]";
return .{ .named = arg_name };
}
if (self.number()) |i| return .{ .number = i };
return .{ .none = {} };
}
pub fn peek(self: *@This(), i: usize) ?u8 {
const peek_index = self.i + i;
if (peek_index >= self.bytes.len) return null;
return self.bytes[peek_index];
}
};
pub const ArgSetType = u32;
pub const ArgState = struct {
next_arg: usize = 0,
used_args: ArgSetType = 0,
args_len: usize,
pub fn hasUnusedArgs(self: *@This()) bool {
return @popCount(self.used_args) != self.args_len;
}
pub fn nextArg(self: *@This(), arg_index: ?usize) ?usize {
const next_index = arg_index orelse init: {
const arg = self.next_arg;
self.next_arg += 1;
break :init arg;
};
if (next_index >= self.args_len) {
return null;
}
// 将此参数标记为已使用
self.used_args |= @as(ArgSetType, 1) << @as(u5, @intCast(next_index));
return next_index;
}
};
/// 断言渲染后的整数值能放入 `buffer`。
/// 返回其在 `buffer` 内的结束索引。
pub fn printInt(buffer: []u8, value: anytype, base: u8, case: Case, options: Options) usize {
var w: Writer = .fixed(buffer);
w.printInt(value, base, case, options) catch unreachable;
return w.end;
}
test printInt {
const x: i32 = -3;
var buffer: [64]u8 = undefined;
const s = buffer[0..printInt(&buffer, x, 10, .lower, .{})];
try testing.expectEqualStrings("-3", s);
}
/// 将 [0, 100) 范围内的值转换为十进制字符串。
pub fn digits2(value: u8) [2]u8 {
if (builtin.mode == .small) {
return .{ @intCast('0' + value / 10), @intCast('0' + value % 10) };
} else {
return "00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899"[value * 2 ..][0..2].*;
}
}
/// 创建适合实例化并传给 "{f}" 占位符的类型。
pub fn Alt(
comptime Data: type,
comptime formatFn: fn (data: Data, writer: *Writer) Writer.Error!void,
) type {
return struct {
data: Data,
pub inline fn format(self: @This(), writer: *Writer) Writer.Error!void {
try formatFn(self.data, writer);
}
};
}
/// 用于调用名称不是“format”的其他格式化方法的辅助函数。
pub fn alt(
context: anytype,
comptime func_name: @EnumLiteral(),
) Alt(@TypeOf(context), @field(@TypeOf(context), @tagName(func_name))) {
return .{ .data = context };
}
test alt {
const Example = struct {
number: u8,
pub fn other(ex: @This(), w: *Writer) Writer.Error!void {
try w.writeByte(ex.number);
}
};
const ex: Example = .{ .number = 'a' };
try expectFmt("a", "{f}", .{alt(ex, .other)});
}
pub const ParseIntError = error{
/// 结果无法由指定类型容纳。
Overflow,
/// 输入为空或包含无效字符。
InvalidCharacter,
};
/// 将字符串 `buf` 解析为以指定进制表示的有符号或无符号
/// `T` 类型整数值。
///
/// 当 `base` 为零时,检查字符串前缀以确定实际进制:
/// * 前缀 "0b" 表示 base=2;
/// * 前缀 "0o" 表示 base=8;
/// * 前缀 "0x" 表示 base=16;
/// * 否则假定 base=10。
///
/// 忽略 `buf` 中的字符 '_'。
/// 另请参见 `parseUnsigned`。
pub fn parseInt(comptime T: type, buf: []const u8, base: u8) ParseIntError!T {
return parseIntWithGenericCharacter(T, u8, buf, base);
}
/// 类似于 `parseInt`,但使用泛型 `Character` 类型。
pub fn parseIntWithGenericCharacter(
comptime Result: type,
comptime Character: type,
buf: []const Character,
base: u8,
) ParseIntError!Result {
if (buf.len == 0) return error.InvalidCharacter;
if (buf[0] == '+') return parseIntWithSign(Result, Character, buf[1..], base, .pos);
if (buf[0] == '-') return parseIntWithSign(Result, Character, buf[1..], base, .neg);
return parseIntWithSign(Result, Character, buf, base, .pos);
}
test parseInt {
try testing.expectEqual(-10, try parseInt(i32, "-10", 10));
try testing.expectEqual(10, try parseInt(i32, "+10", 10));
try testing.expectEqual(10, try parseInt(u32, "+10", 10));
try testing.expectError(error.Overflow, parseInt(u32, "-10", 10));
try testing.expectError(error.InvalidCharacter, parseInt(u32, " 10", 10));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "10 ", 10));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "_10_", 10));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "0x_10_", 10));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "0x10_", 10));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "0x_10", 10));
try testing.expectEqual(255, try parseInt(u8, "255", 10));
try testing.expectError(error.Overflow, parseInt(u8, "256", 10));
// +0 和 -0 应适用于无符号类型
try testing.expectEqual(0, try parseInt(u8, "-0", 10));
try testing.expectEqual(0, try parseInt(u8, "+0", 10));
// 确保正确解析 minInt
try testing.expectEqual(math.minInt(i1), try parseInt(i1, "-1", 10));
try testing.expectEqual(math.minInt(i8), try parseInt(i8, "-128", 10));
try testing.expectEqual(math.minInt(i43), try parseInt(i43, "-4398046511104", 10));
// 空字符串或单独的 +- 无效
try testing.expectError(error.InvalidCharacter, parseInt(u32, "", 10));
try testing.expectError(error.InvalidCharacter, parseInt(i32, "", 10));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "+", 10));
try testing.expectError(error.InvalidCharacter, parseInt(i32, "+", 10));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "-", 10));
try testing.expectError(error.InvalidCharacter, parseInt(i32, "-", 10));
// 自动检测基数
try testing.expectEqual(111, try parseInt(i32, "111", 0));
try testing.expectEqual(111, try parseInt(i32, "1_1_1", 0));
try testing.expectEqual(111, try parseInt(i32, "1_1_1", 0));
try testing.expectEqual(7, try parseInt(i32, "+0b111", 0));
try testing.expectEqual(7, try parseInt(i32, "+0B111", 0));
try testing.expectEqual(7, try parseInt(i32, "+0b1_11", 0));
try testing.expectEqual(73, try parseInt(i32, "+0o111", 0));
try testing.expectEqual(73, try parseInt(i32, "+0O111", 0));
try testing.expectEqual(73, try parseInt(i32, "+0o11_1", 0));
try testing.expectEqual(273, try parseInt(i32, "+0x111", 0));
try testing.expectEqual(-7, try parseInt(i32, "-0b111", 0));
try testing.expectEqual(-7, try parseInt(i32, "-0b11_1", 0));
try testing.expectEqual(-73, try parseInt(i32, "-0o111", 0));
try testing.expectEqual(-273, try parseInt(i32, "-0x111", 0));
try testing.expectEqual(-273, try parseInt(i32, "-0X111", 0));
try testing.expectEqual(-273, try parseInt(i32, "-0x1_11", 0));
// 仅有二进制、八进制或十进制前缀无效
try testing.expectError(error.InvalidCharacter, parseInt(u32, "0b", 0));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "0o", 0));
try testing.expectError(error.InvalidCharacter, parseInt(u32, "0x", 0));
// 此前因基数使 T 溢出而报错的边界情况
try testing.expectEqual(@as(i2, -2), try std.fmt.parseInt(i2, "-10", 2));
try testing.expectEqual(@as(i4, -8), try std.fmt.parseInt(i4, "-10", 8));
try testing.expectEqual(@as(i5, -16), try std.fmt.parseInt(i5, "-10", 16));
}
fn parseIntWithSign(
comptime Result: type,
comptime Character: type,
buf: []const Character,
base: u8,
comptime sign: enum { pos, neg },
) ParseIntError!Result {
if (buf.len == 0) return error.InvalidCharacter;
var buf_base = base;
var buf_start = buf;
if (base == 0) {
// 默认将其视为十进制数。
buf_base = 10;
// 通过查看 buf 前缀检测基数。
if (buf.len > 2 and buf[0] == '0') {
if (math.cast(u8, buf[1])) |c| switch (std.ascii.toLower(c)) {
'b' => {
buf_base = 2;
buf_start = buf[2..];
},
'o' => {
buf_base = 8;
buf_start = buf[2..];
},
'x' => {
buf_base = 16;
buf_start = buf[2..];
},
else => {},
};
}
}
const add = switch (sign) {
.pos => math.add,
.neg => math.sub,
};
// 累加到始终至少为 8 位的 Accumulate 中,以防止
// `buf_base` 使 Result 溢出。
const info = @typeInfo(Result);
const Accumulate = @Int(info.int.signedness, @max(8, info.int.bits));
var accumulate: Accumulate = 0;
if (buf_start[0] == '_' or buf_start[buf_start.len - 1] == '_') return error.InvalidCharacter;
for (buf_start) |c| {
if (c == '_') continue;
const digit = try charToDigit(math.cast(u8, c) orelse return error.InvalidCharacter, buf_base);
if (accumulate != 0) {
accumulate = try math.mul(Accumulate, accumulate, math.cast(Accumulate, buf_base) orelse return error.Overflow);
} else if (sign == .neg) {
// 负数的第一个数字。
// 考虑将 "-4" 解析为 i3。
// 这本应成功,但正数 4 会使 i3 溢出,因此不能将该数字转换为 T 后再相减。
accumulate = math.cast(Accumulate, -@as(i8, @intCast(digit))) orelse return error.Overflow;
continue;
}
accumulate = try add(Accumulate, accumulate, math.cast(Accumulate, digit) orelse return error.Overflow);
}
return if (Result == Accumulate)
accumulate
else
math.cast(Result, accumulate) orelse return error.Overflow;
}
/// 按指定进制,将字符串 `buf` 作为无符号表示解析为 `T` 类型的整数值。
///
/// `base` 为零时,检查字符串前缀以确定实际进制:
/// * 前缀“0b”表示 base=2。
/// * 前缀“0o”表示 base=8。
/// * 前缀“0x”表示 base=16。
/// * 否则假定 base=10。
///
/// 忽略 `buf` 中的“_”字符。
/// 另见 `parseInt`。
///
pub fn parseUnsigned(comptime T: type, buf: []const u8, base: u8) ParseIntError!T {
return parseIntWithSign(T, u8, buf, base, .pos);
}
test parseUnsigned {
try testing.expectEqual(50124, try parseUnsigned(u16, "050124", 10));
try testing.expectEqual(65535, try parseUnsigned(u16, "65535", 10));
try testing.expectEqual(65535, try parseUnsigned(u16, "65_535", 10));
try testing.expectError(error.Overflow, parseUnsigned(u16, "65536", 10));
try testing.expectEqual(0xffffffffffffffff, try parseUnsigned(u64, "0ffffffffffffffff", 16));
try testing.expectEqual(0xffffffffffffffff, try parseUnsigned(u64, "0f_fff_fff_fff_fff_fff", 16));
try testing.expectError(error.Overflow, parseUnsigned(u64, "10000000000000000", 16));
try testing.expectEqual(0xDEADBEEF, try parseUnsigned(u32, "DeadBeef", 16));
try testing.expectEqual(1, try parseUnsigned(u7, "1", 10));
try testing.expectEqual(8, try parseUnsigned(u7, "1000", 2));
try testing.expectError(error.InvalidCharacter, parseUnsigned(u32, "f", 10));
try testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "109", 8));
try testing.expectEqual(1442151747, try parseUnsigned(u32, "NUMBER", 36));
// 尽管基数本身无法容纳于目标类型,这些数值仍应能够容纳
try testing.expectEqual(0, try parseUnsigned(u1, "0", 10));
try testing.expectEqual(1, try parseUnsigned(u1, "1", 10));
try testing.expectError(error.Overflow, parseUnsigned(u1, "2", 10));
try testing.expectEqual(1, try parseUnsigned(u1, "001", 16));
try testing.expectEqual(3, try parseUnsigned(u2, "3", 16));
try testing.expectError(error.Overflow, parseUnsigned(u2, "4", 16));
// parseUnsigned 不接受符号
try testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "+0", 10));
try testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "-0", 10));
// 测试空字符串错误
try testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "", 10));
}
/// 解析形如 '2G'、'2Gi' 或 '2GiB' 的数字。
pub fn parseIntSizeSuffix(buf: []const u8, digit_base: u8) ParseIntError!usize {
var without_B = buf;
if (mem.endsWith(u8, buf, "B")) without_B.len -= 1;
var without_i = without_B;
var magnitude_base: usize = 1000;
if (mem.endsWith(u8, without_B, "i")) {
without_i.len -= 1;
magnitude_base = 1024;
}
if (without_i.len == 0) return error.InvalidCharacter;
const orders_of_magnitude: usize = switch (without_i[without_i.len - 1]) {
'k', 'K' => 1,
'M' => 2,
'G' => 3,
'T' => 4,
'P' => 5,
'E' => 6,
'Z' => 7,
'Y' => 8,
'R' => 9,
'Q' => 10,
else => 0,
};
var without_suffix = without_i;
if (orders_of_magnitude > 0) {
without_suffix.len -= 1;
} else if (without_i.len != without_B.len) {
return error.InvalidCharacter;
}
const multiplier = math.powi(usize, magnitude_base, orders_of_magnitude) catch |err| switch (err) {
error.Underflow => unreachable,
error.Overflow => |e| return e,
};
const number = try std.fmt.parseInt(usize, without_suffix, digit_base);
return math.mul(usize, number, multiplier);
}
test parseIntSizeSuffix {
try testing.expectEqual(2, try parseIntSizeSuffix("2", 10));
try testing.expectEqual(2, try parseIntSizeSuffix("2B", 10));
try testing.expectEqual(2000, try parseIntSizeSuffix("2kB", 10));
try testing.expectEqual(2000, try parseIntSizeSuffix("2k", 10));
try testing.expectEqual(2048, try parseIntSizeSuffix("2KiB", 10));
try testing.expectEqual(2048, try parseIntSizeSuffix("2Ki", 10));
try testing.expectEqual(10240, try parseIntSizeSuffix("aKiB", 16));
try testing.expectError(error.InvalidCharacter, parseIntSizeSuffix("", 10));
try testing.expectError(error.InvalidCharacter, parseIntSizeSuffix("2iB", 10));
}
pub const parseFloat = @import("fmt/parse_float.zig").parseFloat;
pub const ParseFloatError = @import("fmt/parse_float.zig").ParseFloatError;
test {
_ = &parseFloat;
}
pub fn charToDigit(c: u8, base: u8) (error{InvalidCharacter}!u8) {
const value = switch (c) {
'0'...'9' => c - '0',
'A'...'Z' => c - 'A' + 10,
'a'...'z' => c - 'a' + 10,
else => return error.InvalidCharacter,
};
if (value >= base) return error.InvalidCharacter;
return value;
}
pub fn digitToChar(digit: u8, case: Case) u8 {
return switch (digit) {
0...9 => digit + '0',
10...35 => digit + ((if (case == .upper) @as(u8, 'A') else @as(u8, 'a')) - 10),
else => unreachable,
};
}
/// 已弃用,改用 `mem.PrintError`。
pub const BufPrintError = mem.PrintError;
/// 已弃用,改用 `mem.print`。
pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintError![]u8 {
return mem.print(buf, fmt, args);
}
/// 已弃用,改用 `mem.printSentinel`。
pub fn bufPrintSentinel(
buf: []u8,
comptime fmt: []const u8,
args: anytype,
comptime sentinel: u8,
) BufPrintError![:sentinel]u8 {
return mem.printSentinel(buf, fmt, args, sentinel);
}
/// 计算格式化所需的字符数。
pub fn count(comptime fmt: []const u8, args: anytype) usize {
var trash_buffer: [64]u8 = undefined;
var dw: Writer.Discarding = .init(&trash_buffer);
dw.writer.print(fmt, args) catch |err| switch (err) {
error.WriteFailed => unreachable,
};
return @intCast(dw.count + dw.writer.end);
}
/// 已弃用,请改用 `Allocator.print`。
pub fn allocPrint(gpa: Allocator, comptime fmt: []const u8, args: anytype) Allocator.Error![]u8 {
return gpa.print(fmt, args);
}
/// 已弃用,请改用 `Allocator.printSentinel`。
pub fn allocPrintSentinel(
gpa: Allocator,
comptime fmt: []const u8,
args: anytype,
comptime sentinel: u8,
) Allocator.Error![:sentinel]u8 {
return gpa.printSentinel(fmt, args, sentinel);
}
pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args):0]u8 {
comptime {
var buf: [count(fmt, args):0]u8 = undefined;
_ = mem.print(&buf, fmt, args) catch unreachable;
buf[buf.len] = 0;
const final = buf;
return &final;
}
}
test comptimePrint {
@setEvalBranchQuota(2000);
try testing.expectEqual(*const [3:0]u8, @TypeOf(comptimePrint("{}", .{100})));
try testing.expectEqualSlices(u8, "100", comptimePrint("{}", .{100}));
try testing.expectEqualStrings("30", comptimePrint("{d}", .{30.0}));
try testing.expectEqualStrings("30.0", comptimePrint("{d:3.1}", .{30.0}));
try testing.expectEqualStrings("0.05", comptimePrint("{d}", .{0.05}));
try testing.expectEqualStrings("5e-2", comptimePrint("{e}", .{0.05}));
}
test "parse u64 digit too big" {
_ = parseUnsigned(u64, "123a", 10) catch |err| {
if (err == error.InvalidCharacter) return;
unreachable;
};
unreachable;
}
test "parse unsigned comptime" {
comptime {
try testing.expectEqual(2, try parseUnsigned(usize, "2", 10));
}
}
test "escaped braces" {
try expectFmt("escaped: {{foo}}\n", "escaped: {{{{foo}}}}\n", .{});
try expectFmt("escaped: {foo}\n", "escaped: {{foo}}\n", .{});
}
test "optional" {
{
const value: ?i32 = 1234;
try expectFmt("optional: 1234\n", "optional: {?}\n", .{value});
try expectFmt("optional: 1234\n", "optional: {?d}\n", .{value});
try expectFmt("optional: 4d2\n", "optional: {?x}\n", .{value});
}
{
const value: ?[]const u8 = "string";
try expectFmt("optional: string\n", "optional: {?s}\n", .{value});
}
{
const value: ?i32 = null;
try expectFmt("optional: null\n", "optional: {?}\n", .{value});
}
{
const value = @as(?*i32, @ptrFromInt(0xf000d000));
try expectFmt("optional: *i32@f000d000\n", "optional: {*}\n", .{value});
}
}
test "error" {
{
const value: anyerror!i32 = 1234;
try expectFmt("error union: 1234\n", "error union: {!}\n", .{value});
try expectFmt("error union: 1234\n", "error union: {!d}\n", .{value});
try expectFmt("error union: 4d2\n", "error union: {!x}\n", .{value});
}
{
const value: anyerror![]const u8 = "string";
try expectFmt("error union: string\n", "error union: {!s}\n", .{value});
}
{
const value: anyerror!i32 = error.InvalidChar;
try expectFmt("error union: error.InvalidChar\n", "error union: {!}\n", .{value});
}
}
test "int.small" {
{
const value: u3 = 0b101;
try expectFmt("u3: 5\n", "u3: {}\n", .{value});
}
}
test "int.specifier" {
{
const value: u8 = 'a';
try expectFmt("u8: a\n", "u8: {c}\n", .{value});
}
{
const value: u8 = 0b1100;
try expectFmt("u8: 0b1100\n", "u8: 0b{b}\n", .{value});
}
{
const value: u16 = 0o1234;
try expectFmt("u16: 0o1234\n", "u16: 0o{o}\n", .{value});
}
{
const value: u8 = 'a';
try expectFmt("UTF-8: a\n", "UTF-8: {u}\n", .{value});
}
{
const value: u21 = 0x1F310;
try expectFmt("UTF-8: 🌐\n", "UTF-8: {u}\n", .{value});
}
{
const value: u21 = 0xD800;
try expectFmt("UTF-8: �\n", "UTF-8: {u}\n", .{value});
}
{
const value: u21 = 0x110001;
try expectFmt("UTF-8: �\n", "UTF-8: {u}\n", .{value});
}
}
test "int.padded" {
try expectFmt("u8: ' 1'", "u8: '{:4}'", .{@as(u8, 1)});
try expectFmt("u8: '1000'", "u8: '{:0<4}'", .{@as(u8, 1)});
try expectFmt("u8: '0001'", "u8: '{:0>4}'", .{@as(u8, 1)});
try expectFmt("u8: '0100'", "u8: '{:0^4}'", .{@as(u8, 1)});
try expectFmt("i8: '-1 '", "i8: '{:<4}'", .{@as(i8, -1)});
try expectFmt("i8: ' -1'", "i8: '{:>4}'", .{@as(i8, -1)});
try expectFmt("i8: ' -1 '", "i8: '{:^4}'", .{@as(i8, -1)});
try expectFmt("i16: '-1234'", "i16: '{:4}'", .{@as(i16, -1234)});
try expectFmt("i16: '+1234'", "i16: '{:4}'", .{@as(i16, 1234)});
try expectFmt("i16: '-12345'", "i16: '{:4}'", .{@as(i16, -12345)});
try expectFmt("i16: '+12345'", "i16: '{:4}'", .{@as(i16, 12345)});
try expectFmt("u16: '12345'", "u16: '{:4}'", .{@as(u16, 12345)});
}
test "buffer" {
{
var buf1: [32]u8 = undefined;
var w: Writer = .fixed(&buf1);
try w.printValue("", .{}, 1234, std.options.fmt_max_depth);
try testing.expectEqualStrings("1234", w.buffered());
w = .fixed(&buf1);
try w.printValue("c", .{}, 'a', std.options.fmt_max_depth);
try testing.expectEqualStrings("a", w.buffered());
w = .fixed(&buf1);
try w.printValue("b", .{}, 0b1100, std.options.fmt_max_depth);
try testing.expectEqualStrings("1100", w.buffered());
}
}
// 测试按值、通过单元素指针及作为切片格式化数组。
fn expectArrayFmt(expected: []const u8, comptime template: []const u8, comptime array_value: anytype) !void {
try expectFmt(expected, template, .{array_value});
try expectFmt(expected, template, .{&array_value});
var runtime_zero: usize = 0;
_ = &runtime_zero;
try expectFmt(expected, template, .{array_value[runtime_zero..]});
}
test "array" {
const value: [3]u8 = "abc".*;
try expectArrayFmt("array: abc\n", "array: {s}\n", value);
try expectArrayFmt("array: 616263\n", "array: {x}\n", value);
try expectArrayFmt("array: { 97, 98, 99 }\n", "array: {any}\n", value);
var buf: [100]u8 = undefined;
try expectFmt(
try mem.print(buf[0..], "array: [3]u8@{x}\n", .{@intFromPtr(&value)}),
"array: {*}\n",
.{&value},
);
}
test "slice" {
{
const value: []const u8 = "abc";
try expectFmt("slice: abc\n", "slice: {s}\n", .{value});
try expectFmt("slice: 616263\n", "slice: {x}\n", .{value});
try expectFmt("slice: { 97, 98, 99 }\n", "slice: {any}\n", .{value});
}
{
var runtime_zero: usize = 0;
_ = &runtime_zero;
const value = @as([*]align(1) const []const u8, @ptrFromInt(0xdeadbeef))[runtime_zero..runtime_zero];
try expectFmt("slice: []const u8@deadbeef\n", "slice: {*}\n", .{value});
}
{
const null_term_slice: [:0]const u8 = "\x00hello\x00";
try expectFmt("buf: \x00hello\x00\n", "buf: {s}\n", .{null_term_slice});
}
try expectFmt("buf: Test\n Other text", "buf: {s}\n Other text", .{"Test"});
{
var int_slice = [_]u32{ 1, 4096, 391891, 1111111111 };
const input: []const u32 = &int_slice;
try expectFmt("int: { 1, 4096, 391891, 1111111111 }", "int: {any}", .{input});
}
{
const S1 = struct {
x: u8,
};
const struct_slice: []const S1 = &[_]S1{ S1{ .x = 8 }, S1{ .x = 42 } };
try expectFmt("slice: { .{ .x = 8 }, .{ .x = 42 } }", "slice: {any}", .{struct_slice});
}
{
const S2 = struct {
x: u8,
pub fn format(s: @This(), writer: *Writer) Writer.Error!void {
try writer.print("S2({})", .{s.x});
}
};
const struct_slice: []const S2 = &[_]S2{ S2{ .x = 8 }, S2{ .x = 42 } };
try expectFmt("slice: { .{ .x = 8 }, .{ .x = 42 } }", "slice: {any}", .{struct_slice});
}
}
test "pointer" {
{
const value = @as(*align(1) i32, @ptrFromInt(0xdeadbeef));
try expectFmt("pointer: i32@deadbeef\n", "pointer: {}\n", .{value});
try expectFmt("pointer: i32@deadbeef\n", "pointer: {*}\n", .{value});
}
const FnPtr = *align(1) const fn () void;
{
const value = @as(FnPtr, @ptrFromInt(0xdeadbeef));
try expectFmt("pointer: fn () void@deadbeef\n", "pointer: {}\n", .{value});
}
{
const value = @as(FnPtr, @ptrFromInt(0xdeadbeef));
try expectFmt("pointer: fn () void@deadbeef\n", "pointer: {}\n", .{value});
}
}
test "cstr" {
try expectFmt(
"cstr: Test C\n",
"cstr: {s}\n",
.{@as([*c]const u8, @ptrCast("Test C"))},
);
}
test "struct" {
{
const Struct = struct {
field: u8,
};
const value = Struct{ .field = 42 };
try expectFmt("struct: .{ .field = 42 }\n", "struct: {}\n", .{value});
try expectFmt("struct: .{ .field = 42 }\n", "struct: {}\n", .{&value});
}
{
const Struct = struct {
a: u0,
b: u1,
};
const value = Struct{ .a = 0, .b = 1 };
try expectFmt("struct: .{ .a = 0, .b = 1 }\n", "struct: {}\n", .{value});
}
const S = struct {
a: u32,
b: anyerror,
};
const inst = S{
.a = 456,
.b = error.Unused,
};
try expectFmt(".{ .a = 456, .b = error.Unused }", "{}", .{inst});
// 元组
try expectFmt(".{ }", "{}", .{.{}});
try expectFmt(".{ -1 }", "{}", .{.{-1}});
try expectFmt(".{ -1, 42, 25000 }", "{}", .{.{ -1, 42, 0.25e5 }});
}
test "enum" {
const Enum = enum {
One,
Two,
};
const value = Enum.Two;
try expectFmt("enum: .Two\n", "enum: {}\n", .{value});
try expectFmt("enum: .Two\n", "enum: {}\n", .{&value});
try expectFmt("enum: .One\n", "enum: {}\n", .{Enum.One});
try expectFmt("enum: .Two\n", "enum: {}\n", .{Enum.Two});
// 测试非常大的枚举,确认编译期分支配额足够大
// TODO: https://github.com/ziglang/zig/issues/15609
if (!((builtin.cpu.arch == .wasm32) and builtin.mode == .debug)) {
try expectFmt("enum: .INVALID_FUNCTION\n", "enum: {}\n", .{std.os.windows.Win32Error.INVALID_FUNCTION});
}
const E = enum {
One,
Two,
Three,
};
const inst = E.Two;
try expectFmt(".Two", "{}", .{inst});
}
test "non-exhaustive enum" {
const Enum = enum(u16) {
One = 0x000f,
Two = 0xbeef,
_,
};
try expectFmt("enum: .One\n", "enum: {}\n", .{Enum.One});
try expectFmt("enum: .Two\n", "enum: {}\n", .{Enum.Two});
try expectFmt("enum: @fromBackingInt(4660)\n", "enum: {}\n", .{@as(Enum, @fromBackingInt(@intCast(0x1234)))});
try expectFmt("enum: f\n", "enum: {x}\n", .{Enum.One});
try expectFmt("enum: beef\n", "enum: {x}\n", .{Enum.Two});
try expectFmt("enum: BEEF\n", "enum: {X}\n", .{Enum.Two});
try expectFmt("enum: 1234\n", "enum: {x}\n", .{@as(Enum, @fromBackingInt(@intCast(0x1234)))});
try expectFmt("enum: 15\n", "enum: {d}\n", .{Enum.One});
try expectFmt("enum: 48879\n", "enum: {d}\n", .{Enum.Two});
try expectFmt("enum: 4660\n", "enum: {d}\n", .{@as(Enum, @fromBackingInt(@intCast(0x1234)))});
}
test "float.scientific" {
try expectFmt("f32: 1.34e0", "f32: {e}", .{@as(f32, 1.34)});
try expectFmt("f32: 1.234e1", "f32: {e}", .{@as(f32, 12.34)});
try expectFmt("f64: -1.234e11", "f64: {e}", .{@as(f64, -12.34e10)});
try expectFmt("f64: 9.99996e-40", "f64: {e}", .{@as(f64, 9.999960e-40)});
}
test "float.scientific.precision" {
try expectFmt("f64: 1.40971e-42", "f64: {e:.5}", .{@as(f64, 1.409706e-42)});
try expectFmt("f64: 1.00000e-9", "f64: {e:.5}", .{@as(f64, @as(f32, @bitCast(@as(u32, 814313563))))});
try expectFmt("f64: 7.81250e-3", "f64: {e:.5}", .{@as(f64, @as(f32, @bitCast(@as(u32, 1006632960))))});
// libc 将 1.000005e5 舍入为 1.00000e5,而 Zig 得到 1.00001e5。
// 实际上,libc 对许多精度位之后紧邻 5 的情况都不会向上舍入。
try expectFmt("f64: 1.00001e5", "f64: {e:.5}", .{@as(f64, @as(f32, @bitCast(@as(u32, 1203982400))))});
}
test "float.special" {
try expectFmt("f64: nan", "f64: {}", .{math.nan(f64)});
// IEEE 754 未定义负 nan,因此 ARM 会将其规范化为正 nan
//
if (builtin.target.cpu.arch != .arm) {
try expectFmt("f64: -nan", "f64: {}", .{-math.nan(f64)});
}
try expectFmt("f64: inf", "f64: {}", .{math.inf(f64)});
try expectFmt("f64: -inf", "f64: {}", .{-math.inf(f64)});
}
test "float.hexadecimal.special" {
try expectFmt("f64: nan", "f64: {x}", .{math.nan(f64)});
// IEEE 754 未定义负 nan,因此 ARM 会将其规范化为正 nan
//
if (builtin.target.cpu.arch != .arm) {
try expectFmt("f64: -nan", "f64: {x}", .{-math.nan(f64)});
}
try expectFmt("f64: inf", "f64: {x}", .{math.inf(f64)});
try expectFmt("f64: -inf", "f64: {x}", .{-math.inf(f64)});
try expectFmt("f64: 0x0.0p0", "f64: {x}", .{@as(f64, 0)});
try expectFmt("f64: -0x0.0p0", "f64: {x}", .{-@as(f64, 0)});
}
test "float.hexadecimal" {
try expectFmt("f16: 0x1.554p-2", "f16: {x}", .{@as(f16, 1.0 / 3.0)});
try expectFmt("f32: 0x1.555556p-2", "f32: {x}", .{@as(f32, 1.0 / 3.0)});
try expectFmt("f64: 0x1.5555555555555p-2", "f64: {x}", .{@as(f64, 1.0 / 3.0)});
try expectFmt("f80: 0x1.5555555555555556p-2", "f80: {x}", .{@as(f80, 1.0 / 3.0)});
try expectFmt("f128: 0x1.5555555555555555555555555555p-2", "f128: {x}", .{@as(f128, 1.0 / 3.0)});
try expectFmt("f16: 0x1p-14", "f16: {x}", .{math.floatMin(f16)});
try expectFmt("f32: 0x1p-126", "f32: {x}", .{math.floatMin(f32)});
try expectFmt("f64: 0x1p-1022", "f64: {x}", .{math.floatMin(f64)});
try expectFmt("f80: 0x1p-16382", "f80: {x}", .{math.floatMin(f80)});
try expectFmt("f128: 0x1p-16382", "f128: {x}", .{math.floatMin(f128)});
try expectFmt("f16: 0x0.004p-14", "f16: {x}", .{math.floatTrueMin(f16)});
try expectFmt("f32: 0x0.000002p-126", "f32: {x}", .{math.floatTrueMin(f32)});
try expectFmt("f64: 0x0.0000000000001p-1022", "f64: {x}", .{math.floatTrueMin(f64)});
try expectFmt("f80: 0x0.0000000000000002p-16382", "f80: {x}", .{math.floatTrueMin(f80)});
try expectFmt("f128: 0x0.0000000000000000000000000001p-16382", "f128: {x}", .{math.floatTrueMin(f128)});
try expectFmt("f16: 0x1.ffcp15", "f16: {x}", .{math.floatMax(f16)});
try expectFmt("f32: 0x1.fffffep127", "f32: {x}", .{math.floatMax(f32)});
try expectFmt("f64: 0x1.fffffffffffffp1023", "f64: {x}", .{math.floatMax(f64)});
try expectFmt("f80: 0x1.fffffffffffffffep16383", "f80: {x}", .{math.floatMax(f80)});
try expectFmt("f128: 0x1.ffffffffffffffffffffffffffffp16383", "f128: {x}", .{math.floatMax(f128)});
}
test "float.hexadecimal.precision" {
try expectFmt("f16: 0x1.5p-2", "f16: {x:.1}", .{@as(f16, 1.0 / 3.0)});
try expectFmt("f32: 0x1.555p-2", "f32: {x:.3}", .{@as(f32, 1.0 / 3.0)});
try expectFmt("f64: 0x1.55555p-2", "f64: {x:.5}", .{@as(f64, 1.0 / 3.0)});
try expectFmt("f80: 0x1.5555555p-2", "f80: {x:.7}", .{@as(f80, 1.0 / 3.0)});
try expectFmt("f128: 0x1.555555555p-2", "f128: {x:.9}", .{@as(f128, 1.0 / 3.0)});
try expectFmt("f16: 0x1.00000p0", "f16: {x:.5}", .{@as(f16, 1.0)});
try expectFmt("f32: 0x1.00000p0", "f32: {x:.5}", .{@as(f32, 1.0)});
try expectFmt("f64: 0x1.00000p0", "f64: {x:.5}", .{@as(f64, 1.0)});
try expectFmt("f80: 0x1.00000p0", "f80: {x:.5}", .{@as(f80, 1.0)});
try expectFmt("f128: 0x1.00000p0", "f128: {x:.5}", .{@as(f128, 1.0)});
}
test "float.decimal" {
try expectFmt("f64: 152314000000000000000000000000", "f64: {d}", .{@as(f64, 1.52314e29)});
try expectFmt("f32: 0", "f32: {d}", .{@as(f32, 0.0)});
try expectFmt("f32: 0", "f32: {d:.0}", .{@as(f32, 0.0)});
try expectFmt("f32: 1.1", "f32: {d:.1}", .{@as(f32, 1.1234)});
try expectFmt("f32: 1234.57", "f32: {d:.2}", .{@as(f32, 1234.567)});
// -11.1234 在内部转换为 f64 -11.12339...(errol3() 函数接受 f64)。
// -11.12339... 再舍入回 -11.1234
try expectFmt("f32: -11.1234", "f32: {d:.4}", .{@as(f32, -11.1234)});
try expectFmt("f32: 91.12345", "f32: {d:.5}", .{@as(f32, 91.12345)});
try expectFmt("f64: 91.1234567890", "f64: {d:.10}", .{@as(f64, 91.12345678901235)});
try expectFmt("f64: 0.00000", "f64: {d:.5}", .{@as(f64, 0.0)});
try expectFmt("f64: 6", "f64: {d:.0}", .{@as(f64, 5.700)});
try expectFmt("f64: 10.0", "f64: {d:.1}", .{@as(f64, 9.999)});
try expectFmt("f64: 1.000", "f64: {d:.3}", .{@as(f64, 1.0)});
try expectFmt("f64: 0.00030000", "f64: {d:.8}", .{@as(f64, 0.0003)});
try expectFmt("f64: 0.00000", "f64: {d:.5}", .{@as(f64, 1.40130e-45)});
try expectFmt("f64: 0.00000", "f64: {d:.5}", .{@as(f64, 9.999960e-40)});
try expectFmt("f64: 10000000000000.00", "f64: {d:.2}", .{@as(f64, 9999999999999.999)});
try expectFmt("f64: 10000000000000000000000000000000000000", "f64: {d}", .{@as(f64, 1e37)});
try expectFmt("f64: 100000000000000000000000000000000000000", "f64: {d}", .{@as(f64, 1e38)});
}
test "float.libc.sanity" {
try expectFmt("f64: 0.00001", "f64: {d:.5}", .{@as(f64, @as(f32, @bitCast(@as(u32, 916964781))))});
try expectFmt("f64: 0.00001", "f64: {d:.5}", .{@as(f64, @as(f32, @bitCast(@as(u32, 925353389))))});
try expectFmt("f64: 0.10000", "f64: {d:.5}", .{@as(f64, @as(f32, @bitCast(@as(u32, 1036831278))))});
try expectFmt("f64: 1.00000", "f64: {d:.5}", .{@as(f64, @as(f32, @bitCast(@as(u32, 1065353133))))});
try expectFmt("f64: 10.00000", "f64: {d:.5}", .{@as(f64, @as(f32, @bitCast(@as(u32, 1092616192))))});
// libc 差异
//
// 根据 gdb,这个值恰好是 0.015625。因此我们向下舍入,
// 但 glibc 因某种原因向上舍入。所有在指定精度位置
// 呈 x.yyyy25 形式的浮点数均会出现此现象。
try expectFmt("f64: 0.01563", "f64: {d:.5}", .{@as(f64, @as(f32, @bitCast(@as(u32, 1015021568))))});
// errol3 舍入为 ...630,而 libc 舍入为 ...632。
// Grisu3 也舍入为 630,因此更倾向于认为
// libc 在此处并非最优。
try expectFmt("f64: 18014400656965630.00000", "f64: {d:.5}", .{@as(f64, @as(f32, @bitCast(@as(u32, 1518338049))))});
}
test "union" {
const TU = union(enum) {
float: f32,
int: u32,
};
const UU = union {
float: f32,
int: u32,
};
const EU = extern union {
float: f32,
int: u32,
};
const tu_inst: TU = .{ .int = 123 };
const uu_inst: UU = .{ .int = 456 };
const eu_inst: EU = .{ .float = 321.123 };
try expectFmt(".{ .int = 123 }", "{}", .{tu_inst});
try expectFmt(".{ ... }", "{}", .{uu_inst});
try expectFmt(".{ .float = 321.123, .int = 1134596030 }", "{}", .{eu_inst});
}
test "struct.self-referential" {
const S = struct {
const SelfType = @This();
a: ?*SelfType,
};
var inst = S{
.a = null,
};
inst.a = &inst;
try expectFmt(".{ .a = .{ .a = .{ .a = .{ ... } } } }", "{}", .{inst});
}
test "struct.zero-size" {
const A = struct {
fn foo() void {}
};
const B = struct {
a: A,
c: i32,
};
const a = A{};
const b = B{ .a = a, .c = 0 };
try expectFmt(".{ .a = .{ }, .c = 0 }", "{}", .{b});
}
/// 将字节序列编码为十六进制数字。
/// 返回包含编码后字节的数组。
pub fn bytesToHex(input: anytype, case: Case) [input.len * 2]u8 {
if (input.len == 0) return [_]u8{};
comptime assert(@TypeOf(input[0]) == u8); // 待编码元素必须是无符号字节
const charset = "0123456789" ++ if (case == .upper) "ABCDEF" else "abcdef";
var result: [input.len * 2]u8 = undefined;
for (input, 0..) |b, i| {
result[i * 2 + 0] = charset[b >> 4];
result[i * 2 + 1] = charset[b & 15];
}
return result;
}
/// 解码指定十六进制字符字符串表示的字节序列。
/// 返回包含解码字节的输出缓冲区切片。
///
pub fn hexToBytes(out: []u8, input: []const u8) ![]u8 {
// 预期有 0 对或 n 对十六进制数字。
if (input.len & 1 != 0)
return error.InvalidLength;
if (out.len * 2 < input.len)
return error.NoSpaceLeft;
var in_i: usize = 0;
while (in_i < input.len) : (in_i += 2) {
const hi = try charToDigit(input[in_i], 16);
const lo = try charToDigit(input[in_i + 1], 16);
out[in_i / 2] = (hi << 4) | lo;
}
return out[0 .. in_i / 2];
}
test bytesToHex {
const input = "input slice";
const encoded = bytesToHex(input, .lower);
var decoded: [input.len]u8 = undefined;
try testing.expectEqualSlices(u8, input, try hexToBytes(&decoded, &encoded));
}
test hexToBytes {
const repeated: []const u8 = repeated: {
const buf: [32][2]u8 = @splat("90".*);
break :repeated @ptrCast(&buf);
};
var buf: [32]u8 = undefined;
try expectFmt(repeated, "{X}", .{try hexToBytes(&buf, repeated)});
try expectFmt("ABCD", "{X}", .{try hexToBytes(&buf, "ABCD")});
try expectFmt("", "{X}", .{try hexToBytes(&buf, "")});
try testing.expectError(error.InvalidCharacter, hexToBytes(&buf, "012Z"));
try testing.expectError(error.InvalidLength, hexToBytes(&buf, "AAA"));
try testing.expectError(error.NoSpaceLeft, hexToBytes(buf[0..1], "ABAB"));
}
test "positional" {
try expectFmt("2 1 0", "{2} {1} {0}", .{ @as(usize, 0), @as(usize, 1), @as(usize, 2) });
try expectFmt("2 1 0", "{2} {1} {}", .{ @as(usize, 0), @as(usize, 1), @as(usize, 2) });
try expectFmt("0 0", "{0} {0}", .{@as(usize, 0)});
try expectFmt("0 1", "{} {1}", .{ @as(usize, 0), @as(usize, 1) });
try expectFmt("1 0 0 1", "{1} {} {0} {}", .{ @as(usize, 0), @as(usize, 1) });
}
test "positional with specifier" {
try expectFmt("10.0", "{0d:.1}", .{@as(f64, 9.999)});
}
test "positional/alignment/width/precision" {
try expectFmt("10.0", "{0d: >3.1}", .{@as(f64, 9.999)});
}
test "vector" {
const vbool: @Vector(4, bool) = [_]bool{ true, false, true, false };
const vi64: @Vector(4, i64) = [_]i64{ -2, -1, 0, 1 };
const vu64: @Vector(4, u64) = [_]u64{ 1000, 2000, 3000, 4000 };
try expectFmt("{ true, false, true, false }", "{}", .{vbool});
try expectFmt("{ -2, -1, 0, 1 }", "{}", .{vi64});
try expectFmt("{ -2, -1, +0, +1 }", "{d:5}", .{vi64});
try expectFmt("{ 1000, 2000, 3000, 4000 }", "{}", .{vu64});
try expectFmt("{ 3e8, 7d0, bb8, fa0 }", "{x}", .{vu64});
const x: [4]u64 = undefined;
const vp: @Vector(4, *const u64) = [_]*const u64{ &x[0], &x[1], &x[2], &x[3] };
const vop: @Vector(4, ?*const u64) = [_]?*const u64{ &x[0], null, null, &x[3] };
var expect_buffer: [@sizeOf(usize) * 2 * 4 + 64]u8 = undefined;
try expectFmt(try mem.print(
&expect_buffer,
"{{ {}, {}, {}, {} }}",
.{ &x[0], &x[1], &x[2], &x[3] },
), "{}", .{vp});
try expectFmt(try mem.print(
&expect_buffer,
"{{ {?}, null, null, {?} }}",
.{ &x[0], &x[3] },
), "{any}", .{vop});
}
test "enum-literal" {
try expectFmt(".hello_world", "{}", .{.hello_world});
try expectFmt("hello_world", "{t}", .{.hello_world});
}
test "padding" {
try expectFmt("Simple", "{s}", .{"Simple"});
try expectFmt(" 1234", "{:10}", .{1234});
try expectFmt(" 1234", "{:>10}", .{1234});
try expectFmt("======1234", "{:=>10}", .{1234});
try expectFmt("1234======", "{:=<10}", .{1234});
try expectFmt(" 1234 ", "{:^10}", .{1234});
try expectFmt("===1234===", "{:=^10}", .{1234});
try expectFmt("====a", "{c:=>5}", .{'a'});
try expectFmt("==a==", "{c:=^5}", .{'a'});
try expectFmt("a====", "{c:=<5}", .{'a'});
}
test "decimal float padding" {
const number: f32 = 3.1415;
try expectFmt("left-pad: **3.142\n", "left-pad: {d:*>7.3}\n", .{number});
try expectFmt("center-pad: *3.142*\n", "center-pad: {d:*^7.3}\n", .{number});
try expectFmt("right-pad: 3.142**\n", "right-pad: {d:*<7.3}\n", .{number});
}
test "sci float padding" {
const number: f32 = 3.1415;
try expectFmt("left-pad: ****3.142e0\n", "left-pad: {e:*>11.3}\n", .{number});
try expectFmt("center-pad: **3.142e0**\n", "center-pad: {e:*^11.3}\n", .{number});
try expectFmt("right-pad: 3.142e0****\n", "right-pad: {e:*<11.3}\n", .{number});
}
test "padding.zero" {
try expectFmt("zero-pad: '0042'", "zero-pad: '{:04}'", .{42});
try expectFmt("std-pad: ' 42'", "std-pad: '{:10}'", .{42});
try expectFmt("std-pad-1: '001'", "std-pad-1: '{:0>3}'", .{1});
try expectFmt("std-pad-2: '911'", "std-pad-2: '{:1<03}'", .{9});
try expectFmt("std-pad-3: ' 1'", "std-pad-3: '{:>03}'", .{1});
try expectFmt("center-pad: '515'", "center-pad: '{:5^03}'", .{1});
}
test "null" {
const inst = null;
try expectFmt("null", "{}", .{inst});
}
test "type" {
try expectFmt("u8", "{}", .{u8});
try expectFmt("?f32", "{}", .{?f32});
try expectFmt("[]const u8", "{}", .{[]const u8});
}
test "named arguments" {
try expectFmt("hello world!", "{s} world{c}", .{ "hello", '!' });
try expectFmt("hello world!", "{[greeting]s} world{[punctuation]c}", .{ .punctuation = '!', .greeting = "hello" });
try expectFmt("hello world!", "{[1]s} world{[0]c}", .{ '!', "hello" });
}
test "runtime width specifier" {
const width: usize = 9;
try expectFmt("~~12345~~", "{d:~^[1]}", .{ 12345, width });
try expectFmt("~~12345~~", "{d:~^[width]}", .{ .string = 12345, .width = width });
try expectFmt(" 12345", "{d:[1]}", .{ 12345, width });
try expectFmt("42 12345", "{d} {d:[2]}", .{ 42, 12345, width });
}
test "runtime precision specifier" {
const number: f32 = 3.1415;
const precision: usize = 2;
try expectFmt("3.14e0", "{e:1.[1]}", .{ number, precision });
try expectFmt("3.14e0", "{e:1.[precision]}", .{ .number = number, .precision = precision });
}
test "recursive format function" {
const R = union(enum) {
const R = @This();
Leaf: i32,
Branch: struct { left: *const R, right: *const R },
pub fn format(self: R, writer: *Writer) Writer.Error!void {
return switch (self) {
.Leaf => |n| writer.print("Leaf({})", .{n}),
.Branch => |b| writer.print("Branch({f}, {f})", .{ b.left, b.right }),
};
}
};
var r: R = .{ .Leaf = 1 };
try expectFmt("Leaf(1)\n", "{f}\n", .{&r});
}
pub const hex_charset = "0123456789abcdef";
/// 将位宽为 u8 整数倍的无符号整数转换为小端序的
/// 小写十六进制字节数组。
pub fn hex(x: anytype) [@typeInfo(@TypeOf(x)).int.bits / 4]u8 {
comptime assert(@typeInfo(@TypeOf(x)).int.signedness == .unsigned);
var result: [@typeInfo(@TypeOf(x)).int.bits / 4]u8 = undefined;
var i: usize = 0;
while (i < result.len / 2) : (i += 1) {
const byte: u8 = @truncate(x >> @intCast(8 * i));
result[i * 2 + 0] = hex_charset[byte >> 4];
result[i * 2 + 1] = hex_charset[byte & 15];
}
return result;
}
test hex {
{
const x = hex(@as(u32, 0xdeadbeef));
try testing.expect(x.len == 8);
try testing.expectEqualStrings("efbeadde", &x);
}
{
const s = "[" ++ hex(@as(u48, 0x12345678_abcd)) ++ "]";
try testing.expect(s.len == 14);
try testing.expectEqualStrings("[cdab78563412]", s);
}
{
const s = "[" ++ hex(@as(u64, 0x12345678_abcdef00)) ++ "]";
try testing.expect(s.len == 18);
try testing.expectEqualStrings("[00efcdab78563412]", s);
}
}
test "parser until" {
{ // 返回截至 ':' 的子串
var parser: Parser = .{ .bytes = "abc:1234", .i = 0 };
try testing.expectEqualStrings("abc", parser.until(':'));
}
{ // 返回整个字符串 - 未找到 `ch`
var parser: Parser = .{ .bytes = "abc1234", .i = 0 };
try testing.expectEqualStrings("abc1234", parser.until(':'));
}
{ // 子字符串为空——`ch` 是唯一字符
var parser: Parser = .{ .bytes = ":", .i = 0 };
try testing.expectEqualStrings("", parser.until(':'));
}
{ // 字符串为空且未找到 `ch`
var parser: Parser = .{ .bytes = "", .i = 0 };
try testing.expectEqualStrings("", parser.until(':'));
}
{ // 子字符串从索引 2 开始,延伸至 `ch`
var parser: Parser = .{ .bytes = "abc:1234", .i = 2 };
try testing.expectEqualStrings("c", parser.until(':'));
}
{ // 子字符串从索引 4 开始直至末尾,因为未找到 `ch`
var parser: Parser = .{ .bytes = "abc1234", .i = 4 };
try testing.expectEqualStrings("234", parser.until(':'));
}
}
test "parser peek" {
{ // 从第一个索引开始迭代
var parser: Parser = .{ .bytes = "hello world", .i = 0 };
try testing.expectEqual('h', parser.peek(0));
try testing.expectEqual('e', parser.peek(1));
try testing.expectEqual(' ', parser.peek(5));
try testing.expectEqual('d', parser.peek(10));
try testing.expectEqual(null, parser.peek(11));
}
{ // 从倒数第二个索引开始迭代
var parser: Parser = .{ .bytes = "hello world!", .i = 10 };
try testing.expectEqual('d', parser.peek(0));
try testing.expectEqual('!', parser.peek(1));
try testing.expectEqual(null, parser.peek(5));
}
{ // 从字符串长度之外的位置开始迭代
var parser: Parser = .{ .bytes = "hello", .i = 5 };
try testing.expectEqual(null, parser.peek(0));
try testing.expectEqual(null, parser.peek(1));
}
{ // 空字符串
var parser: Parser = .{ .bytes = "", .i = 0 };
try testing.expectEqual(null, parser.peek(0));
try testing.expectEqual(null, parser.peek(2));
}
}
test "parser char" {
// 字符存在,迭代器位于索引 0
var parser: Parser = .{ .bytes = "~~hello", .i = 0 };
try testing.expectEqual('~', parser.char());
// 字符存在,迭代器位于中间
parser = .{ .bytes = "~~hello", .i = 3 };
try testing.expectEqual('e', parser.char());
// 字符存在——迭代器位于末尾
parser = .{ .bytes = "~~hello", .i = 6 };
try testing.expectEqual('o', parser.char());
// 字符不存在:迭代器已超出字符串长度。
parser = .{ .bytes = "~~hello", .i = 7 };
try testing.expectEqual(null, parser.char());
}
test "parser maybe" {
// 字符存在,迭代器位于索引 0
var parser: Parser = .{ .bytes = "hello world", .i = 0 };
try testing.expect(parser.maybe('h'));
// 字符存在 - 迭代器位于空格
parser = .{ .bytes = "hello world", .i = 5 };
try testing.expect(parser.maybe(' '));
// 字符存在——迭代器位于末尾
parser = .{ .bytes = "hello world", .i = 10 };
try testing.expect(parser.maybe('d'));
// 字符不存在:迭代器已超出字符串长度。
parser = .{ .bytes = "hello world", .i = 11 };
try testing.expect(!parser.maybe('e'));
}
test "parser number" {
// 输入是单个数字的自然数;迭代器位于 0。
var parser: Parser = .{ .bytes = "7", .i = 0 };
try testing.expect(7 == parser.number());
// 输入是两位自然数;迭代器位于 1。
parser = .{ .bytes = "29", .i = 1 };
try testing.expect(9 == parser.number());
// 输入为两位自然数——迭代器超过字符串长度
parser = .{ .bytes = "32", .i = 2 };
try testing.expectEqual(null, parser.number());
// 输入为整数
parser = .{ .bytes = "0", .i = 0 };
try testing.expect(0 == parser.number());
// 输入是负整数
parser = .{ .bytes = "-2", .i = 0 };
try testing.expectEqual(null, parser.number());
// 输入为字符串
parser = .{ .bytes = "no_number", .i = 2 };
try testing.expectEqual(null, parser.number());
// 输入为单字符字符串
parser = .{ .bytes = "n", .i = 0 };
try testing.expectEqual(null, parser.number());
// 输入为空字符串
parser = .{ .bytes = "", .i = 0 };
try testing.expectEqual(null, parser.number());
}
test "parser specifier" {
{ // 输入字符串为数字;迭代器位于 0
const expected: Specifier = Specifier{ .number = 1 };
var parser: Parser = .{ .bytes = "1", .i = 0 };
const result = try parser.specifier();
try testing.expect(expected.number == result.number);
}
{ // 输入字符串为两位数字;迭代器位于 0
const digit: Specifier = Specifier{ .number = 42 };
var parser: Parser = .{ .bytes = "42", .i = 0 };
const result = try parser.specifier();
try testing.expect(digit.number == result.number);
}
{ // 输入字符串为两位数;迭代器位于 1。
const digit: Specifier = Specifier{ .number = 8 };
var parser: Parser = .{ .bytes = "28", .i = 1 };
const result = try parser.specifier();
try testing.expect(digit.number == result.number);
}
{ // 输入字符串是带方括号的两位数;迭代器位于 0。
const digit: Specifier = Specifier{ .named = "15" };
var parser: Parser = .{ .bytes = "[15]", .i = 0 };
const result = try parser.specifier();
try testing.expectEqualStrings(digit.named, result.named);
}
{ // 输入字符串不是数值,且包含方括号;迭代器位于 0
const digit: Specifier = Specifier{ .named = "hello" };
var parser: Parser = .{ .bytes = "[hello]", .i = 0 };
const result = try parser.specifier();
try testing.expectEqualStrings(digit.named, result.named);
}
{ // 输入字符串不是数值,且不包含右方括号;迭代器位于 0
var parser: Parser = .{ .bytes = "[hello", .i = 0 };
const result = parser.specifier();
try testing.expectError(@field(anyerror, "Expected closing ]"), result);
}
{ // 输入字符串不是数字,也不包含右方括号;迭代器位于 2
var parser: Parser = .{ .bytes = "[[[[hello", .i = 2 };
const result = parser.specifier();
try testing.expectError(@field(anyerror, "Expected closing ]"), result);
}
{ // 输入字符串不是数值,且包含不平衡的方括号;迭代器位于 0
const digit: Specifier = Specifier{ .named = "[[hello" };
var parser: Parser = .{ .bytes = "[[[hello]", .i = 0 };
const result = try parser.specifier();
try testing.expectEqualStrings(digit.named, result.named);
}
{ // 输入字符串不是数字,且方括号不成对;迭代器位于 1
const digit: Specifier = Specifier{ .named = "[[hello" };
var parser: Parser = .{ .bytes = "[[[[hello]]]]]", .i = 1 };
const result = try parser.specifier();
try testing.expectEqualStrings(digit.named, result.named);
}
{ // 输入字符串既不是数字,也不是命名参数
const char: Specifier = Specifier{ .none = {} };
var parser: Parser = .{ .bytes = "hello", .i = 0 };
const result = try parser.specifier();
try testing.expectEqual(char.none, result.none);
}
}
test {
_ = float;
}