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"
31 #define ATTR_TABLE(Page) (((Page) <= G_UNICODE_LAST_PAGE_PART1) \
32 ? attr_table_part1[Page] \
33 : attr_table_part2[(Page) - 0xe00])
35 #define ATTTABLE(Page, Char) \
36 ((ATTR_TABLE(Page) == G_UNICODE_MAX_TABLE_INDEX) ? 0 : (attr_data[ATTR_TABLE(Page)][Char]))
38 #define TTYPE_PART1(Page, Char) \
39 ((type_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
40 ? (type_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
41 : (type_data[type_table_part1[Page]][Char]))
43 #define TTYPE_PART2(Page, Char) \
44 ((type_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
45 ? (type_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
46 : (type_data[type_table_part2[Page]][Char]))
49 (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
50 ? TTYPE_PART1 ((Char) >> 8, (Char) & 0xff) \
51 : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
52 ? TTYPE_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
53 : G_UNICODE_UNASSIGNED))
56 #define ISDIGIT(Type) ((Type) == G_UNICODE_DECIMAL_NUMBER \
57 || (Type) == G_UNICODE_LETTER_NUMBER \
58 || (Type) == G_UNICODE_OTHER_NUMBER)
60 #define ISALPHA(Type) ((Type) == G_UNICODE_LOWERCASE_LETTER \
61 || (Type) == G_UNICODE_UPPERCASE_LETTER \
62 || (Type) == G_UNICODE_TITLECASE_LETTER \
63 || (Type) == G_UNICODE_MODIFIER_LETTER \
64 || (Type) == G_UNICODE_OTHER_LETTER)
66 #define ISMARK(Type) ((Type) == G_UNICODE_NON_SPACING_MARK || \
67 (Type) == G_UNICODE_COMBINING_MARK || \
68 (Type) == G_UNICODE_ENCLOSING_MARK)
73 * @c: a Unicode character
75 * Determines whether a character is alphanumeric.
76 * Given some UTF-8 text, obtain a character value
77 * with g_utf8_get_char().
79 * Return value: %TRUE if @c is an alphanumeric character
82 g_unichar_isalnum (gunichar c)
85 return ISDIGIT (t) || ISALPHA (t);
90 * @c: a Unicode character
92 * Determines whether a character is alphabetic (i.e. a letter).
93 * Given some UTF-8 text, obtain a character value with
96 * Return value: %TRUE if @c is an alphabetic character
99 g_unichar_isalpha (gunichar c)
108 * @c: a Unicode character
110 * Determines whether a character is a control character.
111 * Given some UTF-8 text, obtain a character value with
114 * Return value: %TRUE if @c is a control character
117 g_unichar_iscntrl (gunichar c)
119 return TYPE (c) == G_UNICODE_CONTROL;
124 * @c: a Unicode character
126 * Determines whether a character is numeric (i.e. a digit). This
127 * covers ASCII 0-9 and also digits in other languages/scripts. Given
128 * some UTF-8 text, obtain a character value with g_utf8_get_char().
130 * Return value: %TRUE if @c is a digit
133 g_unichar_isdigit (gunichar c)
135 return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
141 * @c: a Unicode character
143 * Determines whether a character is printable and not a space
144 * (returns %FALSE for control characters, format characters, and
145 * spaces). g_unichar_isprint() is similar, but returns %TRUE for
146 * spaces. Given some UTF-8 text, obtain a character value with
149 * Return value: %TRUE if @c is printable unless it's a space
152 g_unichar_isgraph (gunichar c)
155 return (t != G_UNICODE_CONTROL
156 && t != G_UNICODE_FORMAT
157 && t != G_UNICODE_UNASSIGNED
158 && t != G_UNICODE_PRIVATE_USE
159 && t != G_UNICODE_SURROGATE
160 && t != G_UNICODE_SPACE_SEPARATOR);
165 * @c: a Unicode character
167 * Determines whether a character is a lowercase letter.
168 * Given some UTF-8 text, obtain a character value with
171 * Return value: %TRUE if @c is a lowercase letter
174 g_unichar_islower (gunichar c)
176 return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
182 * @c: a Unicode character
184 * Determines whether a character is printable.
185 * Unlike g_unichar_isgraph(), returns %TRUE for spaces.
186 * Given some UTF-8 text, obtain a character value with
189 * Return value: %TRUE if @c is printable
192 g_unichar_isprint (gunichar c)
195 return (t != G_UNICODE_CONTROL
196 && t != G_UNICODE_FORMAT
197 && t != G_UNICODE_UNASSIGNED
198 && t != G_UNICODE_PRIVATE_USE
199 && t != G_UNICODE_SURROGATE);
204 * @c: a Unicode character
206 * Determines whether a character is punctuation or a symbol.
207 * Given some UTF-8 text, obtain a character value with
210 * Return value: %TRUE if @c is a punctuation or symbol character
213 g_unichar_ispunct (gunichar c)
216 return (t == G_UNICODE_CONNECT_PUNCTUATION || t == G_UNICODE_DASH_PUNCTUATION
217 || t == G_UNICODE_CLOSE_PUNCTUATION || t == G_UNICODE_FINAL_PUNCTUATION
218 || t == G_UNICODE_INITIAL_PUNCTUATION || t == G_UNICODE_OTHER_PUNCTUATION
219 || t == G_UNICODE_OPEN_PUNCTUATION || t == G_UNICODE_CURRENCY_SYMBOL
220 || t == G_UNICODE_MODIFIER_SYMBOL || t == G_UNICODE_MATH_SYMBOL
221 || t == G_UNICODE_OTHER_SYMBOL);
226 * @c: a Unicode character
228 * Determines whether a character is a space, tab, or line separator
229 * (newline, carriage return, etc.). Given some UTF-8 text, obtain a
230 * character value with g_utf8_get_char().
232 * (Note: don't use this to do word breaking; you have to use
233 * Pango or equivalent to get word breaking right, the algorithm
234 * is fairly complex.)
236 * Return value: %TRUE if @c is a punctuation character
239 g_unichar_isspace (gunichar c)
243 /* special-case these since Unicode thinks they are not spaces */
254 return (t == G_UNICODE_SPACE_SEPARATOR || t == G_UNICODE_LINE_SEPARATOR
255 || t == G_UNICODE_PARAGRAPH_SEPARATOR);
263 * @c: a Unicode character
265 * Determines if a character is uppercase.
267 * Return value: %TRUE if @c is an uppercase character
270 g_unichar_isupper (gunichar c)
272 return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
277 * @c: a Unicode character
279 * Determines if a character is titlecase. Some characters in
280 * Unicode which are composites, such as the DZ digraph
281 * have three case variants instead of just two. The titlecase
282 * form is used at the beginning of a word where only the
283 * first letter is capitalized. The titlecase form of the DZ
284 * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z.
286 * Return value: %TRUE if the character is titlecase
289 g_unichar_istitle (gunichar c)
292 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
293 if (title_table[i][0] == c)
299 * g_unichar_isxdigit:
300 * @c: a Unicode character.
302 * Determines if a character is a hexidecimal digit.
304 * Return value: %TRUE if the character is a hexadecimal digit
307 g_unichar_isxdigit (gunichar c)
310 return ((c >= 'a' && c <= 'f')
311 || (c >= 'A' && c <= 'F')
316 * g_unichar_isdefined:
317 * @c: a Unicode character
319 * Determines if a given character is assigned in the Unicode
322 * Return value: %TRUE if the character has an assigned value
325 g_unichar_isdefined (gunichar c)
328 return t != G_UNICODE_UNASSIGNED;
333 * @c: a Unicode character
335 * Determines if a character is typically rendered in a double-width
338 * Return value: %TRUE if the character is wide
340 /* This function stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>. */
342 g_unichar_iswide (gunichar c)
347 return (c <= 0x115f /* Hangul Jamo init. consonants */
348 || c == 0x2329 || c == 0x232a /* angle brackets */
349 || (c >= 0x2e80 && c <= 0xa4cf && (c < 0x302a || c > 0x302f)
350 && c != 0x303f && c != 0x3099 && c!= 0x309a) /* CJK ... Yi */
351 || (c >= 0xac00 && c <= 0xd7a3) /* Hangul Syllables */
352 || (c >= 0xf900 && c <= 0xfaff) /* CJK Compatibility Ideographs */
353 || (c >= 0xfe30 && c <= 0xfe6f) /* CJK Compatibility Forms */
354 || (c >= 0xff00 && c <= 0xff60) /* Fullwidth Forms */
355 || (c >= 0xffe0 && c <= 0xffe6) /* Fullwidth Forms */
356 || (c >= 0x20000 && c <= 0x2fffd) /* CJK extra stuff */
357 || (c >= 0x30000 && c <= 0x3fffd));
362 * @c: a Unicode character
364 * Converts a character to uppercase.
366 * Return value: the result of converting @c to uppercase.
367 * If @c is not an lowercase or titlecase character,
368 * or has no upper case equivalent @c is returned unchanged.
371 g_unichar_toupper (gunichar c)
374 if (t == G_UNICODE_LOWERCASE_LETTER)
376 gunichar val = ATTTABLE (c >> 8, c & 0xff);
377 if (val >= 0x1000000)
379 const guchar *p = special_case_table + val - 0x1000000;
380 return g_utf8_get_char (p);
383 return val ? val : c;
385 else if (t == G_UNICODE_TITLECASE_LETTER)
388 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
390 if (title_table[i][0] == c)
391 return title_table[i][1];
399 * @c: a Unicode character.
401 * Converts a character to lower case.
403 * Return value: the result of converting @c to lower case.
404 * If @c is not an upperlower or titlecase character,
405 * or has no lowercase equivalent @c is returned unchanged.
408 g_unichar_tolower (gunichar c)
411 if (t == G_UNICODE_UPPERCASE_LETTER)
413 gunichar val = ATTTABLE (c >> 8, c & 0xff);
414 if (val >= 0x1000000)
416 const guchar *p = special_case_table + val - 0x1000000;
417 return g_utf8_get_char (p);
420 return val ? val : c;
422 else if (t == G_UNICODE_TITLECASE_LETTER)
425 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
427 if (title_table[i][0] == c)
428 return title_table[i][2];
436 * @c: a Unicode character
438 * Converts a character to the titlecase.
440 * Return value: the result of converting @c to titlecase.
441 * If @c is not an uppercase or lowercase character,
442 * @c is returned unchanged.
445 g_unichar_totitle (gunichar c)
448 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
450 if (title_table[i][0] == c || title_table[i][1] == c
451 || title_table[i][2] == c)
452 return title_table[i][0];
454 return (TYPE (c) == G_UNICODE_LOWERCASE_LETTER
455 ? ATTTABLE (c >> 8, c & 0xff)
460 * g_unichar_digit_value:
461 * @c: a Unicode character
463 * Determines the numeric value of a character as a decimal
466 * Return value: If @c is a decimal digit (according to
467 * g_unichar_isdigit()), its numeric value. Otherwise, -1.
470 g_unichar_digit_value (gunichar c)
472 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
473 return ATTTABLE (c >> 8, c & 0xff);
478 * g_unichar_xdigit_value:
479 * @c: a Unicode character
481 * Determines the numeric value of a character as a hexidecimal
484 * Return value: If @c is a hex digit (according to
485 * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
488 g_unichar_xdigit_value (gunichar c)
490 if (c >= 'A' && c <= 'F')
492 if (c >= 'a' && c <= 'f')
494 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
495 return ATTTABLE (c >> 8, c & 0xff);
501 * @c: a Unicode character
503 * Classifies a Unicode character by type.
505 * Return value: the type of the character.
508 g_unichar_type (gunichar c)
514 * Case mapping functions
524 get_locale_type (void)
526 const char *locale = setlocale (LC_CTYPE, NULL);
531 if (locale[1] == 'z')
532 return LOCALE_TURKIC;
535 if (locale[1] == 't')
536 return LOCALE_LITHUANIAN;
539 if (locale[1] == 'r')
540 return LOCALE_TURKIC;
544 return LOCALE_NORMAL;
548 output_marks (const char **p_inout,
552 const char *p = *p_inout;
557 gunichar c = g_utf8_get_char (p);
562 if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
563 len += g_unichar_to_utf8 (c, out_buffer);
564 p = g_utf8_next_char (p);
575 output_special_case (gchar *out_buffer,
580 const guchar *p = special_case_table + offset;
583 if (type != G_UNICODE_TITLECASE_LETTER)
584 p = g_utf8_next_char (p);
591 memcpy (out_buffer, p, len);
597 real_toupper (const gchar *str,
600 LocaleType locale_type)
602 const gchar *p = str;
603 const char *last = NULL;
605 gboolean last_was_i = FALSE;
607 while ((max_len < 0 || p < str + max_len) && *p)
609 gunichar c = g_utf8_get_char (p);
614 p = g_utf8_next_char (p);
616 if (locale_type == LOCALE_LITHUANIAN)
624 /* Nasty, need to remove any dot above. Though
625 * I think only E WITH DOT ABOVE occurs in practice
626 * which could simplify this considerably.
631 decomp = g_unicode_canonical_decomposition (c, &decomp_len);
632 for (i=0; i < decomp_len; i++)
634 if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
635 len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
639 len += output_marks (&p, out_buffer ? out_buffer + len : NULL, TRUE);
649 if (locale_type == LOCALE_TURKIC && c == 'i')
651 /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
652 len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL);
654 else if (c == 0x0345) /* COMBINING GREEK YPOGEGRAMMENI */
656 /* Nasty, need to move it after other combining marks .. this would go away if
657 * we normalized first.
659 len += output_marks (&p, out_buffer ? out_buffer + len : NULL, FALSE);
661 /* And output as GREEK CAPITAL LETTER IOTA */
662 len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);
664 else if (t == G_UNICODE_LOWERCASE_LETTER || t == G_UNICODE_TITLECASE_LETTER)
666 val = ATTTABLE (c >> 8, c & 0xff);
668 if (val >= 0x1000000)
670 len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t,
671 t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
675 if (t == G_UNICODE_TITLECASE_LETTER)
678 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
680 if (title_table[i][0] == c)
681 val = title_table[i][1];
685 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
690 gsize char_len = g_utf8_skip[*(guchar *)last];
693 memcpy (out_buffer + len, last, char_len);
705 * @str: a UTF-8 encoded string
706 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
708 * Converts all Unicode characters in the string that have a case
709 * to uppercase. The exact manner that this is done depends
710 * on the current locale, and may result in the number of
711 * characters in the string increasing. (For instance, the
712 * German ess-zet will be changed to SS.)
714 * Return value: a newly allocated string, with all characters
715 * converted to uppercase.
718 g_utf8_strup (const gchar *str,
722 LocaleType locale_type;
725 g_return_val_if_fail (str != NULL, NULL);
727 locale_type = get_locale_type ();
730 * We use a two pass approach to keep memory management simple
732 result_len = real_toupper (str, len, NULL, locale_type);
733 result = g_malloc (result_len + 1);
734 real_toupper (str, len, result, locale_type);
735 result[result_len] = '\0';
741 real_tolower (const gchar *str,
744 LocaleType locale_type)
746 const gchar *p = str;
747 const char *last = NULL;
750 while ((max_len < 0 || p < str + max_len) && *p)
752 gunichar c = g_utf8_get_char (p);
757 p = g_utf8_next_char (p);
759 if (locale_type == LOCALE_TURKIC && c == 'I')
761 /* I => LATIN SMALL LETTER DOTLESS I */
762 len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL);
764 else if (c == 0x03A3) /* GREEK CAPITAL LETTER SIGMA */
766 if ((max_len < 0 || p < str + max_len) && *p)
768 gunichar next_c = g_utf8_get_char (p);
769 int next_type = TYPE(next_c);
771 /* SIGMA mapps differently depending on whether it is
772 * final or not. The following simplified test would
773 * fail in the case of combining marks following the
774 * sigma, but I don't think that occurs in real text.
775 * The test here matches that in ICU.
777 if (ISALPHA(next_type)) /* Lu,Ll,Lt,Lm,Lo */
778 val = 0x3c3; /* GREEK SMALL SIGMA */
780 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
783 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
785 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
787 else if (t == G_UNICODE_UPPERCASE_LETTER || t == G_UNICODE_TITLECASE_LETTER)
789 val = ATTTABLE (c >> 8, c & 0xff);
791 if (val >= 0x1000000)
793 len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t, 0);
797 if (t == G_UNICODE_TITLECASE_LETTER)
800 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
802 if (title_table[i][0] == c)
803 val = title_table[i][2];
807 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
812 gsize char_len = g_utf8_skip[*(guchar *)last];
815 memcpy (out_buffer + len, last, char_len);
827 * @str: a UTF-8 encoded string
828 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
830 * Converts all Unicode characters in the string that have a case
831 * to lowercase. The exact manner that this is done depends
832 * on the current locale, and may result in the number of
833 * characters in the string changing.
835 * Return value: a newly allocated string, with all characters
836 * converted to lowercase.
839 g_utf8_strdown (const gchar *str,
843 LocaleType locale_type;
846 g_return_val_if_fail (str != NULL, NULL);
848 locale_type = get_locale_type ();
851 * We use a two pass approach to keep memory management simple
853 result_len = real_tolower (str, len, NULL, locale_type);
854 result = g_malloc (result_len + 1);
855 real_tolower (str, len, result, locale_type);
856 result[result_len] = '\0';
863 * @str: a UTF-8 encoded string
864 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
866 * Converts a string into a form that is independent of case. The
867 * result will not correspond to any particular case, but can be
868 * compared for equality or ordered with the results of calling
869 * g_utf8_casefold() on other strings.
871 * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
872 * only an approximation to the correct linguistic case insensitive
873 * ordering, though it is a fairly good one. Getting this exactly
874 * right would require a more sophisticated collation function that
875 * takes case sensitivity into account. GLib does not currently
876 * provide such a function.
878 * Return value: a newly allocated string, that is a
879 * case independent form of @str.
882 g_utf8_casefold (const gchar *str,
885 GString *result = g_string_new (NULL);
889 while ((len < 0 || p < str + len) && *p)
891 gunichar ch = g_utf8_get_char (p);
894 int end = G_N_ELEMENTS (casefold_table);
896 if (ch >= casefold_table[start].ch &&
897 ch <= casefold_table[end - 1].ch)
901 int half = (start + end) / 2;
902 if (ch == casefold_table[half].ch)
904 g_string_append (result, casefold_table[half].data);
907 else if (half == start)
909 else if (ch > casefold_table[half].ch)
916 g_string_append_unichar (result, g_unichar_tolower (ch));
919 p = g_utf8_next_char (p);
922 return g_string_free (result, FALSE);