Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2019-08-06 10:16:27 +0300
committerCampbell Barton <ideasman42@gmail.com>2019-08-06 14:59:13 +0300
commitdcad1eb03ccc0782229d81cdbeaa71c035c09955 (patch)
treec06a59d5751331a2a5eda36d27ba66a2fc9357f3 /source/blender/blenlib
parent8b2810a32f094f4d95663d630700c6f147703ad6 (diff)
Cleanup: move utf8 offset conversion into BLI_string_utf8
There isn't anything specific to text data with these functions.
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r--source/blender/blenlib/BLI_string_utf8.h5
-rw-r--r--source/blender/blenlib/intern/string_utf8.c50
2 files changed, 55 insertions, 0 deletions
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;
+}
+
+/** \} */