2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2011 Blender Foundation.
19 * All rights reserved.
21 * Contributor(s): Campbell Barton.
23 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/blenlib/intern/string_cursor_utf8.c
34 #include "BLI_utildefines.h"
35 #include "BLI_string_utf8.h"
37 #include "BLI_string_cursor_utf8.h" /* own include */
40 # pragma GCC diagnostic error "-Wsign-conversion"
43 typedef enum strCursorDelimType {
45 STRCUR_DELIM_ALPHANUMERIC,
48 STRCUR_DELIM_OPERATOR,
50 STRCUR_DELIM_WHITESPACE,
54 static strCursorDelimType cursor_delim_type_unicode(const unsigned int uch)
59 return STRCUR_DELIM_PUNCT;
67 return STRCUR_DELIM_BRACE;
81 return STRCUR_DELIM_OPERATOR;
85 return STRCUR_DELIM_QUOTE;
90 return STRCUR_DELIM_WHITESPACE;
100 case 0xA3: /* pound */
101 case 0x80: /* euro */
102 /* case '_': *//* special case, for python */
103 return STRCUR_DELIM_OTHER;
108 return STRCUR_DELIM_ALPHANUMERIC; /* Not quite true, but ok for now */
111 static strCursorDelimType cursor_delim_type_utf8(const char *ch_utf8)
113 /* for full unicode support we really need to have large lookup tables to figure
114 * out whats what in every possible char set - and python, glib both have these. */
115 unsigned int uch = BLI_str_utf8_as_unicode(ch_utf8);
116 return cursor_delim_type_unicode(uch);
119 int BLI_str_cursor_step_next_utf8(const char *str, size_t maxlen, int *pos)
121 const char *str_end = str + (maxlen + 1);
122 const char *str_pos = str + (*pos);
123 const char *str_next = BLI_str_find_next_char_utf8(str_pos, str_end);
125 (*pos) += (str_next - str_pos);
126 if ((*pos) > (int)maxlen) {
127 (*pos) = (int)maxlen;
135 int BLI_str_cursor_step_prev_utf8(const char *str, size_t UNUSED(maxlen), int *pos)
138 const char *str_pos = str + (*pos);
139 const char *str_prev = BLI_str_find_prev_char_utf8(str, str_pos);
141 (*pos) -= (str_pos - str_prev);
149 void BLI_str_cursor_step_utf8(const char *str, size_t maxlen,
150 int *pos, strCursorJumpDirection direction,
151 strCursorJumpType jump, bool use_init_step)
153 const int pos_orig = *pos;
155 if (direction == STRCUR_DIR_NEXT) {
157 BLI_str_cursor_step_next_utf8(str, maxlen, pos);
160 BLI_assert(jump == STRCUR_JUMP_DELIM);
163 if (jump != STRCUR_JUMP_NONE) {
164 const strCursorDelimType delim_type = (*pos) < maxlen ? cursor_delim_type_utf8(&str[*pos]) : STRCUR_DELIM_NONE;
165 /* jump between special characters (/,\,_,-, etc.),
166 * look at function cursor_delim_type() for complete
167 * list of special character, ctr -> */
168 while ((*pos) < maxlen) {
169 if (BLI_str_cursor_step_next_utf8(str, maxlen, pos)) {
170 if ((jump != STRCUR_JUMP_ALL) && (delim_type != cursor_delim_type_utf8(&str[*pos]))) {
175 break; /* unlikely but just in case */
180 else if (direction == STRCUR_DIR_PREV) {
182 BLI_str_cursor_step_prev_utf8(str, maxlen, pos);
185 BLI_assert(jump == STRCUR_JUMP_DELIM);
188 if (jump != STRCUR_JUMP_NONE) {
189 const strCursorDelimType delim_type = (*pos) > 0 ? cursor_delim_type_utf8(&str[(*pos) - 1]) : STRCUR_DELIM_NONE;
190 /* jump between special characters (/,\,_,-, etc.),
191 * look at function cursor_delim_type() for complete
192 * list of special character, ctr -> */
194 const int pos_prev = *pos;
195 if (BLI_str_cursor_step_prev_utf8(str, maxlen, pos)) {
196 if ((jump != STRCUR_JUMP_ALL) && (delim_type != cursor_delim_type_utf8(&str[*pos]))) {
197 /* left only: compensate for index/change in direction */
198 if ((pos_orig - (*pos)) >= 1) {
215 /* wchar_t version of BLI_str_cursor_step_utf8 (keep in sync!)
216 * less complex since it doesn't need to do multi-byte stepping.
219 /* helper funcs so we can match BLI_str_cursor_step_utf8 */
220 static bool wchar_t_step_next(const wchar_t *UNUSED(str), size_t maxlen, int *pos)
222 if ((*pos) >= (int)maxlen) {
229 static bool wchar_t_step_prev(const wchar_t *UNUSED(str), size_t UNUSED(maxlen), int *pos)
238 void BLI_str_cursor_step_wchar(const wchar_t *str, size_t maxlen,
239 int *pos, strCursorJumpDirection direction,
240 strCursorJumpType jump, bool use_init_step)
242 const int pos_orig = *pos;
244 if (direction == STRCUR_DIR_NEXT) {
246 wchar_t_step_next(str, maxlen, pos);
249 BLI_assert(jump == STRCUR_JUMP_DELIM);
252 if (jump != STRCUR_JUMP_NONE) {
253 const strCursorDelimType delim_type = (*pos) < maxlen ? cursor_delim_type_unicode((unsigned int)str[*pos]) : STRCUR_DELIM_NONE;
254 /* jump between special characters (/,\,_,-, etc.),
255 * look at function cursor_delim_type_unicode() for complete
256 * list of special character, ctr -> */
257 while ((*pos) < maxlen) {
258 if (wchar_t_step_next(str, maxlen, pos)) {
259 if ((jump != STRCUR_JUMP_ALL) && (delim_type != cursor_delim_type_unicode((unsigned int)str[*pos]))) {
264 break; /* unlikely but just in case */
269 else if (direction == STRCUR_DIR_PREV) {
271 wchar_t_step_prev(str, maxlen, pos);
274 BLI_assert(jump == STRCUR_JUMP_DELIM);
277 if (jump != STRCUR_JUMP_NONE) {
278 const strCursorDelimType delim_type = (*pos) > 0 ? cursor_delim_type_unicode((unsigned int)str[(*pos) - 1]) : STRCUR_DELIM_NONE;
279 /* jump between special characters (/,\,_,-, etc.),
280 * look at function cursor_delim_type() for complete
281 * list of special character, ctr -> */
283 const int pos_prev = *pos;
284 if (wchar_t_step_prev(str, maxlen, pos)) {
285 if ((jump != STRCUR_JUMP_ALL) && (delim_type != cursor_delim_type_unicode((unsigned int)str[*pos]))) {
286 /* left only: compensate for index/change in direction */
287 if ((pos_orig - (*pos)) >= 1) {