summaryrefslogtreecommitdiff
path: root/include/fud_c_string.hpp
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2025-03-31 08:33:08 -0500
committerDominick Allen <djallen@librehumanitas.org>2025-03-31 08:33:08 -0500
commit8b0bc70db73b48d833a3b5791e55921768cf6932 (patch)
tree862ae34933a7fc9f480038d974f59d7683a82605 /include/fud_c_string.hpp
parentc426110f24516f92ecb8a5374e2a281f2c79787a (diff)
Remove reinterpret_cast usage in favor of std::bit_cast.
Diffstat (limited to 'include/fud_c_string.hpp')
-rw-r--r--include/fud_c_string.hpp21
1 files changed, 3 insertions, 18 deletions
diff --git a/include/fud_c_string.hpp b/include/fud_c_string.hpp
index a1ab51a..0632619 100644
--- a/include/fud_c_string.hpp
+++ b/include/fud_c_string.hpp
@@ -18,6 +18,7 @@
#ifndef FUD_C_STRING_HPP
#define FUD_C_STRING_HPP
+#include <bit>
#include <climits>
#include <cstddef>
#include <limits>
@@ -54,28 +55,12 @@ constexpr ssize_t cStringLength(const char* str)
constexpr ssize_t cStringLength(const char8_t* str, size_t maxLength)
{
- // Cannot cast str to const char* without breaking constexpr
- // return cStringLength(reinterpret_cast<const char*>(str), maxLength);
- if (str == nullptr || maxLength > MAX_C_STRING_LENGTH) {
- return -1;
- }
-
- ssize_t size = 0;
-
- while (str[size] != 0 && static_cast<size_t>(size) < maxLength) {
- size++;
- }
-
- if (str[size] != 0 && static_cast<size_t>(size) == maxLength) {
- return static_cast<ssize_t>(maxLength) + 1;
- }
-
- return size;
+ return cStringLength(std::bit_cast<const char*>(str), maxLength);
}
constexpr ssize_t cStringLength(const char8_t* str)
{
- return cStringLength(str, MAX_C_STRING_LENGTH);
+ return cStringLength(std::bit_cast<const char*>(str), MAX_C_STRING_LENGTH);
}
} // namespace fud