diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-10-29 23:16:46 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-10-29 23:16:46 -0500 |
commit | 8dcb1de91e15ff7fc66279cd9cd9ad8a70f624e0 (patch) | |
tree | c73840b15a074cf37f59bc3b2eff56cde982d74f /include/fud_c_string.hpp | |
parent | 8ce397e8c0a83e49e390de9deb73d588e4931ecf (diff) |
u8 string literals
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 |