From fb5fcbafc1aa6241ecc3c5ec79d373ee292f0f77 Mon Sep 17 00:00:00 2001 From: paxcut <53811119+paxcut@users.noreply.github.com> Date: Tue, 9 Jan 2024 16:36:17 -0700 Subject: [PATCH] includes/type: Changes to magic type to include address of failed asserts. (#217) --- includes/type/magic.pat | 43 +++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/includes/type/magic.pat b/includes/type/magic.pat index 37be42f..9a1f34c 100644 --- a/includes/type/magic.pat +++ b/includes/type/magic.pat @@ -7,32 +7,37 @@ Types used to parse and enforce specific magic numbers */ -namespace type { +namespace type +{ + fn fm(ref auto value) + { + str result; + for (u32 i = 0, i < sizeof(value), i += 1) + { + char c = value[i]; + + if (std::ctype::isprint(c)) + result += c; + else + result += std::format("\\x{:02X}", u8(c)); + } + return std::format("\"{}\"", result); + }; /** A Magic number. Throws an error if the magic number does not match the expected value @tparam ExpectedValue A string representing the expected value */ - struct Magic { + struct Magic + { char value[std::string::length(ExpectedValue)]; - std::assert(value == ExpectedValue, std::format("Invalid magic value! Expected \"{}\", got \"{}\".", ExpectedValue, value)); + std::assert_warn(value == ExpectedValue, std::format("Invalid magic value! Expected {}, got {} at position 0x{:X}", type::fm(ExpectedValue), type::fm(value), $ - std::string::length(ExpectedValue))); } [[sealed, format("type::impl::format_magic")]]; - - namespace impl { - - fn format_magic(ref auto magic) { - str result; - for (u32 i = 0, i < sizeof(magic.value), i += 1) { - char c = magic.value[i]; - - if (std::ctype::isprint(c)) - result += c; - else - result += std::format("\\x{:02X}", u8(c)); - } - return std::format("\"{}\"", result); + namespace impl + { + fn format_magic(ref auto magic) + { + return fm(magic.value); }; - } - } \ No newline at end of file