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.
25 #include "gunichartables.h"
31 #define ATTTABLE(Page, Char) \
32 ((attr_table[Page] == G_UNICODE_MAX_TABLE_INDEX) ? 0 : (attr_data[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 ((type_table[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
38 ? (type_table[Page] - G_UNICODE_MAX_TABLE_INDEX) \
39 : (type_data[type_table[Page]][Char]))
42 #define TYPE(Char) (((Char) > (G_UNICODE_LAST_CHAR)) ? G_UNICODE_UNASSIGNED : TTYPE ((Char) >> 8, (Char) & 0xff))
44 #define ISDIGIT(Type) ((Type) == G_UNICODE_DECIMAL_NUMBER \
45 || (Type) == G_UNICODE_LETTER_NUMBER \
46 || (Type) == G_UNICODE_OTHER_NUMBER)
48 #define ISALPHA(Type) ((Type) == G_UNICODE_LOWERCASE_LETTER \
49 || (Type) == G_UNICODE_UPPERCASE_LETTER \
50 || (Type) == G_UNICODE_TITLECASE_LETTER \
51 || (Type) == G_UNICODE_MODIFIER_LETTER \
52 || (Type) == G_UNICODE_OTHER_LETTER)
54 #define ISMARK(Type) ((Type) == G_UNICODE_NON_SPACING_MARK || \
55 (Type) == G_UNICODE_COMBINING_MARK || \
56 (Type) == G_UNICODE_ENCLOSING_MARK)
61 * @c: a Unicode character
63 * Determines whether a character is alphanumeric.
64 * Given some UTF-8 text, obtain a character value
65 * with g_utf8_get_char().
67 * Return value: %TRUE if @c is an alphanumeric character
70 g_unichar_isalnum (gunichar c)
73 return ISDIGIT (t) || ISALPHA (t);
78 * @c: a Unicode character
80 * Determines whether a character is alphabetic (i.e. a letter).
81 * Given some UTF-8 text, obtain a character value with
84 * Return value: %TRUE if @c is an alphabetic character
87 g_unichar_isalpha (gunichar c)
96 * @c: a Unicode character
98 * Determines whether a character is a control character.
99 * Given some UTF-8 text, obtain a character value with
102 * Return value: %TRUE if @c is a control character
105 g_unichar_iscntrl (gunichar c)
107 return TYPE (c) == G_UNICODE_CONTROL;
112 * @c: a Unicode character
114 * Determines whether a character is numeric (i.e. a digit). This
115 * covers ASCII 0-9 and also digits in other languages/scripts. Given
116 * some UTF-8 text, obtain a character value with g_utf8_get_char().
118 * Return value: %TRUE if @c is a digit
121 g_unichar_isdigit (gunichar c)
123 return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
129 * @c: a Unicode character
131 * Determines whether a character is printable and not a space
132 * (returns %FALSE for control characters, format characters, and
133 * spaces). g_unichar_isprint() is similar, but returns %TRUE for
134 * spaces. Given some UTF-8 text, obtain a character value with
137 * Return value: %TRUE if @c is printable unless it's a space
140 g_unichar_isgraph (gunichar c)
143 return (t != G_UNICODE_CONTROL
144 && t != G_UNICODE_FORMAT
145 && t != G_UNICODE_UNASSIGNED
146 && t != G_UNICODE_PRIVATE_USE
147 && t != G_UNICODE_SURROGATE
148 && t != G_UNICODE_SPACE_SEPARATOR);
153 * @c: a Unicode character
155 * Determines whether a character is a lowercase letter.
156 * Given some UTF-8 text, obtain a character value with
159 * Return value: %TRUE if @c is a lowercase letter
162 g_unichar_islower (gunichar c)
164 return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
170 * @c: a Unicode character
172 * Determines whether a character is printable.
173 * Unlike g_unichar_isgraph(), returns %TRUE for spaces.
174 * Given some UTF-8 text, obtain a character value with
177 * Return value: %TRUE if @c is printable
180 g_unichar_isprint (gunichar c)
183 return (t != G_UNICODE_CONTROL
184 && t != G_UNICODE_FORMAT
185 && t != G_UNICODE_UNASSIGNED
186 && t != G_UNICODE_PRIVATE_USE
187 && t != G_UNICODE_SURROGATE);
192 * @c: a Unicode character
194 * Determines whether a character is punctuation or a symbol.
195 * Given some UTF-8 text, obtain a character value with
198 * Return value: %TRUE if @c is a punctuation or symbol character
201 g_unichar_ispunct (gunichar c)
204 return (t == G_UNICODE_CONNECT_PUNCTUATION || t == G_UNICODE_DASH_PUNCTUATION
205 || t == G_UNICODE_CLOSE_PUNCTUATION || t == G_UNICODE_FINAL_PUNCTUATION
206 || t == G_UNICODE_INITIAL_PUNCTUATION || t == G_UNICODE_OTHER_PUNCTUATION
207 || t == G_UNICODE_OPEN_PUNCTUATION || t == G_UNICODE_CURRENCY_SYMBOL
208 || t == G_UNICODE_MODIFIER_SYMBOL || t == G_UNICODE_MATH_SYMBOL
209 || t == G_UNICODE_OTHER_SYMBOL);
214 * @c: a Unicode character
216 * Determines whether a character is a space, tab, or line separator
217 * (newline, carriage return, etc.). Given some UTF-8 text, obtain a
218 * character value with g_utf8_get_char().
220 * (Note: don't use this to do word breaking; you have to use
221 * Pango or equivalent to get word breaking right, the algorithm
222 * is fairly complex.)
224 * Return value: %TRUE if @c is a punctuation character
227 g_unichar_isspace (gunichar c)
231 /* special-case these since Unicode thinks they are not spaces */
242 return (t == G_UNICODE_SPACE_SEPARATOR || t == G_UNICODE_LINE_SEPARATOR
243 || t == G_UNICODE_PARAGRAPH_SEPARATOR);
251 * @c: a Unicode character
253 * Determines if a character is uppercase.
255 * Return value: %TRUE if @c is an uppercase character
258 g_unichar_isupper (gunichar c)
260 return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
265 * @c: a Unicode character
267 * Determines if a character is titlecase. Some characters in
268 * Unicode which are composites, such as the DZ digraph
269 * have three case variants instead of just two. The titlecase
270 * form is used at the beginning of a word where only the
271 * first letter is capitalized. The titlecase form of the DZ
272 * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z.
274 * Return value: %TRUE if the character is titlecase
277 g_unichar_istitle (gunichar c)
280 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
281 if (title_table[i][0] == c)
287 * g_unichar_isxdigit:
288 * @c: a Unicode character.
290 * Determines if a character is a hexidecimal digit.
292 * Return value: %TRUE if the character is a hexadecimal digit
295 g_unichar_isxdigit (gunichar c)
298 return ((c >= 'a' && c <= 'f')
299 || (c >= 'A' && c <= 'F')
304 * g_unichar_isdefined:
305 * @c: a Unicode character
307 * Determines if a given character is assigned in the Unicode
310 * Return value: %TRUE if the character has an assigned value
313 g_unichar_isdefined (gunichar c)
316 return t != G_UNICODE_UNASSIGNED;
321 * @c: a Unicode character
323 * Determines if a character is typically rendered in a double-width
326 * Return value: %TRUE if the character is wide
328 /* This function stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>. */
330 g_unichar_iswide (gunichar c)
335 return ((c >= 0x1100 && c <= 0x115f) /* Hangul Jamo */
336 || (c >= 0x2e80 && c <= 0xa4cf && (c & ~0x0011) != 0x300a &&
337 c != 0x303f) /* CJK ... Yi */
338 || (c >= 0xac00 && c <= 0xd7a3) /* Hangul Syllables */
339 || (c >= 0xf900 && c <= 0xfaff) /* CJK Compatibility Ideographs */
340 || (c >= 0xfe30 && c <= 0xfe6f) /* CJK Compatibility Forms */
341 || (c >= 0xff00 && c <= 0xff5f) /* Fullwidth Forms */
342 || (c >= 0xffe0 && c <= 0xffe6));
347 * @c: a Unicode character
349 * Converts a character to uppercase.
351 * Return value: the result of converting @c to uppercase.
352 * If @c is not an lowercase or titlecase character,
353 * or has no upper case equivalent @c is returned unchanged.
356 g_unichar_toupper (gunichar c)
359 if (t == G_UNICODE_LOWERCASE_LETTER)
361 gunichar val = ATTTABLE (c >> 8, c & 0xff);
362 if (val >= 0xd800 && val < 0xdc00)
364 const guchar *p = special_case_table[val - 0xd800];
365 return p[0] * 256 + p[1];
368 return val ? val : c;
370 else if (t == G_UNICODE_TITLECASE_LETTER)
373 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
375 if (title_table[i][0] == c)
376 return title_table[i][1];
384 * @c: a Unicode character.
386 * Converts a character to lower case.
388 * Return value: the result of converting @c to lower case.
389 * If @c is not an upperlower or titlecase character,
390 * or has no lowercase equivalent @c is returned unchanged.
393 g_unichar_tolower (gunichar c)
396 if (t == G_UNICODE_UPPERCASE_LETTER)
398 gunichar val = ATTTABLE (c >> 8, c & 0xff);
399 if (val >= 0xd800 && val < 0xdc00)
401 const guchar *p = special_case_table[val - 0xd800];
402 return p[0] * 256 + p[1];
405 return val ? val : c;
407 else if (t == G_UNICODE_TITLECASE_LETTER)
410 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
412 if (title_table[i][0] == c)
413 return title_table[i][2];
421 * @c: a Unicode character
423 * Converts a character to the titlecase.
425 * Return value: the result of converting @c to titlecase.
426 * If @c is not an uppercase or lowercase character,
427 * @c is returned unchanged.
430 g_unichar_totitle (gunichar c)
433 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
435 if (title_table[i][0] == c || title_table[i][1] == c
436 || title_table[i][2] == c)
437 return title_table[i][0];
439 return (TYPE (c) == G_UNICODE_LOWERCASE_LETTER
440 ? ATTTABLE (c >> 8, c & 0xff)
445 * g_unichar_digit_value:
446 * @c: a Unicode character
448 * Determines the numeric value of a character as a decimal
451 * Return value: If @c is a decimal digit (according to
452 * g_unichar_isdigit()), its numeric value. Otherwise, -1.
455 g_unichar_digit_value (gunichar c)
457 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
458 return ATTTABLE (c >> 8, c & 0xff);
463 * g_unichar_xdigit_value:
464 * @c: a Unicode character
466 * Determines the numeric value of a character as a hexidecimal
469 * Return value: If @c is a hex digit (according to
470 * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
473 g_unichar_xdigit_value (gunichar c)
475 if (c >= 'A' && c <= 'F')
477 if (c >= 'a' && c <= 'f')
479 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
480 return ATTTABLE (c >> 8, c & 0xff);
486 * @c: a Unicode character
488 * Classifies a Unicode character by type.
490 * Return value: the type of the character.
493 g_unichar_type (gunichar c)
499 * Case mapping functions
509 get_locale_type (void)
511 const char *locale = setlocale (LC_CTYPE, NULL);
516 if (locale[1] == 'z')
517 return LOCALE_TURKIC;
520 if (locale[1] == 't')
521 return LOCALE_LITHUANIAN;
524 if (locale[1] == 'r')
525 return LOCALE_TURKIC;
529 return LOCALE_NORMAL;
533 output_marks (const char **p_inout,
538 const char *p = *p_inout;
542 gunichar c = g_utf8_get_char (p);
547 if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
548 len += g_unichar_to_utf8 (c, out_buffer ? out_buffer + len : NULL);
549 p = g_utf8_next_char (p);
560 output_special_case (gchar *out_buffer,
566 const guchar *p = special_case_table[index];
568 if (type != G_UNICODE_TITLECASE_LETTER)
569 p += 2; /* +2 to skip over "best single match" */
580 gunichar ch = p[0] * 256 + p[1];
584 len += g_unichar_to_utf8 (ch, out_buffer ? out_buffer + len : NULL);
592 real_toupper (const gchar *str,
595 LocaleType locale_type)
597 const gchar *p = str;
598 const char *last = NULL;
600 gboolean last_was_i = FALSE;
602 while ((max_len < 0 || p < str + max_len) && *p)
604 gunichar c = g_utf8_get_char (p);
609 p = g_utf8_next_char (p);
611 if (locale_type == LOCALE_LITHUANIAN)
619 /* Nasty, need to remove any dot above. Though
620 * I think only E WITH DOT ABOVE occurs in practice
621 * which could simplify this considerably.
626 decomp = g_unicode_canonical_decomposition (c, &decomp_len);
627 for (i=0; i < decomp_len; i++)
629 if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
630 len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
634 len = output_marks (&p, out_buffer, len, TRUE);
644 if (locale_type == LOCALE_TURKIC && c == 'i')
646 /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
647 len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL);
649 else if (c == 0x0345) /* COMBINING GREEK YPOGEGRAMMENI */
651 /* Nasty, need to move it after other combining marks .. this would go away if
652 * we normalized first.
654 len = output_marks (&p, out_buffer, len, FALSE);
656 /* And output as GREEK CAPITAL LETTER IOTA */
657 len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);
659 else if (t == G_UNICODE_LOWERCASE_LETTER || t == G_UNICODE_TITLECASE_LETTER)
661 val = ATTTABLE (c >> 8, c & 0xff);
663 if (val >= 0xd800 && val < 0xdc00)
665 len += output_special_case (out_buffer, len, val - 0xd800, t,
666 t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
670 if (t == G_UNICODE_TITLECASE_LETTER)
673 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
675 if (title_table[i][0] == c)
676 val = title_table[i][1];
680 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
685 gsize char_len = g_utf8_skip[*(guchar *)last];
688 memcpy (out_buffer + len, last, char_len);
700 * @str: a UTF-8 encoded string
701 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
703 * Converts all Unicode characters in the string that have a case
704 * to uppercase. The exact manner that this is done depends
705 * on the current locale, and may result in the number of
706 * characters in the string increasing. (For instance, the
707 * German ess-zet will be changed to SS.)
709 * Return value: a newly allocated string, with all characters
710 * converted to uppercase.
713 g_utf8_strup (const gchar *str,
717 LocaleType locale_type;
720 g_return_val_if_fail (str != NULL, NULL);
722 locale_type = get_locale_type ();
725 * We use a two pass approach to keep memory management simple
727 result_len = real_toupper (str, len, NULL, locale_type);
728 result = g_malloc (result_len + 1);
729 real_toupper (str, len, result, locale_type);
730 result[result_len] = '\0';
736 real_tolower (const gchar *str,
739 LocaleType locale_type)
741 const gchar *p = str;
742 const char *last = NULL;
745 while ((max_len < 0 || p < str + max_len) && *p)
747 gunichar c = g_utf8_get_char (p);
752 p = g_utf8_next_char (p);
754 if (locale_type == LOCALE_TURKIC && c == 'I')
756 /* I => LATIN SMALL LETTER DOTLESS I */
757 len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL);
759 else if (c == 0x03A3) /* GREEK CAPITAL LETTER SIGMA */
761 gunichar next_c = g_utf8_get_char (p);
762 int next_t = TYPE(next_c);
764 /* SIGMA mapps differently depending on whether it is
765 * final or not. The following simplified test would
766 * fail in the case of combining marks following the
767 * sigma, but I don't think that occurs in real text.
768 * The test here matches that in ICU.
770 if (ISALPHA(next_t)) /* Lu,Ll,Lt,Lm,Lo */
771 val = 0x3c3; /* GREEK SMALL SIGMA */
773 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
775 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
777 else if (t == G_UNICODE_UPPERCASE_LETTER || t == G_UNICODE_TITLECASE_LETTER)
779 val = ATTTABLE (c >> 8, c & 0xff);
781 if (val >= 0xd800 && val < 0xdc00)
783 len += output_special_case (out_buffer, len, val - 0xd800, t, 0);
787 if (t == G_UNICODE_TITLECASE_LETTER)
790 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
792 if (title_table[i][0] == c)
793 val = title_table[i][2];
797 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
802 gsize char_len = g_utf8_skip[*(guchar *)last];
805 memcpy (out_buffer + len, last, char_len);
817 * @str: a UTF-8 encoded string
818 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
820 * Converts all Unicode characters in the string that have a case
821 * to lowercase. The exact manner that this is done depends
822 * on the current locale, and may result in the number of
823 * characters in the string changing.
825 * Return value: a newly allocated string, with all characters
826 * converted to lowercase.
829 g_utf8_strdown (const gchar *str,
833 LocaleType locale_type;
836 g_return_val_if_fail (str != NULL, NULL);
838 locale_type = get_locale_type ();
841 * We use a two pass approach to keep memory management simple
843 result_len = real_tolower (str, len, NULL, locale_type);
844 result = g_malloc (result_len + 1);
845 real_tolower (str, len, result, locale_type);
846 result[result_len] = '\0';
853 * @str: a UTF-8 encoded string
854 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
856 * Converts a string into a form that is independent of case. The
857 * result will not correspond to any particular case, but can be
858 * compared for equality or ordered with the results of calling
859 * g_utf8_casefold() on other strings.
861 * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
862 * only an approximation to the correct linguistic case insensitive
863 * ordering, though it is a fairly good one. Getting this exactly
864 * right would require a more sophisticated collation function that
865 * takes case sensitivity into account. GLib does not currently
866 * provide such a function.
868 * Return value: a newly allocated string, that is a
869 * case independent form of @str.
872 g_utf8_casefold (const gchar *str,
875 GString *result = g_string_new (NULL);
879 while ((len < 0 || p < str + len) && *p)
881 gunichar ch = g_utf8_get_char (p);
884 int end = G_N_ELEMENTS (casefold_table);
886 if (ch >= casefold_table[start].ch &&
887 ch <= casefold_table[end - 1].ch)
891 int half = (start + end) / 2;
892 if (ch == casefold_table[half].ch)
894 g_string_append (result, casefold_table[half].data);
897 else if (half == start)
899 else if (ch > casefold_table[half].ch)
906 g_string_append_unichar (result, g_unichar_tolower (ch));
909 p = g_utf8_next_char (p);
912 return g_string_free (result, FALSE);