diff options
Diffstat (limited to 'include/fud_c_string.hpp')
-rw-r--r-- | include/fud_c_string.hpp | 21 |
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 |