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.
This commit is contained in:
Campbell Barton
2023-05-15 10:52:27 +10:00
parent 5eee40a63e
commit 26362f283f
6 changed files with 32 additions and 35 deletions

View File

@@ -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
}

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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];

View File

@@ -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);