string: add glyphrlen

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2023-12-22 21:36:21 +01:00
parent 9c9fd9d05a
commit e1f53f31ad

View File

@ -209,6 +209,27 @@ namespace stdex
return 0; 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 /// Convert to ASCII-lower-case
/// ///