gio/gdbusaddress.c: Silence RunDLL errors
[glib.git] / glib / guniprop.c
blob22118186c6e07192b17e1e4c3e8ba82bd8e25a7f
1 /* guniprop.c - Unicode character properties.
3 * Copyright (C) 1999 Tom Tromey
4 * Copyright (C) 2000 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #include "config.h"
24 #include <stdlib.h>
25 #include <stddef.h>
26 #include <string.h>
27 #include <locale.h>
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
42 #define ATTR_TABLE(Page) (((Page) <= G_UNICODE_LAST_PAGE_PART1) \
43 ? attr_table_part1[Page] \
44 : attr_table_part2[(Page) - 0xe00])
46 #define ATTTABLE(Page, Char) \
47 ((ATTR_TABLE(Page) == G_UNICODE_MAX_TABLE_INDEX) ? 0 : (attr_data[ATTR_TABLE(Page)][Char]))
49 #define TTYPE_PART1(Page, Char) \
50 ((type_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
51 ? (type_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
52 : (type_data[type_table_part1[Page]][Char]))
54 #define TTYPE_PART2(Page, Char) \
55 ((type_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
56 ? (type_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
57 : (type_data[type_table_part2[Page]][Char]))
59 #define TYPE(Char) \
60 (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
61 ? TTYPE_PART1 ((Char) >> 8, (Char) & 0xff) \
62 : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
63 ? TTYPE_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
64 : G_UNICODE_UNASSIGNED))
67 #define IS(Type, Class) (((guint)1 << (Type)) & (Class))
68 #define OR(Type, Rest) (((guint)1 << (Type)) | (Rest))
72 #define ISALPHA(Type) IS ((Type), \
73 OR (G_UNICODE_LOWERCASE_LETTER, \
74 OR (G_UNICODE_UPPERCASE_LETTER, \
75 OR (G_UNICODE_TITLECASE_LETTER, \
76 OR (G_UNICODE_MODIFIER_LETTER, \
77 OR (G_UNICODE_OTHER_LETTER, 0))))))
79 #define ISALDIGIT(Type) IS ((Type), \
80 OR (G_UNICODE_DECIMAL_NUMBER, \
81 OR (G_UNICODE_LETTER_NUMBER, \
82 OR (G_UNICODE_OTHER_NUMBER, \
83 OR (G_UNICODE_LOWERCASE_LETTER, \
84 OR (G_UNICODE_UPPERCASE_LETTER, \
85 OR (G_UNICODE_TITLECASE_LETTER, \
86 OR (G_UNICODE_MODIFIER_LETTER, \
87 OR (G_UNICODE_OTHER_LETTER, 0)))))))))
89 #define ISMARK(Type) IS ((Type), \
90 OR (G_UNICODE_NON_SPACING_MARK, \
91 OR (G_UNICODE_SPACING_MARK, \
92 OR (G_UNICODE_ENCLOSING_MARK, 0))))
94 #define ISZEROWIDTHTYPE(Type) IS ((Type), \
95 OR (G_UNICODE_NON_SPACING_MARK, \
96 OR (G_UNICODE_ENCLOSING_MARK, \
97 OR (G_UNICODE_FORMAT, 0))))
99 /**
100 * g_unichar_isalnum:
101 * @c: a Unicode character
103 * Determines whether a character is alphanumeric.
104 * Given some UTF-8 text, obtain a character value
105 * with g_utf8_get_char().
107 * Return value: %TRUE if @c is an alphanumeric character
109 gboolean
110 g_unichar_isalnum (gunichar c)
112 return ISALDIGIT (TYPE (c)) ? TRUE : FALSE;
116 * g_unichar_isalpha:
117 * @c: a Unicode character
119 * Determines whether a character is alphabetic (i.e. a letter).
120 * Given some UTF-8 text, obtain a character value with
121 * g_utf8_get_char().
123 * Return value: %TRUE if @c is an alphabetic character
125 gboolean
126 g_unichar_isalpha (gunichar c)
128 return ISALPHA (TYPE (c)) ? TRUE : FALSE;
133 * g_unichar_iscntrl:
134 * @c: a Unicode character
136 * Determines whether a character is a control character.
137 * Given some UTF-8 text, obtain a character value with
138 * g_utf8_get_char().
140 * Return value: %TRUE if @c is a control character
142 gboolean
143 g_unichar_iscntrl (gunichar c)
145 return TYPE (c) == G_UNICODE_CONTROL;
149 * g_unichar_isdigit:
150 * @c: a Unicode character
152 * Determines whether a character is numeric (i.e. a digit). This
153 * covers ASCII 0-9 and also digits in other languages/scripts. Given
154 * some UTF-8 text, obtain a character value with g_utf8_get_char().
156 * Return value: %TRUE if @c is a digit
158 gboolean
159 g_unichar_isdigit (gunichar c)
161 return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
166 * g_unichar_isgraph:
167 * @c: a Unicode character
169 * Determines whether a character is printable and not a space
170 * (returns %FALSE for control characters, format characters, and
171 * spaces). g_unichar_isprint() is similar, but returns %TRUE for
172 * spaces. Given some UTF-8 text, obtain a character value with
173 * g_utf8_get_char().
175 * Return value: %TRUE if @c is printable unless it's a space
177 gboolean
178 g_unichar_isgraph (gunichar c)
180 return !IS (TYPE(c),
181 OR (G_UNICODE_CONTROL,
182 OR (G_UNICODE_FORMAT,
183 OR (G_UNICODE_UNASSIGNED,
184 OR (G_UNICODE_SURROGATE,
185 OR (G_UNICODE_SPACE_SEPARATOR,
186 0))))));
190 * g_unichar_islower:
191 * @c: a Unicode character
193 * Determines whether a character is a lowercase letter.
194 * Given some UTF-8 text, obtain a character value with
195 * g_utf8_get_char().
197 * Return value: %TRUE if @c is a lowercase letter
199 gboolean
200 g_unichar_islower (gunichar c)
202 return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
207 * g_unichar_isprint:
208 * @c: a Unicode character
210 * Determines whether a character is printable.
211 * Unlike g_unichar_isgraph(), returns %TRUE for spaces.
212 * Given some UTF-8 text, obtain a character value with
213 * g_utf8_get_char().
215 * Return value: %TRUE if @c is printable
217 gboolean
218 g_unichar_isprint (gunichar c)
220 return !IS (TYPE(c),
221 OR (G_UNICODE_CONTROL,
222 OR (G_UNICODE_FORMAT,
223 OR (G_UNICODE_UNASSIGNED,
224 OR (G_UNICODE_SURROGATE,
225 0)))));
229 * g_unichar_ispunct:
230 * @c: a Unicode character
232 * Determines whether a character is punctuation or a symbol.
233 * Given some UTF-8 text, obtain a character value with
234 * g_utf8_get_char().
236 * Return value: %TRUE if @c is a punctuation or symbol character
238 gboolean
239 g_unichar_ispunct (gunichar c)
241 return IS (TYPE(c),
242 OR (G_UNICODE_CONNECT_PUNCTUATION,
243 OR (G_UNICODE_DASH_PUNCTUATION,
244 OR (G_UNICODE_CLOSE_PUNCTUATION,
245 OR (G_UNICODE_FINAL_PUNCTUATION,
246 OR (G_UNICODE_INITIAL_PUNCTUATION,
247 OR (G_UNICODE_OTHER_PUNCTUATION,
248 OR (G_UNICODE_OPEN_PUNCTUATION,
249 OR (G_UNICODE_CURRENCY_SYMBOL,
250 OR (G_UNICODE_MODIFIER_SYMBOL,
251 OR (G_UNICODE_MATH_SYMBOL,
252 OR (G_UNICODE_OTHER_SYMBOL,
253 0)))))))))))) ? TRUE : FALSE;
257 * g_unichar_isspace:
258 * @c: a Unicode character
260 * Determines whether a character is a space, tab, or line separator
261 * (newline, carriage return, etc.). Given some UTF-8 text, obtain a
262 * character value with g_utf8_get_char().
264 * (Note: don't use this to do word breaking; you have to use
265 * Pango or equivalent to get word breaking right, the algorithm
266 * is fairly complex.)
268 * Return value: %TRUE if @c is a space character
270 gboolean
271 g_unichar_isspace (gunichar c)
273 switch (c)
275 /* special-case these since Unicode thinks they are not spaces */
276 case '\t':
277 case '\n':
278 case '\r':
279 case '\f':
280 return TRUE;
281 break;
283 default:
285 return IS (TYPE(c),
286 OR (G_UNICODE_SPACE_SEPARATOR,
287 OR (G_UNICODE_LINE_SEPARATOR,
288 OR (G_UNICODE_PARAGRAPH_SEPARATOR,
289 0)))) ? TRUE : FALSE;
291 break;
296 * g_unichar_ismark:
297 * @c: a Unicode character
299 * Determines whether a character is a mark (non-spacing mark,
300 * combining mark, or enclosing mark in Unicode speak).
301 * Given some UTF-8 text, obtain a character value
302 * with g_utf8_get_char().
304 * Note: in most cases where isalpha characters are allowed,
305 * ismark characters should be allowed to as they are essential
306 * for writing most European languages as well as many non-Latin
307 * scripts.
309 * Return value: %TRUE if @c is a mark character
311 * Since: 2.14
313 gboolean
314 g_unichar_ismark (gunichar c)
316 return ISMARK (TYPE (c));
320 * g_unichar_isupper:
321 * @c: a Unicode character
323 * Determines if a character is uppercase.
325 * Return value: %TRUE if @c is an uppercase character
327 gboolean
328 g_unichar_isupper (gunichar c)
330 return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
334 * g_unichar_istitle:
335 * @c: a Unicode character
337 * Determines if a character is titlecase. Some characters in
338 * Unicode which are composites, such as the DZ digraph
339 * have three case variants instead of just two. The titlecase
340 * form is used at the beginning of a word where only the
341 * first letter is capitalized. The titlecase form of the DZ
342 * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z.
344 * Return value: %TRUE if the character is titlecase
346 gboolean
347 g_unichar_istitle (gunichar c)
349 unsigned int i;
350 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
351 if (title_table[i][0] == c)
352 return TRUE;
353 return FALSE;
357 * g_unichar_isxdigit:
358 * @c: a Unicode character.
360 * Determines if a character is a hexidecimal digit.
362 * Return value: %TRUE if the character is a hexadecimal digit
364 gboolean
365 g_unichar_isxdigit (gunichar c)
367 return ((c >= 'a' && c <= 'f')
368 || (c >= 'A' && c <= 'F')
369 || (TYPE (c) == G_UNICODE_DECIMAL_NUMBER));
373 * g_unichar_isdefined:
374 * @c: a Unicode character
376 * Determines if a given character is assigned in the Unicode
377 * standard.
379 * Return value: %TRUE if the character has an assigned value
381 gboolean
382 g_unichar_isdefined (gunichar c)
384 return !IS (TYPE(c),
385 OR (G_UNICODE_UNASSIGNED,
386 OR (G_UNICODE_SURROGATE,
387 0)));
391 * g_unichar_iszerowidth:
392 * @c: a Unicode character
394 * Determines if a given character typically takes zero width when rendered.
395 * The return value is %TRUE for all non-spacing and enclosing marks
396 * (e.g., combining accents), format characters, zero-width
397 * space, but not U+00AD SOFT HYPHEN.
399 * A typical use of this function is with one of g_unichar_iswide() or
400 * g_unichar_iswide_cjk() to determine the number of cells a string occupies
401 * when displayed on a grid display (terminals). However, note that not all
402 * terminals support zero-width rendering of zero-width marks.
404 * Return value: %TRUE if the character has zero width
406 * Since: 2.14
408 gboolean
409 g_unichar_iszerowidth (gunichar c)
411 if (G_UNLIKELY (c == 0x00AD))
412 return FALSE;
414 if (G_UNLIKELY (ISZEROWIDTHTYPE (TYPE (c))))
415 return TRUE;
417 if (G_UNLIKELY ((c >= 0x1160 && c < 0x1200) ||
418 c == 0x200B))
419 return TRUE;
421 return FALSE;
424 struct Interval
426 gunichar start, end;
429 static int
430 interval_compare (const void *key, const void *elt)
432 gunichar c = GPOINTER_TO_UINT (key);
433 struct Interval *interval = (struct Interval *)elt;
435 if (c < interval->start)
436 return -1;
437 if (c > interval->end)
438 return +1;
440 return 0;
445 * NOTE:
447 * The tables for g_unichar_iswide() and g_unichar_iswide_cjk() are
448 * generated from the Unicode Character Database's file
449 * extracted/DerivedEastAsianWidth.txt using the gen-iswide-table.py
450 * in this way:
452 * ./gen-iswide-table.py < path/to/ucd/extracted/DerivedEastAsianWidth.txt | fmt
454 * Last update for Unicode 6.0.
458 * g_unichar_iswide:
459 * @c: a Unicode character
461 * Determines if a character is typically rendered in a double-width
462 * cell.
464 * Return value: %TRUE if the character is wide
466 gboolean
467 g_unichar_iswide (gunichar c)
469 /* See NOTE earlier for how to update this table. */
470 static const struct Interval wide[] = {
471 {0x1100, 0x115F}, {0x2329, 0x232A}, {0x2E80, 0x2E99}, {0x2E9B, 0x2EF3},
472 {0x2F00, 0x2FD5}, {0x2FF0, 0x2FFB}, {0x3000, 0x303E}, {0x3041, 0x3096},
473 {0x3099, 0x30FF}, {0x3105, 0x312D}, {0x3131, 0x318E}, {0x3190, 0x31BA},
474 {0x31C0, 0x31E3}, {0x31F0, 0x321E}, {0x3220, 0x3247}, {0x3250, 0x32FE},
475 {0x3300, 0x4DBF}, {0x4E00, 0xA48C}, {0xA490, 0xA4C6}, {0xA960, 0xA97C},
476 {0xAC00, 0xD7A3}, {0xF900, 0xFAFF}, {0xFE10, 0xFE19}, {0xFE30, 0xFE52},
477 {0xFE54, 0xFE66}, {0xFE68, 0xFE6B}, {0xFF01, 0xFF60}, {0xFFE0, 0xFFE6},
478 {0x1B000, 0x1B001}, {0x1F200, 0x1F202}, {0x1F210, 0x1F23A}, {0x1F240,
479 0x1F248}, {0x1F250, 0x1F251}, {0x20000, 0x2FFFD}, {0x30000, 0x3FFFD}
482 if (bsearch (GUINT_TO_POINTER (c), wide, G_N_ELEMENTS (wide), sizeof wide[0],
483 interval_compare))
484 return TRUE;
486 return FALSE;
491 * g_unichar_iswide_cjk:
492 * @c: a Unicode character
494 * Determines if a character is typically rendered in a double-width
495 * cell under legacy East Asian locales. If a character is wide according to
496 * g_unichar_iswide(), then it is also reported wide with this function, but
497 * the converse is not necessarily true. See the
498 * <ulink url="http://www.unicode.org/reports/tr11/">Unicode Standard
499 * Annex #11</ulink> for details.
501 * If a character passes the g_unichar_iswide() test then it will also pass
502 * this test, but not the other way around. Note that some characters may
503 * pas both this test and g_unichar_iszerowidth().
505 * Return value: %TRUE if the character is wide in legacy East Asian locales
507 * Since: 2.12
509 gboolean
510 g_unichar_iswide_cjk (gunichar c)
512 /* See NOTE earlier for how to update this table. */
513 static const struct Interval ambiguous[] = {
514 {0x00A1, 0x00A1}, {0x00A4, 0x00A4}, {0x00A7, 0x00A8}, {0x00AA, 0x00AA},
515 {0x00AD, 0x00AE}, {0x00B0, 0x00B4}, {0x00B6, 0x00BA}, {0x00BC, 0x00BF},
516 {0x00C6, 0x00C6}, {0x00D0, 0x00D0}, {0x00D7, 0x00D8}, {0x00DE, 0x00E1},
517 {0x00E6, 0x00E6}, {0x00E8, 0x00EA}, {0x00EC, 0x00ED}, {0x00F0, 0x00F0},
518 {0x00F2, 0x00F3}, {0x00F7, 0x00FA}, {0x00FC, 0x00FC}, {0x00FE, 0x00FE},
519 {0x0101, 0x0101}, {0x0111, 0x0111}, {0x0113, 0x0113}, {0x011B, 0x011B},
520 {0x0126, 0x0127}, {0x012B, 0x012B}, {0x0131, 0x0133}, {0x0138, 0x0138},
521 {0x013F, 0x0142}, {0x0144, 0x0144}, {0x0148, 0x014B}, {0x014D, 0x014D},
522 {0x0152, 0x0153}, {0x0166, 0x0167}, {0x016B, 0x016B}, {0x01CE, 0x01CE},
523 {0x01D0, 0x01D0}, {0x01D2, 0x01D2}, {0x01D4, 0x01D4}, {0x01D6, 0x01D6},
524 {0x01D8, 0x01D8}, {0x01DA, 0x01DA}, {0x01DC, 0x01DC}, {0x0251, 0x0251},
525 {0x0261, 0x0261}, {0x02C4, 0x02C4}, {0x02C7, 0x02C7}, {0x02C9, 0x02CB},
526 {0x02CD, 0x02CD}, {0x02D0, 0x02D0}, {0x02D8, 0x02DB}, {0x02DD, 0x02DD},
527 {0x02DF, 0x02DF}, {0x0300, 0x036F}, {0x0391, 0x03A1}, {0x03A3, 0x03A9},
528 {0x03B1, 0x03C1}, {0x03C3, 0x03C9}, {0x0401, 0x0401}, {0x0410, 0x044F},
529 {0x0451, 0x0451}, {0x2010, 0x2010}, {0x2013, 0x2016}, {0x2018, 0x2019},
530 {0x201C, 0x201D}, {0x2020, 0x2022}, {0x2024, 0x2027}, {0x2030, 0x2030},
531 {0x2032, 0x2033}, {0x2035, 0x2035}, {0x203B, 0x203B}, {0x203E, 0x203E},
532 {0x2074, 0x2074}, {0x207F, 0x207F}, {0x2081, 0x2084}, {0x20AC, 0x20AC},
533 {0x2103, 0x2103}, {0x2105, 0x2105}, {0x2109, 0x2109}, {0x2113, 0x2113},
534 {0x2116, 0x2116}, {0x2121, 0x2122}, {0x2126, 0x2126}, {0x212B, 0x212B},
535 {0x2153, 0x2154}, {0x215B, 0x215E}, {0x2160, 0x216B}, {0x2170, 0x2179},
536 {0x2189, 0x2189}, {0x2190, 0x2199}, {0x21B8, 0x21B9}, {0x21D2, 0x21D2},
537 {0x21D4, 0x21D4}, {0x21E7, 0x21E7}, {0x2200, 0x2200}, {0x2202, 0x2203},
538 {0x2207, 0x2208}, {0x220B, 0x220B}, {0x220F, 0x220F}, {0x2211, 0x2211},
539 {0x2215, 0x2215}, {0x221A, 0x221A}, {0x221D, 0x2220}, {0x2223, 0x2223},
540 {0x2225, 0x2225}, {0x2227, 0x222C}, {0x222E, 0x222E}, {0x2234, 0x2237},
541 {0x223C, 0x223D}, {0x2248, 0x2248}, {0x224C, 0x224C}, {0x2252, 0x2252},
542 {0x2260, 0x2261}, {0x2264, 0x2267}, {0x226A, 0x226B}, {0x226E, 0x226F},
543 {0x2282, 0x2283}, {0x2286, 0x2287}, {0x2295, 0x2295}, {0x2299, 0x2299},
544 {0x22A5, 0x22A5}, {0x22BF, 0x22BF}, {0x2312, 0x2312}, {0x2460, 0x24E9},
545 {0x24EB, 0x254B}, {0x2550, 0x2573}, {0x2580, 0x258F}, {0x2592, 0x2595},
546 {0x25A0, 0x25A1}, {0x25A3, 0x25A9}, {0x25B2, 0x25B3}, {0x25B6, 0x25B7},
547 {0x25BC, 0x25BD}, {0x25C0, 0x25C1}, {0x25C6, 0x25C8}, {0x25CB, 0x25CB},
548 {0x25CE, 0x25D1}, {0x25E2, 0x25E5}, {0x25EF, 0x25EF}, {0x2605, 0x2606},
549 {0x2609, 0x2609}, {0x260E, 0x260F}, {0x2614, 0x2615}, {0x261C, 0x261C},
550 {0x261E, 0x261E}, {0x2640, 0x2640}, {0x2642, 0x2642}, {0x2660, 0x2661},
551 {0x2663, 0x2665}, {0x2667, 0x266A}, {0x266C, 0x266D}, {0x266F, 0x266F},
552 {0x269E, 0x269F}, {0x26BE, 0x26BF}, {0x26C4, 0x26CD}, {0x26CF, 0x26E1},
553 {0x26E3, 0x26E3}, {0x26E8, 0x26FF}, {0x273D, 0x273D}, {0x2757, 0x2757},
554 {0x2776, 0x277F}, {0x2B55, 0x2B59}, {0x3248, 0x324F}, {0xE000, 0xF8FF},
555 {0xFE00, 0xFE0F}, {0xFFFD, 0xFFFD}, {0x1F100, 0x1F10A}, {0x1F110,
556 0x1F12D}, {0x1F130, 0x1F169}, {0x1F170, 0x1F19A}, {0xE0100, 0xE01EF},
557 {0xF0000, 0xFFFFD}, {0x100000, 0x10FFFD}
560 if (g_unichar_iswide (c))
561 return TRUE;
563 if (bsearch (GUINT_TO_POINTER (c), ambiguous, G_N_ELEMENTS (ambiguous), sizeof ambiguous[0],
564 interval_compare))
565 return TRUE;
567 return FALSE;
572 * g_unichar_toupper:
573 * @c: a Unicode character
575 * Converts a character to uppercase.
577 * Return value: the result of converting @c to uppercase.
578 * If @c is not an lowercase or titlecase character,
579 * or has no upper case equivalent @c is returned unchanged.
581 gunichar
582 g_unichar_toupper (gunichar c)
584 int t = TYPE (c);
585 if (t == G_UNICODE_LOWERCASE_LETTER)
587 gunichar val = ATTTABLE (c >> 8, c & 0xff);
588 if (val >= 0x1000000)
590 const gchar *p = special_case_table + val - 0x1000000;
591 val = g_utf8_get_char (p);
593 /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
594 * do not have an uppercase equivalent, in which case val will be
595 * zero.
597 return val ? val : c;
599 else if (t == G_UNICODE_TITLECASE_LETTER)
601 unsigned int i;
602 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
604 if (title_table[i][0] == c)
605 return title_table[i][1] ? title_table[i][1] : c;
608 return c;
612 * g_unichar_tolower:
613 * @c: a Unicode character.
615 * Converts a character to lower case.
617 * Return value: the result of converting @c to lower case.
618 * If @c is not an upperlower or titlecase character,
619 * or has no lowercase equivalent @c is returned unchanged.
621 gunichar
622 g_unichar_tolower (gunichar c)
624 int t = TYPE (c);
625 if (t == G_UNICODE_UPPERCASE_LETTER)
627 gunichar val = ATTTABLE (c >> 8, c & 0xff);
628 if (val >= 0x1000000)
630 const gchar *p = special_case_table + val - 0x1000000;
631 return g_utf8_get_char (p);
633 else
635 /* Not all uppercase letters are guaranteed to have a lowercase
636 * equivalent. If this is the case, val will be zero. */
637 return val ? val : c;
640 else if (t == G_UNICODE_TITLECASE_LETTER)
642 unsigned int i;
643 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
645 if (title_table[i][0] == c)
646 return title_table[i][2];
649 return c;
653 * g_unichar_totitle:
654 * @c: a Unicode character
656 * Converts a character to the titlecase.
658 * Return value: the result of converting @c to titlecase.
659 * If @c is not an uppercase or lowercase character,
660 * @c is returned unchanged.
662 gunichar
663 g_unichar_totitle (gunichar c)
665 unsigned int i;
666 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
668 if (title_table[i][0] == c || title_table[i][1] == c
669 || title_table[i][2] == c)
670 return title_table[i][0];
673 if (TYPE (c) == G_UNICODE_LOWERCASE_LETTER)
674 return g_unichar_toupper (c);
676 return c;
680 * g_unichar_digit_value:
681 * @c: a Unicode character
683 * Determines the numeric value of a character as a decimal
684 * digit.
686 * Return value: If @c is a decimal digit (according to
687 * g_unichar_isdigit()), its numeric value. Otherwise, -1.
690 g_unichar_digit_value (gunichar c)
692 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
693 return ATTTABLE (c >> 8, c & 0xff);
694 return -1;
698 * g_unichar_xdigit_value:
699 * @c: a Unicode character
701 * Determines the numeric value of a character as a hexidecimal
702 * digit.
704 * Return value: If @c is a hex digit (according to
705 * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
708 g_unichar_xdigit_value (gunichar c)
710 if (c >= 'A' && c <= 'F')
711 return c - 'A' + 10;
712 if (c >= 'a' && c <= 'f')
713 return c - 'a' + 10;
714 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
715 return ATTTABLE (c >> 8, c & 0xff);
716 return -1;
720 * g_unichar_type:
721 * @c: a Unicode character
723 * Classifies a Unicode character by type.
725 * Return value: the type of the character.
727 GUnicodeType
728 g_unichar_type (gunichar c)
730 return TYPE (c);
734 * Case mapping functions
737 typedef enum {
738 LOCALE_NORMAL,
739 LOCALE_TURKIC,
740 LOCALE_LITHUANIAN
741 } LocaleType;
743 static LocaleType
744 get_locale_type (void)
746 #ifdef G_OS_WIN32
747 char *tem = g_win32_getlocale ();
748 char locale[2];
750 locale[0] = tem[0];
751 locale[1] = tem[1];
752 g_free (tem);
753 #else
754 const char *locale = setlocale (LC_CTYPE, NULL);
756 if (locale == NULL)
757 return LOCALE_NORMAL;
758 #endif
760 switch (locale[0])
762 case 'a':
763 if (locale[1] == 'z')
764 return LOCALE_TURKIC;
765 break;
766 case 'l':
767 if (locale[1] == 't')
768 return LOCALE_LITHUANIAN;
769 break;
770 case 't':
771 if (locale[1] == 'r')
772 return LOCALE_TURKIC;
773 break;
776 return LOCALE_NORMAL;
779 static gint
780 output_marks (const char **p_inout,
781 char *out_buffer,
782 gboolean remove_dot)
784 const char *p = *p_inout;
785 gint len = 0;
787 while (*p)
789 gunichar c = g_utf8_get_char (p);
791 if (ISMARK (TYPE (c)))
793 if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
794 len += g_unichar_to_utf8 (c, out_buffer ? out_buffer + len : NULL);
795 p = g_utf8_next_char (p);
797 else
798 break;
801 *p_inout = p;
802 return len;
805 static gint
806 output_special_case (gchar *out_buffer,
807 int offset,
808 int type,
809 int which)
811 const gchar *p = special_case_table + offset;
812 gint len;
814 if (type != G_UNICODE_TITLECASE_LETTER)
815 p = g_utf8_next_char (p);
817 if (which == 1)
818 p += strlen (p) + 1;
820 len = strlen (p);
821 if (out_buffer)
822 memcpy (out_buffer, p, len);
824 return len;
827 static gsize
828 real_toupper (const gchar *str,
829 gssize max_len,
830 gchar *out_buffer,
831 LocaleType locale_type)
833 const gchar *p = str;
834 const char *last = NULL;
835 gsize len = 0;
836 gboolean last_was_i = FALSE;
838 while ((max_len < 0 || p < str + max_len) && *p)
840 gunichar c = g_utf8_get_char (p);
841 int t = TYPE (c);
842 gunichar val;
844 last = p;
845 p = g_utf8_next_char (p);
847 if (locale_type == LOCALE_LITHUANIAN)
849 if (c == 'i')
850 last_was_i = TRUE;
851 else
853 if (last_was_i)
855 /* Nasty, need to remove any dot above. Though
856 * I think only E WITH DOT ABOVE occurs in practice
857 * which could simplify this considerably.
859 gsize decomp_len, i;
860 gunichar decomp[G_UNICHAR_MAX_DECOMPOSITION_LENGTH];
862 decomp_len = g_unichar_fully_decompose (c, FALSE, decomp, G_N_ELEMENTS (decomp));
863 for (i=0; i < decomp_len; i++)
865 if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
866 len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
869 len += output_marks (&p, out_buffer ? out_buffer + len : NULL, TRUE);
871 continue;
874 if (!ISMARK (t))
875 last_was_i = FALSE;
879 if (locale_type == LOCALE_TURKIC && c == 'i')
881 /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
882 len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL);
884 else if (c == 0x0345) /* COMBINING GREEK YPOGEGRAMMENI */
886 /* Nasty, need to move it after other combining marks .. this would go away if
887 * we normalized first.
889 len += output_marks (&p, out_buffer ? out_buffer + len : NULL, FALSE);
891 /* And output as GREEK CAPITAL LETTER IOTA */
892 len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);
894 else if (IS (t,
895 OR (G_UNICODE_LOWERCASE_LETTER,
896 OR (G_UNICODE_TITLECASE_LETTER,
897 0))))
899 val = ATTTABLE (c >> 8, c & 0xff);
901 if (val >= 0x1000000)
903 len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t,
904 t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
906 else
908 if (t == G_UNICODE_TITLECASE_LETTER)
910 unsigned int i;
911 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
913 if (title_table[i][0] == c)
915 val = title_table[i][1];
916 break;
921 /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
922 * do not have an uppercase equivalent, in which case val will be
923 * zero. */
924 len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
927 else
929 gsize char_len = g_utf8_skip[*(guchar *)last];
931 if (out_buffer)
932 memcpy (out_buffer + len, last, char_len);
934 len += char_len;
939 return len;
943 * g_utf8_strup:
944 * @str: a UTF-8 encoded string
945 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
947 * Converts all Unicode characters in the string that have a case
948 * to uppercase. The exact manner that this is done depends
949 * on the current locale, and may result in the number of
950 * characters in the string increasing. (For instance, the
951 * German ess-zet will be changed to SS.)
953 * Return value: a newly allocated string, with all characters
954 * converted to uppercase.
956 gchar *
957 g_utf8_strup (const gchar *str,
958 gssize len)
960 gsize result_len;
961 LocaleType locale_type;
962 gchar *result;
964 g_return_val_if_fail (str != NULL, NULL);
966 locale_type = get_locale_type ();
969 * We use a two pass approach to keep memory management simple
971 result_len = real_toupper (str, len, NULL, locale_type);
972 result = g_malloc (result_len + 1);
973 real_toupper (str, len, result, locale_type);
974 result[result_len] = '\0';
976 return result;
979 /* traverses the string checking for characters with combining class == 230
980 * until a base character is found */
981 static gboolean
982 has_more_above (const gchar *str)
984 const gchar *p = str;
985 gint combining_class;
987 while (*p)
989 combining_class = g_unichar_combining_class (g_utf8_get_char (p));
990 if (combining_class == 230)
991 return TRUE;
992 else if (combining_class == 0)
993 break;
995 p = g_utf8_next_char (p);
998 return FALSE;
1001 static gsize
1002 real_tolower (const gchar *str,
1003 gssize max_len,
1004 gchar *out_buffer,
1005 LocaleType locale_type)
1007 const gchar *p = str;
1008 const char *last = NULL;
1009 gsize len = 0;
1011 while ((max_len < 0 || p < str + max_len) && *p)
1013 gunichar c = g_utf8_get_char (p);
1014 int t = TYPE (c);
1015 gunichar val;
1017 last = p;
1018 p = g_utf8_next_char (p);
1020 if (locale_type == LOCALE_TURKIC && c == 'I')
1022 if (g_utf8_get_char (p) == 0x0307)
1024 /* I + COMBINING DOT ABOVE => i (U+0069) */
1025 len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL);
1026 p = g_utf8_next_char (p);
1028 else
1030 /* I => LATIN SMALL LETTER DOTLESS I */
1031 len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL);
1034 /* Introduce an explicit dot above when lowercasing capital I's and J's
1035 * whenever there are more accents above. [SpecialCasing.txt] */
1036 else if (locale_type == LOCALE_LITHUANIAN &&
1037 (c == 0x00cc || c == 0x00cd || c == 0x0128))
1039 len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL);
1040 len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL);
1042 switch (c)
1044 case 0x00cc:
1045 len += g_unichar_to_utf8 (0x0300, out_buffer ? out_buffer + len : NULL);
1046 break;
1047 case 0x00cd:
1048 len += g_unichar_to_utf8 (0x0301, out_buffer ? out_buffer + len : NULL);
1049 break;
1050 case 0x0128:
1051 len += g_unichar_to_utf8 (0x0303, out_buffer ? out_buffer + len : NULL);
1052 break;
1055 else if (locale_type == LOCALE_LITHUANIAN &&
1056 (c == 'I' || c == 'J' || c == 0x012e) &&
1057 has_more_above (p))
1059 len += g_unichar_to_utf8 (g_unichar_tolower (c), out_buffer ? out_buffer + len : NULL);
1060 len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL);
1062 else if (c == 0x03A3) /* GREEK CAPITAL LETTER SIGMA */
1064 if ((max_len < 0 || p < str + max_len) && *p)
1066 gunichar next_c = g_utf8_get_char (p);
1067 int next_type = TYPE(next_c);
1069 /* SIGMA mapps differently depending on whether it is
1070 * final or not. The following simplified test would
1071 * fail in the case of combining marks following the
1072 * sigma, but I don't think that occurs in real text.
1073 * The test here matches that in ICU.
1075 if (ISALPHA (next_type)) /* Lu,Ll,Lt,Lm,Lo */
1076 val = 0x3c3; /* GREEK SMALL SIGMA */
1077 else
1078 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
1080 else
1081 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
1083 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
1085 else if (IS (t,
1086 OR (G_UNICODE_UPPERCASE_LETTER,
1087 OR (G_UNICODE_TITLECASE_LETTER,
1088 0))))
1090 val = ATTTABLE (c >> 8, c & 0xff);
1092 if (val >= 0x1000000)
1094 len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t, 0);
1096 else
1098 if (t == G_UNICODE_TITLECASE_LETTER)
1100 unsigned int i;
1101 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
1103 if (title_table[i][0] == c)
1105 val = title_table[i][2];
1106 break;
1111 /* Not all uppercase letters are guaranteed to have a lowercase
1112 * equivalent. If this is the case, val will be zero. */
1113 len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
1116 else
1118 gsize char_len = g_utf8_skip[*(guchar *)last];
1120 if (out_buffer)
1121 memcpy (out_buffer + len, last, char_len);
1123 len += char_len;
1128 return len;
1132 * g_utf8_strdown:
1133 * @str: a UTF-8 encoded string
1134 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1136 * Converts all Unicode characters in the string that have a case
1137 * to lowercase. The exact manner that this is done depends
1138 * on the current locale, and may result in the number of
1139 * characters in the string changing.
1141 * Return value: a newly allocated string, with all characters
1142 * converted to lowercase.
1144 gchar *
1145 g_utf8_strdown (const gchar *str,
1146 gssize len)
1148 gsize result_len;
1149 LocaleType locale_type;
1150 gchar *result;
1152 g_return_val_if_fail (str != NULL, NULL);
1154 locale_type = get_locale_type ();
1157 * We use a two pass approach to keep memory management simple
1159 result_len = real_tolower (str, len, NULL, locale_type);
1160 result = g_malloc (result_len + 1);
1161 real_tolower (str, len, result, locale_type);
1162 result[result_len] = '\0';
1164 return result;
1168 * g_utf8_casefold:
1169 * @str: a UTF-8 encoded string
1170 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1172 * Converts a string into a form that is independent of case. The
1173 * result will not correspond to any particular case, but can be
1174 * compared for equality or ordered with the results of calling
1175 * g_utf8_casefold() on other strings.
1177 * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
1178 * only an approximation to the correct linguistic case insensitive
1179 * ordering, though it is a fairly good one. Getting this exactly
1180 * right would require a more sophisticated collation function that
1181 * takes case sensitivity into account. GLib does not currently
1182 * provide such a function.
1184 * Return value: a newly allocated string, that is a
1185 * case independent form of @str.
1187 gchar *
1188 g_utf8_casefold (const gchar *str,
1189 gssize len)
1191 GString *result;
1192 const char *p;
1194 g_return_val_if_fail (str != NULL, NULL);
1196 result = g_string_new (NULL);
1197 p = str;
1198 while ((len < 0 || p < str + len) && *p)
1200 gunichar ch = g_utf8_get_char (p);
1202 int start = 0;
1203 int end = G_N_ELEMENTS (casefold_table);
1205 if (ch >= casefold_table[start].ch &&
1206 ch <= casefold_table[end - 1].ch)
1208 while (TRUE)
1210 int half = (start + end) / 2;
1211 if (ch == casefold_table[half].ch)
1213 g_string_append (result, casefold_table[half].data);
1214 goto next;
1216 else if (half == start)
1217 break;
1218 else if (ch > casefold_table[half].ch)
1219 start = half;
1220 else
1221 end = half;
1225 g_string_append_unichar (result, g_unichar_tolower (ch));
1227 next:
1228 p = g_utf8_next_char (p);
1231 return g_string_free (result, FALSE);
1235 * g_unichar_get_mirror_char:
1236 * @ch: a Unicode character
1237 * @mirrored_ch: location to store the mirrored character
1239 * In Unicode, some characters are <firstterm>mirrored</firstterm>. This
1240 * means that their images are mirrored horizontally in text that is laid
1241 * out from right to left. For instance, "(" would become its mirror image,
1242 * ")", in right-to-left text.
1244 * If @ch has the Unicode mirrored property and there is another unicode
1245 * character that typically has a glyph that is the mirror image of @ch's
1246 * glyph and @mirrored_ch is set, it puts that character in the address
1247 * pointed to by @mirrored_ch. Otherwise the original character is put.
1249 * Return value: %TRUE if @ch has a mirrored character, %FALSE otherwise
1251 * Since: 2.4
1253 gboolean
1254 g_unichar_get_mirror_char (gunichar ch,
1255 gunichar *mirrored_ch)
1257 gboolean found;
1258 gunichar mirrored;
1260 mirrored = GLIB_GET_MIRRORING(ch);
1262 found = ch != mirrored;
1263 if (mirrored_ch)
1264 *mirrored_ch = mirrored;
1266 return found;
1270 #define G_SCRIPT_TABLE_MIDPOINT (G_N_ELEMENTS (g_script_table) / 2)
1272 static inline GUnicodeScript
1273 g_unichar_get_script_bsearch (gunichar ch)
1275 int lower = 0;
1276 int upper = G_N_ELEMENTS (g_script_table) - 1;
1277 static int saved_mid = G_SCRIPT_TABLE_MIDPOINT;
1278 int mid = saved_mid;
1283 if (ch < g_script_table[mid].start)
1284 upper = mid - 1;
1285 else if (ch >= g_script_table[mid].start + g_script_table[mid].chars)
1286 lower = mid + 1;
1287 else
1288 return g_script_table[saved_mid = mid].script;
1290 mid = (lower + upper) / 2;
1292 while (lower <= upper);
1294 return G_UNICODE_SCRIPT_UNKNOWN;
1298 * g_unichar_get_script:
1299 * @ch: a Unicode character
1301 * Looks up the #GUnicodeScript for a particular character (as defined
1302 * by Unicode Standard Annex \#24). No check is made for @ch being a
1303 * valid Unicode character; if you pass in invalid character, the
1304 * result is undefined.
1306 * This function is equivalent to pango_script_for_unichar() and the
1307 * two are interchangeable.
1309 * Return value: the #GUnicodeScript for the character.
1311 * Since: 2.14
1313 GUnicodeScript
1314 g_unichar_get_script (gunichar ch)
1316 if (ch < G_EASY_SCRIPTS_RANGE)
1317 return g_script_easy_table[ch];
1318 else
1319 return g_unichar_get_script_bsearch (ch);
1323 /* http://unicode.org/iso15924/ */
1324 static const guint32 iso15924_tags[] =
1326 #define PACK(a,b,c,d) ((guint32)((((guint8)(a))<<24)|(((guint8)(b))<<16)|(((guint8)(c))<<8)|((guint8)(d))))
1328 PACK ('Z','y','y','y'), /* G_UNICODE_SCRIPT_COMMON */
1329 PACK ('Z','i','n','h'), /* G_UNICODE_SCRIPT_INHERITED */
1330 PACK ('A','r','a','b'), /* G_UNICODE_SCRIPT_ARABIC */
1331 PACK ('A','r','m','n'), /* G_UNICODE_SCRIPT_ARMENIAN */
1332 PACK ('B','e','n','g'), /* G_UNICODE_SCRIPT_BENGALI */
1333 PACK ('B','o','p','o'), /* G_UNICODE_SCRIPT_BOPOMOFO */
1334 PACK ('C','h','e','r'), /* G_UNICODE_SCRIPT_CHEROKEE */
1335 PACK ('C','o','p','t'), /* G_UNICODE_SCRIPT_COPTIC */
1336 PACK ('C','y','r','l'), /* G_UNICODE_SCRIPT_CYRILLIC */
1337 PACK ('D','s','r','t'), /* G_UNICODE_SCRIPT_DESERET */
1338 PACK ('D','e','v','a'), /* G_UNICODE_SCRIPT_DEVANAGARI */
1339 PACK ('E','t','h','i'), /* G_UNICODE_SCRIPT_ETHIOPIC */
1340 PACK ('G','e','o','r'), /* G_UNICODE_SCRIPT_GEORGIAN */
1341 PACK ('G','o','t','h'), /* G_UNICODE_SCRIPT_GOTHIC */
1342 PACK ('G','r','e','k'), /* G_UNICODE_SCRIPT_GREEK */
1343 PACK ('G','u','j','r'), /* G_UNICODE_SCRIPT_GUJARATI */
1344 PACK ('G','u','r','u'), /* G_UNICODE_SCRIPT_GURMUKHI */
1345 PACK ('H','a','n','i'), /* G_UNICODE_SCRIPT_HAN */
1346 PACK ('H','a','n','g'), /* G_UNICODE_SCRIPT_HANGUL */
1347 PACK ('H','e','b','r'), /* G_UNICODE_SCRIPT_HEBREW */
1348 PACK ('H','i','r','a'), /* G_UNICODE_SCRIPT_HIRAGANA */
1349 PACK ('K','n','d','a'), /* G_UNICODE_SCRIPT_KANNADA */
1350 PACK ('K','a','n','a'), /* G_UNICODE_SCRIPT_KATAKANA */
1351 PACK ('K','h','m','r'), /* G_UNICODE_SCRIPT_KHMER */
1352 PACK ('L','a','o','o'), /* G_UNICODE_SCRIPT_LAO */
1353 PACK ('L','a','t','n'), /* G_UNICODE_SCRIPT_LATIN */
1354 PACK ('M','l','y','m'), /* G_UNICODE_SCRIPT_MALAYALAM */
1355 PACK ('M','o','n','g'), /* G_UNICODE_SCRIPT_MONGOLIAN */
1356 PACK ('M','y','m','r'), /* G_UNICODE_SCRIPT_MYANMAR */
1357 PACK ('O','g','a','m'), /* G_UNICODE_SCRIPT_OGHAM */
1358 PACK ('I','t','a','l'), /* G_UNICODE_SCRIPT_OLD_ITALIC */
1359 PACK ('O','r','y','a'), /* G_UNICODE_SCRIPT_ORIYA */
1360 PACK ('R','u','n','r'), /* G_UNICODE_SCRIPT_RUNIC */
1361 PACK ('S','i','n','h'), /* G_UNICODE_SCRIPT_SINHALA */
1362 PACK ('S','y','r','c'), /* G_UNICODE_SCRIPT_SYRIAC */
1363 PACK ('T','a','m','l'), /* G_UNICODE_SCRIPT_TAMIL */
1364 PACK ('T','e','l','u'), /* G_UNICODE_SCRIPT_TELUGU */
1365 PACK ('T','h','a','a'), /* G_UNICODE_SCRIPT_THAANA */
1366 PACK ('T','h','a','i'), /* G_UNICODE_SCRIPT_THAI */
1367 PACK ('T','i','b','t'), /* G_UNICODE_SCRIPT_TIBETAN */
1368 PACK ('C','a','n','s'), /* G_UNICODE_SCRIPT_CANADIAN_ABORIGINAL */
1369 PACK ('Y','i','i','i'), /* G_UNICODE_SCRIPT_YI */
1370 PACK ('T','g','l','g'), /* G_UNICODE_SCRIPT_TAGALOG */
1371 PACK ('H','a','n','o'), /* G_UNICODE_SCRIPT_HANUNOO */
1372 PACK ('B','u','h','d'), /* G_UNICODE_SCRIPT_BUHID */
1373 PACK ('T','a','g','b'), /* G_UNICODE_SCRIPT_TAGBANWA */
1375 /* Unicode-4.0 additions */
1376 PACK ('B','r','a','i'), /* G_UNICODE_SCRIPT_BRAILLE */
1377 PACK ('C','p','r','t'), /* G_UNICODE_SCRIPT_CYPRIOT */
1378 PACK ('L','i','m','b'), /* G_UNICODE_SCRIPT_LIMBU */
1379 PACK ('O','s','m','a'), /* G_UNICODE_SCRIPT_OSMANYA */
1380 PACK ('S','h','a','w'), /* G_UNICODE_SCRIPT_SHAVIAN */
1381 PACK ('L','i','n','b'), /* G_UNICODE_SCRIPT_LINEAR_B */
1382 PACK ('T','a','l','e'), /* G_UNICODE_SCRIPT_TAI_LE */
1383 PACK ('U','g','a','r'), /* G_UNICODE_SCRIPT_UGARITIC */
1385 /* Unicode-4.1 additions */
1386 PACK ('T','a','l','u'), /* G_UNICODE_SCRIPT_NEW_TAI_LUE */
1387 PACK ('B','u','g','i'), /* G_UNICODE_SCRIPT_BUGINESE */
1388 PACK ('G','l','a','g'), /* G_UNICODE_SCRIPT_GLAGOLITIC */
1389 PACK ('T','f','n','g'), /* G_UNICODE_SCRIPT_TIFINAGH */
1390 PACK ('S','y','l','o'), /* G_UNICODE_SCRIPT_SYLOTI_NAGRI */
1391 PACK ('X','p','e','o'), /* G_UNICODE_SCRIPT_OLD_PERSIAN */
1392 PACK ('K','h','a','r'), /* G_UNICODE_SCRIPT_KHAROSHTHI */
1394 /* Unicode-5.0 additions */
1395 PACK ('Z','z','z','z'), /* G_UNICODE_SCRIPT_UNKNOWN */
1396 PACK ('B','a','l','i'), /* G_UNICODE_SCRIPT_BALINESE */
1397 PACK ('X','s','u','x'), /* G_UNICODE_SCRIPT_CUNEIFORM */
1398 PACK ('P','h','n','x'), /* G_UNICODE_SCRIPT_PHOENICIAN */
1399 PACK ('P','h','a','g'), /* G_UNICODE_SCRIPT_PHAGS_PA */
1400 PACK ('N','k','o','o'), /* G_UNICODE_SCRIPT_NKO */
1402 /* Unicode-5.1 additions */
1403 PACK ('K','a','l','i'), /* G_UNICODE_SCRIPT_KAYAH_LI */
1404 PACK ('L','e','p','c'), /* G_UNICODE_SCRIPT_LEPCHA */
1405 PACK ('R','j','n','g'), /* G_UNICODE_SCRIPT_REJANG */
1406 PACK ('S','u','n','d'), /* G_UNICODE_SCRIPT_SUNDANESE */
1407 PACK ('S','a','u','r'), /* G_UNICODE_SCRIPT_SAURASHTRA */
1408 PACK ('C','h','a','m'), /* G_UNICODE_SCRIPT_CHAM */
1409 PACK ('O','l','c','k'), /* G_UNICODE_SCRIPT_OL_CHIKI */
1410 PACK ('V','a','i','i'), /* G_UNICODE_SCRIPT_VAI */
1411 PACK ('C','a','r','i'), /* G_UNICODE_SCRIPT_CARIAN */
1412 PACK ('L','y','c','i'), /* G_UNICODE_SCRIPT_LYCIAN */
1413 PACK ('L','y','d','i'), /* G_UNICODE_SCRIPT_LYDIAN */
1415 /* Unicode-5.2 additions */
1416 PACK ('A','v','s','t'), /* G_UNICODE_SCRIPT_AVESTAN */
1417 PACK ('B','a','m','u'), /* G_UNICODE_SCRIPT_BAMUM */
1418 PACK ('E','g','y','p'), /* G_UNICODE_SCRIPT_EGYPTIAN_HIEROGLYPHS */
1419 PACK ('A','r','m','i'), /* G_UNICODE_SCRIPT_IMPERIAL_ARAMAIC */
1420 PACK ('P','h','l','i'), /* G_UNICODE_SCRIPT_INSCRIPTIONAL_PAHLAVI */
1421 PACK ('P','r','t','i'), /* G_UNICODE_SCRIPT_INSCRIPTIONAL_PARTHIAN */
1422 PACK ('J','a','v','a'), /* G_UNICODE_SCRIPT_JAVANESE */
1423 PACK ('K','t','h','i'), /* G_UNICODE_SCRIPT_KAITHI */
1424 PACK ('L','i','s','u'), /* G_UNICODE_SCRIPT_LISU */
1425 PACK ('M','t','e','i'), /* G_UNICODE_SCRIPT_MEETEI_MAYEK */
1426 PACK ('S','a','r','b'), /* G_UNICODE_SCRIPT_OLD_SOUTH_ARABIAN */
1427 PACK ('O','r','k','h'), /* G_UNICODE_SCRIPT_OLD_TURKIC */
1428 PACK ('S','a','m','r'), /* G_UNICODE_SCRIPT_SAMARITAN */
1429 PACK ('L','a','n','a'), /* G_UNICODE_SCRIPT_TAI_THAM */
1430 PACK ('T','a','v','t'), /* G_UNICODE_SCRIPT_TAI_VIET */
1432 /* Unicode-6.0 additions */
1433 PACK ('B','a','t','k'), /* G_UNICODE_SCRIPT_BATAK */
1434 PACK ('B','r','a','h'), /* G_UNICODE_SCRIPT_BRAHMI */
1435 PACK ('M','a','n','d'), /* G_UNICODE_SCRIPT_MANDAIC */
1437 /* Unicode-6.1 additions */
1438 PACK ('C','a','k','m'), /* G_UNICODE_SCRIPT_CHAKMA */
1439 PACK ('M','e','r','c'), /* G_UNICODE_SCRIPT_MEROITIC_CURSIVE */
1440 PACK ('M','e','r','o'), /* G_UNICODE_SCRIPT_MEROITIC_HIEROGLYPHS */
1441 PACK ('P','l','r','d'), /* G_UNICODE_SCRIPT_MIAO */
1442 PACK ('S','h','r','d'), /* G_UNICODE_SCRIPT_SHARADA */
1443 PACK ('S','o','r','a'), /* G_UNICODE_SCRIPT_SORA_SOMPENG */
1444 PACK ('T','a','k','r'), /* G_UNICODE_SCRIPT_TAKRI */
1446 #undef PACK
1450 * g_unicode_script_to_iso15924:
1451 * @script: a Unicode script
1453 * Looks up the ISO 15924 code for @script. ISO 15924 assigns four-letter
1454 * codes to scripts. For example, the code for Arabic is 'Arab'. The
1455 * four letter codes are encoded as a @guint32 by this function in a
1456 * big-endian fashion. That is, the code returned for Arabic is
1457 * 0x41726162 (0x41 is ASCII code for 'A', 0x72 is ASCII code for 'r', etc).
1459 * See <ulink url="http://unicode.org/iso15924/codelists.html">Codes for the
1460 * representation of names of scripts</ulink> for details.
1462 * Return value: the ISO 15924 code for @script, encoded as an integer,
1463 * of zero if @script is %G_UNICODE_SCRIPT_INVALID_CODE or
1464 * ISO 15924 code 'Zzzz' (script code for UNKNOWN) if @script is not understood.
1466 * Since: 2.30
1468 guint32
1469 g_unicode_script_to_iso15924 (GUnicodeScript script)
1471 if (G_UNLIKELY (script == G_UNICODE_SCRIPT_INVALID_CODE))
1472 return 0;
1474 if (G_UNLIKELY (script < 0 || script >= (int) G_N_ELEMENTS (iso15924_tags)))
1475 return 0x5A7A7A7A;
1477 return iso15924_tags[script];
1481 * g_unicode_script_from_iso15924:
1482 * @iso15924: a Unicode script
1484 * Looks up the Unicode script for @iso15924. ISO 15924 assigns four-letter
1485 * codes to scripts. For example, the code for Arabic is 'Arab'.
1486 * This function accepts four letter codes encoded as a @guint32 in a
1487 * big-endian fashion. That is, the code expected for Arabic is
1488 * 0x41726162 (0x41 is ASCII code for 'A', 0x72 is ASCII code for 'r', etc).
1490 * See <ulink url="http://unicode.org/iso15924/codelists.html">Codes for the
1491 * representation of names of scripts</ulink> for details.
1493 * Return value: the Unicode script for @iso15924, or
1494 * of %G_UNICODE_SCRIPT_INVALID_CODE if @iso15924 is zero and
1495 * %G_UNICODE_SCRIPT_UNKNOWN if @iso15924 is unknown.
1497 * Since: 2.30
1499 GUnicodeScript
1500 g_unicode_script_from_iso15924 (guint32 iso15924)
1502 unsigned int i;
1504 if (!iso15924)
1505 return G_UNICODE_SCRIPT_INVALID_CODE;
1507 for (i = 0; i < G_N_ELEMENTS (iso15924_tags); i++)
1508 if (iso15924_tags[i] == iso15924)
1509 return (GUnicodeScript) i;
1511 return G_UNICODE_SCRIPT_UNKNOWN;