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"
36 #define ATTR_TABLE(Page) (((Page) <= G_UNICODE_LAST_PAGE_PART1) \
37 ? attr_table_part1[Page] \
38 : attr_table_part2[(Page) - 0xe00])
40 #define ATTTABLE(Page, Char) \
41 ((ATTR_TABLE(Page) == G_UNICODE_MAX_TABLE_INDEX) ? 0 : (attr_data[ATTR_TABLE(Page)][Char]))
43 #define TTYPE_PART1(Page, Char) \
44 ((type_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
45 ? (type_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
46 : (type_data[type_table_part1[Page]][Char]))
48 #define TTYPE_PART2(Page, Char) \
49 ((type_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
50 ? (type_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
51 : (type_data[type_table_part2[Page]][Char]))
54 (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
55 ? TTYPE_PART1 ((Char) >> 8, (Char) & 0xff) \
56 : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
57 ? TTYPE_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
58 : G_UNICODE_UNASSIGNED))
61 #define IS(Type, Class) (((guint)1 << (Type)) & (Class))
62 #define OR(Type, Rest) (((guint)1 << (Type)) | (Rest))
66 #define ISALPHA(Type) IS ((Type), \
67 OR (G_UNICODE_LOWERCASE_LETTER, \
68 OR (G_UNICODE_UPPERCASE_LETTER, \
69 OR (G_UNICODE_TITLECASE_LETTER, \
70 OR (G_UNICODE_MODIFIER_LETTER, \
71 OR (G_UNICODE_OTHER_LETTER, 0))))))
73 #define ISALDIGIT(Type) IS ((Type), \
74 OR (G_UNICODE_DECIMAL_NUMBER, \
75 OR (G_UNICODE_LETTER_NUMBER, \
76 OR (G_UNICODE_OTHER_NUMBER, \
77 OR (G_UNICODE_LOWERCASE_LETTER, \
78 OR (G_UNICODE_UPPERCASE_LETTER, \
79 OR (G_UNICODE_TITLECASE_LETTER, \
80 OR (G_UNICODE_MODIFIER_LETTER, \
81 OR (G_UNICODE_OTHER_LETTER, 0)))))))))
83 #define ISMARK(Type) IS ((Type), \
84 OR (G_UNICODE_NON_SPACING_MARK, \
85 OR (G_UNICODE_COMBINING_MARK, \
86 OR (G_UNICODE_ENCLOSING_MARK, 0))))
88 #define ISZEROWIDTHTYPE(Type) IS ((Type), \
89 OR (G_UNICODE_NON_SPACING_MARK, \
90 OR (G_UNICODE_ENCLOSING_MARK, \
91 OR (G_UNICODE_FORMAT, 0))))
95 * @c: a Unicode character
97 * Determines whether a character is alphanumeric.
98 * Given some UTF-8 text, obtain a character value
99 * with g_utf8_get_char().
101 * Return value: %TRUE if @c is an alphanumeric character
104 g_unichar_isalnum (gunichar c)
106 return ISALDIGIT (TYPE (c)) ? TRUE : FALSE;
111 * @c: a Unicode character
113 * Determines whether a character is alphabetic (i.e. a letter).
114 * Given some UTF-8 text, obtain a character value with
117 * Return value: %TRUE if @c is an alphabetic character
120 g_unichar_isalpha (gunichar c)
122 return ISALPHA (TYPE (c)) ? TRUE : FALSE;
128 * @c: a Unicode character
130 * Determines whether a character is a control character.
131 * Given some UTF-8 text, obtain a character value with
134 * Return value: %TRUE if @c is a control character
137 g_unichar_iscntrl (gunichar c)
139 return TYPE (c) == G_UNICODE_CONTROL;
144 * @c: a Unicode character
146 * Determines whether a character is numeric (i.e. a digit). This
147 * covers ASCII 0-9 and also digits in other languages/scripts. Given
148 * some UTF-8 text, obtain a character value with g_utf8_get_char().
150 * Return value: %TRUE if @c is a digit
153 g_unichar_isdigit (gunichar c)
155 return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
161 * @c: a Unicode character
163 * Determines whether a character is printable and not a space
164 * (returns %FALSE for control characters, format characters, and
165 * spaces). g_unichar_isprint() is similar, but returns %TRUE for
166 * spaces. Given some UTF-8 text, obtain a character value with
169 * Return value: %TRUE if @c is printable unless it's a space
172 g_unichar_isgraph (gunichar c)
175 OR (G_UNICODE_CONTROL,
176 OR (G_UNICODE_FORMAT,
177 OR (G_UNICODE_UNASSIGNED,
178 OR (G_UNICODE_SURROGATE,
179 OR (G_UNICODE_SPACE_SEPARATOR,
185 * @c: a Unicode character
187 * Determines whether a character is a lowercase letter.
188 * Given some UTF-8 text, obtain a character value with
191 * Return value: %TRUE if @c is a lowercase letter
194 g_unichar_islower (gunichar c)
196 return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
202 * @c: a Unicode character
204 * Determines whether a character is printable.
205 * Unlike g_unichar_isgraph(), returns %TRUE for spaces.
206 * Given some UTF-8 text, obtain a character value with
209 * Return value: %TRUE if @c is printable
212 g_unichar_isprint (gunichar c)
215 OR (G_UNICODE_CONTROL,
216 OR (G_UNICODE_FORMAT,
217 OR (G_UNICODE_UNASSIGNED,
218 OR (G_UNICODE_SURROGATE,
224 * @c: a Unicode character
226 * Determines whether a character is punctuation or a symbol.
227 * Given some UTF-8 text, obtain a character value with
230 * Return value: %TRUE if @c is a punctuation or symbol character
233 g_unichar_ispunct (gunichar c)
236 OR (G_UNICODE_CONNECT_PUNCTUATION,
237 OR (G_UNICODE_DASH_PUNCTUATION,
238 OR (G_UNICODE_CLOSE_PUNCTUATION,
239 OR (G_UNICODE_FINAL_PUNCTUATION,
240 OR (G_UNICODE_INITIAL_PUNCTUATION,
241 OR (G_UNICODE_OTHER_PUNCTUATION,
242 OR (G_UNICODE_OPEN_PUNCTUATION,
243 OR (G_UNICODE_CURRENCY_SYMBOL,
244 OR (G_UNICODE_MODIFIER_SYMBOL,
245 OR (G_UNICODE_MATH_SYMBOL,
246 OR (G_UNICODE_OTHER_SYMBOL,
247 0)))))))))))) ? TRUE : FALSE;
252 * @c: a Unicode character
254 * Determines whether a character is a space, tab, or line separator
255 * (newline, carriage return, etc.). Given some UTF-8 text, obtain a
256 * character value with g_utf8_get_char().
258 * (Note: don't use this to do word breaking; you have to use
259 * Pango or equivalent to get word breaking right, the algorithm
260 * is fairly complex.)
262 * Return value: %TRUE if @c is a space character
265 g_unichar_isspace (gunichar c)
269 /* special-case these since Unicode thinks they are not spaces */
280 OR (G_UNICODE_SPACE_SEPARATOR,
281 OR (G_UNICODE_LINE_SEPARATOR,
282 OR (G_UNICODE_PARAGRAPH_SEPARATOR,
283 0)))) ? TRUE : FALSE;
291 * @c: a Unicode character
293 * Determines whether a character is a mark (non-spacing mark,
294 * combining mark, or enclosing mark in Unicode speak).
295 * Given some UTF-8 text, obtain a character value
296 * with g_utf8_get_char().
298 * Note: in most cases where isalpha characters are allowed,
299 * ismark characters should be allowed to as they are essential
300 * for writing most European languages as well as many non-Latin
303 * Return value: %TRUE if @c is a mark character
308 g_unichar_ismark (gunichar c)
310 return ISMARK (TYPE (c));
315 * @c: a Unicode character
317 * Determines if a character is uppercase.
319 * Return value: %TRUE if @c is an uppercase character
322 g_unichar_isupper (gunichar c)
324 return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
329 * @c: a Unicode character
331 * Determines if a character is titlecase. Some characters in
332 * Unicode which are composites, such as the DZ digraph
333 * have three case variants instead of just two. The titlecase
334 * form is used at the beginning of a word where only the
335 * first letter is capitalized. The titlecase form of the DZ
336 * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z.
338 * Return value: %TRUE if the character is titlecase
341 g_unichar_istitle (gunichar c)
344 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
345 if (title_table[i][0] == c)
351 * g_unichar_isxdigit:
352 * @c: a Unicode character.
354 * Determines if a character is a hexidecimal digit.
356 * Return value: %TRUE if the character is a hexadecimal digit
359 g_unichar_isxdigit (gunichar c)
361 return ((c >= 'a' && c <= 'f')
362 || (c >= 'A' && c <= 'F')
363 || (TYPE (c) == G_UNICODE_DECIMAL_NUMBER));
367 * g_unichar_isdefined:
368 * @c: a Unicode character
370 * Determines if a given character is assigned in the Unicode
373 * Return value: %TRUE if the character has an assigned value
376 g_unichar_isdefined (gunichar c)
379 OR (G_UNICODE_UNASSIGNED,
380 OR (G_UNICODE_SURROGATE,
385 * g_unichar_iszerowidth:
386 * @c: a Unicode character
388 * Determines if a given character typically takes zero width when rendered.
389 * The return value is %TRUE for all non-spacing and enclosing marks
390 * (e.g., combining accents), format characters, zero-width
391 * space, but not U+00AD SOFT HYPHEN.
393 * A typical use of this function is with one of g_unichar_iswide() or
394 * g_unichar_iswide_cjk() to determine the number of cells a string occupies
395 * when displayed on a grid display (terminals). However, note that not all
396 * terminals support zero-width rendering of zero-width marks.
398 * Return value: %TRUE if the character has zero width
403 g_unichar_iszerowidth (gunichar c)
405 if (G_UNLIKELY (c == 0x00AD))
408 if (G_UNLIKELY (ISZEROWIDTHTYPE (TYPE (c))))
411 if (G_UNLIKELY ((c >= 0x1160 && c < 0x1200) ||
424 interval_compare (const void *key, const void *elt)
426 gunichar c = GPOINTER_TO_UINT (key);
427 struct Interval *interval = (struct Interval *)elt;
429 if (c < interval->start)
431 if (c > interval->end)
439 * @c: a Unicode character
441 * Determines if a character is typically rendered in a double-width
444 * Return value: %TRUE if the character is wide
447 g_unichar_iswide (gunichar c)
449 /* sorted list of intervals of East_Asian_Width = W and F characters
450 * from Unicode 5.1.0. produced by mungling output of:
451 * grep ';[FW]\>' EastAsianWidth.txt */
452 static const struct Interval wide[] = {
453 {0x1100, 0x1159}, {0x115F, 0x115F}, {0x2329, 0x232A}, {0x2E80, 0x2E99},
454 {0x2E9B, 0x2EF3}, {0x2F00, 0x2FD5}, {0x2FF0, 0x2FFB}, {0x3000, 0x303E},
455 {0x3041, 0x3096}, {0x3099, 0x30FF}, {0x3105, 0x312D}, {0x3131, 0x318E},
456 {0x3190, 0x31B7}, {0x31C0, 0x31E3}, {0x31F0, 0x321E}, {0x3220, 0x3243},
457 {0x3250, 0x32FE}, {0x3300, 0x4DB5}, {0x4E00, 0x9FC3}, {0xA000, 0xA48C},
458 {0xA490, 0xA4C6}, {0xAC00, 0xD7A3}, {0xF900, 0xFA2D}, {0xFA30, 0xFA6A},
459 {0xFA70, 0xFAD9}, {0xFE10, 0xFE19}, {0xFE30, 0xFE52}, {0xFE54, 0xFE66},
460 {0xFE68, 0xFE6B}, {0xFF01, 0xFF60}, {0xFFE0, 0xFFE6}, {0x20000, 0x2FFFD},
464 if (bsearch (GUINT_TO_POINTER (c), wide, G_N_ELEMENTS (wide), sizeof wide[0],
473 * g_unichar_iswide_cjk:
474 * @c: a Unicode character
476 * Determines if a character is typically rendered in a double-width
477 * cell under legacy East Asian locales. If a character is wide according to
478 * g_unichar_iswide(), then it is also reported wide with this function, but
479 * the converse is not necessarily true. See the
480 * <ulink url="http://www.unicode.org/reports/tr11/">Unicode Standard
481 * Annex #11</ulink> for details.
483 * Return value: %TRUE if the character is wide in legacy East Asian locales
488 g_unichar_iswide_cjk (gunichar c)
490 /* sorted list of intervals of East_Asian_Width = A and F characters
491 * from Unicode 5.1.0. produced by mungling output of:
492 * grep ';[A]\>' EastAsianWidth.txt */
493 static const struct Interval ambiguous[] = {
494 {0x00A1, 0x00A1}, {0x00A4, 0x00A4}, {0x00A7, 0x00A8}, {0x00AA, 0x00AA},
495 {0x00AD, 0x00AE}, {0x00B0, 0x00B4}, {0x00B6, 0x00BA}, {0x00BC, 0x00BF},
496 {0x00C6, 0x00C6}, {0x00D0, 0x00D0}, {0x00D7, 0x00D8}, {0x00DE, 0x00E1},
497 {0x00E6, 0x00E6}, {0x00E8, 0x00EA}, {0x00EC, 0x00ED}, {0x00F0, 0x00F0},
498 {0x00F2, 0x00F3}, {0x00F7, 0x00FA}, {0x00FC, 0x00FC}, {0x00FE, 0x00FE},
499 {0x0101, 0x0101}, {0x0111, 0x0111}, {0x0113, 0x0113}, {0x011B, 0x011B},
500 {0x0126, 0x0127}, {0x012B, 0x012B}, {0x0131, 0x0133}, {0x0138, 0x0138},
501 {0x013F, 0x0142}, {0x0144, 0x0144}, {0x0148, 0x014B}, {0x014D, 0x014D},
502 {0x0152, 0x0153}, {0x0166, 0x0167}, {0x016B, 0x016B}, {0x01CE, 0x01CE},
503 {0x01D0, 0x01D0}, {0x01D2, 0x01D2}, {0x01D4, 0x01D4}, {0x01D6, 0x01D6},
504 {0x01D8, 0x01D8}, {0x01DA, 0x01DA}, {0x01DC, 0x01DC}, {0x0251, 0x0251},
505 {0x0261, 0x0261}, {0x02C4, 0x02C4}, {0x02C7, 0x02C7}, {0x02C9, 0x02CB},
506 {0x02CD, 0x02CD}, {0x02D0, 0x02D0}, {0x02D8, 0x02DB}, {0x02DD, 0x02DD},
507 {0x02DF, 0x02DF}, {0x0300, 0x036F}, {0x0391, 0x03A1}, {0x03A3, 0x03A9},
508 {0x03B1, 0x03C1}, {0x03C3, 0x03C9}, {0x0401, 0x0401}, {0x0410, 0x044F},
509 {0x0451, 0x0451}, {0x2010, 0x2010}, {0x2013, 0x2016}, {0x2018, 0x2019},
510 {0x201C, 0x201D}, {0x2020, 0x2022}, {0x2024, 0x2027}, {0x2030, 0x2030},
511 {0x2032, 0x2033}, {0x2035, 0x2035}, {0x203B, 0x203B}, {0x203E, 0x203E},
512 {0x2074, 0x2074}, {0x207F, 0x207F}, {0x2081, 0x2084}, {0x20AC, 0x20AC},
513 {0x2103, 0x2103}, {0x2105, 0x2105}, {0x2109, 0x2109}, {0x2113, 0x2113},
514 {0x2116, 0x2116}, {0x2121, 0x2122}, {0x2126, 0x2126}, {0x212B, 0x212B},
515 {0x2153, 0x2154}, {0x215B, 0x215E}, {0x2160, 0x216B}, {0x2170, 0x2179},
516 {0x2190, 0x2199}, {0x21B8, 0x21B9}, {0x21D2, 0x21D2}, {0x21D4, 0x21D4},
517 {0x21E7, 0x21E7}, {0x2200, 0x2200}, {0x2202, 0x2203}, {0x2207, 0x2208},
518 {0x220B, 0x220B}, {0x220F, 0x220F}, {0x2211, 0x2211}, {0x2215, 0x2215},
519 {0x221A, 0x221A}, {0x221D, 0x2220}, {0x2223, 0x2223}, {0x2225, 0x2225},
520 {0x2227, 0x222C}, {0x222E, 0x222E}, {0x2234, 0x2237}, {0x223C, 0x223D},
521 {0x2248, 0x2248}, {0x224C, 0x224C}, {0x2252, 0x2252}, {0x2260, 0x2261},
522 {0x2264, 0x2267}, {0x226A, 0x226B}, {0x226E, 0x226F}, {0x2282, 0x2283},
523 {0x2286, 0x2287}, {0x2295, 0x2295}, {0x2299, 0x2299}, {0x22A5, 0x22A5},
524 {0x22BF, 0x22BF}, {0x2312, 0x2312}, {0x2460, 0x24E9}, {0x24EB, 0x254B},
525 {0x2550, 0x2573}, {0x2580, 0x258F}, {0x2592, 0x2595}, {0x25A0, 0x25A1},
526 {0x25A3, 0x25A9}, {0x25B2, 0x25B3}, {0x25B6, 0x25B7}, {0x25BC, 0x25BD},
527 {0x25C0, 0x25C1}, {0x25C6, 0x25C8}, {0x25CB, 0x25CB}, {0x25CE, 0x25D1},
528 {0x25E2, 0x25E5}, {0x25EF, 0x25EF}, {0x2605, 0x2606}, {0x2609, 0x2609},
529 {0x260E, 0x260F}, {0x2614, 0x2615}, {0x261C, 0x261C}, {0x261E, 0x261E},
530 {0x2640, 0x2640}, {0x2642, 0x2642}, {0x2660, 0x2661}, {0x2663, 0x2665},
531 {0x2667, 0x266A}, {0x266C, 0x266D}, {0x266F, 0x266F}, {0x273D, 0x273D},
532 {0x2776, 0x277F}, {0xE000, 0xF8FF}, {0xFE00, 0xFE0F}, {0xFFFD, 0xFFFD},
533 {0xE0100, 0xE01EF}, {0xF0000, 0xFFFFD}, {0x100000, 0x10FFFD}
536 if (g_unichar_iswide (c))
539 if (bsearch (GUINT_TO_POINTER (c), ambiguous, G_N_ELEMENTS (ambiguous), sizeof ambiguous[0],
549 * @c: a Unicode character
551 * Converts a character to uppercase.
553 * Return value: the result of converting @c to uppercase.
554 * If @c is not an lowercase or titlecase character,
555 * or has no upper case equivalent @c is returned unchanged.
558 g_unichar_toupper (gunichar c)
561 if (t == G_UNICODE_LOWERCASE_LETTER)
563 gunichar val = ATTTABLE (c >> 8, c & 0xff);
564 if (val >= 0x1000000)
566 const gchar *p = special_case_table + val - 0x1000000;
567 val = g_utf8_get_char (p);
569 /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
570 * do not have an uppercase equivalent, in which case val will be
573 return val ? val : c;
575 else if (t == G_UNICODE_TITLECASE_LETTER)
578 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
580 if (title_table[i][0] == c)
581 return title_table[i][1];
589 * @c: a Unicode character.
591 * Converts a character to lower case.
593 * Return value: the result of converting @c to lower case.
594 * If @c is not an upperlower or titlecase character,
595 * or has no lowercase equivalent @c is returned unchanged.
598 g_unichar_tolower (gunichar c)
601 if (t == G_UNICODE_UPPERCASE_LETTER)
603 gunichar val = ATTTABLE (c >> 8, c & 0xff);
604 if (val >= 0x1000000)
606 const gchar *p = special_case_table + val - 0x1000000;
607 return g_utf8_get_char (p);
611 /* Not all uppercase letters are guaranteed to have a lowercase
612 * equivalent. If this is the case, val will be zero. */
613 return val ? val : c;
616 else if (t == G_UNICODE_TITLECASE_LETTER)
619 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
621 if (title_table[i][0] == c)
622 return title_table[i][2];
630 * @c: a Unicode character
632 * Converts a character to the titlecase.
634 * Return value: the result of converting @c to titlecase.
635 * If @c is not an uppercase or lowercase character,
636 * @c is returned unchanged.
639 g_unichar_totitle (gunichar c)
642 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
644 if (title_table[i][0] == c || title_table[i][1] == c
645 || title_table[i][2] == c)
646 return title_table[i][0];
649 if (TYPE (c) == G_UNICODE_LOWERCASE_LETTER)
650 return g_unichar_toupper (c);
656 * g_unichar_digit_value:
657 * @c: a Unicode character
659 * Determines the numeric value of a character as a decimal
662 * Return value: If @c is a decimal digit (according to
663 * g_unichar_isdigit()), its numeric value. Otherwise, -1.
666 g_unichar_digit_value (gunichar c)
668 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
669 return ATTTABLE (c >> 8, c & 0xff);
674 * g_unichar_xdigit_value:
675 * @c: a Unicode character
677 * Determines the numeric value of a character as a hexidecimal
680 * Return value: If @c is a hex digit (according to
681 * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
684 g_unichar_xdigit_value (gunichar c)
686 if (c >= 'A' && c <= 'F')
688 if (c >= 'a' && c <= 'f')
690 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
691 return ATTTABLE (c >> 8, c & 0xff);
697 * @c: a Unicode character
699 * Classifies a Unicode character by type.
701 * Return value: the type of the character.
704 g_unichar_type (gunichar c)
710 * Case mapping functions
720 get_locale_type (void)
723 char *tem = g_win32_getlocale ();
730 const char *locale = setlocale (LC_CTYPE, NULL);
736 if (locale[1] == 'z')
737 return LOCALE_TURKIC;
740 if (locale[1] == 't')
741 return LOCALE_LITHUANIAN;
744 if (locale[1] == 'r')
745 return LOCALE_TURKIC;
749 return LOCALE_NORMAL;
753 output_marks (const char **p_inout,
757 const char *p = *p_inout;
762 gunichar c = g_utf8_get_char (p);
764 if (ISMARK (TYPE (c)))
766 if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
767 len += g_unichar_to_utf8 (c, out_buffer ? out_buffer + len : NULL);
768 p = g_utf8_next_char (p);
779 output_special_case (gchar *out_buffer,
784 const gchar *p = special_case_table + offset;
787 if (type != G_UNICODE_TITLECASE_LETTER)
788 p = g_utf8_next_char (p);
795 memcpy (out_buffer, p, len);
801 real_toupper (const gchar *str,
804 LocaleType locale_type)
806 const gchar *p = str;
807 const char *last = NULL;
809 gboolean last_was_i = FALSE;
811 while ((max_len < 0 || p < str + max_len) && *p)
813 gunichar c = g_utf8_get_char (p);
818 p = g_utf8_next_char (p);
820 if (locale_type == LOCALE_LITHUANIAN)
828 /* Nasty, need to remove any dot above. Though
829 * I think only E WITH DOT ABOVE occurs in practice
830 * which could simplify this considerably.
835 decomp = g_unicode_canonical_decomposition (c, &decomp_len);
836 for (i=0; i < decomp_len; i++)
838 if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
839 len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
843 len += output_marks (&p, out_buffer ? out_buffer + len : NULL, TRUE);
853 if (locale_type == LOCALE_TURKIC && c == 'i')
855 /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
856 len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL);
858 else if (c == 0x0345) /* COMBINING GREEK YPOGEGRAMMENI */
860 /* Nasty, need to move it after other combining marks .. this would go away if
861 * we normalized first.
863 len += output_marks (&p, out_buffer ? out_buffer + len : NULL, FALSE);
865 /* And output as GREEK CAPITAL LETTER IOTA */
866 len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);
869 OR (G_UNICODE_LOWERCASE_LETTER,
870 OR (G_UNICODE_TITLECASE_LETTER,
873 val = ATTTABLE (c >> 8, c & 0xff);
875 if (val >= 0x1000000)
877 len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t,
878 t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
882 if (t == G_UNICODE_TITLECASE_LETTER)
885 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
887 if (title_table[i][0] == c)
889 val = title_table[i][1];
895 /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
896 * do not have an uppercase equivalent, in which case val will be
898 len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
903 gsize char_len = g_utf8_skip[*(guchar *)last];
906 memcpy (out_buffer + len, last, char_len);
918 * @str: a UTF-8 encoded string
919 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
921 * Converts all Unicode characters in the string that have a case
922 * to uppercase. The exact manner that this is done depends
923 * on the current locale, and may result in the number of
924 * characters in the string increasing. (For instance, the
925 * German ess-zet will be changed to SS.)
927 * Return value: a newly allocated string, with all characters
928 * converted to uppercase.
931 g_utf8_strup (const gchar *str,
935 LocaleType locale_type;
938 g_return_val_if_fail (str != NULL, NULL);
940 locale_type = get_locale_type ();
943 * We use a two pass approach to keep memory management simple
945 result_len = real_toupper (str, len, NULL, locale_type);
946 result = g_malloc (result_len + 1);
947 real_toupper (str, len, result, locale_type);
948 result[result_len] = '\0';
953 /* traverses the string checking for characters with combining class == 230
954 * until a base character is found */
956 has_more_above (const gchar *str)
958 const gchar *p = str;
959 gint combining_class;
963 combining_class = g_unichar_combining_class (g_utf8_get_char (p));
964 if (combining_class == 230)
966 else if (combining_class == 0)
969 p = g_utf8_next_char (p);
976 real_tolower (const gchar *str,
979 LocaleType locale_type)
981 const gchar *p = str;
982 const char *last = NULL;
985 while ((max_len < 0 || p < str + max_len) && *p)
987 gunichar c = g_utf8_get_char (p);
992 p = g_utf8_next_char (p);
994 if (locale_type == LOCALE_TURKIC && c == 'I')
996 if (g_utf8_get_char (p) == 0x0307)
998 /* I + COMBINING DOT ABOVE => i (U+0069) */
999 len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL);
1000 p = g_utf8_next_char (p);
1004 /* I => LATIN SMALL LETTER DOTLESS I */
1005 len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL);
1008 /* Introduce an explicit dot above when lowercasing capital I's and J's
1009 * whenever there are more accents above. [SpecialCasing.txt] */
1010 else if (locale_type == LOCALE_LITHUANIAN &&
1011 (c == 0x00cc || c == 0x00cd || c == 0x0128))
1013 len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL);
1014 len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL);
1019 len += g_unichar_to_utf8 (0x0300, out_buffer ? out_buffer + len : NULL);
1022 len += g_unichar_to_utf8 (0x0301, out_buffer ? out_buffer + len : NULL);
1025 len += g_unichar_to_utf8 (0x0303, out_buffer ? out_buffer + len : NULL);
1029 else if (locale_type == LOCALE_LITHUANIAN &&
1030 (c == 'I' || c == 'J' || c == 0x012e) &&
1033 len += g_unichar_to_utf8 (g_unichar_tolower (c), out_buffer ? out_buffer + len : NULL);
1034 len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL);
1036 else if (c == 0x03A3) /* GREEK CAPITAL LETTER SIGMA */
1038 if ((max_len < 0 || p < str + max_len) && *p)
1040 gunichar next_c = g_utf8_get_char (p);
1041 int next_type = TYPE(next_c);
1043 /* SIGMA mapps differently depending on whether it is
1044 * final or not. The following simplified test would
1045 * fail in the case of combining marks following the
1046 * sigma, but I don't think that occurs in real text.
1047 * The test here matches that in ICU.
1049 if (ISALPHA (next_type)) /* Lu,Ll,Lt,Lm,Lo */
1050 val = 0x3c3; /* GREEK SMALL SIGMA */
1052 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
1055 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
1057 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
1060 OR (G_UNICODE_UPPERCASE_LETTER,
1061 OR (G_UNICODE_TITLECASE_LETTER,
1064 val = ATTTABLE (c >> 8, c & 0xff);
1066 if (val >= 0x1000000)
1068 len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t, 0);
1072 if (t == G_UNICODE_TITLECASE_LETTER)
1075 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
1077 if (title_table[i][0] == c)
1079 val = title_table[i][2];
1085 /* Not all uppercase letters are guaranteed to have a lowercase
1086 * equivalent. If this is the case, val will be zero. */
1087 len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
1092 gsize char_len = g_utf8_skip[*(guchar *)last];
1095 memcpy (out_buffer + len, last, char_len);
1107 * @str: a UTF-8 encoded string
1108 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1110 * Converts all Unicode characters in the string that have a case
1111 * to lowercase. The exact manner that this is done depends
1112 * on the current locale, and may result in the number of
1113 * characters in the string changing.
1115 * Return value: a newly allocated string, with all characters
1116 * converted to lowercase.
1119 g_utf8_strdown (const gchar *str,
1123 LocaleType locale_type;
1126 g_return_val_if_fail (str != NULL, NULL);
1128 locale_type = get_locale_type ();
1131 * We use a two pass approach to keep memory management simple
1133 result_len = real_tolower (str, len, NULL, locale_type);
1134 result = g_malloc (result_len + 1);
1135 real_tolower (str, len, result, locale_type);
1136 result[result_len] = '\0';
1143 * @str: a UTF-8 encoded string
1144 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1146 * Converts a string into a form that is independent of case. The
1147 * result will not correspond to any particular case, but can be
1148 * compared for equality or ordered with the results of calling
1149 * g_utf8_casefold() on other strings.
1151 * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
1152 * only an approximation to the correct linguistic case insensitive
1153 * ordering, though it is a fairly good one. Getting this exactly
1154 * right would require a more sophisticated collation function that
1155 * takes case sensitivity into account. GLib does not currently
1156 * provide such a function.
1158 * Return value: a newly allocated string, that is a
1159 * case independent form of @str.
1162 g_utf8_casefold (const gchar *str,
1168 g_return_val_if_fail (str != NULL, NULL);
1170 result = g_string_new (NULL);
1172 while ((len < 0 || p < str + len) && *p)
1174 gunichar ch = g_utf8_get_char (p);
1177 int end = G_N_ELEMENTS (casefold_table);
1179 if (ch >= casefold_table[start].ch &&
1180 ch <= casefold_table[end - 1].ch)
1184 int half = (start + end) / 2;
1185 if (ch == casefold_table[half].ch)
1187 g_string_append (result, casefold_table[half].data);
1190 else if (half == start)
1192 else if (ch > casefold_table[half].ch)
1199 g_string_append_unichar (result, g_unichar_tolower (ch));
1202 p = g_utf8_next_char (p);
1205 return g_string_free (result, FALSE);
1209 * g_unichar_get_mirror_char:
1210 * @ch: a Unicode character
1211 * @mirrored_ch: location to store the mirrored character
1213 * In Unicode, some characters are <firstterm>mirrored</firstterm>. This
1214 * means that their images are mirrored horizontally in text that is laid
1215 * out from right to left. For instance, "(" would become its mirror image,
1216 * ")", in right-to-left text.
1218 * If @ch has the Unicode mirrored property and there is another unicode
1219 * character that typically has a glyph that is the mirror image of @ch's
1220 * glyph and @mirrored_ch is set, it puts that character in the address
1221 * pointed to by @mirrored_ch. Otherwise the original character is put.
1223 * Return value: %TRUE if @ch has a mirrored character, %FALSE otherwise
1228 g_unichar_get_mirror_char (gunichar ch,
1229 gunichar *mirrored_ch)
1234 mirrored = GLIB_GET_MIRRORING(ch);
1236 found = ch != mirrored;
1238 *mirrored_ch = mirrored;
1244 #define G_SCRIPT_TABLE_MIDPOINT (G_N_ELEMENTS (g_script_table) / 2)
1246 static inline GUnicodeScript
1247 g_unichar_get_script_bsearch (gunichar ch)
1250 int upper = G_N_ELEMENTS (g_script_table) - 1;
1251 static int saved_mid = G_SCRIPT_TABLE_MIDPOINT;
1252 int mid = saved_mid;
1257 if (ch < g_script_table[mid].start)
1259 else if (ch >= g_script_table[mid].start + g_script_table[mid].chars)
1262 return g_script_table[saved_mid = mid].script;
1264 mid = (lower + upper) / 2;
1266 while (lower <= upper);
1268 return G_UNICODE_SCRIPT_UNKNOWN;
1272 * g_unichar_get_script:
1273 * @ch: a Unicode character
1275 * Looks up the #GUnicodeScript for a particular character (as defined
1276 * by Unicode Standard Annex #24). No check is made for @ch being a
1277 * valid Unicode character; if you pass in invalid character, the
1278 * result is undefined.
1280 * This function is equivalent to pango_script_for_unichar() and the
1281 * two are interchangeable.
1283 * Return value: the #GUnicodeScript for the character.
1288 g_unichar_get_script (gunichar ch)
1290 if (ch < G_EASY_SCRIPTS_RANGE)
1291 return g_script_easy_table[ch];
1293 return g_unichar_get_script_bsearch (ch);
1297 #define __G_UNIPROP_C__
1298 #include "galiasdef.c"