Branch data Line data Source code
1 : : /* guniprop.c - Unicode character properties.
2 : : *
3 : : * Copyright (C) 1999 Tom Tromey
4 : : * Copyright (C) 2000 Red Hat, Inc.
5 : : *
6 : : * SPDX-License-Identifier: LGPL-2.1-or-later
7 : : *
8 : : * This library is free software; you can redistribute it and/or
9 : : * modify it under the terms of the GNU Lesser General Public
10 : : * License as published by the Free Software Foundation; either
11 : : * version 2.1 of the License, or (at your option) any later version.
12 : : *
13 : : * This library is distributed in the hope that it will be useful,
14 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : : * Lesser General Public License for more details.
17 : : *
18 : : * You should have received a copy of the GNU Lesser General Public
19 : : * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 : : */
21 : :
22 : : #include "config.h"
23 : :
24 : : #include <stdlib.h>
25 : : #include <stddef.h>
26 : : #include <string.h>
27 : : #include <locale.h>
28 : :
29 : : #include "gmem.h"
30 : : #include "gstring.h"
31 : : #include "gtestutils.h"
32 : : #include "gtypes.h"
33 : : #include "gunicode.h"
34 : : #include "gunichartables.h"
35 : : #include "gmirroringtable.h"
36 : : #include "gscripttable.h"
37 : : #include "gunicodeprivate.h"
38 : : #ifdef G_OS_WIN32
39 : : #include "gwin32.h"
40 : : #endif
41 : :
42 : : #define G_UNICHAR_FULLWIDTH_A 0xff21
43 : : #define G_UNICHAR_FULLWIDTH_I 0xff29
44 : : #define G_UNICHAR_FULLWIDTH_J 0xff2a
45 : : #define G_UNICHAR_FULLWIDTH_F 0xff26
46 : : #define G_UNICHAR_FULLWIDTH_a 0xff41
47 : : #define G_UNICHAR_FULLWIDTH_f 0xff46
48 : :
49 : : #define ATTR_TABLE(Page) (((Page) <= G_UNICODE_LAST_PAGE_PART1) \
50 : : ? attr_table_part1[Page] \
51 : : : attr_table_part2[(Page) - 0xe00])
52 : :
53 : : #define ATTTABLE(Page, Char) \
54 : : ((ATTR_TABLE(Page) == G_UNICODE_MAX_TABLE_INDEX) ? 0 : (attr_data[ATTR_TABLE(Page)][Char]))
55 : :
56 : : #define TTYPE_PART1(Page, Char) \
57 : : ((type_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
58 : : ? (type_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
59 : : : (type_data[type_table_part1[Page]][Char]))
60 : :
61 : : #define TTYPE_PART2(Page, Char) \
62 : : ((type_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
63 : : ? (type_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
64 : : : (type_data[type_table_part2[Page]][Char]))
65 : :
66 : : #define TYPE(Char) \
67 : : (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
68 : : ? TTYPE_PART1 ((Char) >> 8, (Char) & 0xff) \
69 : : : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
70 : : ? TTYPE_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
71 : : : G_UNICODE_UNASSIGNED))
72 : :
73 : :
74 : : #define IS(Type, Class) (((guint)1 << (Type)) & (Class))
75 : : #define OR(Type, Rest) (((guint)1 << (Type)) | (Rest))
76 : :
77 : :
78 : :
79 : : #define ISALPHA(Type) IS ((Type), \
80 : : OR (G_UNICODE_LOWERCASE_LETTER, \
81 : : OR (G_UNICODE_UPPERCASE_LETTER, \
82 : : OR (G_UNICODE_TITLECASE_LETTER, \
83 : : OR (G_UNICODE_MODIFIER_LETTER, \
84 : : OR (G_UNICODE_OTHER_LETTER, 0))))))
85 : :
86 : : #define ISALDIGIT(Type) IS ((Type), \
87 : : OR (G_UNICODE_DECIMAL_NUMBER, \
88 : : OR (G_UNICODE_LETTER_NUMBER, \
89 : : OR (G_UNICODE_OTHER_NUMBER, \
90 : : OR (G_UNICODE_LOWERCASE_LETTER, \
91 : : OR (G_UNICODE_UPPERCASE_LETTER, \
92 : : OR (G_UNICODE_TITLECASE_LETTER, \
93 : : OR (G_UNICODE_MODIFIER_LETTER, \
94 : : OR (G_UNICODE_OTHER_LETTER, 0)))))))))
95 : :
96 : : #define ISMARK(Type) IS ((Type), \
97 : : OR (G_UNICODE_NON_SPACING_MARK, \
98 : : OR (G_UNICODE_SPACING_MARK, \
99 : : OR (G_UNICODE_ENCLOSING_MARK, 0))))
100 : :
101 : : #define ISZEROWIDTHTYPE(Type) IS ((Type), \
102 : : OR (G_UNICODE_NON_SPACING_MARK, \
103 : : OR (G_UNICODE_ENCLOSING_MARK, \
104 : : OR (G_UNICODE_FORMAT, 0))))
105 : :
106 : : /**
107 : : * g_unichar_isalnum:
108 : : * @c: a Unicode character
109 : : *
110 : : * Determines whether a character is alphanumeric.
111 : : * Given some UTF-8 text, obtain a character value
112 : : * with g_utf8_get_char().
113 : : *
114 : : * Returns: %TRUE if @c is an alphanumeric character
115 : : **/
116 : : gboolean
117 : 167003 : g_unichar_isalnum (gunichar c)
118 : : {
119 : 167003 : return ISALDIGIT (TYPE (c)) ? TRUE : FALSE;
120 : : }
121 : :
122 : : /**
123 : : * g_unichar_isalpha:
124 : : * @c: a Unicode character
125 : : *
126 : : * Determines whether a character is alphabetic (i.e. a letter).
127 : : * Given some UTF-8 text, obtain a character value with
128 : : * g_utf8_get_char().
129 : : *
130 : : * Returns: %TRUE if @c is an alphabetic character
131 : : **/
132 : : gboolean
133 : 414 : g_unichar_isalpha (gunichar c)
134 : : {
135 : 414 : return ISALPHA (TYPE (c)) ? TRUE : FALSE;
136 : : }
137 : :
138 : :
139 : : /**
140 : : * g_unichar_iscntrl:
141 : : * @c: a Unicode character
142 : : *
143 : : * Determines whether a character is a control character.
144 : : * Given some UTF-8 text, obtain a character value with
145 : : * g_utf8_get_char().
146 : : *
147 : : * Returns: %TRUE if @c is a control character
148 : : **/
149 : : gboolean
150 : 278 : g_unichar_iscntrl (gunichar c)
151 : : {
152 : 278 : return TYPE (c) == G_UNICODE_CONTROL;
153 : : }
154 : :
155 : : /**
156 : : * g_unichar_isdigit:
157 : : * @c: a Unicode character
158 : : *
159 : : * Determines whether a character is numeric (i.e. a digit). This
160 : : * covers ASCII 0-9 and also digits in other languages/scripts. Given
161 : : * some UTF-8 text, obtain a character value with g_utf8_get_char().
162 : : *
163 : : * Returns: %TRUE if @c is a digit
164 : : **/
165 : : gboolean
166 : 298 : g_unichar_isdigit (gunichar c)
167 : : {
168 : 298 : return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
169 : : }
170 : :
171 : :
172 : : /**
173 : : * g_unichar_isgraph:
174 : : * @c: a Unicode character
175 : : *
176 : : * Determines whether a character is printable and not a space
177 : : * (returns %FALSE for control characters, format characters, and
178 : : * spaces). g_unichar_isprint() is similar, but returns %TRUE for
179 : : * spaces. Given some UTF-8 text, obtain a character value with
180 : : * g_utf8_get_char().
181 : : *
182 : : * Returns: %TRUE if @c is printable unless it's a space
183 : : **/
184 : : gboolean
185 : 304 : g_unichar_isgraph (gunichar c)
186 : : {
187 : 304 : return !IS (TYPE(c),
188 : : OR (G_UNICODE_CONTROL,
189 : : OR (G_UNICODE_FORMAT,
190 : : OR (G_UNICODE_UNASSIGNED,
191 : : OR (G_UNICODE_SURROGATE,
192 : : OR (G_UNICODE_SPACE_SEPARATOR,
193 : : 0))))));
194 : : }
195 : :
196 : : /**
197 : : * g_unichar_islower:
198 : : * @c: a Unicode character
199 : : *
200 : : * Determines whether a character is a lowercase letter.
201 : : * Given some UTF-8 text, obtain a character value with
202 : : * g_utf8_get_char().
203 : : *
204 : : * Returns: %TRUE if @c is a lowercase letter
205 : : **/
206 : : gboolean
207 : 282 : g_unichar_islower (gunichar c)
208 : : {
209 : 282 : return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
210 : : }
211 : :
212 : :
213 : : /**
214 : : * g_unichar_isprint:
215 : : * @c: a Unicode character
216 : : *
217 : : * Determines whether a character is printable.
218 : : * Unlike g_unichar_isgraph(), returns %TRUE for spaces.
219 : : * Given some UTF-8 text, obtain a character value with
220 : : * g_utf8_get_char().
221 : : *
222 : : * Returns: %TRUE if @c is printable
223 : : **/
224 : : gboolean
225 : 91990516 : g_unichar_isprint (gunichar c)
226 : : {
227 : 91990516 : return !IS (TYPE(c),
228 : : OR (G_UNICODE_CONTROL,
229 : : OR (G_UNICODE_FORMAT,
230 : : OR (G_UNICODE_UNASSIGNED,
231 : : OR (G_UNICODE_SURROGATE,
232 : : 0)))));
233 : : }
234 : :
235 : : /**
236 : : * g_unichar_ispunct:
237 : : * @c: a Unicode character
238 : : *
239 : : * Determines whether a character is punctuation or a symbol.
240 : : * Given some UTF-8 text, obtain a character value with
241 : : * g_utf8_get_char().
242 : : *
243 : : * Returns: %TRUE if @c is a punctuation or symbol character
244 : : **/
245 : : gboolean
246 : 290 : g_unichar_ispunct (gunichar c)
247 : : {
248 : 149 : return IS (TYPE(c),
249 : : OR (G_UNICODE_CONNECT_PUNCTUATION,
250 : : OR (G_UNICODE_DASH_PUNCTUATION,
251 : : OR (G_UNICODE_CLOSE_PUNCTUATION,
252 : : OR (G_UNICODE_FINAL_PUNCTUATION,
253 : : OR (G_UNICODE_INITIAL_PUNCTUATION,
254 : : OR (G_UNICODE_OTHER_PUNCTUATION,
255 : : OR (G_UNICODE_OPEN_PUNCTUATION,
256 : : OR (G_UNICODE_CURRENCY_SYMBOL,
257 : : OR (G_UNICODE_MODIFIER_SYMBOL,
258 : : OR (G_UNICODE_MATH_SYMBOL,
259 : : OR (G_UNICODE_OTHER_SYMBOL,
260 : 149 : 0)))))))))))) ? TRUE : FALSE;
261 : : }
262 : :
263 : : /**
264 : : * g_unichar_isspace:
265 : : * @c: a Unicode character
266 : : *
267 : : * Determines whether a character is a space, tab, or line separator
268 : : * (newline, carriage return, etc.). Given some UTF-8 text, obtain a
269 : : * character value with g_utf8_get_char().
270 : : *
271 : : * (Note: don't use this to do word breaking; you have to use
272 : : * Pango or equivalent to get word breaking right, the algorithm
273 : : * is fairly complex.)
274 : : *
275 : : * Returns: %TRUE if @c is a space character
276 : : **/
277 : : gboolean
278 : 290 : g_unichar_isspace (gunichar c)
279 : : {
280 : 290 : switch (c)
281 : : {
282 : : /* special-case these since Unicode thinks they are not spaces */
283 : 8 : case '\t':
284 : : case '\n':
285 : : case '\r':
286 : : case '\f':
287 : 16 : return TRUE;
288 : : break;
289 : :
290 : 137 : default:
291 : : {
292 : 141 : return IS (TYPE(c),
293 : : OR (G_UNICODE_SPACE_SEPARATOR,
294 : : OR (G_UNICODE_LINE_SEPARATOR,
295 : : OR (G_UNICODE_PARAGRAPH_SEPARATOR,
296 : 141 : 0)))) ? TRUE : FALSE;
297 : : }
298 : : break;
299 : : }
300 : 145 : }
301 : :
302 : : /**
303 : : * g_unichar_ismark:
304 : : * @c: a Unicode character
305 : : *
306 : : * Determines whether a character is a mark (non-spacing mark,
307 : : * combining mark, or enclosing mark in Unicode speak).
308 : : * Given some UTF-8 text, obtain a character value
309 : : * with g_utf8_get_char().
310 : : *
311 : : * Note: in most cases where isalpha characters are allowed,
312 : : * ismark characters should be allowed to as they are essential
313 : : * for writing most European languages as well as many non-Latin
314 : : * scripts.
315 : : *
316 : : * Returns: %TRUE if @c is a mark character
317 : : *
318 : : * Since: 2.14
319 : : **/
320 : : gboolean
321 : 3279 : g_unichar_ismark (gunichar c)
322 : : {
323 : 3279 : return ISMARK (TYPE (c));
324 : : }
325 : :
326 : : /**
327 : : * g_unichar_isupper:
328 : : * @c: a Unicode character
329 : : *
330 : : * Determines if a character is uppercase.
331 : : *
332 : : * Returns: %TRUE if @c is an uppercase character
333 : : **/
334 : : gboolean
335 : 3924 : g_unichar_isupper (gunichar c)
336 : : {
337 : 3924 : return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
338 : : }
339 : :
340 : : /**
341 : : * g_unichar_istitle:
342 : : * @c: a Unicode character
343 : : *
344 : : * Determines if a character is titlecase. Some characters in
345 : : * Unicode which are composites, such as the DZ digraph
346 : : * have three case variants instead of just two. The titlecase
347 : : * form is used at the beginning of a word where only the
348 : : * first letter is capitalized. The titlecase form of the DZ
349 : : * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z.
350 : : *
351 : : * Returns: %TRUE if the character is titlecase
352 : : **/
353 : : gboolean
354 : 26 : g_unichar_istitle (gunichar c)
355 : : {
356 : : unsigned int i;
357 : 712 : for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
358 : 692 : if (title_table[i][0] == c)
359 : 6 : return TRUE;
360 : 20 : return FALSE;
361 : 13 : }
362 : :
363 : : /**
364 : : * g_unichar_isxdigit:
365 : : * @c: a Unicode character.
366 : : *
367 : : * Determines if a character is a hexadecimal digit.
368 : : *
369 : : * Returns: %TRUE if the character is a hexadecimal digit
370 : : **/
371 : : gboolean
372 : 318 : g_unichar_isxdigit (gunichar c)
373 : : {
374 : 560 : return ((c >= 'a' && c <= 'f') ||
375 : 393 : (c >= 'A' && c <= 'F') ||
376 : 304 : (c >= G_UNICHAR_FULLWIDTH_a && c <= G_UNICHAR_FULLWIDTH_f) ||
377 : 920 : (c >= G_UNICHAR_FULLWIDTH_A && c <= G_UNICHAR_FULLWIDTH_F) ||
378 : 277 : (TYPE (c) == G_UNICODE_DECIMAL_NUMBER));
379 : : }
380 : :
381 : : /**
382 : : * g_unichar_isdefined:
383 : : * @c: a Unicode character
384 : : *
385 : : * Determines if a given character is assigned in the Unicode
386 : : * standard.
387 : : *
388 : : * Returns: %TRUE if the character has an assigned value
389 : : **/
390 : : gboolean
391 : 30 : g_unichar_isdefined (gunichar c)
392 : : {
393 : 30 : return !IS (TYPE(c),
394 : : OR (G_UNICODE_UNASSIGNED,
395 : : OR (G_UNICODE_SURROGATE,
396 : : 0)));
397 : : }
398 : :
399 : : /**
400 : : * g_unichar_iszerowidth:
401 : : * @c: a Unicode character
402 : : *
403 : : * Determines if a given character typically takes zero width when rendered.
404 : : * The return value is %TRUE for all non-spacing and enclosing marks
405 : : * (e.g., combining accents), format characters, zero-width
406 : : * space, but not U+00AD SOFT HYPHEN.
407 : : *
408 : : * A typical use of this function is with one of g_unichar_iswide() or
409 : : * g_unichar_iswide_cjk() to determine the number of cells a string occupies
410 : : * when displayed on a grid display (terminals). However, note that not all
411 : : * terminals support zero-width rendering of zero-width marks.
412 : : *
413 : : * Returns: %TRUE if the character has zero width
414 : : *
415 : : * Since: 2.14
416 : : **/
417 : : gboolean
418 : 2672 : g_unichar_iszerowidth (gunichar c)
419 : : {
420 : 2672 : if (G_UNLIKELY (c == 0x00AD))
421 : 2 : return FALSE;
422 : :
423 : 2670 : if (G_UNLIKELY (ISZEROWIDTHTYPE (TYPE (c))))
424 : 30 : return TRUE;
425 : :
426 : : /* A few additional codepoints are zero-width:
427 : : * - Part of the Hangul Jamo block covering medial/vowels/jungseong and
428 : : * final/trailing_consonants/jongseong Jamo
429 : : * - Jungseong and jongseong for Old Korean
430 : : * - Zero-width space (U+200B)
431 : : */
432 : 4006 : if (G_UNLIKELY ((c >= 0x1160 && c < 0x1200) ||
433 : 1321 : (c >= 0xD7B0 && c < 0xD800) ||
434 : 1314 : c == 0x200B))
435 : 40 : return TRUE;
436 : :
437 : 2652 : return FALSE;
438 : 1331 : }
439 : :
440 : : static int
441 : 444 : interval_compare (const void *key, const void *elt)
442 : : {
443 : 444 : gunichar c = GPOINTER_TO_UINT (key);
444 : 444 : struct Interval *interval = (struct Interval *)elt;
445 : :
446 : 444 : if (c < interval->start)
447 : 165 : return -1;
448 : 279 : if (c > interval->end)
449 : 243 : return +1;
450 : :
451 : 36 : return 0;
452 : 220 : }
453 : :
454 : : #define G_WIDTH_TABLE_MIDPOINT (G_N_ELEMENTS (g_unicode_width_table_wide) / 2)
455 : :
456 : : static inline gboolean
457 : 136 : g_unichar_iswide_bsearch (gunichar ch)
458 : : {
459 : 136 : int lower = 0;
460 : 136 : int upper = G_N_ELEMENTS (g_unicode_width_table_wide) - 1;
461 : : static int saved_mid = G_WIDTH_TABLE_MIDPOINT;
462 : 136 : int mid = saved_mid;
463 : :
464 : 68 : do
465 : : {
466 : 912 : if (ch < g_unicode_width_table_wide[mid].start)
467 : 288 : upper = mid - 1;
468 : 624 : else if (ch > g_unicode_width_table_wide[mid].end)
469 : 572 : lower = mid + 1;
470 : : else
471 : 52 : return TRUE;
472 : :
473 : 860 : mid = (lower + upper) / 2;
474 : 430 : }
475 : 860 : while (lower <= upper);
476 : :
477 : 84 : return FALSE;
478 : 68 : }
479 : :
480 : : static const struct Interval default_wide_blocks[] = {
481 : : { 0x3400, 0x4dbf },
482 : : { 0x4e00, 0x9fff },
483 : : { 0xf900, 0xfaff },
484 : : { 0x20000, 0x2fffd },
485 : : { 0x30000, 0x3fffd }
486 : : };
487 : :
488 : : /**
489 : : * g_unichar_iswide:
490 : : * @c: a Unicode character
491 : : *
492 : : * Determines if a character is typically rendered in a double-width
493 : : * cell.
494 : : *
495 : : * Returns: %TRUE if the character is wide
496 : : **/
497 : : gboolean
498 : 2806 : g_unichar_iswide (gunichar c)
499 : : {
500 : 2806 : if (c < g_unicode_width_table_wide[0].start)
501 : 2670 : return FALSE;
502 : 136 : else if (g_unichar_iswide_bsearch (c))
503 : 52 : return TRUE;
504 : 98 : else if (g_unichar_type (c) == G_UNICODE_UNASSIGNED &&
505 : 28 : bsearch (GUINT_TO_POINTER (c),
506 : : default_wide_blocks,
507 : : G_N_ELEMENTS (default_wide_blocks),
508 : : sizeof default_wide_blocks[0],
509 : : interval_compare))
510 : 16 : return TRUE;
511 : :
512 : 68 : return FALSE;
513 : 1376 : }
514 : :
515 : :
516 : : /**
517 : : * g_unichar_iswide_cjk:
518 : : * @c: a Unicode character
519 : : *
520 : : * Determines if a character is typically rendered in a double-width
521 : : * cell under legacy East Asian locales. If a character is wide according to
522 : : * g_unichar_iswide(), then it is also reported wide with this function, but
523 : : * the converse is not necessarily true. See the
524 : : * [Unicode Standard Annex #11](http://www.unicode.org/reports/tr11/)
525 : : * for details.
526 : : *
527 : : * If a character passes the g_unichar_iswide() test then it will also pass
528 : : * this test, but not the other way around. Note that some characters may
529 : : * pass both this test and g_unichar_iszerowidth().
530 : : *
531 : : * Returns: %TRUE if the character is wide in legacy East Asian locales
532 : : *
533 : : * Since: 2.12
534 : : */
535 : : gboolean
536 : 86 : g_unichar_iswide_cjk (gunichar c)
537 : : {
538 : 86 : if (g_unichar_iswide (c))
539 : 34 : return TRUE;
540 : :
541 : : /* bsearch() is declared attribute(nonnull(1)) so we can't validly search
542 : : * for a NULL key */
543 : 52 : if (c == 0)
544 : 2 : return FALSE;
545 : :
546 : 50 : if (bsearch (GUINT_TO_POINTER (c),
547 : : g_unicode_width_table_ambiguous,
548 : : G_N_ELEMENTS (g_unicode_width_table_ambiguous),
549 : : sizeof g_unicode_width_table_ambiguous[0],
550 : : interval_compare))
551 : 20 : return TRUE;
552 : :
553 : 30 : return FALSE;
554 : 43 : }
555 : :
556 : :
557 : : /**
558 : : * g_unichar_toupper:
559 : : * @c: a Unicode character
560 : : *
561 : : * Converts a character to uppercase.
562 : : *
563 : : * Returns: the result of converting @c to uppercase.
564 : : * If @c is not a lowercase or titlecase character,
565 : : * or has no upper case equivalent @c is returned unchanged.
566 : : **/
567 : : gunichar
568 : 432 : g_unichar_toupper (gunichar c)
569 : : {
570 : 432 : int t = TYPE (c);
571 : 432 : if (t == G_UNICODE_LOWERCASE_LETTER)
572 : : {
573 : 160 : gunichar val = ATTTABLE (c >> 8, c & 0xff);
574 : 160 : if (val >= 0x1000000)
575 : : {
576 : 2 : const gchar *p = special_case_table + (val - 0x1000000);
577 : 2 : val = g_utf8_get_char (p);
578 : 1 : }
579 : : /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
580 : : * do not have an uppercase equivalent, in which case val will be
581 : : * zero.
582 : : */
583 : 160 : return val ? val : c;
584 : : }
585 : 272 : else if (t == G_UNICODE_TITLECASE_LETTER)
586 : : {
587 : : unsigned int i;
588 : 16 : for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
589 : : {
590 : 16 : if (title_table[i][0] == c)
591 : 4 : return title_table[i][1] ? title_table[i][1] : c;
592 : 6 : }
593 : 0 : }
594 : 268 : return c;
595 : 216 : }
596 : :
597 : : /**
598 : : * g_unichar_tolower:
599 : : * @c: a Unicode character.
600 : : *
601 : : * Converts a character to lower case.
602 : : *
603 : : * Returns: the result of converting @c to lower case.
604 : : * If @c is not an upperlower or titlecase character,
605 : : * or has no lowercase equivalent @c is returned unchanged.
606 : : **/
607 : : gunichar
608 : 3069578 : g_unichar_tolower (gunichar c)
609 : : {
610 : 3069578 : int t = TYPE (c);
611 : 3069578 : if (t == G_UNICODE_UPPERCASE_LETTER)
612 : : {
613 : 536528 : gunichar val = ATTTABLE (c >> 8, c & 0xff);
614 : 536528 : if (val >= 0x1000000)
615 : : {
616 : 2 : const gchar *p = special_case_table + (val - 0x1000000);
617 : 2 : return g_utf8_get_char (p);
618 : : }
619 : : else
620 : : {
621 : : /* Not all uppercase letters are guaranteed to have a lowercase
622 : : * equivalent. If this is the case, val will be zero. */
623 : 536526 : return val ? val : c;
624 : : }
625 : : }
626 : 2533050 : else if (t == G_UNICODE_TITLECASE_LETTER)
627 : : {
628 : : unsigned int i;
629 : 36 : for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
630 : : {
631 : 36 : if (title_table[i][0] == c)
632 : 12 : return title_table[i][2];
633 : 12 : }
634 : 0 : }
635 : 2533038 : return c;
636 : 3032824 : }
637 : :
638 : : /**
639 : : * g_unichar_totitle:
640 : : * @c: a Unicode character
641 : : *
642 : : * Converts a character to the titlecase.
643 : : *
644 : : * Returns: the result of converting @c to titlecase.
645 : : * If @c is not an uppercase or lowercase character,
646 : : * @c is returned unchanged.
647 : : **/
648 : : gunichar
649 : 34 : g_unichar_totitle (gunichar c)
650 : : {
651 : : unsigned int i;
652 : :
653 : : /* We handle U+0000 explicitly because some elements in
654 : : * title_table[i][1] may be null. */
655 : 34 : if (c == 0)
656 : 2 : return c;
657 : :
658 : 680 : for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
659 : : {
660 : 660 : if (title_table[i][0] == c || title_table[i][1] == c
661 : 653 : || title_table[i][2] == c)
662 : 12 : return title_table[i][0];
663 : 324 : }
664 : :
665 : 20 : if (TYPE (c) == G_UNICODE_LOWERCASE_LETTER)
666 : 2 : return g_unichar_toupper (c);
667 : :
668 : 18 : return c;
669 : 17 : }
670 : :
671 : : /**
672 : : * g_unichar_digit_value:
673 : : * @c: a Unicode character
674 : : *
675 : : * Determines the numeric value of a character as a decimal
676 : : * digit.
677 : : *
678 : : * Returns: If @c is a decimal digit (according to
679 : : * g_unichar_isdigit()), its numeric value. Otherwise, -1.
680 : : **/
681 : : int
682 : 296 : g_unichar_digit_value (gunichar c)
683 : : {
684 : 296 : if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
685 : 28 : return ATTTABLE (c >> 8, c & 0xff);
686 : 268 : return -1;
687 : 148 : }
688 : :
689 : : /**
690 : : * g_unichar_xdigit_value:
691 : : * @c: a Unicode character
692 : : *
693 : : * Determines the numeric value of a character as a hexadecimal
694 : : * digit.
695 : : *
696 : : * Returns: If @c is a hex digit (according to
697 : : * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
698 : : **/
699 : : int
700 : 312 : g_unichar_xdigit_value (gunichar c)
701 : : {
702 : 312 : if (c >= 'A' && c <= 'F')
703 : 16 : return c - 'A' + 10;
704 : 296 : if (c >= 'a' && c <= 'f')
705 : 16 : return c - 'a' + 10;
706 : 280 : if (c >= G_UNICHAR_FULLWIDTH_A && c <= G_UNICHAR_FULLWIDTH_F)
707 : 4 : return c - G_UNICHAR_FULLWIDTH_A + 10;
708 : 276 : if (c >= G_UNICHAR_FULLWIDTH_a && c <= G_UNICHAR_FULLWIDTH_f)
709 : 4 : return c - G_UNICHAR_FULLWIDTH_a + 10;
710 : 272 : if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
711 : 28 : return ATTTABLE (c >> 8, c & 0xff);
712 : 244 : return -1;
713 : 156 : }
714 : :
715 : : /**
716 : : * g_unichar_type:
717 : : * @c: a Unicode character
718 : : *
719 : : * Classifies a Unicode character by type.
720 : : *
721 : : * Returns: the type of the character.
722 : : **/
723 : : GUnicodeType
724 : 1200 : g_unichar_type (gunichar c)
725 : : {
726 : 1200 : return TYPE (c);
727 : : }
728 : :
729 : : /*
730 : : * Case mapping functions
731 : : */
732 : :
733 : : typedef enum {
734 : : LOCALE_NORMAL,
735 : : LOCALE_TURKIC,
736 : : LOCALE_LITHUANIAN
737 : : } LocaleType;
738 : :
739 : : static LocaleType
740 : 37562 : get_locale_type (void)
741 : : {
742 : : #ifdef G_OS_WIN32
743 : 8498 : char *tem = g_win32_getlocale ();
744 : : char locale[2];
745 : :
746 : 8498 : locale[0] = tem[0];
747 : 8498 : locale[1] = tem[1];
748 : 8498 : g_free (tem);
749 : : #else
750 : 29064 : const char *locale = setlocale (LC_CTYPE, NULL);
751 : :
752 : 29064 : if (locale == NULL)
753 : 0 : return LOCALE_NORMAL;
754 : : #endif
755 : :
756 : 37562 : switch (locale[0])
757 : : {
758 : 12 : case 'a':
759 : 24 : if (locale[1] == 'z')
760 : 24 : return LOCALE_TURKIC;
761 : 0 : break;
762 : 45 : case 'l':
763 : 90 : if (locale[1] == 't')
764 : 90 : return LOCALE_LITHUANIAN;
765 : 0 : break;
766 : 15 : case 't':
767 : 30 : if (locale[1] == 'r')
768 : 30 : return LOCALE_TURKIC;
769 : 0 : break;
770 : : }
771 : :
772 : 37418 : return LOCALE_NORMAL;
773 : 8498 : }
774 : :
775 : : G_ALWAYS_INLINE static inline void
776 : 17374 : increase_size (size_t *sizeptr, size_t add)
777 : : {
778 : 108496 : g_assert (G_MAXSIZE - *(sizeptr) >= add);
779 : 117072 : *(sizeptr) += add;
780 : 117072 : }
781 : :
782 : : G_ALWAYS_INLINE static inline void
783 : 8476 : append_utf8_char_to_buffer (gunichar c,
784 : : char *out_buffer,
785 : : size_t *in_out_len)
786 : : {
787 : : gint utf8_len;
788 : : char *buffer;
789 : :
790 : 17052 : buffer = out_buffer ? out_buffer + *(in_out_len) : NULL;
791 : 17052 : utf8_len = g_unichar_to_utf8 (c, buffer);
792 : :
793 : 17052 : g_assert (utf8_len >= 0);
794 : 17052 : increase_size (in_out_len, utf8_len);
795 : 17052 : }
796 : :
797 : : static void
798 : 28 : append_mark (const char **p_inout,
799 : : char *out_buffer,
800 : : size_t *in_out_len,
801 : : const char *limit,
802 : : gboolean remove_dot)
803 : : {
804 : 28 : const char *p = *p_inout;
805 : :
806 : 40 : while (p < limit && *p)
807 : : {
808 : 12 : gunichar c = g_utf8_get_char (p);
809 : :
810 : 12 : if (ISMARK (TYPE (c)))
811 : : {
812 : 12 : if (g_utf8_next_char (p) > limit)
813 : 0 : break;
814 : 12 : if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
815 : 2 : append_utf8_char_to_buffer (c, out_buffer, in_out_len);
816 : 12 : p = g_utf8_next_char (p);
817 : 6 : }
818 : : else
819 : 0 : break;
820 : : }
821 : :
822 : 28 : *p_inout = p;
823 : 28 : }
824 : :
825 : : static void
826 : 524 : append_special_case (char *out_buffer,
827 : : size_t *in_out_len,
828 : : int offset,
829 : : int type,
830 : : int which)
831 : : {
832 : 524 : const gchar *p = special_case_table + offset;
833 : : size_t len;
834 : :
835 : 524 : if (type != G_UNICODE_TITLECASE_LETTER)
836 : 308 : p = g_utf8_next_char (p);
837 : :
838 : 524 : if (which == 1)
839 : 108 : p += strlen (p) + 1;
840 : :
841 : 524 : len = strlen (p);
842 : 524 : g_assert (len < G_MAXSIZE - *in_out_len);
843 : :
844 : 524 : if (out_buffer)
845 : 262 : memcpy (out_buffer + *in_out_len, p, len);
846 : :
847 : 262 : increase_size (in_out_len, len);
848 : 524 : }
849 : :
850 : : static gsize
851 : 58110 : real_toupper (const gchar *str,
852 : : gssize max_len,
853 : : gchar *out_buffer,
854 : : LocaleType locale_type)
855 : : {
856 : 58110 : const gchar *p = str;
857 : 58110 : const gchar *limit = (max_len < 0) ? (gpointer) G_MAXSIZE : str + max_len;
858 : 58110 : const char *last = NULL;
859 : 58110 : gsize len = 0;
860 : 58110 : gboolean last_was_i = FALSE;
861 : :
862 : 157634 : while ((max_len < 0 || p < str + max_len) && *p)
863 : : {
864 : 99524 : gunichar c = g_utf8_get_char (p);
865 : 99524 : int t = TYPE (c);
866 : : gunichar val;
867 : :
868 : 99524 : last = p;
869 : 99524 : p = g_utf8_next_char (p);
870 : :
871 : 99524 : if (locale_type == LOCALE_LITHUANIAN)
872 : : {
873 : 160 : if (c == 'i')
874 : 16 : last_was_i = TRUE;
875 : : else
876 : : {
877 : 144 : if (last_was_i)
878 : 8 : {
879 : : /* Nasty, need to remove any dot above. Though
880 : : * I think only E WITH DOT ABOVE occurs in practice
881 : : * which could simplify this considerably.
882 : : */
883 : : gsize decomp_len, i;
884 : : gunichar decomp[G_UNICHAR_MAX_DECOMPOSITION_LENGTH];
885 : :
886 : 16 : decomp_len = g_unichar_fully_decompose (c, FALSE, decomp, G_N_ELEMENTS (decomp));
887 : 40 : for (i=0; i < decomp_len; i++)
888 : : {
889 : :
890 : 24 : if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
891 : 24 : append_utf8_char_to_buffer (g_unichar_toupper (decomp[i]),
892 : 8 : out_buffer, &len);
893 : 12 : }
894 : :
895 : 16 : append_mark (&p, out_buffer, &len, limit, TRUE);
896 : :
897 : 16 : continue;
898 : : }
899 : :
900 : 128 : if (!ISMARK (t))
901 : 72 : last_was_i = FALSE;
902 : : }
903 : 72 : }
904 : :
905 : 99508 : if (locale_type == LOCALE_TURKIC && c == 'i')
906 : : {
907 : : /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
908 : 20 : append_utf8_char_to_buffer (0x130, out_buffer, &len);
909 : 10 : }
910 : 99488 : else if (c == 0x0345) /* COMBINING GREEK YPOGEGRAMMENI */
911 : : {
912 : : /* Nasty, need to move it after other combining marks .. this would go away if
913 : : * we normalized first.
914 : : */
915 : 12 : append_mark (&p, out_buffer, &len, limit, TRUE);
916 : :
917 : : /* And output as GREEK CAPITAL LETTER IOTA */
918 : 6 : append_utf8_char_to_buffer (0x399, out_buffer, &len);
919 : 6 : }
920 : 99476 : else if (IS (t,
921 : : OR (G_UNICODE_LOWERCASE_LETTER,
922 : : OR (G_UNICODE_TITLECASE_LETTER,
923 : : 0))))
924 : : {
925 : 9388 : val = ATTTABLE (c >> 8, c & 0xff);
926 : :
927 : 9388 : if (val >= 0x1000000)
928 : : {
929 : 618 : append_special_case (out_buffer, &len, val - 0x1000000, t,
930 : 206 : t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
931 : 206 : }
932 : : else
933 : : {
934 : 8976 : if (t == G_UNICODE_TITLECASE_LETTER)
935 : : {
936 : : unsigned int i;
937 : 40 : for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
938 : : {
939 : 40 : if (title_table[i][0] == c)
940 : : {
941 : 16 : val = title_table[i][1];
942 : 16 : break;
943 : : }
944 : 12 : }
945 : 8 : }
946 : :
947 : : /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
948 : : * do not have an uppercase equivalent, in which case val will be
949 : : * zero. */
950 : 8976 : append_utf8_char_to_buffer (val ? val : c, out_buffer, &len);
951 : : }
952 : 4656 : }
953 : : else
954 : : {
955 : 90088 : gsize char_len = g_utf8_skip[*(guchar *)last];
956 : :
957 : 90088 : if (out_buffer)
958 : 45044 : memcpy (out_buffer + len, last, char_len);
959 : :
960 : 3932 : increase_size (&len, char_len);
961 : : }
962 : :
963 : : }
964 : :
965 : 58110 : return len;
966 : : }
967 : :
968 : : /**
969 : : * g_utf8_strup:
970 : : * @str: a UTF-8 encoded string
971 : : * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
972 : : *
973 : : * Converts all Unicode characters in the string that have a case
974 : : * to uppercase. The exact manner that this is done depends
975 : : * on the current locale, and may result in the number of
976 : : * characters in the string increasing. (For instance, the
977 : : * German ess-zet will be changed to SS.)
978 : : *
979 : : * Returns: a newly allocated string, with all characters
980 : : * converted to uppercase.
981 : : **/
982 : : gchar *
983 : 29057 : g_utf8_strup (const gchar *str,
984 : : gssize len)
985 : : {
986 : : gsize result_len;
987 : : LocaleType locale_type;
988 : : gchar *result;
989 : :
990 : 29057 : g_return_val_if_fail (str != NULL, NULL);
991 : 29055 : g_return_val_if_fail (len < 0 || (gsize) len <= G_MAXSIZE - (guintptr) str, NULL);
992 : :
993 : 29055 : locale_type = get_locale_type ();
994 : :
995 : : /*
996 : : * We use a two pass approach to keep memory management simple
997 : : */
998 : 29055 : result_len = real_toupper (str, len, NULL, locale_type);
999 : 29055 : g_assert (result_len < G_MAXSIZE);
1000 : :
1001 : 29055 : result = g_malloc (result_len + 1);
1002 : 29055 : real_toupper (str, len, result, locale_type);
1003 : 29055 : result[result_len] = '\0';
1004 : :
1005 : 29055 : return result;
1006 : 4248 : }
1007 : :
1008 : : /* traverses the string checking for characters with combining class == 230
1009 : : * until a base character is found */
1010 : : static gboolean
1011 : 52 : has_more_above (const gchar *str,
1012 : : const gchar *limit)
1013 : : {
1014 : 52 : const gchar *p = str;
1015 : : gint combining_class;
1016 : :
1017 : 60 : while (p < limit && *p)
1018 : : {
1019 : 56 : combining_class = g_unichar_combining_class (g_utf8_get_char (p));
1020 : 56 : if (combining_class == 230)
1021 : 48 : return TRUE;
1022 : 8 : else if (combining_class == 0)
1023 : 0 : break;
1024 : 8 : if (g_utf8_next_char (p) > limit)
1025 : 0 : break;
1026 : 8 : p = g_utf8_next_char (p);
1027 : : }
1028 : :
1029 : 4 : return FALSE;
1030 : 26 : }
1031 : :
1032 : : static gsize
1033 : 17014 : real_tolower (const gchar *str,
1034 : : gssize max_len,
1035 : : gchar *out_buffer,
1036 : : LocaleType locale_type)
1037 : : {
1038 : 17014 : const gchar *p = str;
1039 : 17014 : const gchar *limit = (max_len < 0) ? (gpointer) G_MAXSIZE : str + max_len;
1040 : 17014 : const char *last = NULL;
1041 : 17014 : gsize len = 0;
1042 : :
1043 : 34462 : while ((max_len < 0 || p < str + max_len) && *p)
1044 : : {
1045 : 17452 : gunichar c = g_utf8_get_char (p);
1046 : 17452 : int t = TYPE (c);
1047 : : gunichar val;
1048 : :
1049 : 17452 : last = p;
1050 : 17452 : p = g_utf8_next_char (p);
1051 : :
1052 : 17452 : if (locale_type == LOCALE_TURKIC && (c == 'I' || c == 0x130 ||
1053 : 12 : c == G_UNICHAR_FULLWIDTH_I))
1054 : 24 : {
1055 : 64 : gboolean combining_dot = (p < limit) &&
1056 : 88 : (c == 'I' || c == G_UNICHAR_FULLWIDTH_I) &&
1057 : 40 : g_utf8_get_char (p) == 0x0307;
1058 : 48 : if (combining_dot || c == 0x130)
1059 : : {
1060 : : /* I + COMBINING DOT ABOVE => i (U+0069)
1061 : : * LATIN CAPITAL LETTER I WITH DOT ABOVE => i (U+0069) */
1062 : 12 : append_utf8_char_to_buffer (0x0069, out_buffer, &len);
1063 : :
1064 : 24 : if (combining_dot)
1065 : 20 : p = g_utf8_next_char (p);
1066 : 12 : }
1067 : : else
1068 : : {
1069 : : /* I => LATIN SMALL LETTER DOTLESS I */
1070 : 12 : append_utf8_char_to_buffer (0x131, out_buffer, &len);
1071 : : }
1072 : 24 : }
1073 : : /* Introduce an explicit dot above when lowercasing capital I's and J's
1074 : : * whenever there are more accents above. [SpecialCasing.txt] */
1075 : 17474 : else if (locale_type == LOCALE_LITHUANIAN &&
1076 : 168 : (c == 0x00cc || c == 0x00cd || c == 0x0128))
1077 : : {
1078 : 12 : append_utf8_char_to_buffer (0x0069, out_buffer, &len);
1079 : 12 : append_utf8_char_to_buffer (0x0307, out_buffer, &len);
1080 : :
1081 : 24 : switch (c)
1082 : : {
1083 : 4 : case 0x00cc:
1084 : 4 : append_utf8_char_to_buffer (0x0300, out_buffer, &len);
1085 : 8 : break;
1086 : 4 : case 0x00cd:
1087 : 4 : append_utf8_char_to_buffer (0x0301, out_buffer, &len);
1088 : 8 : break;
1089 : 4 : case 0x0128:
1090 : 4 : append_utf8_char_to_buffer (0x0303, out_buffer, &len);
1091 : 8 : break;
1092 : : }
1093 : 12 : }
1094 : 17372 : else if (locale_type == LOCALE_LITHUANIAN &&
1095 : 130 : (c == 'I' || c == G_UNICHAR_FULLWIDTH_I ||
1096 : 152 : c == 'J' || c == G_UNICHAR_FULLWIDTH_J || c == 0x012e) &&
1097 : 100 : has_more_above (p, limit))
1098 : : {
1099 : 48 : append_utf8_char_to_buffer (g_unichar_tolower (c), out_buffer, &len);
1100 : 48 : append_utf8_char_to_buffer (0x0307, out_buffer, &len);
1101 : 24 : }
1102 : 17324 : else if (c == 0x03A3) /* GREEK CAPITAL LETTER SIGMA */
1103 : : {
1104 : 8 : if ((max_len < 0 || p < str + max_len) && *p)
1105 : 4 : {
1106 : 8 : gunichar next_c = g_utf8_get_char (p);
1107 : 8 : int next_type = TYPE(next_c);
1108 : :
1109 : : /* SIGMA mapps differently depending on whether it is
1110 : : * final or not. The following simplified test would
1111 : : * fail in the case of combining marks following the
1112 : : * sigma, but I don't think that occurs in real text.
1113 : : * The test here matches that in ICU.
1114 : : */
1115 : 8 : if (ISALPHA (next_type)) /* Lu,Ll,Lt,Lm,Lo */
1116 : 4 : val = 0x3c3; /* GREEK SMALL SIGMA */
1117 : : else
1118 : 4 : val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
1119 : 4 : }
1120 : : else
1121 : 4 : val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
1122 : :
1123 : 6 : append_utf8_char_to_buffer (val, out_buffer, &len);
1124 : 6 : }
1125 : 17316 : else if (IS (t,
1126 : : OR (G_UNICODE_UPPERCASE_LETTER,
1127 : : OR (G_UNICODE_TITLECASE_LETTER,
1128 : : 0))))
1129 : : {
1130 : 7908 : val = ATTTABLE (c >> 8, c & 0xff);
1131 : :
1132 : 7908 : if (val >= 0x1000000)
1133 : : {
1134 : 112 : append_special_case (out_buffer, &len, val - 0x1000000, t, 0);
1135 : 56 : }
1136 : : else
1137 : : {
1138 : 7796 : if (t == G_UNICODE_TITLECASE_LETTER)
1139 : : {
1140 : : unsigned int i;
1141 : 40 : for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
1142 : : {
1143 : 40 : if (title_table[i][0] == c)
1144 : : {
1145 : 16 : val = title_table[i][2];
1146 : 16 : break;
1147 : : }
1148 : 12 : }
1149 : 8 : }
1150 : :
1151 : : /* Not all uppercase letters are guaranteed to have a lowercase
1152 : : * equivalent. If this is the case, val will be zero. */
1153 : 7796 : append_utf8_char_to_buffer (val ? val : c, out_buffer, &len);
1154 : : }
1155 : 3942 : }
1156 : : else
1157 : : {
1158 : 9408 : gsize char_len = g_utf8_skip[*(guchar *)last];
1159 : :
1160 : 9408 : if (out_buffer)
1161 : 4704 : memcpy (out_buffer + len, last, char_len);
1162 : :
1163 : 4704 : increase_size (&len, char_len);
1164 : : }
1165 : :
1166 : : }
1167 : :
1168 : 17010 : return len;
1169 : : }
1170 : :
1171 : : /**
1172 : : * g_utf8_strdown:
1173 : : * @str: a UTF-8 encoded string
1174 : : * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1175 : : *
1176 : : * Converts all Unicode characters in the string that have a case
1177 : : * to lowercase. The exact manner that this is done depends
1178 : : * on the current locale, and may result in the number of
1179 : : * characters in the string changing.
1180 : : *
1181 : : * Returns: a newly allocated string, with all characters
1182 : : * converted to lowercase.
1183 : : **/
1184 : : gchar *
1185 : 8509 : g_utf8_strdown (const gchar *str,
1186 : : gssize len)
1187 : : {
1188 : : gsize result_len;
1189 : : LocaleType locale_type;
1190 : : gchar *result;
1191 : :
1192 : 8509 : g_return_val_if_fail (str != NULL, NULL);
1193 : 8507 : g_return_val_if_fail (len < 0 || (gsize) len <= G_MAXSIZE - (guintptr) str, NULL);
1194 : :
1195 : 8507 : locale_type = get_locale_type ();
1196 : :
1197 : : /*
1198 : : * We use a two pass approach to keep memory management simple
1199 : : */
1200 : 8507 : result_len = real_tolower (str, len, NULL, locale_type);
1201 : 8507 : g_assert (result_len < G_MAXSIZE);
1202 : :
1203 : 8507 : result = g_malloc (result_len + 1);
1204 : 8507 : real_tolower (str, len, result, locale_type);
1205 : 8507 : result[result_len] = '\0';
1206 : :
1207 : 8507 : return result;
1208 : 4252 : }
1209 : :
1210 : : /**
1211 : : * g_utf8_casefold:
1212 : : * @str: a UTF-8 encoded string
1213 : : * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1214 : : *
1215 : : * Converts a string into a form that is independent of case. The
1216 : : * result will not correspond to any particular case, but can be
1217 : : * compared for equality or ordered with the results of calling
1218 : : * g_utf8_casefold() on other strings.
1219 : : *
1220 : : * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
1221 : : * only an approximation to the correct linguistic case insensitive
1222 : : * ordering, though it is a fairly good one. Getting this exactly
1223 : : * right would require a more sophisticated collation function that
1224 : : * takes case sensitivity into account. GLib does not currently
1225 : : * provide such a function.
1226 : : *
1227 : : * Returns: a newly allocated string, that is a
1228 : : * case independent form of @str.
1229 : : **/
1230 : : gchar *
1231 : 151020 : g_utf8_casefold (const gchar *str,
1232 : : gssize len)
1233 : : {
1234 : : GString *result;
1235 : : const char *p;
1236 : :
1237 : 151020 : g_return_val_if_fail (str != NULL, NULL);
1238 : 151018 : g_return_val_if_fail (len < 0 || (gsize) len <= G_MAXSIZE - (guintptr) str, NULL);
1239 : :
1240 : 151018 : result = g_string_new (NULL);
1241 : 151018 : p = str;
1242 : 3220738 : while ((len < 0 || p < str + len) && *p)
1243 : : {
1244 : 3069720 : gunichar ch = g_utf8_get_char (p);
1245 : :
1246 : 3069720 : int start = 0;
1247 : 3069720 : int end = G_N_ELEMENTS (casefold_table);
1248 : :
1249 : 3069720 : if (ch >= casefold_table[start].ch &&
1250 : 3679 : ch <= casefold_table[end - 1].ch)
1251 : : {
1252 : 10762 : while (TRUE)
1253 : 13355 : {
1254 : 25853 : int half = (start + end) / 2;
1255 : 25853 : if (ch == casefold_table[half].ch)
1256 : : {
1257 : 508 : g_string_append (result, casefold_table[half].data);
1258 : 508 : goto next;
1259 : : }
1260 : 25345 : else if (half == start)
1261 : 2483 : break;
1262 : 22862 : else if (ch > casefold_table[half].ch)
1263 : 7619 : start = half;
1264 : : else
1265 : 15243 : end = half;
1266 : : }
1267 : 1001 : }
1268 : :
1269 : 3069212 : g_string_append_unichar (result, g_unichar_tolower (ch));
1270 : :
1271 : 36825 : next:
1272 : 3069720 : p = g_utf8_next_char (p);
1273 : : }
1274 : :
1275 : 151018 : return g_string_free (result, FALSE);
1276 : 143623 : }
1277 : :
1278 : : /**
1279 : : * g_unichar_get_mirror_char:
1280 : : * @ch: a Unicode character
1281 : : * @mirrored_ch: (out): location to store the mirrored character
1282 : : *
1283 : : * In Unicode, some characters are "mirrored". This means that their
1284 : : * images are mirrored horizontally in text that is laid out from right
1285 : : * to left. For instance, "(" would become its mirror image, ")", in
1286 : : * right-to-left text.
1287 : : *
1288 : : * If @ch has the Unicode mirrored property and there is another unicode
1289 : : * character that typically has a glyph that is the mirror image of @ch's
1290 : : * glyph and @mirrored_ch is set, it puts that character in the address
1291 : : * pointed to by @mirrored_ch. Otherwise the original character is put.
1292 : : *
1293 : : * Returns: %TRUE if @ch has a mirrored character, %FALSE otherwise
1294 : : *
1295 : : * Since: 2.4
1296 : : **/
1297 : : gboolean
1298 : 14 : g_unichar_get_mirror_char (gunichar ch,
1299 : : gunichar *mirrored_ch)
1300 : : {
1301 : : gboolean found;
1302 : : gunichar mirrored;
1303 : :
1304 : 14 : mirrored = GLIB_GET_MIRRORING(ch);
1305 : :
1306 : 14 : found = ch != mirrored;
1307 : 14 : if (mirrored_ch)
1308 : 14 : *mirrored_ch = mirrored;
1309 : :
1310 : 14 : return found;
1311 : :
1312 : : }
1313 : :
1314 : : #define G_SCRIPT_TABLE_MIDPOINT (G_N_ELEMENTS (g_script_table) / 2)
1315 : :
1316 : : static inline GUnicodeScript
1317 : 2212453 : g_unichar_get_script_bsearch (gunichar ch)
1318 : : {
1319 : 2212453 : int lower = 0;
1320 : 2212453 : int upper = G_N_ELEMENTS (g_script_table) - 1;
1321 : : static int saved_mid = G_SCRIPT_TABLE_MIDPOINT;
1322 : 2212453 : int mid = saved_mid;
1323 : :
1324 : :
1325 : 1106410 : do
1326 : : {
1327 : 5713085 : if (ch < g_script_table[mid].start)
1328 : 3498446 : upper = mid - 1;
1329 : 2214639 : else if (ch >= g_script_table[mid].start + g_script_table[mid].chars)
1330 : 1909078 : lower = mid + 1;
1331 : : else
1332 : 305561 : return g_script_table[saved_mid = mid].script;
1333 : :
1334 : 5407524 : mid = (lower + upper) / 2;
1335 : 2703762 : }
1336 : 5407524 : while (lower <= upper);
1337 : :
1338 : 1906892 : return G_UNICODE_SCRIPT_UNKNOWN;
1339 : 1106410 : }
1340 : :
1341 : : /**
1342 : : * g_unichar_get_script:
1343 : : * @ch: a Unicode character
1344 : : *
1345 : : * Looks up the #GUnicodeScript for a particular character (as defined
1346 : : * by Unicode Standard Annex \#24). No check is made for @ch being a
1347 : : * valid Unicode character; if you pass in invalid character, the
1348 : : * result is undefined.
1349 : : *
1350 : : * This function is equivalent to pango_script_for_unichar() and the
1351 : : * two are interchangeable.
1352 : : *
1353 : : * Returns: the #GUnicodeScript for the character.
1354 : : *
1355 : : * Since: 2.14
1356 : : */
1357 : : GUnicodeScript
1358 : 2229775 : g_unichar_get_script (gunichar ch)
1359 : : {
1360 : 2229775 : if (ch < G_EASY_SCRIPTS_RANGE)
1361 : 17322 : return g_script_easy_table[ch];
1362 : : else
1363 : 2212453 : return g_unichar_get_script_bsearch (ch);
1364 : 1115488 : }
1365 : :
1366 : :
1367 : : /* http://unicode.org/iso15924/ */
1368 : : static const guint32 iso15924_tags[] =
1369 : : {
1370 : : #define PACK(a,b,c,d) ((guint32)((((guint8)(a))<<24)|(((guint8)(b))<<16)|(((guint8)(c))<<8)|((guint8)(d))))
1371 : :
1372 : : PACK ('Z','y','y','y'), /* G_UNICODE_SCRIPT_COMMON */
1373 : : PACK ('Z','i','n','h'), /* G_UNICODE_SCRIPT_INHERITED */
1374 : : PACK ('A','r','a','b'), /* G_UNICODE_SCRIPT_ARABIC */
1375 : : PACK ('A','r','m','n'), /* G_UNICODE_SCRIPT_ARMENIAN */
1376 : : PACK ('B','e','n','g'), /* G_UNICODE_SCRIPT_BENGALI */
1377 : : PACK ('B','o','p','o'), /* G_UNICODE_SCRIPT_BOPOMOFO */
1378 : : PACK ('C','h','e','r'), /* G_UNICODE_SCRIPT_CHEROKEE */
1379 : : PACK ('C','o','p','t'), /* G_UNICODE_SCRIPT_COPTIC */
1380 : : PACK ('C','y','r','l'), /* G_UNICODE_SCRIPT_CYRILLIC */
1381 : : PACK ('D','s','r','t'), /* G_UNICODE_SCRIPT_DESERET */
1382 : : PACK ('D','e','v','a'), /* G_UNICODE_SCRIPT_DEVANAGARI */
1383 : : PACK ('E','t','h','i'), /* G_UNICODE_SCRIPT_ETHIOPIC */
1384 : : PACK ('G','e','o','r'), /* G_UNICODE_SCRIPT_GEORGIAN */
1385 : : PACK ('G','o','t','h'), /* G_UNICODE_SCRIPT_GOTHIC */
1386 : : PACK ('G','r','e','k'), /* G_UNICODE_SCRIPT_GREEK */
1387 : : PACK ('G','u','j','r'), /* G_UNICODE_SCRIPT_GUJARATI */
1388 : : PACK ('G','u','r','u'), /* G_UNICODE_SCRIPT_GURMUKHI */
1389 : : PACK ('H','a','n','i'), /* G_UNICODE_SCRIPT_HAN */
1390 : : PACK ('H','a','n','g'), /* G_UNICODE_SCRIPT_HANGUL */
1391 : : PACK ('H','e','b','r'), /* G_UNICODE_SCRIPT_HEBREW */
1392 : : PACK ('H','i','r','a'), /* G_UNICODE_SCRIPT_HIRAGANA */
1393 : : PACK ('K','n','d','a'), /* G_UNICODE_SCRIPT_KANNADA */
1394 : : PACK ('K','a','n','a'), /* G_UNICODE_SCRIPT_KATAKANA */
1395 : : PACK ('K','h','m','r'), /* G_UNICODE_SCRIPT_KHMER */
1396 : : PACK ('L','a','o','o'), /* G_UNICODE_SCRIPT_LAO */
1397 : : PACK ('L','a','t','n'), /* G_UNICODE_SCRIPT_LATIN */
1398 : : PACK ('M','l','y','m'), /* G_UNICODE_SCRIPT_MALAYALAM */
1399 : : PACK ('M','o','n','g'), /* G_UNICODE_SCRIPT_MONGOLIAN */
1400 : : PACK ('M','y','m','r'), /* G_UNICODE_SCRIPT_MYANMAR */
1401 : : PACK ('O','g','a','m'), /* G_UNICODE_SCRIPT_OGHAM */
1402 : : PACK ('I','t','a','l'), /* G_UNICODE_SCRIPT_OLD_ITALIC */
1403 : : PACK ('O','r','y','a'), /* G_UNICODE_SCRIPT_ORIYA */
1404 : : PACK ('R','u','n','r'), /* G_UNICODE_SCRIPT_RUNIC */
1405 : : PACK ('S','i','n','h'), /* G_UNICODE_SCRIPT_SINHALA */
1406 : : PACK ('S','y','r','c'), /* G_UNICODE_SCRIPT_SYRIAC */
1407 : : PACK ('T','a','m','l'), /* G_UNICODE_SCRIPT_TAMIL */
1408 : : PACK ('T','e','l','u'), /* G_UNICODE_SCRIPT_TELUGU */
1409 : : PACK ('T','h','a','a'), /* G_UNICODE_SCRIPT_THAANA */
1410 : : PACK ('T','h','a','i'), /* G_UNICODE_SCRIPT_THAI */
1411 : : PACK ('T','i','b','t'), /* G_UNICODE_SCRIPT_TIBETAN */
1412 : : PACK ('C','a','n','s'), /* G_UNICODE_SCRIPT_CANADIAN_ABORIGINAL */
1413 : : PACK ('Y','i','i','i'), /* G_UNICODE_SCRIPT_YI */
1414 : : PACK ('T','g','l','g'), /* G_UNICODE_SCRIPT_TAGALOG */
1415 : : PACK ('H','a','n','o'), /* G_UNICODE_SCRIPT_HANUNOO */
1416 : : PACK ('B','u','h','d'), /* G_UNICODE_SCRIPT_BUHID */
1417 : : PACK ('T','a','g','b'), /* G_UNICODE_SCRIPT_TAGBANWA */
1418 : :
1419 : : /* Unicode-4.0 additions */
1420 : : PACK ('B','r','a','i'), /* G_UNICODE_SCRIPT_BRAILLE */
1421 : : PACK ('C','p','r','t'), /* G_UNICODE_SCRIPT_CYPRIOT */
1422 : : PACK ('L','i','m','b'), /* G_UNICODE_SCRIPT_LIMBU */
1423 : : PACK ('O','s','m','a'), /* G_UNICODE_SCRIPT_OSMANYA */
1424 : : PACK ('S','h','a','w'), /* G_UNICODE_SCRIPT_SHAVIAN */
1425 : : PACK ('L','i','n','b'), /* G_UNICODE_SCRIPT_LINEAR_B */
1426 : : PACK ('T','a','l','e'), /* G_UNICODE_SCRIPT_TAI_LE */
1427 : : PACK ('U','g','a','r'), /* G_UNICODE_SCRIPT_UGARITIC */
1428 : :
1429 : : /* Unicode-4.1 additions */
1430 : : PACK ('T','a','l','u'), /* G_UNICODE_SCRIPT_NEW_TAI_LUE */
1431 : : PACK ('B','u','g','i'), /* G_UNICODE_SCRIPT_BUGINESE */
1432 : : PACK ('G','l','a','g'), /* G_UNICODE_SCRIPT_GLAGOLITIC */
1433 : : PACK ('T','f','n','g'), /* G_UNICODE_SCRIPT_TIFINAGH */
1434 : : PACK ('S','y','l','o'), /* G_UNICODE_SCRIPT_SYLOTI_NAGRI */
1435 : : PACK ('X','p','e','o'), /* G_UNICODE_SCRIPT_OLD_PERSIAN */
1436 : : PACK ('K','h','a','r'), /* G_UNICODE_SCRIPT_KHAROSHTHI */
1437 : :
1438 : : /* Unicode-5.0 additions */
1439 : : PACK ('Z','z','z','z'), /* G_UNICODE_SCRIPT_UNKNOWN */
1440 : : PACK ('B','a','l','i'), /* G_UNICODE_SCRIPT_BALINESE */
1441 : : PACK ('X','s','u','x'), /* G_UNICODE_SCRIPT_CUNEIFORM */
1442 : : PACK ('P','h','n','x'), /* G_UNICODE_SCRIPT_PHOENICIAN */
1443 : : PACK ('P','h','a','g'), /* G_UNICODE_SCRIPT_PHAGS_PA */
1444 : : PACK ('N','k','o','o'), /* G_UNICODE_SCRIPT_NKO */
1445 : :
1446 : : /* Unicode-5.1 additions */
1447 : : PACK ('K','a','l','i'), /* G_UNICODE_SCRIPT_KAYAH_LI */
1448 : : PACK ('L','e','p','c'), /* G_UNICODE_SCRIPT_LEPCHA */
1449 : : PACK ('R','j','n','g'), /* G_UNICODE_SCRIPT_REJANG */
1450 : : PACK ('S','u','n','d'), /* G_UNICODE_SCRIPT_SUNDANESE */
1451 : : PACK ('S','a','u','r'), /* G_UNICODE_SCRIPT_SAURASHTRA */
1452 : : PACK ('C','h','a','m'), /* G_UNICODE_SCRIPT_CHAM */
1453 : : PACK ('O','l','c','k'), /* G_UNICODE_SCRIPT_OL_CHIKI */
1454 : : PACK ('V','a','i','i'), /* G_UNICODE_SCRIPT_VAI */
1455 : : PACK ('C','a','r','i'), /* G_UNICODE_SCRIPT_CARIAN */
1456 : : PACK ('L','y','c','i'), /* G_UNICODE_SCRIPT_LYCIAN */
1457 : : PACK ('L','y','d','i'), /* G_UNICODE_SCRIPT_LYDIAN */
1458 : :
1459 : : /* Unicode-5.2 additions */
1460 : : PACK ('A','v','s','t'), /* G_UNICODE_SCRIPT_AVESTAN */
1461 : : PACK ('B','a','m','u'), /* G_UNICODE_SCRIPT_BAMUM */
1462 : : PACK ('E','g','y','p'), /* G_UNICODE_SCRIPT_EGYPTIAN_HIEROGLYPHS */
1463 : : PACK ('A','r','m','i'), /* G_UNICODE_SCRIPT_IMPERIAL_ARAMAIC */
1464 : : PACK ('P','h','l','i'), /* G_UNICODE_SCRIPT_INSCRIPTIONAL_PAHLAVI */
1465 : : PACK ('P','r','t','i'), /* G_UNICODE_SCRIPT_INSCRIPTIONAL_PARTHIAN */
1466 : : PACK ('J','a','v','a'), /* G_UNICODE_SCRIPT_JAVANESE */
1467 : : PACK ('K','t','h','i'), /* G_UNICODE_SCRIPT_KAITHI */
1468 : : PACK ('L','i','s','u'), /* G_UNICODE_SCRIPT_LISU */
1469 : : PACK ('M','t','e','i'), /* G_UNICODE_SCRIPT_MEETEI_MAYEK */
1470 : : PACK ('S','a','r','b'), /* G_UNICODE_SCRIPT_OLD_SOUTH_ARABIAN */
1471 : : PACK ('O','r','k','h'), /* G_UNICODE_SCRIPT_OLD_TURKIC */
1472 : : PACK ('S','a','m','r'), /* G_UNICODE_SCRIPT_SAMARITAN */
1473 : : PACK ('L','a','n','a'), /* G_UNICODE_SCRIPT_TAI_THAM */
1474 : : PACK ('T','a','v','t'), /* G_UNICODE_SCRIPT_TAI_VIET */
1475 : :
1476 : : /* Unicode-6.0 additions */
1477 : : PACK ('B','a','t','k'), /* G_UNICODE_SCRIPT_BATAK */
1478 : : PACK ('B','r','a','h'), /* G_UNICODE_SCRIPT_BRAHMI */
1479 : : PACK ('M','a','n','d'), /* G_UNICODE_SCRIPT_MANDAIC */
1480 : :
1481 : : /* Unicode-6.1 additions */
1482 : : PACK ('C','a','k','m'), /* G_UNICODE_SCRIPT_CHAKMA */
1483 : : PACK ('M','e','r','c'), /* G_UNICODE_SCRIPT_MEROITIC_CURSIVE */
1484 : : PACK ('M','e','r','o'), /* G_UNICODE_SCRIPT_MEROITIC_HIEROGLYPHS */
1485 : : PACK ('P','l','r','d'), /* G_UNICODE_SCRIPT_MIAO */
1486 : : PACK ('S','h','r','d'), /* G_UNICODE_SCRIPT_SHARADA */
1487 : : PACK ('S','o','r','a'), /* G_UNICODE_SCRIPT_SORA_SOMPENG */
1488 : : PACK ('T','a','k','r'), /* G_UNICODE_SCRIPT_TAKRI */
1489 : :
1490 : : /* Unicode 7.0 additions */
1491 : : PACK ('B','a','s','s'), /* G_UNICODE_SCRIPT_BASSA_VAH */
1492 : : PACK ('A','g','h','b'), /* G_UNICODE_SCRIPT_CAUCASIAN_ALBANIAN */
1493 : : PACK ('D','u','p','l'), /* G_UNICODE_SCRIPT_DUPLOYAN */
1494 : : PACK ('E','l','b','a'), /* G_UNICODE_SCRIPT_ELBASAN */
1495 : : PACK ('G','r','a','n'), /* G_UNICODE_SCRIPT_GRANTHA */
1496 : : PACK ('K','h','o','j'), /* G_UNICODE_SCRIPT_KHOJKI*/
1497 : : PACK ('S','i','n','d'), /* G_UNICODE_SCRIPT_KHUDAWADI */
1498 : : PACK ('L','i','n','a'), /* G_UNICODE_SCRIPT_LINEAR_A */
1499 : : PACK ('M','a','h','j'), /* G_UNICODE_SCRIPT_MAHAJANI */
1500 : : PACK ('M','a','n','i'), /* G_UNICODE_SCRIPT_MANICHAEAN */
1501 : : PACK ('M','e','n','d'), /* G_UNICODE_SCRIPT_MENDE_KIKAKUI */
1502 : : PACK ('M','o','d','i'), /* G_UNICODE_SCRIPT_MODI */
1503 : : PACK ('M','r','o','o'), /* G_UNICODE_SCRIPT_MRO */
1504 : : PACK ('N','b','a','t'), /* G_UNICODE_SCRIPT_NABATAEAN */
1505 : : PACK ('N','a','r','b'), /* G_UNICODE_SCRIPT_OLD_NORTH_ARABIAN */
1506 : : PACK ('P','e','r','m'), /* G_UNICODE_SCRIPT_OLD_PERMIC */
1507 : : PACK ('H','m','n','g'), /* G_UNICODE_SCRIPT_PAHAWH_HMONG */
1508 : : PACK ('P','a','l','m'), /* G_UNICODE_SCRIPT_PALMYRENE */
1509 : : PACK ('P','a','u','c'), /* G_UNICODE_SCRIPT_PAU_CIN_HAU */
1510 : : PACK ('P','h','l','p'), /* G_UNICODE_SCRIPT_PSALTER_PAHLAVI */
1511 : : PACK ('S','i','d','d'), /* G_UNICODE_SCRIPT_SIDDHAM */
1512 : : PACK ('T','i','r','h'), /* G_UNICODE_SCRIPT_TIRHUTA */
1513 : : PACK ('W','a','r','a'), /* G_UNICODE_SCRIPT_WARANG_CITI */
1514 : :
1515 : : /* Unicode 8.0 additions */
1516 : : PACK ('A','h','o','m'), /* G_UNICODE_SCRIPT_AHOM */
1517 : : PACK ('H','l','u','w'), /* G_UNICODE_SCRIPT_ANATOLIAN_HIEROGLYPHS */
1518 : : PACK ('H','a','t','r'), /* G_UNICODE_SCRIPT_HATRAN */
1519 : : PACK ('M','u','l','t'), /* G_UNICODE_SCRIPT_MULTANI */
1520 : : PACK ('H','u','n','g'), /* G_UNICODE_SCRIPT_OLD_HUNGARIAN */
1521 : : PACK ('S','g','n','w'), /* G_UNICODE_SCRIPT_SIGNWRITING */
1522 : :
1523 : : /* Unicode 9.0 additions */
1524 : : PACK ('A','d','l','m'), /* G_UNICODE_SCRIPT_ADLAM */
1525 : : PACK ('B','h','k','s'), /* G_UNICODE_SCRIPT_BHAIKSUKI */
1526 : : PACK ('M','a','r','c'), /* G_UNICODE_SCRIPT_MARCHEN */
1527 : : PACK ('N','e','w','a'), /* G_UNICODE_SCRIPT_NEWA */
1528 : : PACK ('O','s','g','e'), /* G_UNICODE_SCRIPT_OSAGE */
1529 : : PACK ('T','a','n','g'), /* G_UNICODE_SCRIPT_TANGUT */
1530 : :
1531 : : /* Unicode 10.0 additions */
1532 : : PACK ('G','o','n','m'), /* G_UNICODE_SCRIPT_MASARAM_GONDI */
1533 : : PACK ('N','s','h','u'), /* G_UNICODE_SCRIPT_NUSHU */
1534 : : PACK ('S','o','y','o'), /* G_UNICODE_SCRIPT_SOYOMBO */
1535 : : PACK ('Z','a','n','b'), /* G_UNICODE_SCRIPT_ZANABAZAR_SQUARE */
1536 : :
1537 : : /* Unicode 11.0 additions */
1538 : : PACK ('D','o','g','r'), /* G_UNICODE_SCRIPT_DOGRA */
1539 : : PACK ('G','o','n','g'), /* G_UNICODE_SCRIPT_GUNJALA_GONDI */
1540 : : PACK ('R','o','h','g'), /* G_UNICODE_SCRIPT_HANIFI_ROHINGYA */
1541 : : PACK ('M','a','k','a'), /* G_UNICODE_SCRIPT_MAKASAR */
1542 : : PACK ('M','e','d','f'), /* G_UNICODE_SCRIPT_MEDEFAIDRIN */
1543 : : PACK ('S','o','g','o'), /* G_UNICODE_SCRIPT_OLD_SOGDIAN */
1544 : : PACK ('S','o','g','d'), /* G_UNICODE_SCRIPT_SOGDIAN */
1545 : :
1546 : : /* Unicode 12.0 additions */
1547 : : PACK ('E','l','y','m'), /* G_UNICODE_SCRIPT_ELYMAIC */
1548 : : PACK ('N','a','n','d'), /* G_UNICODE_SCRIPT_NANDINAGARI */
1549 : : PACK ('H','m','n','p'), /* G_UNICODE_SCRIPT_NYIAKENG_PUACHUE_HMONG */
1550 : : PACK ('W','c','h','o'), /* G_UNICODE_SCRIPT_WANCHO */
1551 : :
1552 : : /* Unicode 13.0 additions */
1553 : : PACK ('C', 'h', 'r', 's'), /* G_UNICODE_SCRIPT_CHORASMIAN */
1554 : : PACK ('D', 'i', 'a', 'k'), /* G_UNICODE_SCRIPT_DIVES_AKURU */
1555 : : PACK ('K', 'i', 't', 's'), /* G_UNICODE_SCRIPT_KHITAN_SMALL_SCRIPT */
1556 : : PACK ('Y', 'e', 'z', 'i'), /* G_UNICODE_SCRIPT_YEZIDI */
1557 : :
1558 : : /* Unicode 14.0 additions */
1559 : : PACK ('C', 'p', 'm', 'n'), /* G_UNICODE_SCRIPT_CYPRO_MINOAN */
1560 : : PACK ('O', 'u', 'g', 'r'), /* G_UNICODE_SCRIPT_OLD_UYHUR */
1561 : : PACK ('T', 'n', 's', 'a'), /* G_UNICODE_SCRIPT_TANGSA */
1562 : : PACK ('T', 'o', 't', 'o'), /* G_UNICODE_SCRIPT_TOTO */
1563 : : PACK ('V', 'i', 't', 'h'), /* G_UNICODE_SCRIPT_VITHKUQI */
1564 : :
1565 : : /* not really a Unicode script, but part of ISO 15924 */
1566 : : PACK ('Z', 'm', 't', 'h'), /* G_UNICODE_SCRIPT_MATH */
1567 : :
1568 : : /* Unicode 15.0 additions */
1569 : : PACK ('K', 'a', 'w', 'i'), /* G_UNICODE_SCRIPT_KAWI */
1570 : : PACK ('N', 'a', 'g', 'm'), /* G_UNICODE_SCRIPT_NAG_MUNDARI */
1571 : :
1572 : : /* Unicode 16.0 additions */
1573 : : PACK ('T', 'o', 'd', 'r'), /* G_UNICODE_SCRIPT_TODHRI */
1574 : : PACK ('G', 'a', 'r', 'a'), /* G_UNICODE_SCRIPT_GARAY */
1575 : : PACK ('T', 'u', 't', 'g'), /* G_UNICODE_SCRIPT_TULU_TIGALARI */
1576 : : PACK ('S', 'u', 'n', 'u'), /* G_UNICODE_SCRIPT_SUNUWAR */
1577 : : PACK ('G', 'u', 'k', 'h'), /* G_UNICODE_SCRIPT_GURUNG_KHEMA */
1578 : : PACK ('K', 'r', 'a', 'i'), /* G_UNICODE_SCRIPT_KIRAT_RAI */
1579 : : PACK ('O', 'n', 'a', 'o'), /* G_UNICODE_SCRIPT_OL_ONAL */
1580 : :
1581 : : PACK ('S', 'i', 'd', 't'), /* G_UNICODE_SCRIPT_SIDETIC */
1582 : : PACK ('T', 'o', 'l', 's'), /* G_UNICODE_SCRIPT_TOLONG_SIKI */
1583 : : PACK ('T', 'a', 'y', 'o'), /* G_UNICODE_SCRIPT_TAI_YO */
1584 : : PACK ('B', 'e', 'r', 'f'), /* G_UNICODE_SCRIPT_BERIA_ERFE */
1585 : :
1586 : : #undef PACK
1587 : : };
1588 : :
1589 : : /**
1590 : : * g_unicode_script_to_iso15924:
1591 : : * @script: a Unicode script
1592 : : *
1593 : : * Looks up the ISO 15924 code for @script. ISO 15924 assigns four-letter
1594 : : * codes to scripts. For example, the code for Arabic is 'Arab'. The
1595 : : * four letter codes are encoded as a @guint32 by this function in a
1596 : : * big-endian fashion. That is, the code returned for Arabic is
1597 : : * 0x41726162 (0x41 is ASCII code for 'A', 0x72 is ASCII code for 'r', etc).
1598 : : *
1599 : : * See
1600 : : * [Codes for the representation of names of scripts](http://unicode.org/iso15924/codelists.html)
1601 : : * for details.
1602 : : *
1603 : : * Returns: the ISO 15924 code for @script, encoded as an integer,
1604 : : * of zero if @script is %G_UNICODE_SCRIPT_INVALID_CODE or
1605 : : * ISO 15924 code 'Zzzz' (script code for UNKNOWN) if @script is not understood.
1606 : : *
1607 : : * Since: 2.30
1608 : : */
1609 : : guint32
1610 : 702 : g_unicode_script_to_iso15924 (GUnicodeScript script)
1611 : : {
1612 : 702 : if (G_UNLIKELY (script == G_UNICODE_SCRIPT_INVALID_CODE))
1613 : 2 : return 0;
1614 : :
1615 : 700 : if (G_UNLIKELY (script < 0 || script >= (int) G_N_ELEMENTS (iso15924_tags)))
1616 : 2 : return 0x5A7A7A7A;
1617 : :
1618 : 698 : return iso15924_tags[script];
1619 : 351 : }
1620 : :
1621 : : /**
1622 : : * g_unicode_script_from_iso15924:
1623 : : * @iso15924: a Unicode script
1624 : : *
1625 : : * Looks up the Unicode script for @iso15924. ISO 15924 assigns four-letter
1626 : : * codes to scripts. For example, the code for Arabic is 'Arab'.
1627 : : * This function accepts four letter codes encoded as a @guint32 in a
1628 : : * big-endian fashion. That is, the code expected for Arabic is
1629 : : * 0x41726162 (0x41 is ASCII code for 'A', 0x72 is ASCII code for 'r', etc).
1630 : : *
1631 : : * See
1632 : : * [Codes for the representation of names of scripts](http://unicode.org/iso15924/codelists.html)
1633 : : * for details.
1634 : : *
1635 : : * Returns: the Unicode script for @iso15924, or
1636 : : * of %G_UNICODE_SCRIPT_INVALID_CODE if @iso15924 is zero and
1637 : : * %G_UNICODE_SCRIPT_UNKNOWN if @iso15924 is unknown.
1638 : : *
1639 : : * Since: 2.30
1640 : : */
1641 : : GUnicodeScript
1642 : 700 : g_unicode_script_from_iso15924 (guint32 iso15924)
1643 : : {
1644 : : unsigned int i;
1645 : :
1646 : 700 : if (!iso15924)
1647 : 2 : return G_UNICODE_SCRIPT_INVALID_CODE;
1648 : :
1649 : 61262 : for (i = 0; i < G_N_ELEMENTS (iso15924_tags); i++)
1650 : 61260 : if (iso15924_tags[i] == iso15924)
1651 : 696 : return (GUnicodeScript) i;
1652 : :
1653 : 2 : return G_UNICODE_SCRIPT_UNKNOWN;
1654 : 350 : }
|