diff options
Diffstat (limited to 'include/fud_c_string.hpp')
-rw-r--r-- | include/fud_c_string.hpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/include/fud_c_string.hpp b/include/fud_c_string.hpp index 44e0dc8..d55ba30 100644 --- a/include/fud_c_string.hpp +++ b/include/fud_c_string.hpp @@ -50,6 +50,31 @@ constexpr ssize_t cStringLength(const char* str) return cStringLength(str, maxLength); } +constexpr ssize_t cStringLength(const char8_t* str, size_t maxLength) +{ + if (str == nullptr || maxLength > (SSIZE_MAX - 1)) { + 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; +} + +constexpr ssize_t cStringLength(const char8_t* str) +{ + constexpr auto maxLength = SSIZE_MAX - 1; + return cStringLength(str, maxLength); +} + } // namespace fud #endif |