diff options
Diffstat (limited to 'include/fud_c_string.hpp')
-rw-r--r-- | include/fud_c_string.hpp | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/include/fud_c_string.hpp b/include/fud_c_string.hpp index d55ba30..a1ab51a 100644 --- a/include/fud_c_string.hpp +++ b/include/fud_c_string.hpp @@ -20,14 +20,17 @@ #include <climits> #include <cstddef> +#include <limits> #include <sys/types.h> namespace fud { +constexpr ssize_t MAX_C_STRING_LENGTH = std::numeric_limits<ssize_t>::max() - 1; + constexpr ssize_t cStringLength(const char* str, size_t maxLength) { - if (str == nullptr || maxLength > (SSIZE_MAX - 1)) { + if (str == nullptr || maxLength > MAX_C_STRING_LENGTH) { return -1; } @@ -46,13 +49,14 @@ constexpr ssize_t cStringLength(const char* str, size_t maxLength) constexpr ssize_t cStringLength(const char* str) { - constexpr auto maxLength = SSIZE_MAX - 1; - return cStringLength(str, maxLength); + return cStringLength(str, MAX_C_STRING_LENGTH); } constexpr ssize_t cStringLength(const char8_t* str, size_t maxLength) { - if (str == nullptr || maxLength > (SSIZE_MAX - 1)) { + // 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; } @@ -71,8 +75,7 @@ constexpr ssize_t cStringLength(const char8_t* str, size_t maxLength) constexpr ssize_t cStringLength(const char8_t* str) { - constexpr auto maxLength = SSIZE_MAX - 1; - return cStringLength(str, maxLength); + return cStringLength(str, MAX_C_STRING_LENGTH); } } // namespace fud |