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.
29 #include "gunichartables.h"
32 #define ATTTABLE(Page, Char) \
33 ((attr_table[Page] == G_UNICODE_MAX_TABLE_INDEX) ? 0 : (attr_data[attr_table[Page]][Char]))
35 /* We cheat a bit and cast type values to (char *). We detect these
36 using the &0xff trick. */
37 #define TTYPE(Page, Char) \
38 ((type_table[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
39 ? (type_table[Page] - G_UNICODE_MAX_TABLE_INDEX) \
40 : (type_data[type_table[Page]][Char]))
43 #define TYPE(Char) (((Char) > (G_UNICODE_LAST_CHAR)) ? G_UNICODE_UNASSIGNED : TTYPE ((Char) >> 8, (Char) & 0xff))
45 #define ISDIGIT(Type) ((Type) == G_UNICODE_DECIMAL_NUMBER \
46 || (Type) == G_UNICODE_LETTER_NUMBER \
47 || (Type) == G_UNICODE_OTHER_NUMBER)
49 #define ISALPHA(Type) ((Type) == G_UNICODE_LOWERCASE_LETTER \
50 || (Type) == G_UNICODE_UPPERCASE_LETTER \
51 || (Type) == G_UNICODE_TITLECASE_LETTER \
52 || (Type) == G_UNICODE_MODIFIER_LETTER \
53 || (Type) == G_UNICODE_OTHER_LETTER)
55 #define ISMARK(Type) ((Type) == G_UNICODE_NON_SPACING_MARK || \
56 (Type) == G_UNICODE_COMBINING_MARK || \
57 (Type) == G_UNICODE_ENCLOSING_MARK)
62 * @c: a Unicode character
64 * Determines whether a character is alphanumeric.
65 * Given some UTF-8 text, obtain a character value
66 * with g_utf8_get_char().
68 * Return value: %TRUE if @c is an alphanumeric character
71 g_unichar_isalnum (gunichar c)
74 return ISDIGIT (t) || ISALPHA (t);
79 * @c: a Unicode character
81 * Determines whether a character is alphabetic (i.e. a letter).
82 * Given some UTF-8 text, obtain a character value with
85 * Return value: %TRUE if @c is an alphabetic character
88 g_unichar_isalpha (gunichar c)
97 * @c: a Unicode character
99 * Determines whether a character is a control character.
100 * Given some UTF-8 text, obtain a character value with
103 * Return value: %TRUE if @c is a control character
106 g_unichar_iscntrl (gunichar c)
108 return TYPE (c) == G_UNICODE_CONTROL;
113 * @c: a Unicode character
115 * Determines whether a character is numeric (i.e. a digit). This
116 * covers ASCII 0-9 and also digits in other languages/scripts. Given
117 * some UTF-8 text, obtain a character value with g_utf8_get_char().
119 * Return value: %TRUE if @c is a digit
122 g_unichar_isdigit (gunichar c)
124 return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
130 * @c: a Unicode character
132 * Determines whether a character is printable and not a space
133 * (returns %FALSE for control characters, format characters, and
134 * spaces). g_unichar_isprint() is similar, but returns %TRUE for
135 * spaces. Given some UTF-8 text, obtain a character value with
138 * Return value: %TRUE if @c is printable unless it's a space
141 g_unichar_isgraph (gunichar c)
144 return (t != G_UNICODE_CONTROL
145 && t != G_UNICODE_FORMAT
146 && t != G_UNICODE_UNASSIGNED
147 && t != G_UNICODE_PRIVATE_USE
148 && t != G_UNICODE_SURROGATE
149 && t != G_UNICODE_SPACE_SEPARATOR);
154 * @c: a Unicode character
156 * Determines whether a character is a lowercase letter.
157 * Given some UTF-8 text, obtain a character value with
160 * Return value: %TRUE if @c is a lowercase letter
163 g_unichar_islower (gunichar c)
165 return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
171 * @c: a Unicode character
173 * Determines whether a character is printable.
174 * Unlike g_unichar_isgraph(), returns %TRUE for spaces.
175 * Given some UTF-8 text, obtain a character value with
178 * Return value: %TRUE if @c is printable
181 g_unichar_isprint (gunichar c)
184 return (t != G_UNICODE_CONTROL
185 && t != G_UNICODE_FORMAT
186 && t != G_UNICODE_UNASSIGNED
187 && t != G_UNICODE_PRIVATE_USE
188 && t != G_UNICODE_SURROGATE);
193 * @c: a Unicode character
195 * Determines whether a character is punctuation or a symbol.
196 * Given some UTF-8 text, obtain a character value with
199 * Return value: %TRUE if @c is a punctuation or symbol character
202 g_unichar_ispunct (gunichar c)
205 return (t == G_UNICODE_CONNECT_PUNCTUATION || t == G_UNICODE_DASH_PUNCTUATION
206 || t == G_UNICODE_CLOSE_PUNCTUATION || t == G_UNICODE_FINAL_PUNCTUATION
207 || t == G_UNICODE_INITIAL_PUNCTUATION || t == G_UNICODE_OTHER_PUNCTUATION
208 || t == G_UNICODE_OPEN_PUNCTUATION || t == G_UNICODE_CURRENCY_SYMBOL
209 || t == G_UNICODE_MODIFIER_SYMBOL || t == G_UNICODE_MATH_SYMBOL
210 || t == G_UNICODE_OTHER_SYMBOL);
215 * @c: a Unicode character
217 * Determines whether a character is a space, tab, or line separator
218 * (newline, carriage return, etc.). Given some UTF-8 text, obtain a
219 * character value with g_utf8_get_char().
221 * (Note: don't use this to do word breaking; you have to use
222 * Pango or equivalent to get word breaking right, the algorithm
223 * is fairly complex.)
225 * Return value: %TRUE if @c is a punctuation character
228 g_unichar_isspace (gunichar c)
232 /* special-case these since Unicode thinks they are not spaces */
243 return (t == G_UNICODE_SPACE_SEPARATOR || t == G_UNICODE_LINE_SEPARATOR
244 || t == G_UNICODE_PARAGRAPH_SEPARATOR);
252 * @c: a Unicode character
254 * Determines if a character is uppercase.
256 * Return value: %TRUE if @c is an uppercase character
259 g_unichar_isupper (gunichar c)
261 return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
266 * @c: a Unicode character
268 * Determines if a character is titlecase. Some characters in
269 * Unicode which are composites, such as the DZ digraph
270 * have three case variants instead of just two. The titlecase
271 * form is used at the beginning of a word where only the
272 * first letter is capitalized. The titlecase form of the DZ
273 * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z.
275 * Return value: %TRUE if the character is titlecase
278 g_unichar_istitle (gunichar c)
281 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
282 if (title_table[i][0] == c)
288 * g_unichar_isxdigit:
289 * @c: a Unicode character.
291 * Determines if a character is a hexidecimal digit.
293 * Return value: %TRUE if the character is a hexadecimal digit
296 g_unichar_isxdigit (gunichar c)
299 return ((c >= 'a' && c <= 'f')
300 || (c >= 'A' && c <= 'F')
305 * g_unichar_isdefined:
306 * @c: a Unicode character
308 * Determines if a given character is assigned in the Unicode
311 * Return value: %TRUE if the character has an assigned value
314 g_unichar_isdefined (gunichar c)
317 return t != G_UNICODE_UNASSIGNED;
322 * @c: a Unicode character
324 * Determines if a character is typically rendered in a double-width
327 * Return value: %TRUE if the character is wide
329 /* This function stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>. */
331 g_unichar_iswide (gunichar c)
336 return ((c >= 0x1100 && c <= 0x115f) /* Hangul Jamo */
337 || (c >= 0x2e80 && c <= 0xa4cf && (c & ~0x0011) != 0x300a &&
338 c != 0x303f) /* CJK ... Yi */
339 || (c >= 0xac00 && c <= 0xd7a3) /* Hangul Syllables */
340 || (c >= 0xf900 && c <= 0xfaff) /* CJK Compatibility Ideographs */
341 || (c >= 0xfe30 && c <= 0xfe6f) /* CJK Compatibility Forms */
342 || (c >= 0xff00 && c <= 0xff5f) /* Fullwidth Forms */
343 || (c >= 0xffe0 && c <= 0xffe6));
348 * @c: a Unicode character
350 * Converts a character to uppercase.
352 * Return value: the result of converting @c to uppercase.
353 * If @c is not an lowercase or titlecase character,
354 * or has no upper case equivalent @c is returned unchanged.
357 g_unichar_toupper (gunichar c)
360 if (t == G_UNICODE_LOWERCASE_LETTER)
362 gunichar val = ATTTABLE (c >> 8, c & 0xff);
363 if (val >= 0xd800 && val < 0xdc00)
365 const guchar *p = special_case_table[val - 0xd800];
366 return p[0] * 256 + p[1];
369 return val ? val : c;
371 else if (t == G_UNICODE_TITLECASE_LETTER)
374 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
376 if (title_table[i][0] == c)
377 return title_table[i][1];
385 * @c: a Unicode character.
387 * Converts a character to lower case.
389 * Return value: the result of converting @c to lower case.
390 * If @c is not an upperlower or titlecase character,
391 * or has no lowercase equivalent @c is returned unchanged.
394 g_unichar_tolower (gunichar c)
397 if (t == G_UNICODE_UPPERCASE_LETTER)
399 gunichar val = ATTTABLE (c >> 8, c & 0xff);
400 if (val >= 0xd800 && val < 0xdc00)
402 const guchar *p = special_case_table[val - 0xd800];
403 return p[0] * 256 + p[1];
406 return val ? val : c;
408 else if (t == G_UNICODE_TITLECASE_LETTER)
411 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
413 if (title_table[i][0] == c)
414 return title_table[i][2];
422 * @c: a Unicode character
424 * Converts a character to the titlecase.
426 * Return value: the result of converting @c to titlecase.
427 * If @c is not an uppercase or lowercase character,
428 * @c is returned unchanged.
431 g_unichar_totitle (gunichar c)
434 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
436 if (title_table[i][0] == c || title_table[i][1] == c
437 || title_table[i][2] == c)
438 return title_table[i][0];
440 return (TYPE (c) == G_UNICODE_LOWERCASE_LETTER
441 ? ATTTABLE (c >> 8, c & 0xff)
446 * g_unichar_digit_value:
447 * @c: a Unicode character
449 * Determines the numeric value of a character as a decimal
452 * Return value: If @c is a decimal digit (according to
453 * g_unichar_isdigit()), its numeric value. Otherwise, -1.
456 g_unichar_digit_value (gunichar c)
458 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
459 return ATTTABLE (c >> 8, c & 0xff);
464 * g_unichar_xdigit_value:
465 * @c: a Unicode character
467 * Determines the numeric value of a character as a hexidecimal
470 * Return value: If @c is a hex digit (according to
471 * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
474 g_unichar_xdigit_value (gunichar c)
476 if (c >= 'A' && c <= 'F')
478 if (c >= 'a' && c <= 'f')
480 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
481 return ATTTABLE (c >> 8, c & 0xff);
487 * @c: a Unicode character
489 * Classifies a Unicode character by type.
491 * Return value: the type of the character.
494 g_unichar_type (gunichar c)
500 * Case mapping functions
510 get_locale_type (void)
512 const char *locale = setlocale (LC_CTYPE, NULL);
517 if (locale[1] == 'z')
518 return LOCALE_TURKIC;
521 if (locale[1] == 't')
522 return LOCALE_LITHUANIAN;
525 if (locale[1] == 'r')
526 return LOCALE_TURKIC;
530 return LOCALE_NORMAL;
534 output_marks (const char **p_inout,
539 const char *p = *p_inout;
543 gunichar c = g_utf8_get_char (p);
548 if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
549 len += g_unichar_to_utf8 (c, out_buffer ? out_buffer + len : NULL);
550 p = g_utf8_next_char (p);
561 output_special_case (gchar *out_buffer,
567 const guchar *p = special_case_table[index];
569 if (type != G_UNICODE_TITLECASE_LETTER)
570 p += 2; /* +2 to skip over "best single match" */
581 gunichar ch = p[0] * 256 + p[1];
585 len += g_unichar_to_utf8 (ch, out_buffer ? out_buffer + len : NULL);
593 real_toupper (const gchar *str,
596 LocaleType locale_type)
598 const gchar *p = str;
599 const char *last = NULL;
601 gboolean last_was_i = FALSE;
603 while ((max_len < 0 || p < str + max_len) && *p)
605 gunichar c = g_utf8_get_char (p);
610 p = g_utf8_next_char (p);
612 if (locale_type == LOCALE_LITHUANIAN)
620 /* Nasty, need to remove any dot above. Though
621 * I think only E WITH DOT ABOVE occurs in practice
622 * which could simplify this considerably.
627 decomp = g_unicode_canonical_decomposition (c, &decomp_len);
628 for (i=0; i < decomp_len; i++)
630 if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
631 len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
635 len = output_marks (&p, out_buffer, len, TRUE);
645 if (locale_type == LOCALE_TURKIC && c == 'i')
647 /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
648 len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL);
650 else if (c == 0x0345) /* COMBINING GREEK YPOGEGRAMMENI */
652 /* Nasty, need to move it after other combining marks .. this would go away if
653 * we normalized first.
655 len = output_marks (&p, out_buffer, len, FALSE);
657 /* And output as GREEK CAPITAL LETTER IOTA */
658 len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);
660 else if (t == G_UNICODE_LOWERCASE_LETTER || t == G_UNICODE_TITLECASE_LETTER)
662 val = ATTTABLE (c >> 8, c & 0xff);
664 if (val >= 0xd800 && val < 0xdc00)
666 len += output_special_case (out_buffer, len, val - 0xd800, t,
667 t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
671 if (t == G_UNICODE_TITLECASE_LETTER)
674 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
676 if (title_table[i][0] == c)
677 val = title_table[i][1];
681 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
686 gsize char_len = g_utf8_skip[*(guchar *)last];
689 memcpy (out_buffer + len, last, char_len);
701 * @str: a UTF-8 encoded string
702 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
704 * Converts all Unicode characters in the string that have a case
705 * to uppercase. The exact manner that this is done depends
706 * on the current locale, and may result in the number of
707 * characters in the string increasing. (For instance, the
708 * German ess-zet will be changed to SS.)
710 * Return value: a newly allocated string, with all characters
711 * converted to uppercase.
714 g_utf8_strup (const gchar *str,
718 LocaleType locale_type;
721 g_return_val_if_fail (str != NULL, NULL);
723 locale_type = get_locale_type ();
726 * We use a two pass approach to keep memory management simple
728 result_len = real_toupper (str, len, NULL, locale_type);
729 result = g_malloc (result_len + 1);
730 real_toupper (str, len, result, locale_type);
731 result[result_len] = '\0';
737 real_tolower (const gchar *str,
740 LocaleType locale_type)
742 const gchar *p = str;
743 const char *last = NULL;
746 while ((max_len < 0 || p < str + max_len) && *p)
748 gunichar c = g_utf8_get_char (p);
753 p = g_utf8_next_char (p);
755 if (locale_type == LOCALE_TURKIC && c == 'I')
757 /* I => LATIN SMALL LETTER DOTLESS I */
758 len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL);
760 else if (c == 0x03A3) /* GREEK CAPITAL LETTER SIGMA */
762 if ((max_len < 0 || p < str + max_len) && *p)
764 gunichar next_c = g_utf8_get_char (p);
765 int next_type = TYPE(next_c);
767 /* SIGMA mapps differently depending on whether it is
768 * final or not. The following simplified test would
769 * fail in the case of combining marks following the
770 * sigma, but I don't think that occurs in real text.
771 * The test here matches that in ICU.
773 if (ISALPHA(next_type)) /* Lu,Ll,Lt,Lm,Lo */
774 val = 0x3c3; /* GREEK SMALL SIGMA */
776 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
779 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
781 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
783 else if (t == G_UNICODE_UPPERCASE_LETTER || t == G_UNICODE_TITLECASE_LETTER)
785 val = ATTTABLE (c >> 8, c & 0xff);
787 if (val >= 0xd800 && val < 0xdc00)
789 len += output_special_case (out_buffer, len, val - 0xd800, t, 0);
793 if (t == G_UNICODE_TITLECASE_LETTER)
796 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
798 if (title_table[i][0] == c)
799 val = title_table[i][2];
803 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
808 gsize char_len = g_utf8_skip[*(guchar *)last];
811 memcpy (out_buffer + len, last, char_len);
823 * @str: a UTF-8 encoded string
824 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
826 * Converts all Unicode characters in the string that have a case
827 * to lowercase. The exact manner that this is done depends
828 * on the current locale, and may result in the number of
829 * characters in the string changing.
831 * Return value: a newly allocated string, with all characters
832 * converted to lowercase.
835 g_utf8_strdown (const gchar *str,
839 LocaleType locale_type;
842 g_return_val_if_fail (str != NULL, NULL);
844 locale_type = get_locale_type ();
847 * We use a two pass approach to keep memory management simple
849 result_len = real_tolower (str, len, NULL, locale_type);
850 result = g_malloc (result_len + 1);
851 real_tolower (str, len, result, locale_type);
852 result[result_len] = '\0';
859 * @str: a UTF-8 encoded string
860 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
862 * Converts a string into a form that is independent of case. The
863 * result will not correspond to any particular case, but can be
864 * compared for equality or ordered with the results of calling
865 * g_utf8_casefold() on other strings.
867 * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
868 * only an approximation to the correct linguistic case insensitive
869 * ordering, though it is a fairly good one. Getting this exactly
870 * right would require a more sophisticated collation function that
871 * takes case sensitivity into account. GLib does not currently
872 * provide such a function.
874 * Return value: a newly allocated string, that is a
875 * case independent form of @str.
878 g_utf8_casefold (const gchar *str,
881 GString *result = g_string_new (NULL);
885 while ((len < 0 || p < str + len) && *p)
887 gunichar ch = g_utf8_get_char (p);
890 int end = G_N_ELEMENTS (casefold_table);
892 if (ch >= casefold_table[start].ch &&
893 ch <= casefold_table[end - 1].ch)
897 int half = (start + end) / 2;
898 if (ch == casefold_table[half].ch)
900 g_string_append (result, casefold_table[half].data);
903 else if (half == start)
905 else if (ch > casefold_table[half].ch)
912 g_string_append_unichar (result, g_unichar_tolower (ch));
915 p = g_utf8_next_char (p);
918 return g_string_free (result, FALSE);