From c6f3c7f0395ba819bb18a87dba8a57f737f0722e Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Thu, 17 Aug 2023 23:57:57 -0500 Subject: [PATCH] feat: add matchExact to the API TODO: write doc comment --- src/lib.zig | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lib.zig b/src/lib.zig index 050cd51..c521a44 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -121,8 +121,6 @@ pub fn extract(comptime bit_string: []const u8, value: anytype) Bitfield(bit_str }; comptime verify(ValT, bit_string); - std.debug.assert(match(bit_string, value)); // prevents branchless impl in ReleaseSafe - var ret: ReturnT = undefined; inline for (@typeInfo(ReturnT).Struct.fields) |field| { @@ -148,6 +146,11 @@ pub fn extract(comptime bit_string: []const u8, value: anytype) Bitfield(bit_str return ret; } +pub fn matchExtract(comptime bit_string: []const u8, value: anytype) ?Bitfield(bit_string) { + if (!match(bit_string, value)) return null; + return extract(bit_string, value); +} + test "extract" { // doc tests {