BLI_string: add BLI_str_utf8_as_unicode_safe

Similar to BLI_str_utf8_size_safe, matches logic from other safe UTF8
decoding functions.
This commit is contained in:
Campbell Barton
2023-09-18 14:25:13 +10:00
parent ed552e9e4f
commit 9e788ddecf
2 changed files with 10 additions and 0 deletions

View File

@@ -55,6 +55,7 @@ int BLI_str_utf8_size_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
*/
unsigned int BLI_str_utf8_as_unicode_or_error(const char *p) ATTR_WARN_UNUSED_RESULT
ATTR_NONNULL(1);
unsigned int BLI_str_utf8_as_unicode_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
/**
* UTF8 decoding that steps over the index (unless an error is encountered).
*

View File

@@ -747,6 +747,15 @@ uint BLI_str_utf8_as_unicode_or_error(const char *p)
return utf8_char_decode(p, mask, len, BLI_UTF8_ERR);
}
unsigned int BLI_str_utf8_as_unicode_safe(const char *p)
{
const uint result = BLI_str_utf8_as_unicode_or_error(p);
if (UNLIKELY(result == BLI_UTF8_ERR)) {
return *p;
}
return result;
}
uint BLI_str_utf8_as_unicode_step_or_error(const char *__restrict p,
const size_t p_len,
size_t *__restrict index)