From 9e788ddecfe2d8cfde9cd86c1da2da830b3e7395 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 18 Sep 2023 14:25:13 +1000 Subject: [PATCH] BLI_string: add BLI_str_utf8_as_unicode_safe Similar to BLI_str_utf8_size_safe, matches logic from other safe UTF8 decoding functions. --- source/blender/blenlib/BLI_string_utf8.h | 1 + source/blender/blenlib/intern/string_utf8.c | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/source/blender/blenlib/BLI_string_utf8.h b/source/blender/blenlib/BLI_string_utf8.h index 2ecdaeaa1f8..ee1c4f6db87 100644 --- a/source/blender/blenlib/BLI_string_utf8.h +++ b/source/blender/blenlib/BLI_string_utf8.h @@ -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). * diff --git a/source/blender/blenlib/intern/string_utf8.c b/source/blender/blenlib/intern/string_utf8.c index 6720597fb97..c6b09ca9ef3 100644 --- a/source/blender/blenlib/intern/string_utf8.c +++ b/source/blender/blenlib/intern/string_utf8.c @@ -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)