#include "fud_assert.hpp" #include "fud_array.hpp" #include "fud_string_view.hpp" #include #include #include #include namespace fud { void assertFail(const char* assertion, const char* file, unsigned int line, const char* function) noexcept(false) { constexpr size_t MAX_FILE_CHARS = 256; constexpr size_t MAX_FUNCTION_CHARS = 256; constexpr size_t BITS_PER_OCTAL = 3; constexpr auto MAX_LINE_CHARS = BITS_PER_OCTAL * sizeof(decltype(line)) + 3; constexpr size_t MAX_ASSERT_CHARS = 512 - MAX_LINE_CHARS; constexpr size_t ASSERT_MSG_SIZE = MAX_FILE_CHARS + MAX_LINE_CHARS + MAX_FUNCTION_CHARS + MAX_ASSERT_CHARS; auto lengthResult = cStringLength(file); size_t filenameLength = 0; auto badLength = lengthResult < 1 || lengthResult > SSIZE_MAX; if (!badLength) { filenameLength = static_cast(lengthResult); } const char* filename = nullptr; if (badLength) { constexpr const char invalidFile[] = "INVALID FILE"; static_assert(sizeof(invalidFile) < MAX_FILE_CHARS); filenameLength = sizeof(invalidFile); filename = invalidFile; } else if (filenameLength > MAX_LINE_CHARS) { filename = file + filenameLength - MAX_LINE_CHARS; filenameLength = MAX_FILE_CHARS; } Array buffer{}; // clang-format off static_cast(std::format_to_n( buffer.data(), buffer.size() - 1U, "{:.{}s}:{}: {:.{}s}: Assertion `{:.{}s}` failed", filename, filenameLength, line, function, MAX_FUNCTION_CHARS, assertion, MAX_ASSERT_CHARS)); // clang-format on throw std::runtime_error(buffer.data()); } } // namespace fud