Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/intern/string_cursor_utf8.c
| Show All 19 Lines | |||||
| /** \file | /** \file | ||||
| * \ingroup bli | * \ingroup bli | ||||
| */ | */ | ||||
| #include <stdio.h> | #include <stdio.h> | ||||
| #include <stdlib.h> | #include <stdlib.h> | ||||
| #include "BLI_utildefines.h" | #include "BLI_utildefines.h" | ||||
| #include "BLI_string_utf8.h" | |||||
| #include "BLI_string_cursor_utf8.h" /* own include */ | #include "BLI_string_cursor_utf8.h" /* own include */ | ||||
| #ifdef __GNUC__ | #ifdef __GNUC__ | ||||
| # pragma GCC diagnostic error "-Wsign-conversion" | # pragma GCC diagnostic error "-Wsign-conversion" | ||||
| #endif | #endif | ||||
| typedef enum eStrCursorDelimType { | typedef enum eStrCursorDelimType { | ||||
| STRCUR_DELIM_NONE, | STRCUR_DELIM_NONE, | ||||
| STRCUR_DELIM_ALPHANUMERIC, | STRCUR_DELIM_ALPHANUMERIC, | ||||
| STRCUR_DELIM_PUNCT, | STRCUR_DELIM_PUNCT, | ||||
| STRCUR_DELIM_BRACE, | STRCUR_DELIM_BRACE, | ||||
| STRCUR_DELIM_OPERATOR, | STRCUR_DELIM_OPERATOR, | ||||
| STRCUR_DELIM_QUOTE, | STRCUR_DELIM_QUOTE, | ||||
| STRCUR_DELIM_WHITESPACE, | STRCUR_DELIM_WHITESPACE, | ||||
| STRCUR_DELIM_OTHER, | STRCUR_DELIM_OTHER, | ||||
| } eStrCursorDelimType; | } eStrCursorDelimType; | ||||
| static eStrCursorDelimType cursor_delim_type_unicode(const uint uch) | static eStrCursorDelimType cursor_delim_type_unicode(const BLI_unicode uch) | ||||
| { | { | ||||
| switch (uch) { | switch (uch) { | ||||
| case ',': | case ',': | ||||
| case '.': | case '.': | ||||
| return STRCUR_DELIM_PUNCT; | return STRCUR_DELIM_PUNCT; | ||||
| case '{': | case '{': | ||||
| case '}': | case '}': | ||||
| ▲ Show 20 Lines • Show All 155 Lines • ▼ Show 20 Lines | void BLI_str_cursor_step_utf8(const char *str, | ||||
| } | } | ||||
| } | } | ||||
| /* wchar_t version of BLI_str_cursor_step_utf8 (keep in sync!) | /* wchar_t version of BLI_str_cursor_step_utf8 (keep in sync!) | ||||
| * less complex since it doesn't need to do multi-byte stepping. | * less complex since it doesn't need to do multi-byte stepping. | ||||
| */ | */ | ||||
| /* helper funcs so we can match BLI_str_cursor_step_utf8 */ | /* helper funcs so we can match BLI_str_cursor_step_utf8 */ | ||||
| static bool wchar_t_step_next(const wchar_t *UNUSED(str), size_t maxlen, int *pos) | static bool wchar_t_step_next(const BLI_unicode *UNUSED(str), size_t maxlen, int *pos) | ||||
| { | { | ||||
| if ((*pos) >= (int)maxlen) { | if ((*pos) >= (int)maxlen) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| (*pos)++; | (*pos)++; | ||||
| return true; | return true; | ||||
| } | } | ||||
| static bool wchar_t_step_prev(const wchar_t *UNUSED(str), size_t UNUSED(maxlen), int *pos) | static bool wchar_t_step_prev(const BLI_unicode *UNUSED(str), size_t UNUSED(maxlen), int *pos) | ||||
| { | { | ||||
| if ((*pos) <= 0) { | if ((*pos) <= 0) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| (*pos)--; | (*pos)--; | ||||
| return true; | return true; | ||||
| } | } | ||||
| void BLI_str_cursor_step_wchar(const wchar_t *str, | void BLI_str_cursor_step_unicode(const BLI_unicode *str, | ||||
| size_t maxlen, | size_t maxlen, | ||||
| int *pos, | int *pos, | ||||
| eStrCursorJumpDirection direction, | eStrCursorJumpDirection direction, | ||||
| eStrCursorJumpType jump, | eStrCursorJumpType jump, | ||||
| bool use_init_step) | bool use_init_step) | ||||
| { | { | ||||
| const int pos_orig = *pos; | const int pos_orig = *pos; | ||||
| if (direction == STRCUR_DIR_NEXT) { | if (direction == STRCUR_DIR_NEXT) { | ||||
| if (use_init_step) { | if (use_init_step) { | ||||
| wchar_t_step_next(str, maxlen, pos); | wchar_t_step_next(str, maxlen, pos); | ||||
| } | } | ||||
| else { | else { | ||||
| BLI_assert(jump == STRCUR_JUMP_DELIM); | BLI_assert(jump == STRCUR_JUMP_DELIM); | ||||
| } | } | ||||
| if (jump != STRCUR_JUMP_NONE) { | if (jump != STRCUR_JUMP_NONE) { | ||||
| const eStrCursorDelimType delim_type = (*pos) < maxlen ? | const eStrCursorDelimType delim_type = (*pos) < maxlen ? | ||||
| cursor_delim_type_unicode((uint)str[*pos]) : | cursor_delim_type_unicode(str[*pos]) : | ||||
| STRCUR_DELIM_NONE; | STRCUR_DELIM_NONE; | ||||
| /* jump between special characters (/,\,_,-, etc.), | /* jump between special characters (/,\,_,-, etc.), | ||||
| * look at function cursor_delim_type_unicode() for complete | * look at function cursor_delim_type_unicode() for complete | ||||
| * list of special character, ctr -> */ | * list of special character, ctr -> */ | ||||
| while ((*pos) < maxlen) { | while ((*pos) < maxlen) { | ||||
| if (wchar_t_step_next(str, maxlen, pos)) { | if (wchar_t_step_next(str, maxlen, pos)) { | ||||
| if ((jump != STRCUR_JUMP_ALL) && | if ((jump != STRCUR_JUMP_ALL) && (delim_type != cursor_delim_type_unicode(str[*pos]))) { | ||||
| (delim_type != cursor_delim_type_unicode((uint)str[*pos]))) { | |||||
| break; | break; | ||||
| } | } | ||||
| } | } | ||||
| else { | else { | ||||
| break; /* unlikely but just in case */ | break; /* unlikely but just in case */ | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| else if (direction == STRCUR_DIR_PREV) { | else if (direction == STRCUR_DIR_PREV) { | ||||
| if (use_init_step) { | if (use_init_step) { | ||||
| wchar_t_step_prev(str, maxlen, pos); | wchar_t_step_prev(str, maxlen, pos); | ||||
| } | } | ||||
| else { | else { | ||||
| BLI_assert(jump == STRCUR_JUMP_DELIM); | BLI_assert(jump == STRCUR_JUMP_DELIM); | ||||
| } | } | ||||
| if (jump != STRCUR_JUMP_NONE) { | if (jump != STRCUR_JUMP_NONE) { | ||||
| const eStrCursorDelimType delim_type = (*pos) > 0 ? | const eStrCursorDelimType delim_type = (*pos) > 0 ? | ||||
| cursor_delim_type_unicode((uint)str[(*pos) - 1]) : | cursor_delim_type_unicode(str[(*pos) - 1]) : | ||||
| STRCUR_DELIM_NONE; | STRCUR_DELIM_NONE; | ||||
| /* jump between special characters (/,\,_,-, etc.), | /* jump between special characters (/,\,_,-, etc.), | ||||
| * look at function cursor_delim_type() for complete | * look at function cursor_delim_type() for complete | ||||
| * list of special character, ctr -> */ | * list of special character, ctr -> */ | ||||
| while ((*pos) > 0) { | while ((*pos) > 0) { | ||||
| const int pos_prev = *pos; | const int pos_prev = *pos; | ||||
| if (wchar_t_step_prev(str, maxlen, pos)) { | if (wchar_t_step_prev(str, maxlen, pos)) { | ||||
| if ((jump != STRCUR_JUMP_ALL) && | if ((jump != STRCUR_JUMP_ALL) && (delim_type != cursor_delim_type_unicode(str[*pos]))) { | ||||
| (delim_type != cursor_delim_type_unicode((uint)str[*pos]))) { | |||||
| /* left only: compensate for index/change in direction */ | /* left only: compensate for index/change in direction */ | ||||
| if ((pos_orig - (*pos)) >= 1) { | if ((pos_orig - (*pos)) >= 1) { | ||||
| *pos = pos_prev; | *pos = pos_prev; | ||||
| } | } | ||||
| break; | break; | ||||
| } | } | ||||
| } | } | ||||
| else { | else { | ||||
| Show All 9 Lines | |||||