mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
this patch renames ComptimeStringMap to StaticStringMap, makes it accept only a single type parameter, and return a known struct type instead of an anonymous struct. initial motivation for these changes was to reduce the 'very long type names' issue described here https://github.com/ziglang/zig/pull/19682. this breaks the previous API. users will now need to write: `const map = std.StaticStringMap(T).initComptime(kvs_list);` * move `kvs_list` param from type param to an `initComptime()` param * new public methods * `keys()`, `values()` helpers * `init(allocator)`, `deinit(allocator)` for runtime data * `getLongestPrefix(str)`, `getLongestPrefixIndex(str)` - i'm not sure these belong but have left in for now incase they are deemed useful * performance notes: * i posted some benchmarking results here: https://github.com/travisstaloch/comptime-string-map-revised/issues/1 * i noticed a speedup reducing the size of the struct from 48 to 32 bytes and thus use u32s instead of usize for all length fields * i noticed speedup storing KVs as a struct of arrays * latest benchmark shows these wall_time improvements for debug/safe/small/fast builds: -6.6% / -10.2% / -19.1% / -8.9%. full output in link above.
64 lines
1.6 KiB
Zig
64 lines
1.6 KiB
Zig
const std = @import("std");
|
|
|
|
/// Set of primitive type and value names.
|
|
/// Does not include `_` or integer type names.
|
|
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"},
|
|
});
|
|
|
|
/// Returns true if a name matches a primitive type or value, excluding `_`.
|
|
/// Integer type names like `u8` or `i32` are only matched for syntax,
|
|
/// so this will still return true when they have an oversized bit count
|
|
/// or leading zeroes.
|
|
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"));
|
|
}
|