diff options
author | Campbell Barton <ideasman42@gmail.com> | 2021-08-27 09:42:31 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2021-08-27 10:02:53 +0300 |
commit | 89dae554f9d5ae0204ad9c51c5ba00e14b16e858 (patch) | |
tree | 2dc6cc9151fe6eba664dfb5b39a14e7c620ce974 /source/blender/blenlib/intern/string_cursor_utf8.c | |
parent | 523bc981cfeecead5050e7af44bbe252c166d718 (diff) |
Cleanup: utf8 stepping functions
Various changes to reduce risk of out of bounds errors in utf8 seeking.
- Remove BLI_str_prev_char_utf8
This function could potentially scan past the beginning of a string.
Use BLI_str_find_prev_char_utf8 instead which takes a limiting
string start argument.
- Swap arguments for BLI_str_find_prev_char_utf8 so the stepping
argument is first and the limiting argument is last.
This matches BLI_str_find_next_char_utf8.
- Change behavior of these functions to return it the start or end
pointers instead of NULL, which complicated use of these functions
to calculate offsets.
Callers that need to check if the limits were reached can compare
the return value with the start/end pointers.
- Return 'const char *' from these functions
so they don't remove const from the input arguments.
Diffstat (limited to 'source/blender/blenlib/intern/string_cursor_utf8.c')
-rw-r--r-- | source/blender/blenlib/intern/string_cursor_utf8.c | 10 |
1 files changed, 4 insertions, 6 deletions
diff --git a/source/blender/blenlib/intern/string_cursor_utf8.c b/source/blender/blenlib/intern/string_cursor_utf8.c index f76a3114e09..eb49572f06c 100644 --- a/source/blender/blenlib/intern/string_cursor_utf8.c +++ b/source/blender/blenlib/intern/string_cursor_utf8.c @@ -117,7 +117,7 @@ bool BLI_str_cursor_step_next_utf8(const char *str, size_t maxlen, int *pos) const char *str_end = str + (maxlen + 1); const char *str_pos = str + (*pos); const char *str_next = BLI_str_find_next_char_utf8(str_pos, str_end); - if (str_next) { + if (str_next != str_end) { (*pos) += (str_next - str_pos); if ((*pos) > (int)maxlen) { (*pos) = (int)maxlen; @@ -132,11 +132,9 @@ bool BLI_str_cursor_step_prev_utf8(const char *str, size_t UNUSED(maxlen), int * { if ((*pos) > 0) { const char *str_pos = str + (*pos); - const char *str_prev = BLI_str_find_prev_char_utf8(str, str_pos); - if (str_prev) { - (*pos) -= (str_pos - str_prev); - return true; - } + const char *str_prev = BLI_str_find_prev_char_utf8(str_pos, str); + (*pos) -= (str_pos - str_prev); + return true; } return false; |