[Bf-blender-cvs] [dcad1eb03cc] master: Cleanup: move utf8 offset conversion into BLI_string_utf8

Campbell Barton noreply at git.blender.org
Tue Aug 6 14:02:37 CEST 2019


Commit: dcad1eb03ccc0782229d81cdbeaa71c035c09955
Author: Campbell Barton
Date:   Tue Aug 6 17:16:27 2019 +1000
Branches: master
https://developer.blender.org/rBdcad1eb03ccc0782229d81cdbeaa71c035c09955

Cleanup: move utf8 offset conversion into BLI_string_utf8

There isn't anything specific to text data with these functions.

===================================================================

M	source/blender/blenkernel/BKE_text.h
M	source/blender/blenkernel/intern/text.c
M	source/blender/blenlib/BLI_string_utf8.h
M	source/blender/blenlib/intern/string_utf8.c
M	source/blender/editors/space_info/textview.c
M	source/blender/editors/space_text/text_draw.c
M	source/blender/editors/space_text/text_ops.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_text.h b/source/blender/blenkernel/BKE_text.h
index f018e912945..65aa43ced7c 100644
--- a/source/blender/blenkernel/BKE_text.h
+++ b/source/blender/blenkernel/BKE_text.h
@@ -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);
diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c
index b2ea5b12603..562e2814efa 100644
--- a/source/blender/blenkernel/intern/text.c
+++ b/source/blender/blenkernel/intern/text.c
@@ -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);
diff --git a/source/blender/blenlib/BLI_string_utf8.h b/source/blender/blenlib/BLI_string_utf8.h
index 70586b671b4..0cdd6e94610 100644
--- a/source/blender/blenlib/BLI_string_utf8.h
+++ b/source/blender/blenlib/BLI_string_utf8.h
@@ -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)
diff --git a/source/blender/blenlib/intern/string_utf8.c b/source/blender/blenlib/intern/string_utf8.c
index 01412416854..22c23727d76 100644
--- a/source/blender/blenlib/intern/string_utf8.c
+++ b/source/blender/blenlib/intern/string_utf8.c
@@ -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;
+}
+
+/** \} */
diff --git a/source/blender/editors/space_info/textview.c b/source/blender/editors/space_info/textview.c
index 108803a865f..24d3008b1e7 100644
--- a/source/blender/editors/space_info/textview.c
+++ b/source/blender/editors/space_info/textview.c
@@ -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;
diff --git a/source/blender/editors/space_text/text_draw.c b/source/blender/editors/space_text/text_draw.c
index c03b804aa2c..9dc8dfa93b6 100644
--- a/source/blender/editors/space_text/text_draw.c
+++ b/source/blender/editors/space_text/text_draw.c
@@ -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;
diff --git a/source/blender/editors/space_text/text_ops.c b/source/blender/editors/space_text/text_ops.c
index 7c4a403d43d..a8af9c73bf2 100644
--- a/source/blender/editors/space_text/text_ops.c
+++ b/source/blender/editors/space_text/text_ops.c
@@ -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;
         }



More information about the Bf-blender-cvs mailing list