summaryrefslogtreecommitdiff
path: root/source/fud_assert.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/fud_assert.cpp')
-rw-r--r--source/fud_assert.cpp55
1 files changed, 18 insertions, 37 deletions
diff --git a/source/fud_assert.cpp b/source/fud_assert.cpp
index 14bf5eb..98f17d0 100644
--- a/source/fud_assert.cpp
+++ b/source/fud_assert.cpp
@@ -1,8 +1,6 @@
#include "fud_assert.hpp"
#include "fud_array.hpp"
-#include "fud_span.hpp"
-#include "fud_string_view.hpp"
#include <climits>
#include <cstdio>
@@ -10,46 +8,29 @@
namespace fud {
-void assertFormat(
- const char* assertion,
- const std::source_location sourceLocation,
- Span<char, ASSERT_MSG_SIZE> buffer)
-{
- auto lengthResult = cStringLength(sourceLocation.file_name());
- size_t filenameLength = 0;
- auto badLength = lengthResult < 1 || lengthResult > SSIZE_MAX;
- if (!badLength) {
- filenameLength = static_cast<size_t>(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 = sourceLocation.file_name() + filenameLength - MAX_LINE_CHARS;
- filenameLength = MAX_FILE_CHARS;
- }
-
- // clang-format off
- static_cast<void>(std::format_to_n(
- buffer.data(), buffer.size() - 1U,
- "{:.{}s}:{}: {:.{}s}: Assertion `{:.{}s}` failed\n",
- filename, filenameLength,
- sourceLocation.line(),
- sourceLocation.function_name(), MAX_FUNCTION_CHARS,
- assertion, MAX_ASSERT_CHARS));
- // clang-format on
-}
+constexpr std::size_t BITS_PER_OCTAL = 3;
+constexpr auto MAX_LINE_CHARS = BITS_PER_OCTAL * sizeof(decltype(std::source_location{}.line())) + 3;
[[noreturn]] void assertFail(const char* assertion, const std::source_location sourceLocation)
{
- Array<char, ASSERT_MSG_SIZE> buffer{};
- assertFormat(assertion, sourceLocation, Span<char, ASSERT_MSG_SIZE>::make(buffer));
+ const char* file_name = sourceLocation.file_name();
+ if (file_name == nullptr) {
+ fputs("Unknown file", stderr);
+ } else {
+ fputs(file_name, stderr);
+ }
+
+ constexpr std::size_t assertMsgSize = MAX_LINE_CHARS + 3;
+ Array<char, assertMsgSize> buffer{};
+ static_cast<void>(std::format_to_n(buffer.data(), buffer.size() - 1U, ":{}:", sourceLocation.line()));
buffer[buffer.size() - 1] = '\0';
fputs(buffer.data(), stderr);
- std::abort();
+
+ fputs(sourceLocation.function_name(), stderr);
+ fputs(": ", stderr);
+ fputs(assertion, stderr);
+
+ std::terminate();
}
} // namespace fud