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.
31 #include "gtestutils.h"
34 #include "gunichartables.h"
35 #include "gmirroringtable.h"
36 #include "gscripttable.h"
37 #include "gunicodeprivate.h"
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]))
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_COMBINING_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))))
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
110 g_unichar_isalnum (gunichar c)
112 return ISALDIGIT (TYPE (c)) ? TRUE : FALSE;
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
123 * Return value: %TRUE if @c is an alphabetic character
126 g_unichar_isalpha (gunichar c)
128 return ISALPHA (TYPE (c)) ? TRUE : FALSE;
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
140 * Return value: %TRUE if @c is a control character
143 g_unichar_iscntrl (gunichar c)
145 return TYPE (c) == G_UNICODE_CONTROL;
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
159 g_unichar_isdigit (gunichar c)
161 return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
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
175 * Return value: %TRUE if @c is printable unless it's a space
178 g_unichar_isgraph (gunichar 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,
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
197 * Return value: %TRUE if @c is a lowercase letter
200 g_unichar_islower (gunichar c)
202 return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
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
215 * Return value: %TRUE if @c is printable
218 g_unichar_isprint (gunichar c)
221 OR (G_UNICODE_CONTROL,
222 OR (G_UNICODE_FORMAT,
223 OR (G_UNICODE_UNASSIGNED,
224 OR (G_UNICODE_SURROGATE,
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
236 * Return value: %TRUE if @c is a punctuation or symbol character
239 g_unichar_ispunct (gunichar 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;
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
271 g_unichar_isspace (gunichar c)
275 /* special-case these since Unicode thinks they are not spaces */
286 OR (G_UNICODE_SPACE_SEPARATOR,
287 OR (G_UNICODE_LINE_SEPARATOR,
288 OR (G_UNICODE_PARAGRAPH_SEPARATOR,
289 0)))) ? TRUE : FALSE;
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
309 * Return value: %TRUE if @c is a mark character
314 g_unichar_ismark (gunichar c)
316 return ISMARK (TYPE (c));
321 * @c: a Unicode character
323 * Determines if a character is uppercase.
325 * Return value: %TRUE if @c is an uppercase character
328 g_unichar_isupper (gunichar c)
330 return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
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
347 g_unichar_istitle (gunichar c)
350 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
351 if (title_table[i][0] == c)
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
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
379 * Return value: %TRUE if the character has an assigned value
382 g_unichar_isdefined (gunichar c)
385 OR (G_UNICODE_UNASSIGNED,
386 OR (G_UNICODE_SURROGATE,
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
409 g_unichar_iszerowidth (gunichar c)
411 if (G_UNLIKELY (c == 0x00AD))
414 if (G_UNLIKELY (ISZEROWIDTHTYPE (TYPE (c))))
417 if (G_UNLIKELY ((c >= 0x1160 && c < 0x1200) ||
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)
437 if (c > interval->end)
445 * @c: a Unicode character
447 * Determines if a character is typically rendered in a double-width
450 * Return value: %TRUE if the character is wide
453 g_unichar_iswide (gunichar c)
455 /* sorted list of intervals of East_Asian_Width = W and F characters
456 * from Unicode 5.1.0. produced by mungling output of:
457 * grep ';[FW]\>' EastAsianWidth.txt */
458 static const struct Interval wide[] = {
459 {0x1100, 0x1159}, {0x115F, 0x115F}, {0x2329, 0x232A}, {0x2E80, 0x2E99},
460 {0x2E9B, 0x2EF3}, {0x2F00, 0x2FD5}, {0x2FF0, 0x2FFB}, {0x3000, 0x303E},
461 {0x3041, 0x3096}, {0x3099, 0x30FF}, {0x3105, 0x312D}, {0x3131, 0x318E},
462 {0x3190, 0x31B7}, {0x31C0, 0x31E3}, {0x31F0, 0x321E}, {0x3220, 0x3243},
463 {0x3250, 0x32FE}, {0x3300, 0x4DB5}, {0x4E00, 0x9FC3}, {0xA000, 0xA48C},
464 {0xA490, 0xA4C6}, {0xAC00, 0xD7A3}, {0xF900, 0xFA2D}, {0xFA30, 0xFA6A},
465 {0xFA70, 0xFAD9}, {0xFE10, 0xFE19}, {0xFE30, 0xFE52}, {0xFE54, 0xFE66},
466 {0xFE68, 0xFE6B}, {0xFF01, 0xFF60}, {0xFFE0, 0xFFE6}, {0x20000, 0x2FFFD},
470 if (bsearch (GUINT_TO_POINTER (c), wide, G_N_ELEMENTS (wide), sizeof wide[0],
479 * g_unichar_iswide_cjk:
480 * @c: a Unicode character
482 * Determines if a character is typically rendered in a double-width
483 * cell under legacy East Asian locales. If a character is wide according to
484 * g_unichar_iswide(), then it is also reported wide with this function, but
485 * the converse is not necessarily true. See the
486 * <ulink url="http://www.unicode.org/reports/tr11/">Unicode Standard
487 * Annex #11</ulink> for details.
489 * If a character passes the g_unichar_iswide() test then it will also pass
490 * this test, but not the other way around. Note that some characters may
491 * pas both this test and g_unichar_iszerowidth().
493 * Return value: %TRUE if the character is wide in legacy East Asian locales
498 g_unichar_iswide_cjk (gunichar c)
500 /* sorted list of intervals of East_Asian_Width = A and F characters
501 * from Unicode 5.1.0. produced by mungling output of:
502 * grep ';[A]\>' EastAsianWidth.txt */
503 static const struct Interval ambiguous[] = {
504 {0x00A1, 0x00A1}, {0x00A4, 0x00A4}, {0x00A7, 0x00A8}, {0x00AA, 0x00AA},
505 {0x00AD, 0x00AE}, {0x00B0, 0x00B4}, {0x00B6, 0x00BA}, {0x00BC, 0x00BF},
506 {0x00C6, 0x00C6}, {0x00D0, 0x00D0}, {0x00D7, 0x00D8}, {0x00DE, 0x00E1},
507 {0x00E6, 0x00E6}, {0x00E8, 0x00EA}, {0x00EC, 0x00ED}, {0x00F0, 0x00F0},
508 {0x00F2, 0x00F3}, {0x00F7, 0x00FA}, {0x00FC, 0x00FC}, {0x00FE, 0x00FE},
509 {0x0101, 0x0101}, {0x0111, 0x0111}, {0x0113, 0x0113}, {0x011B, 0x011B},
510 {0x0126, 0x0127}, {0x012B, 0x012B}, {0x0131, 0x0133}, {0x0138, 0x0138},
511 {0x013F, 0x0142}, {0x0144, 0x0144}, {0x0148, 0x014B}, {0x014D, 0x014D},
512 {0x0152, 0x0153}, {0x0166, 0x0167}, {0x016B, 0x016B}, {0x01CE, 0x01CE},
513 {0x01D0, 0x01D0}, {0x01D2, 0x01D2}, {0x01D4, 0x01D4}, {0x01D6, 0x01D6},
514 {0x01D8, 0x01D8}, {0x01DA, 0x01DA}, {0x01DC, 0x01DC}, {0x0251, 0x0251},
515 {0x0261, 0x0261}, {0x02C4, 0x02C4}, {0x02C7, 0x02C7}, {0x02C9, 0x02CB},
516 {0x02CD, 0x02CD}, {0x02D0, 0x02D0}, {0x02D8, 0x02DB}, {0x02DD, 0x02DD},
517 {0x02DF, 0x02DF}, {0x0300, 0x036F}, {0x0391, 0x03A1}, {0x03A3, 0x03A9},
518 {0x03B1, 0x03C1}, {0x03C3, 0x03C9}, {0x0401, 0x0401}, {0x0410, 0x044F},
519 {0x0451, 0x0451}, {0x2010, 0x2010}, {0x2013, 0x2016}, {0x2018, 0x2019},
520 {0x201C, 0x201D}, {0x2020, 0x2022}, {0x2024, 0x2027}, {0x2030, 0x2030},
521 {0x2032, 0x2033}, {0x2035, 0x2035}, {0x203B, 0x203B}, {0x203E, 0x203E},
522 {0x2074, 0x2074}, {0x207F, 0x207F}, {0x2081, 0x2084}, {0x20AC, 0x20AC},
523 {0x2103, 0x2103}, {0x2105, 0x2105}, {0x2109, 0x2109}, {0x2113, 0x2113},
524 {0x2116, 0x2116}, {0x2121, 0x2122}, {0x2126, 0x2126}, {0x212B, 0x212B},
525 {0x2153, 0x2154}, {0x215B, 0x215E}, {0x2160, 0x216B}, {0x2170, 0x2179},
526 {0x2190, 0x2199}, {0x21B8, 0x21B9}, {0x21D2, 0x21D2}, {0x21D4, 0x21D4},
527 {0x21E7, 0x21E7}, {0x2200, 0x2200}, {0x2202, 0x2203}, {0x2207, 0x2208},
528 {0x220B, 0x220B}, {0x220F, 0x220F}, {0x2211, 0x2211}, {0x2215, 0x2215},
529 {0x221A, 0x221A}, {0x221D, 0x2220}, {0x2223, 0x2223}, {0x2225, 0x2225},
530 {0x2227, 0x222C}, {0x222E, 0x222E}, {0x2234, 0x2237}, {0x223C, 0x223D},
531 {0x2248, 0x2248}, {0x224C, 0x224C}, {0x2252, 0x2252}, {0x2260, 0x2261},
532 {0x2264, 0x2267}, {0x226A, 0x226B}, {0x226E, 0x226F}, {0x2282, 0x2283},
533 {0x2286, 0x2287}, {0x2295, 0x2295}, {0x2299, 0x2299}, {0x22A5, 0x22A5},
534 {0x22BF, 0x22BF}, {0x2312, 0x2312}, {0x2460, 0x24E9}, {0x24EB, 0x254B},
535 {0x2550, 0x2573}, {0x2580, 0x258F}, {0x2592, 0x2595}, {0x25A0, 0x25A1},
536 {0x25A3, 0x25A9}, {0x25B2, 0x25B3}, {0x25B6, 0x25B7}, {0x25BC, 0x25BD},
537 {0x25C0, 0x25C1}, {0x25C6, 0x25C8}, {0x25CB, 0x25CB}, {0x25CE, 0x25D1},
538 {0x25E2, 0x25E5}, {0x25EF, 0x25EF}, {0x2605, 0x2606}, {0x2609, 0x2609},
539 {0x260E, 0x260F}, {0x2614, 0x2615}, {0x261C, 0x261C}, {0x261E, 0x261E},
540 {0x2640, 0x2640}, {0x2642, 0x2642}, {0x2660, 0x2661}, {0x2663, 0x2665},
541 {0x2667, 0x266A}, {0x266C, 0x266D}, {0x266F, 0x266F}, {0x273D, 0x273D},
542 {0x2776, 0x277F}, {0xE000, 0xF8FF}, {0xFE00, 0xFE0F}, {0xFFFD, 0xFFFD},
543 {0xE0100, 0xE01EF}, {0xF0000, 0xFFFFD}, {0x100000, 0x10FFFD}
546 if (g_unichar_iswide (c))
549 if (bsearch (GUINT_TO_POINTER (c), ambiguous, G_N_ELEMENTS (ambiguous), sizeof ambiguous[0],
559 * @c: a Unicode character
561 * Converts a character to uppercase.
563 * Return value: the result of converting @c to uppercase.
564 * If @c is not an lowercase or titlecase character,
565 * or has no upper case equivalent @c is returned unchanged.
568 g_unichar_toupper (gunichar c)
571 if (t == G_UNICODE_LOWERCASE_LETTER)
573 gunichar val = ATTTABLE (c >> 8, c & 0xff);
574 if (val >= 0x1000000)
576 const gchar *p = special_case_table + val - 0x1000000;
577 val = g_utf8_get_char (p);
579 /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
580 * do not have an uppercase equivalent, in which case val will be
583 return val ? val : c;
585 else if (t == G_UNICODE_TITLECASE_LETTER)
588 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
590 if (title_table[i][0] == c)
591 return title_table[i][1];
599 * @c: a Unicode character.
601 * Converts a character to lower case.
603 * Return value: 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.
608 g_unichar_tolower (gunichar c)
611 if (t == G_UNICODE_UPPERCASE_LETTER)
613 gunichar val = ATTTABLE (c >> 8, c & 0xff);
614 if (val >= 0x1000000)
616 const gchar *p = special_case_table + val - 0x1000000;
617 return g_utf8_get_char (p);
621 /* Not all uppercase letters are guaranteed to have a lowercase
622 * equivalent. If this is the case, val will be zero. */
623 return val ? val : c;
626 else if (t == G_UNICODE_TITLECASE_LETTER)
629 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
631 if (title_table[i][0] == c)
632 return title_table[i][2];
640 * @c: a Unicode character
642 * Converts a character to the titlecase.
644 * Return value: the result of converting @c to titlecase.
645 * If @c is not an uppercase or lowercase character,
646 * @c is returned unchanged.
649 g_unichar_totitle (gunichar c)
652 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
654 if (title_table[i][0] == c || title_table[i][1] == c
655 || title_table[i][2] == c)
656 return title_table[i][0];
659 if (TYPE (c) == G_UNICODE_LOWERCASE_LETTER)
660 return g_unichar_toupper (c);
666 * g_unichar_digit_value:
667 * @c: a Unicode character
669 * Determines the numeric value of a character as a decimal
672 * Return value: If @c is a decimal digit (according to
673 * g_unichar_isdigit()), its numeric value. Otherwise, -1.
676 g_unichar_digit_value (gunichar c)
678 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
679 return ATTTABLE (c >> 8, c & 0xff);
684 * g_unichar_xdigit_value:
685 * @c: a Unicode character
687 * Determines the numeric value of a character as a hexidecimal
690 * Return value: If @c is a hex digit (according to
691 * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
694 g_unichar_xdigit_value (gunichar c)
696 if (c >= 'A' && c <= 'F')
698 if (c >= 'a' && c <= 'f')
700 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
701 return ATTTABLE (c >> 8, c & 0xff);
707 * @c: a Unicode character
709 * Classifies a Unicode character by type.
711 * Return value: the type of the character.
714 g_unichar_type (gunichar c)
720 * Case mapping functions
730 get_locale_type (void)
733 char *tem = g_win32_getlocale ();
740 const char *locale = setlocale (LC_CTYPE, NULL);
746 if (locale[1] == 'z')
747 return LOCALE_TURKIC;
750 if (locale[1] == 't')
751 return LOCALE_LITHUANIAN;
754 if (locale[1] == 'r')
755 return LOCALE_TURKIC;
759 return LOCALE_NORMAL;
763 output_marks (const char **p_inout,
767 const char *p = *p_inout;
772 gunichar c = g_utf8_get_char (p);
774 if (ISMARK (TYPE (c)))
776 if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
777 len += g_unichar_to_utf8 (c, out_buffer ? out_buffer + len : NULL);
778 p = g_utf8_next_char (p);
789 output_special_case (gchar *out_buffer,
794 const gchar *p = special_case_table + offset;
797 if (type != G_UNICODE_TITLECASE_LETTER)
798 p = g_utf8_next_char (p);
805 memcpy (out_buffer, p, len);
811 real_toupper (const gchar *str,
814 LocaleType locale_type)
816 const gchar *p = str;
817 const char *last = NULL;
819 gboolean last_was_i = FALSE;
821 while ((max_len < 0 || p < str + max_len) && *p)
823 gunichar c = g_utf8_get_char (p);
828 p = g_utf8_next_char (p);
830 if (locale_type == LOCALE_LITHUANIAN)
838 /* Nasty, need to remove any dot above. Though
839 * I think only E WITH DOT ABOVE occurs in practice
840 * which could simplify this considerably.
845 decomp = g_unicode_canonical_decomposition (c, &decomp_len);
846 for (i=0; i < decomp_len; i++)
848 if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
849 len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
853 len += output_marks (&p, out_buffer ? out_buffer + len : NULL, TRUE);
863 if (locale_type == LOCALE_TURKIC && c == 'i')
865 /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
866 len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL);
868 else if (c == 0x0345) /* COMBINING GREEK YPOGEGRAMMENI */
870 /* Nasty, need to move it after other combining marks .. this would go away if
871 * we normalized first.
873 len += output_marks (&p, out_buffer ? out_buffer + len : NULL, FALSE);
875 /* And output as GREEK CAPITAL LETTER IOTA */
876 len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);
879 OR (G_UNICODE_LOWERCASE_LETTER,
880 OR (G_UNICODE_TITLECASE_LETTER,
883 val = ATTTABLE (c >> 8, c & 0xff);
885 if (val >= 0x1000000)
887 len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t,
888 t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
892 if (t == G_UNICODE_TITLECASE_LETTER)
895 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
897 if (title_table[i][0] == c)
899 val = title_table[i][1];
905 /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
906 * do not have an uppercase equivalent, in which case val will be
908 len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
913 gsize char_len = g_utf8_skip[*(guchar *)last];
916 memcpy (out_buffer + len, last, char_len);
928 * @str: a UTF-8 encoded string
929 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
931 * Converts all Unicode characters in the string that have a case
932 * to uppercase. The exact manner that this is done depends
933 * on the current locale, and may result in the number of
934 * characters in the string increasing. (For instance, the
935 * German ess-zet will be changed to SS.)
937 * Return value: a newly allocated string, with all characters
938 * converted to uppercase.
941 g_utf8_strup (const gchar *str,
945 LocaleType locale_type;
948 g_return_val_if_fail (str != NULL, NULL);
950 locale_type = get_locale_type ();
953 * We use a two pass approach to keep memory management simple
955 result_len = real_toupper (str, len, NULL, locale_type);
956 result = g_malloc (result_len + 1);
957 real_toupper (str, len, result, locale_type);
958 result[result_len] = '\0';
963 /* traverses the string checking for characters with combining class == 230
964 * until a base character is found */
966 has_more_above (const gchar *str)
968 const gchar *p = str;
969 gint combining_class;
973 combining_class = g_unichar_combining_class (g_utf8_get_char (p));
974 if (combining_class == 230)
976 else if (combining_class == 0)
979 p = g_utf8_next_char (p);
986 real_tolower (const gchar *str,
989 LocaleType locale_type)
991 const gchar *p = str;
992 const char *last = NULL;
995 while ((max_len < 0 || p < str + max_len) && *p)
997 gunichar c = g_utf8_get_char (p);
1002 p = g_utf8_next_char (p);
1004 if (locale_type == LOCALE_TURKIC && c == 'I')
1006 if (g_utf8_get_char (p) == 0x0307)
1008 /* I + COMBINING DOT ABOVE => i (U+0069) */
1009 len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL);
1010 p = g_utf8_next_char (p);
1014 /* I => LATIN SMALL LETTER DOTLESS I */
1015 len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL);
1018 /* Introduce an explicit dot above when lowercasing capital I's and J's
1019 * whenever there are more accents above. [SpecialCasing.txt] */
1020 else if (locale_type == LOCALE_LITHUANIAN &&
1021 (c == 0x00cc || c == 0x00cd || c == 0x0128))
1023 len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL);
1024 len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL);
1029 len += g_unichar_to_utf8 (0x0300, out_buffer ? out_buffer + len : NULL);
1032 len += g_unichar_to_utf8 (0x0301, out_buffer ? out_buffer + len : NULL);
1035 len += g_unichar_to_utf8 (0x0303, out_buffer ? out_buffer + len : NULL);
1039 else if (locale_type == LOCALE_LITHUANIAN &&
1040 (c == 'I' || c == 'J' || c == 0x012e) &&
1043 len += g_unichar_to_utf8 (g_unichar_tolower (c), out_buffer ? out_buffer + len : NULL);
1044 len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL);
1046 else if (c == 0x03A3) /* GREEK CAPITAL LETTER SIGMA */
1048 if ((max_len < 0 || p < str + max_len) && *p)
1050 gunichar next_c = g_utf8_get_char (p);
1051 int next_type = TYPE(next_c);
1053 /* SIGMA mapps differently depending on whether it is
1054 * final or not. The following simplified test would
1055 * fail in the case of combining marks following the
1056 * sigma, but I don't think that occurs in real text.
1057 * The test here matches that in ICU.
1059 if (ISALPHA (next_type)) /* Lu,Ll,Lt,Lm,Lo */
1060 val = 0x3c3; /* GREEK SMALL SIGMA */
1062 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
1065 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
1067 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
1070 OR (G_UNICODE_UPPERCASE_LETTER,
1071 OR (G_UNICODE_TITLECASE_LETTER,
1074 val = ATTTABLE (c >> 8, c & 0xff);
1076 if (val >= 0x1000000)
1078 len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t, 0);
1082 if (t == G_UNICODE_TITLECASE_LETTER)
1085 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
1087 if (title_table[i][0] == c)
1089 val = title_table[i][2];
1095 /* Not all uppercase letters are guaranteed to have a lowercase
1096 * equivalent. If this is the case, val will be zero. */
1097 len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
1102 gsize char_len = g_utf8_skip[*(guchar *)last];
1105 memcpy (out_buffer + len, last, char_len);
1117 * @str: a UTF-8 encoded string
1118 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1120 * Converts all Unicode characters in the string that have a case
1121 * to lowercase. The exact manner that this is done depends
1122 * on the current locale, and may result in the number of
1123 * characters in the string changing.
1125 * Return value: a newly allocated string, with all characters
1126 * converted to lowercase.
1129 g_utf8_strdown (const gchar *str,
1133 LocaleType locale_type;
1136 g_return_val_if_fail (str != NULL, NULL);
1138 locale_type = get_locale_type ();
1141 * We use a two pass approach to keep memory management simple
1143 result_len = real_tolower (str, len, NULL, locale_type);
1144 result = g_malloc (result_len + 1);
1145 real_tolower (str, len, result, locale_type);
1146 result[result_len] = '\0';
1153 * @str: a UTF-8 encoded string
1154 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1156 * Converts a string into a form that is independent of case. The
1157 * result will not correspond to any particular case, but can be
1158 * compared for equality or ordered with the results of calling
1159 * g_utf8_casefold() on other strings.
1161 * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
1162 * only an approximation to the correct linguistic case insensitive
1163 * ordering, though it is a fairly good one. Getting this exactly
1164 * right would require a more sophisticated collation function that
1165 * takes case sensitivity into account. GLib does not currently
1166 * provide such a function.
1168 * Return value: a newly allocated string, that is a
1169 * case independent form of @str.
1172 g_utf8_casefold (const gchar *str,
1178 g_return_val_if_fail (str != NULL, NULL);
1180 result = g_string_new (NULL);
1182 while ((len < 0 || p < str + len) && *p)
1184 gunichar ch = g_utf8_get_char (p);
1187 int end = G_N_ELEMENTS (casefold_table);
1189 if (ch >= casefold_table[start].ch &&
1190 ch <= casefold_table[end - 1].ch)
1194 int half = (start + end) / 2;
1195 if (ch == casefold_table[half].ch)
1197 g_string_append (result, casefold_table[half].data);
1200 else if (half == start)
1202 else if (ch > casefold_table[half].ch)
1209 g_string_append_unichar (result, g_unichar_tolower (ch));
1212 p = g_utf8_next_char (p);
1215 return g_string_free (result, FALSE);
1219 * g_unichar_get_mirror_char:
1220 * @ch: a Unicode character
1221 * @mirrored_ch: location to store the mirrored character
1223 * In Unicode, some characters are <firstterm>mirrored</firstterm>. This
1224 * means that their images are mirrored horizontally in text that is laid
1225 * out from right to left. For instance, "(" would become its mirror image,
1226 * ")", in right-to-left text.
1228 * If @ch has the Unicode mirrored property and there is another unicode
1229 * character that typically has a glyph that is the mirror image of @ch's
1230 * glyph and @mirrored_ch is set, it puts that character in the address
1231 * pointed to by @mirrored_ch. Otherwise the original character is put.
1233 * Return value: %TRUE if @ch has a mirrored character, %FALSE otherwise
1238 g_unichar_get_mirror_char (gunichar ch,
1239 gunichar *mirrored_ch)
1244 mirrored = GLIB_GET_MIRRORING(ch);
1246 found = ch != mirrored;
1248 *mirrored_ch = mirrored;
1254 #define G_SCRIPT_TABLE_MIDPOINT (G_N_ELEMENTS (g_script_table) / 2)
1256 static inline GUnicodeScript
1257 g_unichar_get_script_bsearch (gunichar ch)
1260 int upper = G_N_ELEMENTS (g_script_table) - 1;
1261 static int saved_mid = G_SCRIPT_TABLE_MIDPOINT;
1262 int mid = saved_mid;
1267 if (ch < g_script_table[mid].start)
1269 else if (ch >= g_script_table[mid].start + g_script_table[mid].chars)
1272 return g_script_table[saved_mid = mid].script;
1274 mid = (lower + upper) / 2;
1276 while (lower <= upper);
1278 return G_UNICODE_SCRIPT_UNKNOWN;
1282 * g_unichar_get_script:
1283 * @ch: a Unicode character
1285 * Looks up the #GUnicodeScript for a particular character (as defined
1286 * by Unicode Standard Annex #24). No check is made for @ch being a
1287 * valid Unicode character; if you pass in invalid character, the
1288 * result is undefined.
1290 * This function is equivalent to pango_script_for_unichar() and the
1291 * two are interchangeable.
1293 * Return value: the #GUnicodeScript for the character.
1298 g_unichar_get_script (gunichar ch)
1300 if (ch < G_EASY_SCRIPTS_RANGE)
1301 return g_script_easy_table[ch];
1303 return g_unichar_get_script_bsearch (ch);