summaryrefslogtreecommitdiff
path: root/include/fud_c_string.hpp
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-10-29 23:16:46 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-10-29 23:16:46 -0500
commit8dcb1de91e15ff7fc66279cd9cd9ad8a70f624e0 (patch)
treec73840b15a074cf37f59bc3b2eff56cde982d74f /include/fud_c_string.hpp
parent8ce397e8c0a83e49e390de9deb73d588e4931ecf (diff)
u8 string literals
Diffstat (limited to 'include/fud_c_string.hpp')
-rw-r--r--include/fud_c_string.hpp25
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