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"
39 #define ATTR_TABLE(Page) (((Page) <= G_UNICODE_LAST_PAGE_PART1) \
40 ? attr_table_part1[Page] \
41 : attr_table_part2[(Page) - 0xe00])
43 #define ATTTABLE(Page, Char) \
44 ((ATTR_TABLE(Page) == G_UNICODE_MAX_TABLE_INDEX) ? 0 : (attr_data[ATTR_TABLE(Page)][Char]))
46 #define TTYPE_PART1(Page, Char) \
47 ((type_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
48 ? (type_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
49 : (type_data[type_table_part1[Page]][Char]))
51 #define TTYPE_PART2(Page, Char) \
52 ((type_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
53 ? (type_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
54 : (type_data[type_table_part2[Page]][Char]))
57 (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
58 ? TTYPE_PART1 ((Char) >> 8, (Char) & 0xff) \
59 : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
60 ? TTYPE_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
61 : G_UNICODE_UNASSIGNED))
64 #define IS(Type, Class) (((guint)1 << (Type)) & (Class))
65 #define OR(Type, Rest) (((guint)1 << (Type)) | (Rest))
69 #define ISALPHA(Type) IS ((Type), \
70 OR (G_UNICODE_LOWERCASE_LETTER, \
71 OR (G_UNICODE_UPPERCASE_LETTER, \
72 OR (G_UNICODE_TITLECASE_LETTER, \
73 OR (G_UNICODE_MODIFIER_LETTER, \
74 OR (G_UNICODE_OTHER_LETTER, 0))))))
76 #define ISALDIGIT(Type) IS ((Type), \
77 OR (G_UNICODE_DECIMAL_NUMBER, \
78 OR (G_UNICODE_LETTER_NUMBER, \
79 OR (G_UNICODE_OTHER_NUMBER, \
80 OR (G_UNICODE_LOWERCASE_LETTER, \
81 OR (G_UNICODE_UPPERCASE_LETTER, \
82 OR (G_UNICODE_TITLECASE_LETTER, \
83 OR (G_UNICODE_MODIFIER_LETTER, \
84 OR (G_UNICODE_OTHER_LETTER, 0)))))))))
86 #define ISMARK(Type) IS ((Type), \
87 OR (G_UNICODE_NON_SPACING_MARK, \
88 OR (G_UNICODE_COMBINING_MARK, \
89 OR (G_UNICODE_ENCLOSING_MARK, 0))))
91 #define ISZEROWIDTHTYPE(Type) IS ((Type), \
92 OR (G_UNICODE_NON_SPACING_MARK, \
93 OR (G_UNICODE_ENCLOSING_MARK, \
94 OR (G_UNICODE_FORMAT, 0))))
98 * @c: a Unicode character
100 * Determines whether a character is alphanumeric.
101 * Given some UTF-8 text, obtain a character value
102 * with g_utf8_get_char().
104 * Return value: %TRUE if @c is an alphanumeric character
107 g_unichar_isalnum (gunichar c)
109 return ISALDIGIT (TYPE (c)) ? TRUE : FALSE;
114 * @c: a Unicode character
116 * Determines whether a character is alphabetic (i.e. a letter).
117 * Given some UTF-8 text, obtain a character value with
120 * Return value: %TRUE if @c is an alphabetic character
123 g_unichar_isalpha (gunichar c)
125 return ISALPHA (TYPE (c)) ? TRUE : FALSE;
131 * @c: a Unicode character
133 * Determines whether a character is a control character.
134 * Given some UTF-8 text, obtain a character value with
137 * Return value: %TRUE if @c is a control character
140 g_unichar_iscntrl (gunichar c)
142 return TYPE (c) == G_UNICODE_CONTROL;
147 * @c: a Unicode character
149 * Determines whether a character is numeric (i.e. a digit). This
150 * covers ASCII 0-9 and also digits in other languages/scripts. Given
151 * some UTF-8 text, obtain a character value with g_utf8_get_char().
153 * Return value: %TRUE if @c is a digit
156 g_unichar_isdigit (gunichar c)
158 return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
164 * @c: a Unicode character
166 * Determines whether a character is printable and not a space
167 * (returns %FALSE for control characters, format characters, and
168 * spaces). g_unichar_isprint() is similar, but returns %TRUE for
169 * spaces. Given some UTF-8 text, obtain a character value with
172 * Return value: %TRUE if @c is printable unless it's a space
175 g_unichar_isgraph (gunichar c)
178 OR (G_UNICODE_CONTROL,
179 OR (G_UNICODE_FORMAT,
180 OR (G_UNICODE_UNASSIGNED,
181 OR (G_UNICODE_SURROGATE,
182 OR (G_UNICODE_SPACE_SEPARATOR,
188 * @c: a Unicode character
190 * Determines whether a character is a lowercase letter.
191 * Given some UTF-8 text, obtain a character value with
194 * Return value: %TRUE if @c is a lowercase letter
197 g_unichar_islower (gunichar c)
199 return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
205 * @c: a Unicode character
207 * Determines whether a character is printable.
208 * Unlike g_unichar_isgraph(), returns %TRUE for spaces.
209 * Given some UTF-8 text, obtain a character value with
212 * Return value: %TRUE if @c is printable
215 g_unichar_isprint (gunichar c)
218 OR (G_UNICODE_CONTROL,
219 OR (G_UNICODE_FORMAT,
220 OR (G_UNICODE_UNASSIGNED,
221 OR (G_UNICODE_SURROGATE,
227 * @c: a Unicode character
229 * Determines whether a character is punctuation or a symbol.
230 * Given some UTF-8 text, obtain a character value with
233 * Return value: %TRUE if @c is a punctuation or symbol character
236 g_unichar_ispunct (gunichar c)
239 OR (G_UNICODE_CONNECT_PUNCTUATION,
240 OR (G_UNICODE_DASH_PUNCTUATION,
241 OR (G_UNICODE_CLOSE_PUNCTUATION,
242 OR (G_UNICODE_FINAL_PUNCTUATION,
243 OR (G_UNICODE_INITIAL_PUNCTUATION,
244 OR (G_UNICODE_OTHER_PUNCTUATION,
245 OR (G_UNICODE_OPEN_PUNCTUATION,
246 OR (G_UNICODE_CURRENCY_SYMBOL,
247 OR (G_UNICODE_MODIFIER_SYMBOL,
248 OR (G_UNICODE_MATH_SYMBOL,
249 OR (G_UNICODE_OTHER_SYMBOL,
250 0)))))))))))) ? TRUE : FALSE;
255 * @c: a Unicode character
257 * Determines whether a character is a space, tab, or line separator
258 * (newline, carriage return, etc.). Given some UTF-8 text, obtain a
259 * character value with g_utf8_get_char().
261 * (Note: don't use this to do word breaking; you have to use
262 * Pango or equivalent to get word breaking right, the algorithm
263 * is fairly complex.)
265 * Return value: %TRUE if @c is a space character
268 g_unichar_isspace (gunichar c)
272 /* special-case these since Unicode thinks they are not spaces */
283 OR (G_UNICODE_SPACE_SEPARATOR,
284 OR (G_UNICODE_LINE_SEPARATOR,
285 OR (G_UNICODE_PARAGRAPH_SEPARATOR,
286 0)))) ? TRUE : FALSE;
294 * @c: a Unicode character
296 * Determines whether a character is a mark (non-spacing mark,
297 * combining mark, or enclosing mark in Unicode speak).
298 * Given some UTF-8 text, obtain a character value
299 * with g_utf8_get_char().
301 * Note: in most cases where isalpha characters are allowed,
302 * ismark characters should be allowed to as they are essential
303 * for writing most European languages as well as many non-Latin
306 * Return value: %TRUE if @c is a mark character
311 g_unichar_ismark (gunichar c)
313 return ISMARK (TYPE (c));
318 * @c: a Unicode character
320 * Determines if a character is uppercase.
322 * Return value: %TRUE if @c is an uppercase character
325 g_unichar_isupper (gunichar c)
327 return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
332 * @c: a Unicode character
334 * Determines if a character is titlecase. Some characters in
335 * Unicode which are composites, such as the DZ digraph
336 * have three case variants instead of just two. The titlecase
337 * form is used at the beginning of a word where only the
338 * first letter is capitalized. The titlecase form of the DZ
339 * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z.
341 * Return value: %TRUE if the character is titlecase
344 g_unichar_istitle (gunichar c)
347 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
348 if (title_table[i][0] == c)
354 * g_unichar_isxdigit:
355 * @c: a Unicode character.
357 * Determines if a character is a hexidecimal digit.
359 * Return value: %TRUE if the character is a hexadecimal digit
362 g_unichar_isxdigit (gunichar c)
364 return ((c >= 'a' && c <= 'f')
365 || (c >= 'A' && c <= 'F')
366 || (TYPE (c) == G_UNICODE_DECIMAL_NUMBER));
370 * g_unichar_isdefined:
371 * @c: a Unicode character
373 * Determines if a given character is assigned in the Unicode
376 * Return value: %TRUE if the character has an assigned value
379 g_unichar_isdefined (gunichar c)
382 OR (G_UNICODE_UNASSIGNED,
383 OR (G_UNICODE_SURROGATE,
388 * g_unichar_iszerowidth:
389 * @c: a Unicode character
391 * Determines if a given character typically takes zero width when rendered.
392 * The return value is %TRUE for all non-spacing and enclosing marks
393 * (e.g., combining accents), format characters, zero-width
394 * space, but not U+00AD SOFT HYPHEN.
396 * A typical use of this function is with one of g_unichar_iswide() or
397 * g_unichar_iswide_cjk() to determine the number of cells a string occupies
398 * when displayed on a grid display (terminals). However, note that not all
399 * terminals support zero-width rendering of zero-width marks.
401 * Return value: %TRUE if the character has zero width
406 g_unichar_iszerowidth (gunichar c)
408 if (G_UNLIKELY (c == 0x00AD))
411 if (G_UNLIKELY (ISZEROWIDTHTYPE (TYPE (c))))
414 if (G_UNLIKELY ((c >= 0x1160 && c < 0x1200) ||
427 interval_compare (const void *key, const void *elt)
429 gunichar c = GPOINTER_TO_UINT (key);
430 struct Interval *interval = (struct Interval *)elt;
432 if (c < interval->start)
434 if (c > interval->end)
442 * @c: a Unicode character
444 * Determines if a character is typically rendered in a double-width
447 * Return value: %TRUE if the character is wide
450 g_unichar_iswide (gunichar c)
452 /* sorted list of intervals of East_Asian_Width = W and F characters
453 * from Unicode 5.1.0. produced by mungling output of:
454 * grep ';[FW]\>' EastAsianWidth.txt */
455 static const struct Interval wide[] = {
456 {0x1100, 0x1159}, {0x115F, 0x115F}, {0x2329, 0x232A}, {0x2E80, 0x2E99},
457 {0x2E9B, 0x2EF3}, {0x2F00, 0x2FD5}, {0x2FF0, 0x2FFB}, {0x3000, 0x303E},
458 {0x3041, 0x3096}, {0x3099, 0x30FF}, {0x3105, 0x312D}, {0x3131, 0x318E},
459 {0x3190, 0x31B7}, {0x31C0, 0x31E3}, {0x31F0, 0x321E}, {0x3220, 0x3243},
460 {0x3250, 0x32FE}, {0x3300, 0x4DB5}, {0x4E00, 0x9FC3}, {0xA000, 0xA48C},
461 {0xA490, 0xA4C6}, {0xAC00, 0xD7A3}, {0xF900, 0xFA2D}, {0xFA30, 0xFA6A},
462 {0xFA70, 0xFAD9}, {0xFE10, 0xFE19}, {0xFE30, 0xFE52}, {0xFE54, 0xFE66},
463 {0xFE68, 0xFE6B}, {0xFF01, 0xFF60}, {0xFFE0, 0xFFE6}, {0x20000, 0x2FFFD},
467 if (bsearch (GUINT_TO_POINTER (c), wide, G_N_ELEMENTS (wide), sizeof wide[0],
476 * g_unichar_iswide_cjk:
477 * @c: a Unicode character
479 * Determines if a character is typically rendered in a double-width
480 * cell under legacy East Asian locales. If a character is wide according to
481 * g_unichar_iswide(), then it is also reported wide with this function, but
482 * the converse is not necessarily true. See the
483 * <ulink url="http://www.unicode.org/reports/tr11/">Unicode Standard
484 * Annex #11</ulink> for details.
486 * If a character passes the g_unichar_iswide() test then it will also pass
487 * this test, but not the other way around. Note that some characters may
488 * pas both this test and g_unichar_iszerowidth().
490 * Return value: %TRUE if the character is wide in legacy East Asian locales
495 g_unichar_iswide_cjk (gunichar c)
497 /* sorted list of intervals of East_Asian_Width = A and F characters
498 * from Unicode 5.1.0. produced by mungling output of:
499 * grep ';[A]\>' EastAsianWidth.txt */
500 static const struct Interval ambiguous[] = {
501 {0x00A1, 0x00A1}, {0x00A4, 0x00A4}, {0x00A7, 0x00A8}, {0x00AA, 0x00AA},
502 {0x00AD, 0x00AE}, {0x00B0, 0x00B4}, {0x00B6, 0x00BA}, {0x00BC, 0x00BF},
503 {0x00C6, 0x00C6}, {0x00D0, 0x00D0}, {0x00D7, 0x00D8}, {0x00DE, 0x00E1},
504 {0x00E6, 0x00E6}, {0x00E8, 0x00EA}, {0x00EC, 0x00ED}, {0x00F0, 0x00F0},
505 {0x00F2, 0x00F3}, {0x00F7, 0x00FA}, {0x00FC, 0x00FC}, {0x00FE, 0x00FE},
506 {0x0101, 0x0101}, {0x0111, 0x0111}, {0x0113, 0x0113}, {0x011B, 0x011B},
507 {0x0126, 0x0127}, {0x012B, 0x012B}, {0x0131, 0x0133}, {0x0138, 0x0138},
508 {0x013F, 0x0142}, {0x0144, 0x0144}, {0x0148, 0x014B}, {0x014D, 0x014D},
509 {0x0152, 0x0153}, {0x0166, 0x0167}, {0x016B, 0x016B}, {0x01CE, 0x01CE},
510 {0x01D0, 0x01D0}, {0x01D2, 0x01D2}, {0x01D4, 0x01D4}, {0x01D6, 0x01D6},
511 {0x01D8, 0x01D8}, {0x01DA, 0x01DA}, {0x01DC, 0x01DC}, {0x0251, 0x0251},
512 {0x0261, 0x0261}, {0x02C4, 0x02C4}, {0x02C7, 0x02C7}, {0x02C9, 0x02CB},
513 {0x02CD, 0x02CD}, {0x02D0, 0x02D0}, {0x02D8, 0x02DB}, {0x02DD, 0x02DD},
514 {0x02DF, 0x02DF}, {0x0300, 0x036F}, {0x0391, 0x03A1}, {0x03A3, 0x03A9},
515 {0x03B1, 0x03C1}, {0x03C3, 0x03C9}, {0x0401, 0x0401}, {0x0410, 0x044F},
516 {0x0451, 0x0451}, {0x2010, 0x2010}, {0x2013, 0x2016}, {0x2018, 0x2019},
517 {0x201C, 0x201D}, {0x2020, 0x2022}, {0x2024, 0x2027}, {0x2030, 0x2030},
518 {0x2032, 0x2033}, {0x2035, 0x2035}, {0x203B, 0x203B}, {0x203E, 0x203E},
519 {0x2074, 0x2074}, {0x207F, 0x207F}, {0x2081, 0x2084}, {0x20AC, 0x20AC},
520 {0x2103, 0x2103}, {0x2105, 0x2105}, {0x2109, 0x2109}, {0x2113, 0x2113},
521 {0x2116, 0x2116}, {0x2121, 0x2122}, {0x2126, 0x2126}, {0x212B, 0x212B},
522 {0x2153, 0x2154}, {0x215B, 0x215E}, {0x2160, 0x216B}, {0x2170, 0x2179},
523 {0x2190, 0x2199}, {0x21B8, 0x21B9}, {0x21D2, 0x21D2}, {0x21D4, 0x21D4},
524 {0x21E7, 0x21E7}, {0x2200, 0x2200}, {0x2202, 0x2203}, {0x2207, 0x2208},
525 {0x220B, 0x220B}, {0x220F, 0x220F}, {0x2211, 0x2211}, {0x2215, 0x2215},
526 {0x221A, 0x221A}, {0x221D, 0x2220}, {0x2223, 0x2223}, {0x2225, 0x2225},
527 {0x2227, 0x222C}, {0x222E, 0x222E}, {0x2234, 0x2237}, {0x223C, 0x223D},
528 {0x2248, 0x2248}, {0x224C, 0x224C}, {0x2252, 0x2252}, {0x2260, 0x2261},
529 {0x2264, 0x2267}, {0x226A, 0x226B}, {0x226E, 0x226F}, {0x2282, 0x2283},
530 {0x2286, 0x2287}, {0x2295, 0x2295}, {0x2299, 0x2299}, {0x22A5, 0x22A5},
531 {0x22BF, 0x22BF}, {0x2312, 0x2312}, {0x2460, 0x24E9}, {0x24EB, 0x254B},
532 {0x2550, 0x2573}, {0x2580, 0x258F}, {0x2592, 0x2595}, {0x25A0, 0x25A1},
533 {0x25A3, 0x25A9}, {0x25B2, 0x25B3}, {0x25B6, 0x25B7}, {0x25BC, 0x25BD},
534 {0x25C0, 0x25C1}, {0x25C6, 0x25C8}, {0x25CB, 0x25CB}, {0x25CE, 0x25D1},
535 {0x25E2, 0x25E5}, {0x25EF, 0x25EF}, {0x2605, 0x2606}, {0x2609, 0x2609},
536 {0x260E, 0x260F}, {0x2614, 0x2615}, {0x261C, 0x261C}, {0x261E, 0x261E},
537 {0x2640, 0x2640}, {0x2642, 0x2642}, {0x2660, 0x2661}, {0x2663, 0x2665},
538 {0x2667, 0x266A}, {0x266C, 0x266D}, {0x266F, 0x266F}, {0x273D, 0x273D},
539 {0x2776, 0x277F}, {0xE000, 0xF8FF}, {0xFE00, 0xFE0F}, {0xFFFD, 0xFFFD},
540 {0xE0100, 0xE01EF}, {0xF0000, 0xFFFFD}, {0x100000, 0x10FFFD}
543 if (g_unichar_iswide (c))
546 if (bsearch (GUINT_TO_POINTER (c), ambiguous, G_N_ELEMENTS (ambiguous), sizeof ambiguous[0],
556 * @c: a Unicode character
558 * Converts a character to uppercase.
560 * Return value: the result of converting @c to uppercase.
561 * If @c is not an lowercase or titlecase character,
562 * or has no upper case equivalent @c is returned unchanged.
565 g_unichar_toupper (gunichar c)
568 if (t == G_UNICODE_LOWERCASE_LETTER)
570 gunichar val = ATTTABLE (c >> 8, c & 0xff);
571 if (val >= 0x1000000)
573 const gchar *p = special_case_table + val - 0x1000000;
574 val = g_utf8_get_char (p);
576 /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
577 * do not have an uppercase equivalent, in which case val will be
580 return val ? val : c;
582 else if (t == G_UNICODE_TITLECASE_LETTER)
585 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
587 if (title_table[i][0] == c)
588 return title_table[i][1];
596 * @c: a Unicode character.
598 * Converts a character to lower case.
600 * Return value: the result of converting @c to lower case.
601 * If @c is not an upperlower or titlecase character,
602 * or has no lowercase equivalent @c is returned unchanged.
605 g_unichar_tolower (gunichar c)
608 if (t == G_UNICODE_UPPERCASE_LETTER)
610 gunichar val = ATTTABLE (c >> 8, c & 0xff);
611 if (val >= 0x1000000)
613 const gchar *p = special_case_table + val - 0x1000000;
614 return g_utf8_get_char (p);
618 /* Not all uppercase letters are guaranteed to have a lowercase
619 * equivalent. If this is the case, val will be zero. */
620 return val ? val : c;
623 else if (t == G_UNICODE_TITLECASE_LETTER)
626 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
628 if (title_table[i][0] == c)
629 return title_table[i][2];
637 * @c: a Unicode character
639 * Converts a character to the titlecase.
641 * Return value: the result of converting @c to titlecase.
642 * If @c is not an uppercase or lowercase character,
643 * @c is returned unchanged.
646 g_unichar_totitle (gunichar c)
649 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
651 if (title_table[i][0] == c || title_table[i][1] == c
652 || title_table[i][2] == c)
653 return title_table[i][0];
656 if (TYPE (c) == G_UNICODE_LOWERCASE_LETTER)
657 return g_unichar_toupper (c);
663 * g_unichar_digit_value:
664 * @c: a Unicode character
666 * Determines the numeric value of a character as a decimal
669 * Return value: If @c is a decimal digit (according to
670 * g_unichar_isdigit()), its numeric value. Otherwise, -1.
673 g_unichar_digit_value (gunichar c)
675 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
676 return ATTTABLE (c >> 8, c & 0xff);
681 * g_unichar_xdigit_value:
682 * @c: a Unicode character
684 * Determines the numeric value of a character as a hexidecimal
687 * Return value: If @c is a hex digit (according to
688 * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
691 g_unichar_xdigit_value (gunichar c)
693 if (c >= 'A' && c <= 'F')
695 if (c >= 'a' && c <= 'f')
697 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
698 return ATTTABLE (c >> 8, c & 0xff);
704 * @c: a Unicode character
706 * Classifies a Unicode character by type.
708 * Return value: the type of the character.
711 g_unichar_type (gunichar c)
717 * Case mapping functions
727 get_locale_type (void)
730 char *tem = g_win32_getlocale ();
737 const char *locale = setlocale (LC_CTYPE, NULL);
743 if (locale[1] == 'z')
744 return LOCALE_TURKIC;
747 if (locale[1] == 't')
748 return LOCALE_LITHUANIAN;
751 if (locale[1] == 'r')
752 return LOCALE_TURKIC;
756 return LOCALE_NORMAL;
760 output_marks (const char **p_inout,
764 const char *p = *p_inout;
769 gunichar c = g_utf8_get_char (p);
771 if (ISMARK (TYPE (c)))
773 if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
774 len += g_unichar_to_utf8 (c, out_buffer ? out_buffer + len : NULL);
775 p = g_utf8_next_char (p);
786 output_special_case (gchar *out_buffer,
791 const gchar *p = special_case_table + offset;
794 if (type != G_UNICODE_TITLECASE_LETTER)
795 p = g_utf8_next_char (p);
802 memcpy (out_buffer, p, len);
808 real_toupper (const gchar *str,
811 LocaleType locale_type)
813 const gchar *p = str;
814 const char *last = NULL;
816 gboolean last_was_i = FALSE;
818 while ((max_len < 0 || p < str + max_len) && *p)
820 gunichar c = g_utf8_get_char (p);
825 p = g_utf8_next_char (p);
827 if (locale_type == LOCALE_LITHUANIAN)
835 /* Nasty, need to remove any dot above. Though
836 * I think only E WITH DOT ABOVE occurs in practice
837 * which could simplify this considerably.
842 decomp = g_unicode_canonical_decomposition (c, &decomp_len);
843 for (i=0; i < decomp_len; i++)
845 if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
846 len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
850 len += output_marks (&p, out_buffer ? out_buffer + len : NULL, TRUE);
860 if (locale_type == LOCALE_TURKIC && c == 'i')
862 /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
863 len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL);
865 else if (c == 0x0345) /* COMBINING GREEK YPOGEGRAMMENI */
867 /* Nasty, need to move it after other combining marks .. this would go away if
868 * we normalized first.
870 len += output_marks (&p, out_buffer ? out_buffer + len : NULL, FALSE);
872 /* And output as GREEK CAPITAL LETTER IOTA */
873 len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);
876 OR (G_UNICODE_LOWERCASE_LETTER,
877 OR (G_UNICODE_TITLECASE_LETTER,
880 val = ATTTABLE (c >> 8, c & 0xff);
882 if (val >= 0x1000000)
884 len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t,
885 t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
889 if (t == G_UNICODE_TITLECASE_LETTER)
892 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
894 if (title_table[i][0] == c)
896 val = title_table[i][1];
902 /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
903 * do not have an uppercase equivalent, in which case val will be
905 len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
910 gsize char_len = g_utf8_skip[*(guchar *)last];
913 memcpy (out_buffer + len, last, char_len);
925 * @str: a UTF-8 encoded string
926 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
928 * Converts all Unicode characters in the string that have a case
929 * to uppercase. The exact manner that this is done depends
930 * on the current locale, and may result in the number of
931 * characters in the string increasing. (For instance, the
932 * German ess-zet will be changed to SS.)
934 * Return value: a newly allocated string, with all characters
935 * converted to uppercase.
938 g_utf8_strup (const gchar *str,
942 LocaleType locale_type;
945 g_return_val_if_fail (str != NULL, NULL);
947 locale_type = get_locale_type ();
950 * We use a two pass approach to keep memory management simple
952 result_len = real_toupper (str, len, NULL, locale_type);
953 result = g_malloc (result_len + 1);
954 real_toupper (str, len, result, locale_type);
955 result[result_len] = '\0';
960 /* traverses the string checking for characters with combining class == 230
961 * until a base character is found */
963 has_more_above (const gchar *str)
965 const gchar *p = str;
966 gint combining_class;
970 combining_class = g_unichar_combining_class (g_utf8_get_char (p));
971 if (combining_class == 230)
973 else if (combining_class == 0)
976 p = g_utf8_next_char (p);
983 real_tolower (const gchar *str,
986 LocaleType locale_type)
988 const gchar *p = str;
989 const char *last = NULL;
992 while ((max_len < 0 || p < str + max_len) && *p)
994 gunichar c = g_utf8_get_char (p);
999 p = g_utf8_next_char (p);
1001 if (locale_type == LOCALE_TURKIC && c == 'I')
1003 if (g_utf8_get_char (p) == 0x0307)
1005 /* I + COMBINING DOT ABOVE => i (U+0069) */
1006 len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL);
1007 p = g_utf8_next_char (p);
1011 /* I => LATIN SMALL LETTER DOTLESS I */
1012 len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL);
1015 /* Introduce an explicit dot above when lowercasing capital I's and J's
1016 * whenever there are more accents above. [SpecialCasing.txt] */
1017 else if (locale_type == LOCALE_LITHUANIAN &&
1018 (c == 0x00cc || c == 0x00cd || c == 0x0128))
1020 len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL);
1021 len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL);
1026 len += g_unichar_to_utf8 (0x0300, out_buffer ? out_buffer + len : NULL);
1029 len += g_unichar_to_utf8 (0x0301, out_buffer ? out_buffer + len : NULL);
1032 len += g_unichar_to_utf8 (0x0303, out_buffer ? out_buffer + len : NULL);
1036 else if (locale_type == LOCALE_LITHUANIAN &&
1037 (c == 'I' || c == 'J' || c == 0x012e) &&
1040 len += g_unichar_to_utf8 (g_unichar_tolower (c), out_buffer ? out_buffer + len : NULL);
1041 len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL);
1043 else if (c == 0x03A3) /* GREEK CAPITAL LETTER SIGMA */
1045 if ((max_len < 0 || p < str + max_len) && *p)
1047 gunichar next_c = g_utf8_get_char (p);
1048 int next_type = TYPE(next_c);
1050 /* SIGMA mapps differently depending on whether it is
1051 * final or not. The following simplified test would
1052 * fail in the case of combining marks following the
1053 * sigma, but I don't think that occurs in real text.
1054 * The test here matches that in ICU.
1056 if (ISALPHA (next_type)) /* Lu,Ll,Lt,Lm,Lo */
1057 val = 0x3c3; /* GREEK SMALL SIGMA */
1059 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
1062 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
1064 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
1067 OR (G_UNICODE_UPPERCASE_LETTER,
1068 OR (G_UNICODE_TITLECASE_LETTER,
1071 val = ATTTABLE (c >> 8, c & 0xff);
1073 if (val >= 0x1000000)
1075 len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t, 0);
1079 if (t == G_UNICODE_TITLECASE_LETTER)
1082 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
1084 if (title_table[i][0] == c)
1086 val = title_table[i][2];
1092 /* Not all uppercase letters are guaranteed to have a lowercase
1093 * equivalent. If this is the case, val will be zero. */
1094 len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
1099 gsize char_len = g_utf8_skip[*(guchar *)last];
1102 memcpy (out_buffer + len, last, char_len);
1114 * @str: a UTF-8 encoded string
1115 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1117 * Converts all Unicode characters in the string that have a case
1118 * to lowercase. The exact manner that this is done depends
1119 * on the current locale, and may result in the number of
1120 * characters in the string changing.
1122 * Return value: a newly allocated string, with all characters
1123 * converted to lowercase.
1126 g_utf8_strdown (const gchar *str,
1130 LocaleType locale_type;
1133 g_return_val_if_fail (str != NULL, NULL);
1135 locale_type = get_locale_type ();
1138 * We use a two pass approach to keep memory management simple
1140 result_len = real_tolower (str, len, NULL, locale_type);
1141 result = g_malloc (result_len + 1);
1142 real_tolower (str, len, result, locale_type);
1143 result[result_len] = '\0';
1150 * @str: a UTF-8 encoded string
1151 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1153 * Converts a string into a form that is independent of case. The
1154 * result will not correspond to any particular case, but can be
1155 * compared for equality or ordered with the results of calling
1156 * g_utf8_casefold() on other strings.
1158 * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
1159 * only an approximation to the correct linguistic case insensitive
1160 * ordering, though it is a fairly good one. Getting this exactly
1161 * right would require a more sophisticated collation function that
1162 * takes case sensitivity into account. GLib does not currently
1163 * provide such a function.
1165 * Return value: a newly allocated string, that is a
1166 * case independent form of @str.
1169 g_utf8_casefold (const gchar *str,
1175 g_return_val_if_fail (str != NULL, NULL);
1177 result = g_string_new (NULL);
1179 while ((len < 0 || p < str + len) && *p)
1181 gunichar ch = g_utf8_get_char (p);
1184 int end = G_N_ELEMENTS (casefold_table);
1186 if (ch >= casefold_table[start].ch &&
1187 ch <= casefold_table[end - 1].ch)
1191 int half = (start + end) / 2;
1192 if (ch == casefold_table[half].ch)
1194 g_string_append (result, casefold_table[half].data);
1197 else if (half == start)
1199 else if (ch > casefold_table[half].ch)
1206 g_string_append_unichar (result, g_unichar_tolower (ch));
1209 p = g_utf8_next_char (p);
1212 return g_string_free (result, FALSE);
1216 * g_unichar_get_mirror_char:
1217 * @ch: a Unicode character
1218 * @mirrored_ch: location to store the mirrored character
1220 * In Unicode, some characters are <firstterm>mirrored</firstterm>. This
1221 * means that their images are mirrored horizontally in text that is laid
1222 * out from right to left. For instance, "(" would become its mirror image,
1223 * ")", in right-to-left text.
1225 * If @ch has the Unicode mirrored property and there is another unicode
1226 * character that typically has a glyph that is the mirror image of @ch's
1227 * glyph and @mirrored_ch is set, it puts that character in the address
1228 * pointed to by @mirrored_ch. Otherwise the original character is put.
1230 * Return value: %TRUE if @ch has a mirrored character, %FALSE otherwise
1235 g_unichar_get_mirror_char (gunichar ch,
1236 gunichar *mirrored_ch)
1241 mirrored = GLIB_GET_MIRRORING(ch);
1243 found = ch != mirrored;
1245 *mirrored_ch = mirrored;
1251 #define G_SCRIPT_TABLE_MIDPOINT (G_N_ELEMENTS (g_script_table) / 2)
1253 static inline GUnicodeScript
1254 g_unichar_get_script_bsearch (gunichar ch)
1257 int upper = G_N_ELEMENTS (g_script_table) - 1;
1258 static int saved_mid = G_SCRIPT_TABLE_MIDPOINT;
1259 int mid = saved_mid;
1264 if (ch < g_script_table[mid].start)
1266 else if (ch >= g_script_table[mid].start + g_script_table[mid].chars)
1269 return g_script_table[saved_mid = mid].script;
1271 mid = (lower + upper) / 2;
1273 while (lower <= upper);
1275 return G_UNICODE_SCRIPT_UNKNOWN;
1279 * g_unichar_get_script:
1280 * @ch: a Unicode character
1282 * Looks up the #GUnicodeScript for a particular character (as defined
1283 * by Unicode Standard Annex #24). No check is made for @ch being a
1284 * valid Unicode character; if you pass in invalid character, the
1285 * result is undefined.
1287 * This function is equivalent to pango_script_for_unichar() and the
1288 * two are interchangeable.
1290 * Return value: the #GUnicodeScript for the character.
1295 g_unichar_get_script (gunichar ch)
1297 if (ch < G_EASY_SCRIPTS_RANGE)
1298 return g_script_easy_table[ch];
1300 return g_unichar_get_script_bsearch (ch);