Cleanup: move utf8 offset conversion into BLI_string_utf8

There isn't anything specific to text data with these functions.
This commit is contained in:
Campbell Barton 2019-08-06 17:16:27 +10:00
parent 8b2810a32f
commit dcad1eb03c
Notes: blender-bot 2023-02-14 05:53:38 +01:00
Referenced by issue #68335, No textures in Sculpt Mode in Workbench Color Mode Texture
Referenced by issue #68268, Crash when switching to/from Weight Paint mode
7 changed files with 68 additions and 62 deletions

View File

@ -59,10 +59,6 @@ void txt_order_cursors(struct Text *text, const bool reverse);
int txt_find_string(struct Text *text, const char *findstr, int wrap, int match_case);
bool txt_has_sel(struct Text *text);
int txt_get_span(struct TextLine *from, struct TextLine *to);
int txt_utf8_offset_to_index(const char *str, int offset);
int txt_utf8_index_to_offset(const char *str, int index);
int txt_utf8_offset_to_column(const char *str, int offset);
int txt_utf8_column_to_offset(const char *str, int column);
void txt_move_up(struct Text *text, const bool sel);
void txt_move_down(struct Text *text, const bool sel);
void txt_move_left(struct Text *text, const bool sel);

View File

@ -703,50 +703,6 @@ bool txt_cursor_is_line_end(Text *text)
/* Cursor movement functions */
/*****************************/
int txt_utf8_offset_to_index(const char *str, int offset)
{
int index = 0, pos = 0;
while (pos != offset) {
pos += BLI_str_utf8_size(str + pos);
index++;
}
return index;
}
int txt_utf8_index_to_offset(const char *str, int index)
{
int offset = 0, pos = 0;
while (pos != index) {
offset += BLI_str_utf8_size(str + offset);
pos++;
}
return offset;
}
int txt_utf8_offset_to_column(const char *str, int offset)
{
int column = 0, pos = 0;
while (pos < offset) {
column += BLI_str_utf8_char_width_safe(str + pos);
pos += BLI_str_utf8_size_safe(str + pos);
}
return column;
}
int txt_utf8_column_to_offset(const char *str, int column)
{
int offset = 0, pos = 0, col;
while (*(str + offset) && pos < column) {
col = BLI_str_utf8_char_width_safe(str + offset);
if (pos + col > column) {
break;
}
offset += BLI_str_utf8_size_safe(str + offset);
pos += col;
}
return offset;
}
void txt_move_up(Text *text, const bool sel)
{
TextLine **linep;
@ -764,9 +720,9 @@ void txt_move_up(Text *text, const bool sel)
}
if ((*linep)->prev) {
int column = txt_utf8_offset_to_column((*linep)->line, *charp);
int column = BLI_str_utf8_offset_to_column((*linep)->line, *charp);
*linep = (*linep)->prev;
*charp = txt_utf8_column_to_offset((*linep)->line, column);
*charp = BLI_str_utf8_offset_from_column((*linep)->line, column);
}
else {
txt_move_bol(text, sel);
@ -794,9 +750,9 @@ void txt_move_down(Text *text, const bool sel)
}
if ((*linep)->next) {
int column = txt_utf8_offset_to_column((*linep)->line, *charp);
int column = BLI_str_utf8_offset_to_column((*linep)->line, *charp);
*linep = (*linep)->next;
*charp = txt_utf8_column_to_offset((*linep)->line, column);
*charp = BLI_str_utf8_offset_from_column((*linep)->line, column);
}
else {
txt_move_eol(text, sel);

View File

@ -89,6 +89,11 @@ size_t BLI_str_partition_ex_utf8(const char *str,
const char **suf,
const bool from_right) ATTR_NONNULL(1, 3, 4, 5);
int BLI_str_utf8_offset_to_index(const char *str, int offset);
int BLI_str_utf8_offset_from_index(const char *str, int index);
int BLI_str_utf8_offset_to_column(const char *str, int offset);
int BLI_str_utf8_offset_from_column(const char *str, int column);
#define BLI_UTF8_MAX 6 /* mem */
#define BLI_UTF8_WIDTH_MAX 2 /* columns */
#define BLI_UTF8_ERR ((unsigned int)-1)

View File

@ -868,3 +868,53 @@ size_t BLI_str_partition_ex_utf8(const char *str,
*suf = *sep = NULL;
return str_len;
}
/* -------------------------------------------------------------------- */
/** \name Offset Conversion in Strings
* \{ */
int BLI_str_utf8_offset_to_index(const char *str, int offset)
{
int index = 0, pos = 0;
while (pos != offset) {
pos += BLI_str_utf8_size(str + pos);
index++;
}
return index;
}
int BLI_str_utf8_offset_from_index(const char *str, int index)
{
int offset = 0, pos = 0;
while (pos != index) {
offset += BLI_str_utf8_size(str + offset);
pos++;
}
return offset;
}
int BLI_str_utf8_offset_to_column(const char *str, int offset)
{
int column = 0, pos = 0;
while (pos < offset) {
column += BLI_str_utf8_char_width_safe(str + pos);
pos += BLI_str_utf8_size_safe(str + pos);
}
return column;
}
int BLI_str_utf8_offset_from_column(const char *str, int column)
{
int offset = 0, pos = 0, col;
while (*(str + offset) && pos < column) {
col = BLI_str_utf8_char_width_safe(str + offset);
if (pos + col > column) {
break;
}
offset += BLI_str_utf8_size_safe(str + offset);
pos += col;
}
return offset;
}
/** \} */

View File

@ -35,8 +35,6 @@
#include "GPU_immediate.h"
#include "GPU_state.h"
#include "BKE_text.h"
#include "textview.h"
static void console_font_begin(const int font_id, const int lheight)
@ -78,8 +76,8 @@ static void console_draw_sel(const char *str,
const unsigned char bg_sel[4])
{
if (sel[0] <= str_len_draw && sel[1] >= 0) {
const int sta = txt_utf8_offset_to_column(str, max_ii(sel[0], 0));
const int end = txt_utf8_offset_to_column(str, min_ii(sel[1], str_len_draw));
const int sta = BLI_str_utf8_offset_to_column(str, max_ii(sel[0], 0));
const int end = BLI_str_utf8_offset_to_column(str, min_ii(sel[1], str_len_draw));
GPU_blend(true);
GPU_blend_set_func_separate(
@ -156,7 +154,8 @@ static int console_draw_string(ConsoleDrawContext *cdc,
}
/* last part */
ofs += txt_utf8_column_to_offset(str + ofs, (int)floor((float)cdc->mval[0] / cdc->cwidth));
ofs += BLI_str_utf8_offset_from_column(str + ofs,
(int)floor((float)cdc->mval[0] / cdc->cwidth));
CLAMP(ofs, 0, str_len);
*cdc->pos_pick += str_len - ofs;

View File

@ -240,7 +240,7 @@ void wrap_offset(
}
max = wrap_width(st, ar);
cursin = txt_utf8_offset_to_column(linein->line, cursin);
cursin = BLI_str_utf8_offset_to_column(linein->line, cursin);
while (linep) {
start = 0;
@ -323,7 +323,7 @@ void wrap_offset_in_line(
end = max;
chop = 1;
*offc = 0;
cursin = txt_utf8_offset_to_column(linein->line, cursin);
cursin = BLI_str_utf8_offset_to_column(linein->line, cursin);
for (i = 0, j = 0; linein->line[j]; j += BLI_str_utf8_size_safe(linein->line + j)) {
int columns = BLI_str_utf8_char_width_safe(linein->line + j); /* = 1 for tab */
@ -1400,7 +1400,7 @@ static void draw_brackets(const SpaceText *st, const TextDrawContext *tdc, ARegi
linep = startl;
c = startc;
fc = txt_utf8_offset_to_index(linep->line, startc);
fc = BLI_str_utf8_offset_to_index(linep->line, startc);
endl = NULL;
endc = -1;
find = -b;

View File

@ -1827,7 +1827,7 @@ static void txt_wrap_move_bol(SpaceText *st, ARegion *ar, const bool sel)
if (j >= oldc) {
if (ch == '\0') {
*charp = txt_utf8_column_to_offset((*linep)->line, start);
*charp = BLI_str_utf8_offset_from_column((*linep)->line, start);
}
loop = 0;
break;
@ -1843,7 +1843,7 @@ static void txt_wrap_move_bol(SpaceText *st, ARegion *ar, const bool sel)
}
else if (ch == ' ' || ch == '-' || ch == '\0') {
if (j >= oldc) {
*charp = txt_utf8_column_to_offset((*linep)->line, start);
*charp = BLI_str_utf8_offset_from_column((*linep)->line, start);
loop = 0;
break;
}