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.
23 #include "gunichartables.h"
31 #define ATTTABLE(Page, Char) \
32 ((attr_table[Page] == 0) ? 0 : (attr_table[Page][Char]))
34 /* We cheat a bit and cast type values to (char *). We detect these
35 using the &0xff trick. */
36 #define TTYPE(Page, Char) \
37 (((GPOINTER_TO_INT(type_table[Page]) & 0xff) == GPOINTER_TO_INT(type_table[Page])) \
38 ? GPOINTER_TO_INT(type_table[Page]) \
39 : (type_table[Page][Char]))
41 #define TYPE(Char) (((Char) > (G_UNICODE_LAST_CHAR)) ? G_UNICODE_UNASSIGNED : TTYPE ((Char) >> 8, (Char) & 0xff))
43 #define ISDIGIT(Type) ((Type) == G_UNICODE_DECIMAL_NUMBER \
44 || (Type) == G_UNICODE_LETTER_NUMBER \
45 || (Type) == G_UNICODE_OTHER_NUMBER)
47 #define ISALPHA(Type) ((Type) == G_UNICODE_LOWERCASE_LETTER \
48 || (Type) == G_UNICODE_UPPERCASE_LETTER \
49 || (Type) == G_UNICODE_TITLECASE_LETTER \
50 || (Type) == G_UNICODE_MODIFIER_LETTER \
51 || (Type) == G_UNICODE_OTHER_LETTER)
53 #define ISMARK(Type) ((Type) == G_UNICODE_NON_SPACING_MARK || \
54 (Type) == G_UNICODE_COMBINING_MARK || \
55 (Type) == G_UNICODE_ENCLOSING_MARK)
60 * @c: a Unicode character
62 * Determines whether a character is alphanumeric.
63 * Given some UTF-8 text, obtain a character value
64 * with g_utf8_get_char().
66 * Return value: %TRUE if @c is an alphanumeric character
69 g_unichar_isalnum (gunichar c)
72 return ISDIGIT (t) || ISALPHA (t);
77 * @c: a Unicode character
79 * Determines whether a character is alphabetic (i.e. a letter).
80 * Given some UTF-8 text, obtain a character value with
83 * Return value: %TRUE if @c is an alphabetic character
86 g_unichar_isalpha (gunichar c)
95 * @c: a Unicode character
97 * Determines whether a character is a control character.
98 * Given some UTF-8 text, obtain a character value with
101 * Return value: %TRUE if @c is a control character
104 g_unichar_iscntrl (gunichar c)
106 return TYPE (c) == G_UNICODE_CONTROL;
111 * @c: a Unicode character
113 * Determines whether a character is numeric (i.e. a digit). This
114 * covers ASCII 0-9 and also digits in other languages/scripts. Given
115 * some UTF-8 text, obtain a character value with g_utf8_get_char().
117 * Return value: %TRUE if @c is a digit
120 g_unichar_isdigit (gunichar c)
122 return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
128 * @c: a Unicode character
130 * Determines whether a character is printable and not a space
131 * (returns %FALSE for control characters, format characters, and
132 * spaces). g_unichar_isprint() is similar, but returns %TRUE for
133 * spaces. Given some UTF-8 text, obtain a character value with
136 * Return value: %TRUE if @c is printable unless it's a space
139 g_unichar_isgraph (gunichar c)
142 return (t != G_UNICODE_CONTROL
143 && t != G_UNICODE_FORMAT
144 && t != G_UNICODE_UNASSIGNED
145 && t != G_UNICODE_PRIVATE_USE
146 && t != G_UNICODE_SURROGATE
147 && t != G_UNICODE_SPACE_SEPARATOR);
152 * @c: a Unicode character
154 * Determines whether a character is a lowercase letter.
155 * Given some UTF-8 text, obtain a character value with
158 * Return value: %TRUE if @c is a lowercase letter
161 g_unichar_islower (gunichar c)
163 return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
169 * @c: a Unicode character
171 * Determines whether a character is printable.
172 * Unlike g_unichar_isgraph(), returns %TRUE for spaces.
173 * Given some UTF-8 text, obtain a character value with
176 * Return value: %TRUE if @c is printable
179 g_unichar_isprint (gunichar c)
182 return (t != G_UNICODE_CONTROL
183 && t != G_UNICODE_FORMAT
184 && t != G_UNICODE_UNASSIGNED
185 && t != G_UNICODE_PRIVATE_USE
186 && t != G_UNICODE_SURROGATE);
191 * @c: a Unicode character
193 * Determines whether a character is punctuation or a symbol.
194 * Given some UTF-8 text, obtain a character value with
197 * Return value: %TRUE if @c is a punctuation or symbol character
200 g_unichar_ispunct (gunichar c)
203 return (t == G_UNICODE_CONNECT_PUNCTUATION || t == G_UNICODE_DASH_PUNCTUATION
204 || t == G_UNICODE_CLOSE_PUNCTUATION || t == G_UNICODE_FINAL_PUNCTUATION
205 || t == G_UNICODE_INITIAL_PUNCTUATION || t == G_UNICODE_OTHER_PUNCTUATION
206 || t == G_UNICODE_OPEN_PUNCTUATION || t == G_UNICODE_CURRENCY_SYMBOL
207 || t == G_UNICODE_MODIFIER_SYMBOL || t == G_UNICODE_MATH_SYMBOL
208 || t == G_UNICODE_OTHER_SYMBOL);
213 * @c: a Unicode character
215 * Determines whether a character is a space, tab, or line separator
216 * (newline, carriage return, etc.). Given some UTF-8 text, obtain a
217 * character value with g_utf8_get_char().
219 * (Note: don't use this to do word breaking; you have to use
220 * Pango or equivalent to get word breaking right, the algorithm
221 * is fairly complex.)
223 * Return value: %TRUE if @c is a punctuation character
226 g_unichar_isspace (gunichar c)
230 /* special-case these since Unicode thinks they are not spaces */
241 return (t == G_UNICODE_SPACE_SEPARATOR || t == G_UNICODE_LINE_SEPARATOR
242 || t == G_UNICODE_PARAGRAPH_SEPARATOR);
250 * @c: a Unicode character
252 * Determines if a character is uppercase.
254 * Return value: %TRUE if @c is an uppercase character
257 g_unichar_isupper (gunichar c)
259 return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
264 * @c: a Unicode character
266 * Determines if a character is titlecase. Some characters in
267 * Unicode which are composites, such as the DZ digraph
268 * have three case variants instead of just two. The titlecase
269 * form is used at the beginning of a word where only the
270 * first letter is capitalized. The titlecase form of the DZ
271 * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z.
273 * Return value: %TRUE if the character is titlecase
276 g_unichar_istitle (gunichar c)
279 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
280 if (title_table[i][0] == c)
286 * g_unichar_isxdigit:
287 * @c: a Unicode character.
289 * Determines if a character is a hexidecimal digit.
291 * Return value: %TRUE if the character is a hexadecimal digit
294 g_unichar_isxdigit (gunichar c)
297 return ((c >= 'a' && c <= 'f')
298 || (c >= 'A' && c <= 'F')
303 * g_unichar_isdefined:
304 * @c: a Unicode character
306 * Determines if a given character is assigned in the Unicode
309 * Return value: %TRUE if the character has an assigned value
312 g_unichar_isdefined (gunichar c)
315 return t != G_UNICODE_UNASSIGNED;
320 * @c: a Unicode character
322 * Determines if a character is typically rendered in a double-width
325 * Return value: %TRUE if the character is wide
327 /* This function stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>. */
329 g_unichar_iswide (gunichar c)
334 return ((c >= 0x1100 && c <= 0x115f) /* Hangul Jamo */
335 || (c >= 0x2e80 && c <= 0xa4cf && (c & ~0x0011) != 0x300a &&
336 c != 0x303f) /* CJK ... Yi */
337 || (c >= 0xac00 && c <= 0xd7a3) /* Hangul Syllables */
338 || (c >= 0xf900 && c <= 0xfaff) /* CJK Compatibility Ideographs */
339 || (c >= 0xfe30 && c <= 0xfe6f) /* CJK Compatibility Forms */
340 || (c >= 0xff00 && c <= 0xff5f) /* Fullwidth Forms */
341 || (c >= 0xffe0 && c <= 0xffe6));
346 * @c: a Unicode character
348 * Converts a character to uppercase.
350 * Return value: the result of converting @c to uppercase.
351 * If @c is not an lowercase or titlecase character,
352 * @c is returned unchanged.
355 g_unichar_toupper (gunichar c)
358 if (t == G_UNICODE_LOWERCASE_LETTER)
360 gunichar val = ATTTABLE (c >> 8, c & 0xff);
361 if (val >= 0xd800 && val < 0xdc00)
363 const guchar *p = special_case_table[val - 0xd800];
364 return p[0] * 256 + p[1];
369 else if (t == G_UNICODE_TITLECASE_LETTER)
372 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
374 if (title_table[i][0] == c)
375 return title_table[i][1];
383 * @c: a Unicode character.
385 * Converts a character to lower case.
387 * Return value: the result of converting @c to lower case.
388 * If @c is not an upperlower or titlecase character,
389 * @c is returned unchanged.
392 g_unichar_tolower (gunichar c)
395 if (t == G_UNICODE_UPPERCASE_LETTER)
397 gunichar val = ATTTABLE (c >> 8, c & 0xff);
398 if (val >= 0xd800 && val < 0xdc00)
400 const guchar *p = special_case_table[val - 0xd800];
401 return p[0] * 256 + p[1];
406 else if (t == G_UNICODE_TITLECASE_LETTER)
409 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
411 if (title_table[i][0] == c)
412 return title_table[i][2];
420 * @c: a Unicode character
422 * Converts a character to the titlecase.
424 * Return value: the result of converting @c to titlecase.
425 * If @c is not an uppercase or lowercase character,
426 * @c is returned unchanged.
429 g_unichar_totitle (gunichar c)
432 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
434 if (title_table[i][0] == c || title_table[i][1] == c
435 || title_table[i][2] == c)
436 return title_table[i][0];
438 return (TYPE (c) == G_UNICODE_LOWERCASE_LETTER
439 ? ATTTABLE (c >> 8, c & 0xff)
444 * g_unichar_digit_value:
445 * @c: a Unicode character
447 * Determines the numeric value of a character as a decimal
450 * Return value: If @c is a decimal digit (according to
451 * g_unichar_isdigit()), its numeric value. Otherwise, -1.
454 g_unichar_digit_value (gunichar c)
456 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
457 return ATTTABLE (c >> 8, c & 0xff);
462 * g_unichar_xdigit_value:
463 * @c: a Unicode character
465 * Determines the numeric value of a character as a hexidecimal
468 * Return value: If @c is a hex digit (according to
469 * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
472 g_unichar_xdigit_value (gunichar c)
474 if (c >= 'A' && c <= 'F')
476 if (c >= 'a' && c <= 'f')
478 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
479 return ATTTABLE (c >> 8, c & 0xff);
485 * @c: a Unicode character
487 * Classifies a Unicode character by type.
489 * Return value: the type of the character.
492 g_unichar_type (gunichar c)
498 * Case mapping functions
508 get_locale_type (void)
510 const char *locale = setlocale (LC_CTYPE, NULL);
515 if (locale[1] == 'z')
516 return LOCALE_TURKIC;
519 if (locale[1] == 't')
520 return LOCALE_LITHUANIAN;
523 if (locale[1] == 'r')
524 return LOCALE_TURKIC;
528 return LOCALE_NORMAL;
532 output_marks (const char **p_inout,
537 const char *p = *p_inout;
541 gunichar c = g_utf8_get_char (p);
546 if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
547 len += g_unichar_to_utf8 (c, out_buffer ? out_buffer + len : NULL);
548 p = g_utf8_next_char (p);
559 output_special_case (gchar *out_buffer,
565 const guchar *p = special_case_table[index];
567 if (type != G_UNICODE_TITLECASE_LETTER)
568 p += 2; /* +2 to skip over "best single match" */
579 gunichar ch = p[0] * 256 + p[1];
583 len += g_unichar_to_utf8 (ch, out_buffer ? out_buffer + len : NULL);
591 real_toupper (const gchar *str,
594 LocaleType locale_type)
596 const gchar *p = str;
597 const char *last = NULL;
599 gboolean last_was_i = FALSE;
601 while ((max_len < 0 || p < str + max_len) && *p)
603 gunichar c = g_utf8_get_char (p);
608 p = g_utf8_next_char (p);
610 if (locale_type == LOCALE_LITHUANIAN)
618 /* Nasty, need to remove any dot above. Though
619 * I think only E WITH DOT ABOVE occurs in practice
620 * which could simplify this considerably.
625 decomp = g_unicode_canonical_decomposition (c, &decomp_len);
626 for (i=0; i < decomp_len; i++)
628 if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
629 len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
633 len = output_marks (&p, out_buffer, len, TRUE);
643 if (locale_type == LOCALE_TURKIC && c == 'i')
645 /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
646 len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL);
648 else if (c == 0x0345) /* COMBINING GREEK YPOGEGRAMMENI */
650 /* Nasty, need to move it after other combining marks .. this would go away if
651 * we normalized first.
653 len = output_marks (&p, out_buffer, len, FALSE);
655 /* And output as GREEK CAPITAL LETTER IOTA */
656 len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);
658 else if (t == G_UNICODE_LOWERCASE_LETTER || t == G_UNICODE_TITLECASE_LETTER)
660 val = ATTTABLE (c >> 8, c & 0xff);
662 if (val >= 0xd800 && val < 0xdc00)
664 len += output_special_case (out_buffer, len, val - 0xd800, t,
665 t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
669 if (t == G_UNICODE_TITLECASE_LETTER)
672 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
674 if (title_table[i][0] == c)
675 val = title_table[i][1];
679 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
684 gsize char_len = g_utf8_skip[*(guchar *)last];
687 memcpy (out_buffer + len, last, char_len);
699 * @str: a UTF-8 encoded string
700 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
702 * Converts all Unicode characters in the string that have a case
703 * to uppercase. The exact manner that this is done depends
704 * on the current locale, and may result in the number of
705 * characters in the string increasing. (For instance, the
706 * German ess-zet will be changed to SS.)
708 * Return value: a newly allocated string, with all characters
709 * converted to uppercase.
712 g_utf8_strup (const gchar *str,
716 LocaleType locale_type;
719 g_return_val_if_fail (str != NULL, NULL);
721 locale_type = get_locale_type ();
724 * We use a two pass approach to keep memory management simple
726 result_len = real_toupper (str, len, NULL, locale_type);
727 result = g_malloc (result_len + 1);
728 real_toupper (str, len, result, locale_type);
729 result[result_len] = '\0';
735 real_tolower (const gchar *str,
738 LocaleType locale_type)
740 const gchar *p = str;
741 const char *last = NULL;
744 while ((max_len < 0 || p < str + max_len) && *p)
746 gunichar c = g_utf8_get_char (p);
751 p = g_utf8_next_char (p);
753 if (locale_type == LOCALE_TURKIC && c == 'I')
755 /* I => LATIN SMALL LETTER DOTLESS I */
756 len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL);
758 else if (c == 0x03A3) /* GREEK CAPITAL LETTER SIGMA */
760 gunichar next_c = g_utf8_get_char (p);
761 int next_t = TYPE(next_c);
763 /* SIGMA mapps differently depending on whether it is
764 * final or not. The following simplified test would
765 * fail in the case of combining marks following the
766 * sigma, but I don't think that occurs in real text.
767 * The test here matches that in ICU.
769 if (ISALPHA(next_t)) /* Lu,Ll,Lt,Lm,Lo */
770 val = 0x3c3; /* GREEK SMALL SIGMA */
772 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
774 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
776 else if (t == G_UNICODE_UPPERCASE_LETTER || t == G_UNICODE_TITLECASE_LETTER)
778 val = ATTTABLE (c >> 8, c & 0xff);
780 if (val >= 0xd800 && val < 0xdc00)
782 len += output_special_case (out_buffer, len, val - 0xd800, t, 0);
786 if (t == G_UNICODE_TITLECASE_LETTER)
789 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
791 if (title_table[i][0] == c)
792 val = title_table[i][2];
796 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
801 gsize char_len = g_utf8_skip[*(guchar *)last];
804 memcpy (out_buffer + len, last, char_len);
816 * @str: a UTF-8 encoded string
817 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
819 * Converts all Unicode characters in the string that have a case
820 * to lowercase. The exact manner that this is done depends
821 * on the current locale, and may result in the number of
822 * characters in the string changing.
824 * Return value: a newly allocated string, with all characters
825 * converted to lowercase.
828 g_utf8_strdown (const gchar *str,
832 LocaleType locale_type;
835 g_return_val_if_fail (str != NULL, NULL);
837 locale_type = get_locale_type ();
840 * We use a two pass approach to keep memory management simple
842 result_len = real_tolower (str, len, NULL, locale_type);
843 result = g_malloc (result_len + 1);
844 real_tolower (str, len, result, locale_type);
845 result[result_len] = '\0';
852 * @str: a UTF-8 encoded string
853 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
855 * Converts a string into a form that is independent of case. The
856 * result will not correspond to any particular case, but can be
857 * compared for equality or ordered with the results of calling
858 * g_utf8_casefold() on other strings.
860 * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
861 * only an approximation to the correct linguistic case insensitive
862 * ordering, though it is a fairly good one. Getting this exactly
863 * right would require a more sophisticated collation function that
864 * takes case sensitivity into account. GLib does not currently
865 * provide such a function.
867 * Return value: a newly allocated string, that is a
868 * case independent form of @str.
871 g_utf8_casefold (const gchar *str,
874 GString *result = g_string_new (NULL);
878 while ((len < 0 || p < str + len) && *p)
880 gunichar ch = g_utf8_get_char (p);
883 int end = G_N_ELEMENTS (casefold_table);
885 if (ch >= casefold_table[start].ch &&
886 ch <= casefold_table[end - 1].ch)
890 int half = (start + end) / 2;
891 if (ch == casefold_table[half].ch)
893 g_string_append (result, casefold_table[half].data);
896 else if (half == start)
898 else if (ch > casefold_table[half].ch)
905 g_string_append_unichar (result, g_unichar_tolower (ch));
908 p = g_utf8_next_char (p);
911 return g_string_free (result, FALSE);