diff options
author | Jacques Lucke <jacques@blender.org> | 2020-02-28 16:21:51 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2020-02-28 16:26:07 +0300 |
commit | 95d0e04ed1d8ac8c77aa62dc51f3d63bc09c786e (patch) | |
tree | 876e2b1665c6bfcf513a412d573a8e1ee6d5dd33 /source/blender | |
parent | 2269759fdf94cb7017685e4ce3f7677b3d8227ae (diff) |
BLI: fix utf8 character counting when there is an incomplete utf8 char
D6923 by Kim Geonwoo
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenlib/intern/string_utf8.c | 30 |
1 files changed, 12 insertions, 18 deletions
diff --git a/source/blender/blenlib/intern/string_utf8.c b/source/blender/blenlib/intern/string_utf8.c index 7fc95a33092..027afba1f5e 100644 --- a/source/blender/blenlib/intern/string_utf8.c +++ b/source/blender/blenlib/intern/string_utf8.c @@ -373,23 +373,23 @@ size_t BLI_strlen_utf8_ex(const char *strc, size_t *r_len_bytes) size_t BLI_strlen_utf8(const char *strc) { - size_t len; - - for (len = 0; *strc; len++) { - strc += BLI_str_utf8_size_safe(strc); - } - - return len; + size_t len_bytes; + return BLI_strlen_utf8_ex(strc, &len_bytes); } size_t BLI_strnlen_utf8_ex(const char *strc, const size_t maxlen, size_t *r_len_bytes) { - size_t len; + size_t len = 0; const char *strc_orig = strc; const char *strc_end = strc + maxlen; - for (len = 0; *strc && strc < strc_end; len++) { - strc += BLI_str_utf8_size_safe(strc); + while (true) { + size_t step = (size_t)BLI_str_utf8_size_safe(strc); + if (!*strc || strc + step > strc_end) { + break; + } + strc += step; + len++; } *r_len_bytes = (size_t)(strc - strc_orig); @@ -403,14 +403,8 @@ size_t BLI_strnlen_utf8_ex(const char *strc, const size_t maxlen, size_t *r_len_ */ size_t BLI_strnlen_utf8(const char *strc, const size_t maxlen) { - size_t len; - const char *strc_end = strc + maxlen; - - for (len = 0; *strc && strc < strc_end; len++) { - strc += BLI_str_utf8_size_safe(strc); - } - - return len; + size_t len_bytes; + return BLI_strnlen_utf8_ex(strc, maxlen, &len_bytes); } size_t BLI_strncpy_wchar_from_utf8(wchar_t *__restrict dst_w, |