From 8dcb1de91e15ff7fc66279cd9cd9ad8a70f624e0 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Tue, 29 Oct 2024 23:16:46 -0500 Subject: u8 string literals --- include/fud_c_string.hpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'include/fud_c_string.hpp') 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) < maxLength) { + size++; + } + + if (str[size] != 0 && static_cast(size) == maxLength) { + return static_cast(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 -- cgit v1.2.3