From 26362f283f4dd5cecd478c2e30b9d905deba3bbe Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 15 May 2023 10:52:27 +1000 Subject: [PATCH] Cleanup: pass an immutable position to BLI_str_cursor_step_bounds_* Having some arguments be input/output and others output only was confusing, a function that detects a range from a position can simply calculate the range - modifying the input position isn't needed. Instead, note that word select puts the cursor at the end by convention. Also use `r_` prefix for output only arguments. --- .../blender/blenlib/BLI_string_cursor_utf8.h | 13 +++--- .../blenlib/intern/string_cursor_utf8.c | 42 ++++++++----------- source/blender/editors/curve/editfont.c | 3 +- .../editors/interface/interface_handlers.cc | 5 +-- .../editors/space_console/console_ops.c | 2 +- source/blender/editors/space_text/text_ops.c | 2 +- 6 files changed, 32 insertions(+), 35 deletions(-) diff --git a/source/blender/blenlib/BLI_string_cursor_utf8.h b/source/blender/blenlib/BLI_string_cursor_utf8.h index 5db2cf2a790..53e2e6c0f1a 100644 --- a/source/blender/blenlib/BLI_string_cursor_utf8.h +++ b/source/blender/blenlib/BLI_string_cursor_utf8.h @@ -43,21 +43,24 @@ void BLI_str_cursor_step_utf32(const char32_t *str, bool use_init_step); /** - * Word/Sequence Selection. Given a position within a string, return the start and end of the - * closest sequence of delimited characters. Generally a word, but could be a sequence of spaces. + * Given a position within a string, + * return the start and end of the closest sequence of delimited characters. + * Typically a word, but can be a sequence of characters (including spaces). + * + * \note When used for word-selection the caller should set the cursor to `r_end` (by convention). * * \param str: The string with a cursor position * \param str_maxlen: The maximum characters to consider - * \param pos: The starting cursor position (probably moved on completion) + * \param pos: The starting cursor position. * \param r_start: returned start of word/sequence boundary (0-based) * \param r_end: returned end of word/sequence boundary (0-based) */ void BLI_str_cursor_step_bounds_utf8( - const char *str, const size_t str_maxlen, int *pos, int *r_start, int *r_end); + const char *str, const size_t str_maxlen, int pos, int *r_start, int *r_end); /** A UTF32 version of #BLI_str_cursor_step_bounds_utf8 */ void BLI_str_cursor_step_bounds_utf32( - const char32_t *str, const size_t str_maxlen, int *pos, int *r_start, int *r_end); + const char32_t *str, const size_t str_maxlen, int pos, int *r_start, int *r_end); #ifdef __cplusplus } diff --git a/source/blender/blenlib/intern/string_cursor_utf8.c b/source/blender/blenlib/intern/string_cursor_utf8.c index 31119187536..9b08a0e69ea 100644 --- a/source/blender/blenlib/intern/string_cursor_utf8.c +++ b/source/blender/blenlib/intern/string_cursor_utf8.c @@ -316,50 +316,44 @@ void BLI_str_cursor_step_utf32(const char32_t *str, } void BLI_str_cursor_step_bounds_utf8( - const char *str, const size_t str_maxlen, int *pos, int *start, int *end) + const char *str, const size_t str_maxlen, const int pos, int *r_start, int *r_end) { /* What type of characters are on either side of the current cursor position? */ - const eStrCursorDelimType prev = (*pos > 0) ? cursor_delim_type_utf8(str, str_maxlen, *pos - 1) : - STRCUR_DELIM_NONE; - const eStrCursorDelimType next = (*pos < str_maxlen) ? - cursor_delim_type_utf8(str, str_maxlen, *pos) : + const eStrCursorDelimType prev = (pos > 0) ? cursor_delim_type_utf8(str, str_maxlen, pos - 1) : + STRCUR_DELIM_NONE; + const eStrCursorDelimType next = (pos < str_maxlen) ? + cursor_delim_type_utf8(str, str_maxlen, pos) : STRCUR_DELIM_NONE; - *start = *pos; - *end = *pos; + *r_start = pos; + *r_end = pos; if (prev == next || ELEM(next, STRCUR_DELIM_WHITESPACE, STRCUR_DELIM_NONE)) { /* Expand backward if we are between similar content, before whitespace, or at end. */ - BLI_str_cursor_step_utf8(str, str_maxlen, start, STRCUR_DIR_PREV, STRCUR_JUMP_DELIM, false); + BLI_str_cursor_step_utf8(str, str_maxlen, r_start, STRCUR_DIR_PREV, STRCUR_JUMP_DELIM, false); } if (prev == next || ELEM(prev, STRCUR_DELIM_WHITESPACE, STRCUR_DELIM_NONE)) { /* Expand forward if we are between similar content, after whitespace, or at beginning. */ - BLI_str_cursor_step_utf8(str, str_maxlen, end, STRCUR_DIR_NEXT, STRCUR_JUMP_DELIM, false); + BLI_str_cursor_step_utf8(str, str_maxlen, r_end, STRCUR_DIR_NEXT, STRCUR_JUMP_DELIM, false); } - - /* Move cursor position to the end of selection. */ - *pos = *end; } void BLI_str_cursor_step_bounds_utf32( - const char32_t *str, const size_t str_maxlen, int *pos, int *start, int *end) + const char32_t *str, const size_t str_maxlen, const int pos, int *r_start, int *r_end) { /* What type of characters are on either side of the current cursor position? */ - const eStrCursorDelimType prev = (*pos > 0) ? cursor_delim_type_unicode(str[*pos - 1]) : - STRCUR_DELIM_NONE; - const eStrCursorDelimType next = (*pos < str_maxlen) ? cursor_delim_type_unicode(str[*pos]) : - STRCUR_DELIM_NONE; - *start = *pos; - *end = *pos; + const eStrCursorDelimType prev = (pos > 0) ? cursor_delim_type_unicode(str[pos - 1]) : + STRCUR_DELIM_NONE; + const eStrCursorDelimType next = (pos < str_maxlen) ? cursor_delim_type_unicode(str[pos]) : + STRCUR_DELIM_NONE; + *r_start = pos; + *r_end = pos; if (prev == next || ELEM(next, STRCUR_DELIM_WHITESPACE, STRCUR_DELIM_NONE)) { /* Expand backward if we are between similar content, before whitespace, or at end. */ - BLI_str_cursor_step_utf32(str, str_maxlen, start, STRCUR_DIR_PREV, STRCUR_JUMP_DELIM, false); + BLI_str_cursor_step_utf32(str, str_maxlen, r_start, STRCUR_DIR_PREV, STRCUR_JUMP_DELIM, false); } if (prev == next || ELEM(prev, STRCUR_DELIM_WHITESPACE, STRCUR_DELIM_NONE)) { /* Expand forward if we are between similar content, after whitespace, or at beginning. */ - BLI_str_cursor_step_utf32(str, str_maxlen, end, STRCUR_DIR_NEXT, STRCUR_JUMP_DELIM, false); + BLI_str_cursor_step_utf32(str, str_maxlen, r_end, STRCUR_DIR_NEXT, STRCUR_JUMP_DELIM, false); } - - /* Move cursor position to the end of selection. */ - *pos = *end; } diff --git a/source/blender/editors/curve/editfont.c b/source/blender/editors/curve/editfont.c index 227d158497a..5d2ee150ea0 100644 --- a/source/blender/editors/curve/editfont.c +++ b/source/blender/editors/curve/editfont.c @@ -1924,7 +1924,8 @@ static int font_select_word_exec(bContext *C, wmOperator *UNUSED(op)) Curve *cu = obedit->data; EditFont *ef = cu->editfont; - BLI_str_cursor_step_bounds_utf32(ef->textbuf, ef->len, &ef->pos, &ef->selstart, &ef->selend); + BLI_str_cursor_step_bounds_utf32(ef->textbuf, ef->len, ef->pos, &ef->selstart, &ef->selend); + ef->pos = ef->selend; /* XXX: Text object selection start is 1-based, unlike text processing elsewhere in Blender. */ ef->selstart += 1; diff --git a/source/blender/editors/interface/interface_handlers.cc b/source/blender/editors/interface/interface_handlers.cc index 0bec095455a..a799877e2fe 100644 --- a/source/blender/editors/interface/interface_handlers.cc +++ b/source/blender/editors/interface/interface_handlers.cc @@ -3730,10 +3730,9 @@ static void ui_do_but_textedit( /* only select a word in button if there was no selection before */ if (event->val == KM_DBL_CLICK && had_selection == false) { - int pos = (int)but->pos; int selsta, selend; - BLI_str_cursor_step_bounds_utf8(data->str, strlen(data->str), &pos, &selsta, &selend); - but->pos = (short)pos; + BLI_str_cursor_step_bounds_utf8(data->str, strlen(data->str), but->pos, &selsta, &selend); + but->pos = (short)selend; but->selsta = (short)selsta; but->selend = (short)selend; retval = WM_UI_HANDLER_BREAK; diff --git a/source/blender/editors/space_console/console_ops.c b/source/blender/editors/space_console/console_ops.c index 2654877c968..507156d8341 100644 --- a/source/blender/editors/space_console/console_ops.c +++ b/source/blender/editors/space_console/console_ops.c @@ -1258,7 +1258,7 @@ static int console_selectword_invoke(bContext *C, wmOperator *UNUSED(op), const if (console_line_column_from_index(sc, pos, &cl, &offset, &n)) { int sel[2] = {n, n}; - BLI_str_cursor_step_bounds_utf8(cl->line, cl->len, &n, &sel[0], &sel[1]); + BLI_str_cursor_step_bounds_utf8(cl->line, cl->len, n, &sel[0], &sel[1]); sel[0] = offset - sel[0]; sel[1] = offset - sel[1]; diff --git a/source/blender/editors/space_text/text_ops.c b/source/blender/editors/space_text/text_ops.c index 94e861ccfb0..58dffa36b2d 100644 --- a/source/blender/editors/space_text/text_ops.c +++ b/source/blender/editors/space_text/text_ops.c @@ -1580,7 +1580,7 @@ static int text_select_word_exec(bContext *C, wmOperator *UNUSED(op)) Text *text = CTX_data_edit_text(C); BLI_str_cursor_step_bounds_utf8( - text->curl->line, text->curl->len, &text->selc, &text->curc, &text->selc); + text->curl->line, text->curl->len, text->selc, &text->curc, &text->selc); text_update_cursor_moved(C); text_select_update_primary_clipboard(text);