std.zig.primitives.isPrimitive
函数
如果名称匹配基本类型或值(不包括 _),则返回 true。
u8、i32 等整数类型名称只按语法匹配,
因此即使位数过大或包含前导零,此函数仍返回 true。
pub fn isPrimitive(name: []const u8) bool
参数
name: []const u8
用法示例
test isPrimitive {
const expect = std.testing.expect;
try expect(!isPrimitive(""));
try expect(!isPrimitive("_"));
try expect(!isPrimitive("haberdasher"));
try expect(isPrimitive("bool"));
try expect(isPrimitive("false"));
try expect(isPrimitive("comptime_float"));
try expect(isPrimitive("u1"));
try expect(isPrimitive("i99999999999999"));
}
std.zig.primitives.names
常量
原始类型名称和值名称的集合。
不包含 _ 或整数类型名称。
std.zig.primitives.std
别名
别名目标 std
源码
std/zig/primitives.zig
const std = @import("std");
/// 原始类型名称和值名称的集合。
/// 不包含 `_` 或整数类型名称。
pub const names = std.StaticStringMap(void).initComptime(.{
.{"anyerror"},
.{"anyframe"},
.{"anyopaque"},
.{"bool"},
.{"c_int"},
.{"c_long"},
.{"c_longdouble"},
.{"c_longlong"},
.{"c_char"},
.{"c_short"},
.{"c_uint"},
.{"c_ulong"},
.{"c_ulonglong"},
.{"c_ushort"},
.{"comptime_float"},
.{"comptime_int"},
.{"f128"},
.{"f16"},
.{"f32"},
.{"f64"},
.{"f80"},
.{"false"},
.{"isize"},
.{"noreturn"},
.{"null"},
.{"true"},
.{"type"},
.{"undefined"},
.{"usize"},
.{"void"},
});
/// 如果名称匹配基本类型或值(不包括 `_`),则返回 true。
/// `u8`、`i32` 等整数类型名称只按语法匹配,
/// 因此即使位数过大或包含前导零,此函数仍返回 true。
///
pub fn isPrimitive(name: []const u8) bool {
if (names.get(name) != null) return true;
if (name.len < 2) return false;
const first_c = name[0];
if (first_c != 'i' and first_c != 'u') return false;
for (name[1..]) |c| switch (c) {
'0'...'9' => {},
else => return false,
};
return true;
}
test isPrimitive {
const expect = std.testing.expect;
try expect(!isPrimitive(""));
try expect(!isPrimitive("_"));
try expect(!isPrimitive("haberdasher"));
try expect(isPrimitive("bool"));
try expect(isPrimitive("false"));
try expect(isPrimitive("comptime_float"));
try expect(isPrimitive("u1"));
try expect(isPrimitive("i99999999999999"));
}