From e1f53f31ad33045fd3f21277ac2ba2364c3bca31 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Fri, 22 Dec 2023 21:36:21 +0100 Subject: [PATCH] string: add glyphrlen Signed-off-by: Simon Rozman --- include/stdex/string.hpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/include/stdex/string.hpp b/include/stdex/string.hpp index 25ba752a2..97536ae08 100644 --- a/include/stdex/string.hpp +++ b/include/stdex/string.hpp @@ -209,6 +209,27 @@ namespace stdex return 0; } + /// + /// Return number of code units the last glyph in the string represents + /// + /// \param[in] str Start of a string + /// \param[in] count Length of a string in code units + /// + inline size_t glyphrlen(_In_reads_or_z_opt_(count) const wchar_t* str, _In_ size_t count) + { + _Assume_(count && str && str[count - 1]); + for (size_t i = count; i--;) { + if (!iscombining(str[i])) { +#ifdef _WIN32 + return count - (!is_low_surrogate(str[i]) || i == 0 || !is_high_surrogate(str[i - 1]) ? i : i - 1); +#else + return count - i; +#endif + } + } + return count; + } + /// /// Convert to ASCII-lower-case ///