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.
30 #include "gunichartables.h"
31 #include "gmirroringtable.h"
32 #include "gscripttable.h"
33 #include "gunicodeprivate.h"
35 #define ATTR_TABLE(Page) (((Page) <= G_UNICODE_LAST_PAGE_PART1) \
36 ? attr_table_part1[Page] \
37 : attr_table_part2[(Page) - 0xe00])
39 #define ATTTABLE(Page, Char) \
40 ((ATTR_TABLE(Page) == G_UNICODE_MAX_TABLE_INDEX) ? 0 : (attr_data[ATTR_TABLE(Page)][Char]))
42 #define TTYPE_PART1(Page, Char) \
43 ((type_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
44 ? (type_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
45 : (type_data[type_table_part1[Page]][Char]))
47 #define TTYPE_PART2(Page, Char) \
48 ((type_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
49 ? (type_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
50 : (type_data[type_table_part2[Page]][Char]))
53 (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
54 ? TTYPE_PART1 ((Char) >> 8, (Char) & 0xff) \
55 : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
56 ? TTYPE_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
57 : G_UNICODE_UNASSIGNED))
60 #define IS(Type, Class) (((guint)1 << (Type)) & (Class))
61 #define OR(Type, Rest) (((guint)1 << (Type)) | (Rest))
65 #define ISALPHA(Type) IS ((Type), \
66 OR (G_UNICODE_LOWERCASE_LETTER, \
67 OR (G_UNICODE_UPPERCASE_LETTER, \
68 OR (G_UNICODE_TITLECASE_LETTER, \
69 OR (G_UNICODE_MODIFIER_LETTER, \
70 OR (G_UNICODE_OTHER_LETTER, 0))))))
72 #define ISALDIGIT(Type) IS ((Type), \
73 OR (G_UNICODE_DECIMAL_NUMBER, \
74 OR (G_UNICODE_LETTER_NUMBER, \
75 OR (G_UNICODE_OTHER_NUMBER, \
76 OR (G_UNICODE_LOWERCASE_LETTER, \
77 OR (G_UNICODE_UPPERCASE_LETTER, \
78 OR (G_UNICODE_TITLECASE_LETTER, \
79 OR (G_UNICODE_MODIFIER_LETTER, \
80 OR (G_UNICODE_OTHER_LETTER, 0)))))))))
82 #define ISMARK(Type) IS ((Type), \
83 OR (G_UNICODE_NON_SPACING_MARK, \
84 OR (G_UNICODE_COMBINING_MARK, \
85 OR (G_UNICODE_ENCLOSING_MARK, 0))))
87 #define ISZEROWIDTHTYPE(Type) IS ((Type), \
88 OR (G_UNICODE_NON_SPACING_MARK, \
89 OR (G_UNICODE_ENCLOSING_MARK, \
90 OR (G_UNICODE_FORMAT, 0))))
94 * @c: a Unicode character
96 * Determines whether a character is alphanumeric.
97 * Given some UTF-8 text, obtain a character value
98 * with g_utf8_get_char().
100 * Return value: %TRUE if @c is an alphanumeric character
103 g_unichar_isalnum (gunichar c)
105 return ISALDIGIT (TYPE (c)) ? TRUE : FALSE;
110 * @c: a Unicode character
112 * Determines whether a character is alphabetic (i.e. a letter).
113 * Given some UTF-8 text, obtain a character value with
116 * Return value: %TRUE if @c is an alphabetic character
119 g_unichar_isalpha (gunichar c)
121 return ISALPHA (TYPE (c)) ? TRUE : FALSE;
127 * @c: a Unicode character
129 * Determines whether a character is a control character.
130 * Given some UTF-8 text, obtain a character value with
133 * Return value: %TRUE if @c is a control character
136 g_unichar_iscntrl (gunichar c)
138 return TYPE (c) == G_UNICODE_CONTROL;
143 * @c: a Unicode character
145 * Determines whether a character is numeric (i.e. a digit). This
146 * covers ASCII 0-9 and also digits in other languages/scripts. Given
147 * some UTF-8 text, obtain a character value with g_utf8_get_char().
149 * Return value: %TRUE if @c is a digit
152 g_unichar_isdigit (gunichar c)
154 return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
160 * @c: a Unicode character
162 * Determines whether a character is printable and not a space
163 * (returns %FALSE for control characters, format characters, and
164 * spaces). g_unichar_isprint() is similar, but returns %TRUE for
165 * spaces. Given some UTF-8 text, obtain a character value with
168 * Return value: %TRUE if @c is printable unless it's a space
171 g_unichar_isgraph (gunichar c)
174 OR (G_UNICODE_CONTROL,
175 OR (G_UNICODE_FORMAT,
176 OR (G_UNICODE_UNASSIGNED,
177 OR (G_UNICODE_SURROGATE,
178 OR (G_UNICODE_SPACE_SEPARATOR,
184 * @c: a Unicode character
186 * Determines whether a character is a lowercase letter.
187 * Given some UTF-8 text, obtain a character value with
190 * Return value: %TRUE if @c is a lowercase letter
193 g_unichar_islower (gunichar c)
195 return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
201 * @c: a Unicode character
203 * Determines whether a character is printable.
204 * Unlike g_unichar_isgraph(), returns %TRUE for spaces.
205 * Given some UTF-8 text, obtain a character value with
208 * Return value: %TRUE if @c is printable
211 g_unichar_isprint (gunichar c)
214 OR (G_UNICODE_CONTROL,
215 OR (G_UNICODE_FORMAT,
216 OR (G_UNICODE_UNASSIGNED,
217 OR (G_UNICODE_SURROGATE,
223 * @c: a Unicode character
225 * Determines whether a character is punctuation or a symbol.
226 * Given some UTF-8 text, obtain a character value with
229 * Return value: %TRUE if @c is a punctuation or symbol character
232 g_unichar_ispunct (gunichar c)
235 OR (G_UNICODE_CONNECT_PUNCTUATION,
236 OR (G_UNICODE_DASH_PUNCTUATION,
237 OR (G_UNICODE_CLOSE_PUNCTUATION,
238 OR (G_UNICODE_FINAL_PUNCTUATION,
239 OR (G_UNICODE_INITIAL_PUNCTUATION,
240 OR (G_UNICODE_OTHER_PUNCTUATION,
241 OR (G_UNICODE_OPEN_PUNCTUATION,
242 OR (G_UNICODE_CURRENCY_SYMBOL,
243 OR (G_UNICODE_MODIFIER_SYMBOL,
244 OR (G_UNICODE_MATH_SYMBOL,
245 OR (G_UNICODE_OTHER_SYMBOL,
246 0)))))))))))) ? TRUE : FALSE;
251 * @c: a Unicode character
253 * Determines whether a character is a space, tab, or line separator
254 * (newline, carriage return, etc.). Given some UTF-8 text, obtain a
255 * character value with g_utf8_get_char().
257 * (Note: don't use this to do word breaking; you have to use
258 * Pango or equivalent to get word breaking right, the algorithm
259 * is fairly complex.)
261 * Return value: %TRUE if @c is a space character
264 g_unichar_isspace (gunichar c)
268 /* special-case these since Unicode thinks they are not spaces */
279 OR (G_UNICODE_SPACE_SEPARATOR,
280 OR (G_UNICODE_LINE_SEPARATOR,
281 OR (G_UNICODE_PARAGRAPH_SEPARATOR,
282 0)))) ? TRUE : FALSE;
290 * @c: a Unicode character
292 * Determines whether a character is a mark (non-spacing mark,
293 * combining mark, or enclosing mark in Unicode speak).
294 * Given some UTF-8 text, obtain a character value
295 * with g_utf8_get_char().
297 * Note: in most cases where isalpha characters are allowed,
298 * ismark characters should be allowed to as they are essential
299 * for writing most European languages as well as many non-Latin
302 * Return value: %TRUE if @c is a mark character
307 g_unichar_ismark (gunichar c)
309 return ISMARK (TYPE (c));
314 * @c: a Unicode character
316 * Determines if a character is uppercase.
318 * Return value: %TRUE if @c is an uppercase character
321 g_unichar_isupper (gunichar c)
323 return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
328 * @c: a Unicode character
330 * Determines if a character is titlecase. Some characters in
331 * Unicode which are composites, such as the DZ digraph
332 * have three case variants instead of just two. The titlecase
333 * form is used at the beginning of a word where only the
334 * first letter is capitalized. The titlecase form of the DZ
335 * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z.
337 * Return value: %TRUE if the character is titlecase
340 g_unichar_istitle (gunichar c)
343 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
344 if (title_table[i][0] == c)
350 * g_unichar_isxdigit:
351 * @c: a Unicode character.
353 * Determines if a character is a hexidecimal digit.
355 * Return value: %TRUE if the character is a hexadecimal digit
358 g_unichar_isxdigit (gunichar c)
360 return ((c >= 'a' && c <= 'f')
361 || (c >= 'A' && c <= 'F')
362 || (TYPE (c) == G_UNICODE_DECIMAL_NUMBER));
366 * g_unichar_isdefined:
367 * @c: a Unicode character
369 * Determines if a given character is assigned in the Unicode
372 * Return value: %TRUE if the character has an assigned value
375 g_unichar_isdefined (gunichar c)
378 OR (G_UNICODE_UNASSIGNED,
379 OR (G_UNICODE_SURROGATE,
384 * g_unichar_iszerowidth:
385 * @c: a Unicode character
387 * Determines if a given character typically takes zero width when rendered.
388 * The return value is %TRUE for all non-spacing and enclosing marks
389 * (e.g., combining accents), format characters, zero-width
390 * space, but not U+00AD SOFT HYPHEN.
392 * A typical use of this function is with one of g_unichar_iswide() or
393 * g_unichar_iswide_cjk() to determine the number of cells a string occupies
394 * when displayed on a grid display (terminals). However, note that not all
395 * terminals support zero-width rendering of zero-width marks.
397 * Return value: %TRUE if the character has zero width
402 g_unichar_iszerowidth (gunichar c)
404 if (G_UNLIKELY (c == 0x00AD))
407 if (G_UNLIKELY (ISZEROWIDTHTYPE (TYPE (c))))
410 if (G_UNLIKELY ((c >= 0x1160 && c < 0x1200) ||
423 interval_compare (const void *key, const void *elt)
425 gunichar c = GPOINTER_TO_UINT (key);
426 struct Interval *interval = (struct Interval *)elt;
428 if (c < interval->start)
430 if (c > interval->end)
438 * @c: a Unicode character
440 * Determines if a character is typically rendered in a double-width
443 * Return value: %TRUE if the character is wide
446 g_unichar_iswide (gunichar c)
448 /* sorted list of intervals of East_Asian_Width = W and F characters
449 * from Unicode 5.1.0. produced by mungling output of:
450 * grep ';[FW]\>' EastAsianWidth.txt */
451 static const struct Interval wide[] = {
452 {0x1100, 0x1159}, {0x115F, 0x115F}, {0x2329, 0x232A}, {0x2E80, 0x2E99},
453 {0x2E9B, 0x2EF3}, {0x2F00, 0x2FD5}, {0x2FF0, 0x2FFB}, {0x3000, 0x303E},
454 {0x3041, 0x3096}, {0x3099, 0x30FF}, {0x3105, 0x312D}, {0x3131, 0x318E},
455 {0x3190, 0x31B7}, {0x31C0, 0x31E3}, {0x31F0, 0x321E}, {0x3220, 0x3243},
456 {0x3250, 0x32FE}, {0x3300, 0x4DB5}, {0x4E00, 0x9FC3}, {0xA000, 0xA48C},
457 {0xA490, 0xA4C6}, {0xAC00, 0xD7A3}, {0xF900, 0xFA2D}, {0xFA30, 0xFA6A},
458 {0xFA70, 0xFAD9}, {0xFE10, 0xFE19}, {0xFE30, 0xFE52}, {0xFE54, 0xFE66},
459 {0xFE68, 0xFE6B}, {0xFF01, 0xFF60}, {0xFFE0, 0xFFE6}, {0x20000, 0x2FFFD},
463 if (bsearch (GUINT_TO_POINTER (c), wide, G_N_ELEMENTS (wide), sizeof wide[0],
472 * g_unichar_iswide_cjk:
473 * @c: a Unicode character
475 * Determines if a character is typically rendered in a double-width
476 * cell under legacy East Asian locales. If a character is wide according to
477 * g_unichar_iswide(), then it is also reported wide with this function, but
478 * the converse is not necessarily true. See the
479 * <ulink url="http://www.unicode.org/reports/tr11/">Unicode Standard
480 * Annex #11</ulink> for details.
482 * If a character passes the g_unichar_iswide() test then it will also pass
483 * this test, but not the other way around. Note that some characters may
484 * pas both this test and g_unichar_iszerowidth().
486 * Return value: %TRUE if the character is wide in legacy East Asian locales
491 g_unichar_iswide_cjk (gunichar c)
493 /* sorted list of intervals of East_Asian_Width = A and F characters
494 * from Unicode 5.1.0. produced by mungling output of:
495 * grep ';[A]\>' EastAsianWidth.txt */
496 static const struct Interval ambiguous[] = {
497 {0x00A1, 0x00A1}, {0x00A4, 0x00A4}, {0x00A7, 0x00A8}, {0x00AA, 0x00AA},
498 {0x00AD, 0x00AE}, {0x00B0, 0x00B4}, {0x00B6, 0x00BA}, {0x00BC, 0x00BF},
499 {0x00C6, 0x00C6}, {0x00D0, 0x00D0}, {0x00D7, 0x00D8}, {0x00DE, 0x00E1},
500 {0x00E6, 0x00E6}, {0x00E8, 0x00EA}, {0x00EC, 0x00ED}, {0x00F0, 0x00F0},
501 {0x00F2, 0x00F3}, {0x00F7, 0x00FA}, {0x00FC, 0x00FC}, {0x00FE, 0x00FE},
502 {0x0101, 0x0101}, {0x0111, 0x0111}, {0x0113, 0x0113}, {0x011B, 0x011B},
503 {0x0126, 0x0127}, {0x012B, 0x012B}, {0x0131, 0x0133}, {0x0138, 0x0138},
504 {0x013F, 0x0142}, {0x0144, 0x0144}, {0x0148, 0x014B}, {0x014D, 0x014D},
505 {0x0152, 0x0153}, {0x0166, 0x0167}, {0x016B, 0x016B}, {0x01CE, 0x01CE},
506 {0x01D0, 0x01D0}, {0x01D2, 0x01D2}, {0x01D4, 0x01D4}, {0x01D6, 0x01D6},
507 {0x01D8, 0x01D8}, {0x01DA, 0x01DA}, {0x01DC, 0x01DC}, {0x0251, 0x0251},
508 {0x0261, 0x0261}, {0x02C4, 0x02C4}, {0x02C7, 0x02C7}, {0x02C9, 0x02CB},
509 {0x02CD, 0x02CD}, {0x02D0, 0x02D0}, {0x02D8, 0x02DB}, {0x02DD, 0x02DD},
510 {0x02DF, 0x02DF}, {0x0300, 0x036F}, {0x0391, 0x03A1}, {0x03A3, 0x03A9},
511 {0x03B1, 0x03C1}, {0x03C3, 0x03C9}, {0x0401, 0x0401}, {0x0410, 0x044F},
512 {0x0451, 0x0451}, {0x2010, 0x2010}, {0x2013, 0x2016}, {0x2018, 0x2019},
513 {0x201C, 0x201D}, {0x2020, 0x2022}, {0x2024, 0x2027}, {0x2030, 0x2030},
514 {0x2032, 0x2033}, {0x2035, 0x2035}, {0x203B, 0x203B}, {0x203E, 0x203E},
515 {0x2074, 0x2074}, {0x207F, 0x207F}, {0x2081, 0x2084}, {0x20AC, 0x20AC},
516 {0x2103, 0x2103}, {0x2105, 0x2105}, {0x2109, 0x2109}, {0x2113, 0x2113},
517 {0x2116, 0x2116}, {0x2121, 0x2122}, {0x2126, 0x2126}, {0x212B, 0x212B},
518 {0x2153, 0x2154}, {0x215B, 0x215E}, {0x2160, 0x216B}, {0x2170, 0x2179},
519 {0x2190, 0x2199}, {0x21B8, 0x21B9}, {0x21D2, 0x21D2}, {0x21D4, 0x21D4},
520 {0x21E7, 0x21E7}, {0x2200, 0x2200}, {0x2202, 0x2203}, {0x2207, 0x2208},
521 {0x220B, 0x220B}, {0x220F, 0x220F}, {0x2211, 0x2211}, {0x2215, 0x2215},
522 {0x221A, 0x221A}, {0x221D, 0x2220}, {0x2223, 0x2223}, {0x2225, 0x2225},
523 {0x2227, 0x222C}, {0x222E, 0x222E}, {0x2234, 0x2237}, {0x223C, 0x223D},
524 {0x2248, 0x2248}, {0x224C, 0x224C}, {0x2252, 0x2252}, {0x2260, 0x2261},
525 {0x2264, 0x2267}, {0x226A, 0x226B}, {0x226E, 0x226F}, {0x2282, 0x2283},
526 {0x2286, 0x2287}, {0x2295, 0x2295}, {0x2299, 0x2299}, {0x22A5, 0x22A5},
527 {0x22BF, 0x22BF}, {0x2312, 0x2312}, {0x2460, 0x24E9}, {0x24EB, 0x254B},
528 {0x2550, 0x2573}, {0x2580, 0x258F}, {0x2592, 0x2595}, {0x25A0, 0x25A1},
529 {0x25A3, 0x25A9}, {0x25B2, 0x25B3}, {0x25B6, 0x25B7}, {0x25BC, 0x25BD},
530 {0x25C0, 0x25C1}, {0x25C6, 0x25C8}, {0x25CB, 0x25CB}, {0x25CE, 0x25D1},
531 {0x25E2, 0x25E5}, {0x25EF, 0x25EF}, {0x2605, 0x2606}, {0x2609, 0x2609},
532 {0x260E, 0x260F}, {0x2614, 0x2615}, {0x261C, 0x261C}, {0x261E, 0x261E},
533 {0x2640, 0x2640}, {0x2642, 0x2642}, {0x2660, 0x2661}, {0x2663, 0x2665},
534 {0x2667, 0x266A}, {0x266C, 0x266D}, {0x266F, 0x266F}, {0x273D, 0x273D},
535 {0x2776, 0x277F}, {0xE000, 0xF8FF}, {0xFE00, 0xFE0F}, {0xFFFD, 0xFFFD},
536 {0xE0100, 0xE01EF}, {0xF0000, 0xFFFFD}, {0x100000, 0x10FFFD}
539 if (g_unichar_iswide (c))
542 if (bsearch (GUINT_TO_POINTER (c), ambiguous, G_N_ELEMENTS (ambiguous), sizeof ambiguous[0],
552 * @c: a Unicode character
554 * Converts a character to uppercase.
556 * Return value: the result of converting @c to uppercase.
557 * If @c is not an lowercase or titlecase character,
558 * or has no upper case equivalent @c is returned unchanged.
561 g_unichar_toupper (gunichar c)
564 if (t == G_UNICODE_LOWERCASE_LETTER)
566 gunichar val = ATTTABLE (c >> 8, c & 0xff);
567 if (val >= 0x1000000)
569 const gchar *p = special_case_table + val - 0x1000000;
570 val = g_utf8_get_char (p);
572 /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
573 * do not have an uppercase equivalent, in which case val will be
576 return val ? val : c;
578 else if (t == G_UNICODE_TITLECASE_LETTER)
581 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
583 if (title_table[i][0] == c)
584 return title_table[i][1];
592 * @c: a Unicode character.
594 * Converts a character to lower case.
596 * Return value: the result of converting @c to lower case.
597 * If @c is not an upperlower or titlecase character,
598 * or has no lowercase equivalent @c is returned unchanged.
601 g_unichar_tolower (gunichar c)
604 if (t == G_UNICODE_UPPERCASE_LETTER)
606 gunichar val = ATTTABLE (c >> 8, c & 0xff);
607 if (val >= 0x1000000)
609 const gchar *p = special_case_table + val - 0x1000000;
610 return g_utf8_get_char (p);
614 /* Not all uppercase letters are guaranteed to have a lowercase
615 * equivalent. If this is the case, val will be zero. */
616 return val ? val : c;
619 else if (t == G_UNICODE_TITLECASE_LETTER)
622 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
624 if (title_table[i][0] == c)
625 return title_table[i][2];
633 * @c: a Unicode character
635 * Converts a character to the titlecase.
637 * Return value: the result of converting @c to titlecase.
638 * If @c is not an uppercase or lowercase character,
639 * @c is returned unchanged.
642 g_unichar_totitle (gunichar c)
645 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
647 if (title_table[i][0] == c || title_table[i][1] == c
648 || title_table[i][2] == c)
649 return title_table[i][0];
652 if (TYPE (c) == G_UNICODE_LOWERCASE_LETTER)
653 return g_unichar_toupper (c);
659 * g_unichar_digit_value:
660 * @c: a Unicode character
662 * Determines the numeric value of a character as a decimal
665 * Return value: If @c is a decimal digit (according to
666 * g_unichar_isdigit()), its numeric value. Otherwise, -1.
669 g_unichar_digit_value (gunichar c)
671 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
672 return ATTTABLE (c >> 8, c & 0xff);
677 * g_unichar_xdigit_value:
678 * @c: a Unicode character
680 * Determines the numeric value of a character as a hexidecimal
683 * Return value: If @c is a hex digit (according to
684 * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
687 g_unichar_xdigit_value (gunichar c)
689 if (c >= 'A' && c <= 'F')
691 if (c >= 'a' && c <= 'f')
693 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
694 return ATTTABLE (c >> 8, c & 0xff);
700 * @c: a Unicode character
702 * Classifies a Unicode character by type.
704 * Return value: the type of the character.
707 g_unichar_type (gunichar c)
713 * Case mapping functions
723 get_locale_type (void)
726 char *tem = g_win32_getlocale ();
733 const char *locale = setlocale (LC_CTYPE, NULL);
739 if (locale[1] == 'z')
740 return LOCALE_TURKIC;
743 if (locale[1] == 't')
744 return LOCALE_LITHUANIAN;
747 if (locale[1] == 'r')
748 return LOCALE_TURKIC;
752 return LOCALE_NORMAL;
756 output_marks (const char **p_inout,
760 const char *p = *p_inout;
765 gunichar c = g_utf8_get_char (p);
767 if (ISMARK (TYPE (c)))
769 if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
770 len += g_unichar_to_utf8 (c, out_buffer ? out_buffer + len : NULL);
771 p = g_utf8_next_char (p);
782 output_special_case (gchar *out_buffer,
787 const gchar *p = special_case_table + offset;
790 if (type != G_UNICODE_TITLECASE_LETTER)
791 p = g_utf8_next_char (p);
798 memcpy (out_buffer, p, len);
804 real_toupper (const gchar *str,
807 LocaleType locale_type)
809 const gchar *p = str;
810 const char *last = NULL;
812 gboolean last_was_i = FALSE;
814 while ((max_len < 0 || p < str + max_len) && *p)
816 gunichar c = g_utf8_get_char (p);
821 p = g_utf8_next_char (p);
823 if (locale_type == LOCALE_LITHUANIAN)
831 /* Nasty, need to remove any dot above. Though
832 * I think only E WITH DOT ABOVE occurs in practice
833 * which could simplify this considerably.
838 decomp = g_unicode_canonical_decomposition (c, &decomp_len);
839 for (i=0; i < decomp_len; i++)
841 if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
842 len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
846 len += output_marks (&p, out_buffer ? out_buffer + len : NULL, TRUE);
856 if (locale_type == LOCALE_TURKIC && c == 'i')
858 /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
859 len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL);
861 else if (c == 0x0345) /* COMBINING GREEK YPOGEGRAMMENI */
863 /* Nasty, need to move it after other combining marks .. this would go away if
864 * we normalized first.
866 len += output_marks (&p, out_buffer ? out_buffer + len : NULL, FALSE);
868 /* And output as GREEK CAPITAL LETTER IOTA */
869 len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);
872 OR (G_UNICODE_LOWERCASE_LETTER,
873 OR (G_UNICODE_TITLECASE_LETTER,
876 val = ATTTABLE (c >> 8, c & 0xff);
878 if (val >= 0x1000000)
880 len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t,
881 t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
885 if (t == G_UNICODE_TITLECASE_LETTER)
888 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
890 if (title_table[i][0] == c)
892 val = title_table[i][1];
898 /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
899 * do not have an uppercase equivalent, in which case val will be
901 len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
906 gsize char_len = g_utf8_skip[*(guchar *)last];
909 memcpy (out_buffer + len, last, char_len);
921 * @str: a UTF-8 encoded string
922 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
924 * Converts all Unicode characters in the string that have a case
925 * to uppercase. The exact manner that this is done depends
926 * on the current locale, and may result in the number of
927 * characters in the string increasing. (For instance, the
928 * German ess-zet will be changed to SS.)
930 * Return value: a newly allocated string, with all characters
931 * converted to uppercase.
934 g_utf8_strup (const gchar *str,
938 LocaleType locale_type;
941 g_return_val_if_fail (str != NULL, NULL);
943 locale_type = get_locale_type ();
946 * We use a two pass approach to keep memory management simple
948 result_len = real_toupper (str, len, NULL, locale_type);
949 result = g_malloc (result_len + 1);
950 real_toupper (str, len, result, locale_type);
951 result[result_len] = '\0';
956 /* traverses the string checking for characters with combining class == 230
957 * until a base character is found */
959 has_more_above (const gchar *str)
961 const gchar *p = str;
962 gint combining_class;
966 combining_class = g_unichar_combining_class (g_utf8_get_char (p));
967 if (combining_class == 230)
969 else if (combining_class == 0)
972 p = g_utf8_next_char (p);
979 real_tolower (const gchar *str,
982 LocaleType locale_type)
984 const gchar *p = str;
985 const char *last = NULL;
988 while ((max_len < 0 || p < str + max_len) && *p)
990 gunichar c = g_utf8_get_char (p);
995 p = g_utf8_next_char (p);
997 if (locale_type == LOCALE_TURKIC && c == 'I')
999 if (g_utf8_get_char (p) == 0x0307)
1001 /* I + COMBINING DOT ABOVE => i (U+0069) */
1002 len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL);
1003 p = g_utf8_next_char (p);
1007 /* I => LATIN SMALL LETTER DOTLESS I */
1008 len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL);
1011 /* Introduce an explicit dot above when lowercasing capital I's and J's
1012 * whenever there are more accents above. [SpecialCasing.txt] */
1013 else if (locale_type == LOCALE_LITHUANIAN &&
1014 (c == 0x00cc || c == 0x00cd || c == 0x0128))
1016 len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL);
1017 len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL);
1022 len += g_unichar_to_utf8 (0x0300, out_buffer ? out_buffer + len : NULL);
1025 len += g_unichar_to_utf8 (0x0301, out_buffer ? out_buffer + len : NULL);
1028 len += g_unichar_to_utf8 (0x0303, out_buffer ? out_buffer + len : NULL);
1032 else if (locale_type == LOCALE_LITHUANIAN &&
1033 (c == 'I' || c == 'J' || c == 0x012e) &&
1036 len += g_unichar_to_utf8 (g_unichar_tolower (c), out_buffer ? out_buffer + len : NULL);
1037 len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL);
1039 else if (c == 0x03A3) /* GREEK CAPITAL LETTER SIGMA */
1041 if ((max_len < 0 || p < str + max_len) && *p)
1043 gunichar next_c = g_utf8_get_char (p);
1044 int next_type = TYPE(next_c);
1046 /* SIGMA mapps differently depending on whether it is
1047 * final or not. The following simplified test would
1048 * fail in the case of combining marks following the
1049 * sigma, but I don't think that occurs in real text.
1050 * The test here matches that in ICU.
1052 if (ISALPHA (next_type)) /* Lu,Ll,Lt,Lm,Lo */
1053 val = 0x3c3; /* GREEK SMALL SIGMA */
1055 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
1058 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
1060 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
1063 OR (G_UNICODE_UPPERCASE_LETTER,
1064 OR (G_UNICODE_TITLECASE_LETTER,
1067 val = ATTTABLE (c >> 8, c & 0xff);
1069 if (val >= 0x1000000)
1071 len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t, 0);
1075 if (t == G_UNICODE_TITLECASE_LETTER)
1078 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
1080 if (title_table[i][0] == c)
1082 val = title_table[i][2];
1088 /* Not all uppercase letters are guaranteed to have a lowercase
1089 * equivalent. If this is the case, val will be zero. */
1090 len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
1095 gsize char_len = g_utf8_skip[*(guchar *)last];
1098 memcpy (out_buffer + len, last, char_len);
1110 * @str: a UTF-8 encoded string
1111 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1113 * Converts all Unicode characters in the string that have a case
1114 * to lowercase. The exact manner that this is done depends
1115 * on the current locale, and may result in the number of
1116 * characters in the string changing.
1118 * Return value: a newly allocated string, with all characters
1119 * converted to lowercase.
1122 g_utf8_strdown (const gchar *str,
1126 LocaleType locale_type;
1129 g_return_val_if_fail (str != NULL, NULL);
1131 locale_type = get_locale_type ();
1134 * We use a two pass approach to keep memory management simple
1136 result_len = real_tolower (str, len, NULL, locale_type);
1137 result = g_malloc (result_len + 1);
1138 real_tolower (str, len, result, locale_type);
1139 result[result_len] = '\0';
1146 * @str: a UTF-8 encoded string
1147 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1149 * Converts a string into a form that is independent of case. The
1150 * result will not correspond to any particular case, but can be
1151 * compared for equality or ordered with the results of calling
1152 * g_utf8_casefold() on other strings.
1154 * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
1155 * only an approximation to the correct linguistic case insensitive
1156 * ordering, though it is a fairly good one. Getting this exactly
1157 * right would require a more sophisticated collation function that
1158 * takes case sensitivity into account. GLib does not currently
1159 * provide such a function.
1161 * Return value: a newly allocated string, that is a
1162 * case independent form of @str.
1165 g_utf8_casefold (const gchar *str,
1171 g_return_val_if_fail (str != NULL, NULL);
1173 result = g_string_new (NULL);
1175 while ((len < 0 || p < str + len) && *p)
1177 gunichar ch = g_utf8_get_char (p);
1180 int end = G_N_ELEMENTS (casefold_table);
1182 if (ch >= casefold_table[start].ch &&
1183 ch <= casefold_table[end - 1].ch)
1187 int half = (start + end) / 2;
1188 if (ch == casefold_table[half].ch)
1190 g_string_append (result, casefold_table[half].data);
1193 else if (half == start)
1195 else if (ch > casefold_table[half].ch)
1202 g_string_append_unichar (result, g_unichar_tolower (ch));
1205 p = g_utf8_next_char (p);
1208 return g_string_free (result, FALSE);
1212 * g_unichar_get_mirror_char:
1213 * @ch: a Unicode character
1214 * @mirrored_ch: location to store the mirrored character
1216 * In Unicode, some characters are <firstterm>mirrored</firstterm>. This
1217 * means that their images are mirrored horizontally in text that is laid
1218 * out from right to left. For instance, "(" would become its mirror image,
1219 * ")", in right-to-left text.
1221 * If @ch has the Unicode mirrored property and there is another unicode
1222 * character that typically has a glyph that is the mirror image of @ch's
1223 * glyph and @mirrored_ch is set, it puts that character in the address
1224 * pointed to by @mirrored_ch. Otherwise the original character is put.
1226 * Return value: %TRUE if @ch has a mirrored character, %FALSE otherwise
1231 g_unichar_get_mirror_char (gunichar ch,
1232 gunichar *mirrored_ch)
1237 mirrored = GLIB_GET_MIRRORING(ch);
1239 found = ch != mirrored;
1241 *mirrored_ch = mirrored;
1247 #define G_SCRIPT_TABLE_MIDPOINT (G_N_ELEMENTS (g_script_table) / 2)
1249 static inline GUnicodeScript
1250 g_unichar_get_script_bsearch (gunichar ch)
1253 int upper = G_N_ELEMENTS (g_script_table) - 1;
1254 static int saved_mid = G_SCRIPT_TABLE_MIDPOINT;
1255 int mid = saved_mid;
1260 if (ch < g_script_table[mid].start)
1262 else if (ch >= g_script_table[mid].start + g_script_table[mid].chars)
1265 return g_script_table[saved_mid = mid].script;
1267 mid = (lower + upper) / 2;
1269 while (lower <= upper);
1271 return G_UNICODE_SCRIPT_UNKNOWN;
1275 * g_unichar_get_script:
1276 * @ch: a Unicode character
1278 * Looks up the #GUnicodeScript for a particular character (as defined
1279 * by Unicode Standard Annex #24). No check is made for @ch being a
1280 * valid Unicode character; if you pass in invalid character, the
1281 * result is undefined.
1283 * This function is equivalent to pango_script_for_unichar() and the
1284 * two are interchangeable.
1286 * Return value: the #GUnicodeScript for the character.
1291 g_unichar_get_script (gunichar ch)
1293 if (ch < G_EASY_SCRIPTS_RANGE)
1294 return g_script_easy_table[ch];
1296 return g_unichar_get_script_bsearch (ch);