Blender V5.3
string_cursor_utf8.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cstdio>
11#include <cstdlib>
12
13#include "BLI_string_utf8.hh"
14#include "BLI_utildefines.hh"
15
16#include "BLI_string_cursor_utf8.hh" /* own include */
17
18#include "BLI_strict_flags.hh" /* IWYU pragma: keep. Keep last. */
19
20namespace blender {
21
44
46{
47 switch (uch) {
48 case ',':
49 case '.':
50 case 0x2026: /* Horizontal ellipsis. */
51 case 0x3002: /* CJK full width full stop. */
52 case 0xFF0C: /* CJK full width comma. */
53 case 0xFF61: /* CJK half width full stop. */
54 return STRCUR_DELIM_PUNCT;
55
56 case '{':
57 case '}':
58 case '[':
59 case ']':
60 case '(':
61 case ')':
62 case 0x3010: /* CJK full width left black lenticular bracket. */
63 case 0x3011: /* CJK full width right black lenticular bracket. */
64 case 0xFF08: /* CJK full width left parenthesis. */
65 case 0xFF09: /* CJK full width right parenthesis. */
66 return STRCUR_DELIM_BRACE;
67
68 case '+':
69 case '-':
70 case '=':
71 case '~':
72 case '%':
73 case '/':
74 case '<':
75 case '>':
76 case '^':
77 case '*':
78 case '&':
79 case '|':
80 case 0x2014: /* Em dash. */
81 case 0x300A: /* CJK full width left double angle bracket. */
82 case 0x300B: /* CJK full width right double angle bracket. */
83 case 0xFF0F: /* CJK full width solidus (forward slash). */
84 case 0xFF5E: /* CJK full width tilde. */
86
87 case '\'':
88 case '\"':
89 case '`':
90 case 0xB4: /* Acute accent. */
91 case 0x2018: /* Left single quotation mark. */
92 case 0x2019: /* Right single quotation mark. */
93 case 0x201C: /* Left double quotation mark. */
94 case 0x201D: /* Right double quotation mark. */
95 return STRCUR_DELIM_QUOTE;
96
97 case ' ':
98 case '\t':
99 case '\n':
101
102 case '\\':
103 case '@':
104 case '#':
105 case '$':
106 case ':':
107 case ';':
108 case '?':
109 case '!':
110 case 0xA3: /* Pound sign. */
111 case 0x80: /* Euro sign. */
112 case 0x3001: /* CJK ideographic comma. */
113 case 0xFF01: /* CJK full width exclamation mark. */
114 case 0xFF64: /* CJK half width ideographic comma. */
115 case 0xFF65: /* Katakana half width middle dot. */
116 case 0xFF1A: /* CJK full width colon. */
117 case 0xFF1B: /* CJK full width semicolon. */
118 case 0xFF1F: /* CJK full width question mark. */
119 /* case '_': */ /* special case, for python */
120 return STRCUR_DELIM_OTHER;
121
122 default:
123 break;
124 }
125 return STRCUR_DELIM_ALPHANUMERIC; /* Not quite true, but ok for now */
126}
127
129 const int ch_utf8_len,
130 const int pos)
131{
132 BLI_assert(ch_utf8_len >= 0);
133 /* for full unicode support we really need to have large lookup tables to figure
134 * out what's what in every possible char set - and python, glib both have these. */
135 size_t index = size_t(pos);
136 uint uch = BLI_str_utf8_as_unicode_step_or_error(ch_utf8, size_t(ch_utf8_len), &index);
137 return cursor_delim_type_unicode(uch);
138}
139
140bool BLI_str_cursor_step_next_utf8(const char *str, const int str_maxlen, int *pos)
141{
142 /* NOTE: Keep in sync with #BLI_str_cursor_step_next_utf32. */
143 BLI_assert(str_maxlen >= 0);
144 BLI_assert(*pos >= 0);
145
146 if (*pos >= str_maxlen) {
147 return false;
148 }
149 const char *str_end = str + (str_maxlen + 1);
150 const char *str_pos = str + *pos;
151 const char *str_next = str_pos;
152 do {
153 str_next = BLI_str_find_next_char_utf8(str_next, str_end);
154 } while ((str_next < str_end) && (str_next[0] != 0) &&
155 (BLI_str_utf8_char_width_or_error(str_next) == 0));
156 *pos += int(str_next - str_pos);
157 *pos = std::min(*pos, str_maxlen);
158
159 return true;
160}
161
162bool BLI_str_cursor_step_prev_utf8(const char *str, const int str_maxlen, int *pos)
163{
164 /* NOTE: Keep in sync with #BLI_str_cursor_step_prev_utf32. */
165 BLI_assert(str_maxlen >= 0);
166 BLI_assert(*pos >= 0);
167
168 if ((*pos > 0) && (*pos <= str_maxlen)) {
169 const char *str_pos = str + *pos;
170 const char *str_prev = str_pos;
171 do {
172 str_prev = BLI_str_find_prev_char_utf8(str_prev, str);
173 } while ((str_prev > str) && (BLI_str_utf8_char_width_or_error(str_prev) == 0));
174 *pos -= int(str_pos - str_prev);
175 return true;
176 }
177
178 return false;
179}
180
182 const int str_maxlen,
183 int *pos,
184 eStrCursorJumpDirection direction,
186 bool use_init_step)
187{
188 BLI_assert(str_maxlen >= 0);
189 const int pos_orig = *pos;
190
191 if (direction == STRCUR_DIR_NEXT) {
192 if (use_init_step) {
193 BLI_str_cursor_step_next_utf8(str, str_maxlen, pos);
194 }
195 else {
197 }
198
199 if (jump != STRCUR_JUMP_NONE) {
200 const eStrCursorDelimType delim_type = (*pos < str_maxlen) ?
201 cursor_delim_type_utf8(str, str_maxlen, *pos) :
203 /* jump between special characters (/,\,_,-, etc.),
204 * look at function cursor_delim_type() for complete
205 * list of special character, ctr -> */
206 while (*pos < str_maxlen) {
207 if (BLI_str_cursor_step_next_utf8(str, str_maxlen, pos)) {
208 if (*pos == str_maxlen) {
209 break;
210 }
211 if ((jump != STRCUR_JUMP_ALL) &&
212 (delim_type != cursor_delim_type_utf8(str, str_maxlen, *pos)))
213 {
214 break;
215 }
216 }
217 else {
218 break; /* unlikely but just in case */
219 }
220 }
221 }
222 }
223 else if (direction == STRCUR_DIR_PREV) {
224 if (use_init_step) {
225 BLI_str_cursor_step_prev_utf8(str, str_maxlen, pos);
226 }
227 else {
229 }
230
231 if (jump != STRCUR_JUMP_NONE) {
232 const eStrCursorDelimType delim_type = (*pos > 0) ? cursor_delim_type_utf8(
233 str, str_maxlen, *pos - 1) :
235 /* jump between special characters (/,\,_,-, etc.),
236 * look at function cursor_delim_type() for complete
237 * list of special character, ctr -> */
238 while (*pos > 0) {
239 const int pos_prev = *pos;
240 if (BLI_str_cursor_step_prev_utf8(str, str_maxlen, pos)) {
241 if ((jump != STRCUR_JUMP_ALL) &&
242 (delim_type != cursor_delim_type_utf8(str, str_maxlen, *pos)))
243 {
244 /* left only: compensate for index/change in direction */
245 if ((pos_orig - *pos) >= 1) {
246 *pos = pos_prev;
247 }
248 break;
249 }
250 }
251 else {
252 break;
253 }
254 }
255 }
256 }
257 else {
259 }
260}
261
262bool BLI_str_cursor_step_next_utf32(const char32_t *str, const int str_maxlen, int *pos)
263{
264 /* NOTE: Keep in sync with #BLI_str_cursor_step_next_utf8. */
265 BLI_assert(str_maxlen >= 0);
266 BLI_assert(*pos >= 0);
267
268 if (*pos >= str_maxlen) {
269 return false;
270 }
271 do {
272 (*pos)++;
273 } while ((*pos < str_maxlen) && (str[*pos] != 0) && (BLI_wcwidth_or_error(str[*pos]) == 0));
274
275 return true;
276}
277
278bool BLI_str_cursor_step_prev_utf32(const char32_t *str, const int str_maxlen, int *pos)
279{
280 /* NOTE: Keep in sync with #BLI_str_cursor_step_prev_utf8. */
281 BLI_assert(str_maxlen >= 0);
282 BLI_assert(*pos >= 0);
283 UNUSED_VARS_NDEBUG(str_maxlen);
284
285 if (*pos <= 0) {
286 return false;
287 }
288 do {
289 (*pos)--;
290 } while ((*pos > 0) && (BLI_wcwidth_or_error(str[*pos]) == 0));
291
292 return true;
293}
294
295void BLI_str_cursor_step_utf32(const char32_t *str,
296 const int str_maxlen,
297 int *pos,
298 eStrCursorJumpDirection direction,
300 bool use_init_step)
301{
302 BLI_assert(str_maxlen >= 0);
303 const int pos_orig = *pos;
304
305 if (direction == STRCUR_DIR_NEXT) {
306 if (use_init_step) {
307 BLI_str_cursor_step_next_utf32(str, str_maxlen, pos);
308 }
309 else {
311 }
312
313 if (jump != STRCUR_JUMP_NONE) {
314 const eStrCursorDelimType delim_type = (*pos < str_maxlen) ?
317 /* jump between special characters (/,\,_,-, etc.),
318 * look at function cursor_delim_type_unicode() for complete
319 * list of special character, ctr -> */
320 while (*pos < str_maxlen) {
321 if (BLI_str_cursor_step_next_utf32(str, str_maxlen, pos)) {
322 if ((jump != STRCUR_JUMP_ALL) &&
323 (delim_type != cursor_delim_type_unicode(uint(str[*pos]))))
324 {
325 break;
326 }
327 }
328 else {
329 break; /* unlikely but just in case */
330 }
331 }
332 }
333 }
334 else if (direction == STRCUR_DIR_PREV) {
335 if (use_init_step) {
336 BLI_str_cursor_step_prev_utf32(str, str_maxlen, pos);
337 }
338 else {
340 }
341
342 if (jump != STRCUR_JUMP_NONE) {
343 const eStrCursorDelimType delim_type = (*pos > 0) ?
344 cursor_delim_type_unicode(uint(str[(*pos) - 1])) :
346 /* jump between special characters (/,\,_,-, etc.),
347 * look at function cursor_delim_type() for complete
348 * list of special character, ctr -> */
349 while (*pos > 0) {
350 const int pos_prev = *pos;
351 if (BLI_str_cursor_step_prev_utf32(str, str_maxlen, pos)) {
352 if ((jump != STRCUR_JUMP_ALL) &&
353 (delim_type != cursor_delim_type_unicode(uint(str[*pos]))))
354 {
355 /* left only: compensate for index/change in direction */
356 if ((pos_orig - *pos) >= 1) {
357 *pos = pos_prev;
358 }
359 break;
360 }
361 }
362 else {
363 break;
364 }
365 }
366 }
367 }
368 else {
370 }
371}
372
374 const char *str, const int str_maxlen, const int pos, int *r_start, int *r_end)
375{
376 BLI_assert(str_maxlen >= 0);
377 BLI_assert(pos >= 0 && pos <= str_maxlen);
378 /* Identify the type of characters are on either side of the current cursor position. */
379 const eStrCursorDelimType prev = (pos > 0) ? cursor_delim_type_utf8(str, str_maxlen, pos - 1) :
381 const eStrCursorDelimType next = (pos < str_maxlen) ?
382 cursor_delim_type_utf8(str, str_maxlen, pos) :
384 *r_start = pos;
385 *r_end = pos;
386
387 if (prev != STRCUR_DELIM_NONE) {
388 if ((prev <= next) || (next == STRCUR_DELIM_NONE)) {
389 /* Expand backward if we are between similar content. */
391 str, str_maxlen, r_start, STRCUR_DIR_PREV, STRCUR_JUMP_DELIM, false);
392 }
393 }
394 if (next != STRCUR_DELIM_NONE) {
395 if ((next <= prev) || (prev == STRCUR_DELIM_NONE)) {
396 /* Expand forward if we are between similar content. */
398 }
399 }
400}
401
403 const char32_t *str, const int str_maxlen, const int pos, int *r_start, int *r_end)
404{
405 BLI_assert(str_maxlen >= 0);
406 BLI_assert(pos >= 0 && pos <= str_maxlen);
407 /* Identify the type of characters are on either side of the current cursor position. */
408 const eStrCursorDelimType prev = (pos > 0) ? cursor_delim_type_unicode(str[pos - 1]) :
410 const eStrCursorDelimType next = (pos < str_maxlen) ? cursor_delim_type_unicode(str[pos]) :
412 *r_start = pos;
413 *r_end = pos;
414
415 if (prev != STRCUR_DELIM_NONE) {
416 if ((prev <= next) || (next == STRCUR_DELIM_NONE)) {
417 /* Expand backward if we are between similar content. */
419 str, str_maxlen, r_start, STRCUR_DIR_PREV, STRCUR_JUMP_DELIM, false);
420 }
421 }
422 if (next != STRCUR_DELIM_NONE) {
423 if ((next <= prev) || (prev == STRCUR_DELIM_NONE)) {
424 /* Expand forward if we are between similar content. */
426 }
427 }
428}
429
430} // namespace blender
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
void jump(const btVector3 &v=btVector3(0, 0, 0))
#define str(s)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned int uint
Definition BKE_text.h:55
bool BLI_str_cursor_step_next_utf8(const char *str, int str_maxlen, int *pos)
static eStrCursorDelimType cursor_delim_type_utf8(const char *ch_utf8, const int ch_utf8_len, const int pos)
unsigned int unsigned int BLI_str_utf8_as_unicode_step_or_error(const char *__restrict p, size_t p_len, size_t *__restrict index) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
int BLI_str_utf8_char_width_or_error(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
const char const char * BLI_str_find_next_char_utf8(const char *p, const char *str_end) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1
bool BLI_str_cursor_step_prev_utf8(const char *str, int str_maxlen, int *pos)
void BLI_str_cursor_step_utf32(const char32_t *str, int str_maxlen, int *pos, eStrCursorJumpDirection direction, eStrCursorJumpType jump, bool use_init_step)
const char * BLI_str_find_prev_char_utf8(const char *p, const char *str_start) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1
void BLI_str_cursor_step_utf8(const char *str, int str_maxlen, int *pos, eStrCursorJumpDirection direction, eStrCursorJumpType jump, bool use_init_step)
bool BLI_str_cursor_step_next_utf32(const char32_t *str, int str_maxlen, int *pos)
static eStrCursorDelimType cursor_delim_type_unicode(const uint uch)
bool BLI_str_cursor_step_prev_utf32(const char32_t *str, int str_maxlen, int *pos)
void BLI_str_cursor_step_bounds_utf32(const char32_t *str, int str_maxlen, int pos, int *r_start, int *r_end)
size_t size_t int BLI_wcwidth_or_error(char32_t ucs) ATTR_WARN_UNUSED_RESULT
void BLI_str_cursor_step_bounds_utf8(const char *str, int str_maxlen, int pos, int *r_start, int *r_end)