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
<= 0x115f /* Hangul Jamo init. consonants */
337 || c
== 0x2329 || c
== 0x232a /* angle brackets */
338 || (c
>= 0x2e80 && c
<= 0xa4cf && (c
< 0x302a || c
> 0x302f)
339 && c
!= 0x303f && c
!= 0x3099 && c
!= 0x309a) /* CJK ... Yi */
340 || (c
>= 0xac00 && c
<= 0xd7a3) /* Hangul Syllables */
341 || (c
>= 0xf900 && c
<= 0xfaff) /* CJK Compatibility Ideographs */
342 || (c
>= 0xfe30 && c
<= 0xfe6f) /* CJK Compatibility Forms */
343 || (c
>= 0xff00 && c
<= 0xff60) /* Fullwidth Forms */
344 || (c
>= 0xffe0 && c
<= 0xffe6) /* Fullwidth Forms */
345 || (c
>= 0x20000 && c
<= 0x2fffd) /* CJK extra stuff */
346 || (c
>= 0x30000 && c
<= 0x3fffd));
351 * @c: a Unicode character
353 * Converts a character to uppercase.
355 * Return value: the result of converting @c to uppercase.
356 * If @c is not an lowercase or titlecase character,
357 * or has no upper case equivalent @c is returned unchanged.
360 g_unichar_toupper (gunichar c
)
363 if (t
== G_UNICODE_LOWERCASE_LETTER
)
365 gunichar val
= ATTTABLE (c
>> 8, c
& 0xff);
366 if (val
>= 0xd800 && val
< 0xdc00)
368 const guchar
*p
= special_case_table
[val
- 0xd800];
369 return p
[0] * 256 + p
[1];
372 return val
? val
: c
;
374 else if (t
== G_UNICODE_TITLECASE_LETTER
)
377 for (i
= 0; i
< G_N_ELEMENTS (title_table
); ++i
)
379 if (title_table
[i
][0] == c
)
380 return title_table
[i
][1];
388 * @c: a Unicode character.
390 * Converts a character to lower case.
392 * Return value: the result of converting @c to lower case.
393 * If @c is not an upperlower or titlecase character,
394 * or has no lowercase equivalent @c is returned unchanged.
397 g_unichar_tolower (gunichar c
)
400 if (t
== G_UNICODE_UPPERCASE_LETTER
)
402 gunichar val
= ATTTABLE (c
>> 8, c
& 0xff);
403 if (val
>= 0xd800 && val
< 0xdc00)
405 const guchar
*p
= special_case_table
[val
- 0xd800];
406 return p
[0] * 256 + p
[1];
409 return val
? val
: c
;
411 else if (t
== G_UNICODE_TITLECASE_LETTER
)
414 for (i
= 0; i
< G_N_ELEMENTS (title_table
); ++i
)
416 if (title_table
[i
][0] == c
)
417 return title_table
[i
][2];
425 * @c: a Unicode character
427 * Converts a character to the titlecase.
429 * Return value: the result of converting @c to titlecase.
430 * If @c is not an uppercase or lowercase character,
431 * @c is returned unchanged.
434 g_unichar_totitle (gunichar c
)
437 for (i
= 0; i
< G_N_ELEMENTS (title_table
); ++i
)
439 if (title_table
[i
][0] == c
|| title_table
[i
][1] == c
440 || title_table
[i
][2] == c
)
441 return title_table
[i
][0];
443 return (TYPE (c
) == G_UNICODE_LOWERCASE_LETTER
444 ? ATTTABLE (c
>> 8, c
& 0xff)
449 * g_unichar_digit_value:
450 * @c: a Unicode character
452 * Determines the numeric value of a character as a decimal
455 * Return value: If @c is a decimal digit (according to
456 * g_unichar_isdigit()), its numeric value. Otherwise, -1.
459 g_unichar_digit_value (gunichar c
)
461 if (TYPE (c
) == G_UNICODE_DECIMAL_NUMBER
)
462 return ATTTABLE (c
>> 8, c
& 0xff);
467 * g_unichar_xdigit_value:
468 * @c: a Unicode character
470 * Determines the numeric value of a character as a hexidecimal
473 * Return value: If @c is a hex digit (according to
474 * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
477 g_unichar_xdigit_value (gunichar c
)
479 if (c
>= 'A' && c
<= 'F')
481 if (c
>= 'a' && c
<= 'f')
483 if (TYPE (c
) == G_UNICODE_DECIMAL_NUMBER
)
484 return ATTTABLE (c
>> 8, c
& 0xff);
490 * @c: a Unicode character
492 * Classifies a Unicode character by type.
494 * Return value: the type of the character.
497 g_unichar_type (gunichar c
)
503 * Case mapping functions
513 get_locale_type (void)
515 const char *locale
= setlocale (LC_CTYPE
, NULL
);
520 if (locale
[1] == 'z')
521 return LOCALE_TURKIC
;
524 if (locale
[1] == 't')
525 return LOCALE_LITHUANIAN
;
528 if (locale
[1] == 'r')
529 return LOCALE_TURKIC
;
533 return LOCALE_NORMAL
;
537 output_marks (const char **p_inout
,
542 const char *p
= *p_inout
;
546 gunichar c
= g_utf8_get_char (p
);
551 if (!remove_dot
|| c
!= 0x307 /* COMBINING DOT ABOVE */)
552 len
+= g_unichar_to_utf8 (c
, out_buffer
? out_buffer
+ len
: NULL
);
553 p
= g_utf8_next_char (p
);
564 output_special_case (gchar
*out_buffer
,
570 const guchar
*p
= special_case_table
[index
];
572 if (type
!= G_UNICODE_TITLECASE_LETTER
)
573 p
+= 2; /* +2 to skip over "best single match" */
584 gunichar ch
= p
[0] * 256 + p
[1];
588 len
+= g_unichar_to_utf8 (ch
, out_buffer
? out_buffer
+ len
: NULL
);
596 real_toupper (const gchar
*str
,
599 LocaleType locale_type
)
601 const gchar
*p
= str
;
602 const char *last
= NULL
;
604 gboolean last_was_i
= FALSE
;
606 while ((max_len
< 0 || p
< str
+ max_len
) && *p
)
608 gunichar c
= g_utf8_get_char (p
);
613 p
= g_utf8_next_char (p
);
615 if (locale_type
== LOCALE_LITHUANIAN
)
623 /* Nasty, need to remove any dot above. Though
624 * I think only E WITH DOT ABOVE occurs in practice
625 * which could simplify this considerably.
630 decomp
= g_unicode_canonical_decomposition (c
, &decomp_len
);
631 for (i
=0; i
< decomp_len
; i
++)
633 if (decomp
[i
] != 0x307 /* COMBINING DOT ABOVE */)
634 len
+= g_unichar_to_utf8 (g_unichar_toupper (decomp
[i
]), out_buffer
? out_buffer
+ len
: NULL
);
638 len
= output_marks (&p
, out_buffer
, len
, TRUE
);
648 if (locale_type
== LOCALE_TURKIC
&& c
== 'i')
650 /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
651 len
+= g_unichar_to_utf8 (0x130, out_buffer
? out_buffer
+ len
: NULL
);
653 else if (c
== 0x0345) /* COMBINING GREEK YPOGEGRAMMENI */
655 /* Nasty, need to move it after other combining marks .. this would go away if
656 * we normalized first.
658 len
= output_marks (&p
, out_buffer
, len
, FALSE
);
660 /* And output as GREEK CAPITAL LETTER IOTA */
661 len
+= g_unichar_to_utf8 (0x399, out_buffer
? out_buffer
+ len
: NULL
);
663 else if (t
== G_UNICODE_LOWERCASE_LETTER
|| t
== G_UNICODE_TITLECASE_LETTER
)
665 val
= ATTTABLE (c
>> 8, c
& 0xff);
667 if (val
>= 0xd800 && val
< 0xdc00)
669 len
+= output_special_case (out_buffer
, len
, val
- 0xd800, t
,
670 t
== G_UNICODE_LOWERCASE_LETTER
? 0 : 1);
674 if (t
== G_UNICODE_TITLECASE_LETTER
)
677 for (i
= 0; i
< G_N_ELEMENTS (title_table
); ++i
)
679 if (title_table
[i
][0] == c
)
680 val
= title_table
[i
][1];
684 len
+= g_unichar_to_utf8 (val
, out_buffer
? out_buffer
+ len
: NULL
);
689 gsize char_len
= g_utf8_skip
[*(guchar
*)last
];
692 memcpy (out_buffer
+ len
, last
, char_len
);
704 * @str: a UTF-8 encoded string
705 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
707 * Converts all Unicode characters in the string that have a case
708 * to uppercase. The exact manner that this is done depends
709 * on the current locale, and may result in the number of
710 * characters in the string increasing. (For instance, the
711 * German ess-zet will be changed to SS.)
713 * Return value: a newly allocated string, with all characters
714 * converted to uppercase.
717 g_utf8_strup (const gchar
*str
,
721 LocaleType locale_type
;
724 g_return_val_if_fail (str
!= NULL
, NULL
);
726 locale_type
= get_locale_type ();
729 * We use a two pass approach to keep memory management simple
731 result_len
= real_toupper (str
, len
, NULL
, locale_type
);
732 result
= g_malloc (result_len
+ 1);
733 real_toupper (str
, len
, result
, locale_type
);
734 result
[result_len
] = '\0';
740 real_tolower (const gchar
*str
,
743 LocaleType locale_type
)
745 const gchar
*p
= str
;
746 const char *last
= NULL
;
749 while ((max_len
< 0 || p
< str
+ max_len
) && *p
)
751 gunichar c
= g_utf8_get_char (p
);
756 p
= g_utf8_next_char (p
);
758 if (locale_type
== LOCALE_TURKIC
&& c
== 'I')
760 /* I => LATIN SMALL LETTER DOTLESS I */
761 len
+= g_unichar_to_utf8 (0x131, out_buffer
? out_buffer
+ len
: NULL
);
763 else if (c
== 0x03A3) /* GREEK CAPITAL LETTER SIGMA */
765 if ((max_len
< 0 || p
< str
+ max_len
) && *p
)
767 gunichar next_c
= g_utf8_get_char (p
);
768 int next_type
= TYPE(next_c
);
770 /* SIGMA mapps differently depending on whether it is
771 * final or not. The following simplified test would
772 * fail in the case of combining marks following the
773 * sigma, but I don't think that occurs in real text.
774 * The test here matches that in ICU.
776 if (ISALPHA(next_type
)) /* Lu,Ll,Lt,Lm,Lo */
777 val
= 0x3c3; /* GREEK SMALL SIGMA */
779 val
= 0x3c2; /* GREEK SMALL FINAL SIGMA */
782 val
= 0x3c2; /* GREEK SMALL FINAL SIGMA */
784 len
+= g_unichar_to_utf8 (val
, out_buffer
? out_buffer
+ len
: NULL
);
786 else if (t
== G_UNICODE_UPPERCASE_LETTER
|| t
== G_UNICODE_TITLECASE_LETTER
)
788 val
= ATTTABLE (c
>> 8, c
& 0xff);
790 if (val
>= 0xd800 && val
< 0xdc00)
792 len
+= output_special_case (out_buffer
, len
, val
- 0xd800, t
, 0);
796 if (t
== G_UNICODE_TITLECASE_LETTER
)
799 for (i
= 0; i
< G_N_ELEMENTS (title_table
); ++i
)
801 if (title_table
[i
][0] == c
)
802 val
= title_table
[i
][2];
806 len
+= g_unichar_to_utf8 (val
, out_buffer
? out_buffer
+ len
: NULL
);
811 gsize char_len
= g_utf8_skip
[*(guchar
*)last
];
814 memcpy (out_buffer
+ len
, last
, char_len
);
826 * @str: a UTF-8 encoded string
827 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
829 * Converts all Unicode characters in the string that have a case
830 * to lowercase. The exact manner that this is done depends
831 * on the current locale, and may result in the number of
832 * characters in the string changing.
834 * Return value: a newly allocated string, with all characters
835 * converted to lowercase.
838 g_utf8_strdown (const gchar
*str
,
842 LocaleType locale_type
;
845 g_return_val_if_fail (str
!= NULL
, NULL
);
847 locale_type
= get_locale_type ();
850 * We use a two pass approach to keep memory management simple
852 result_len
= real_tolower (str
, len
, NULL
, locale_type
);
853 result
= g_malloc (result_len
+ 1);
854 real_tolower (str
, len
, result
, locale_type
);
855 result
[result_len
] = '\0';
862 * @str: a UTF-8 encoded string
863 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
865 * Converts a string into a form that is independent of case. The
866 * result will not correspond to any particular case, but can be
867 * compared for equality or ordered with the results of calling
868 * g_utf8_casefold() on other strings.
870 * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
871 * only an approximation to the correct linguistic case insensitive
872 * ordering, though it is a fairly good one. Getting this exactly
873 * right would require a more sophisticated collation function that
874 * takes case sensitivity into account. GLib does not currently
875 * provide such a function.
877 * Return value: a newly allocated string, that is a
878 * case independent form of @str.
881 g_utf8_casefold (const gchar
*str
,
884 GString
*result
= g_string_new (NULL
);
888 while ((len
< 0 || p
< str
+ len
) && *p
)
890 gunichar ch
= g_utf8_get_char (p
);
893 int end
= G_N_ELEMENTS (casefold_table
);
895 if (ch
>= casefold_table
[start
].ch
&&
896 ch
<= casefold_table
[end
- 1].ch
)
900 int half
= (start
+ end
) / 2;
901 if (ch
== casefold_table
[half
].ch
)
903 g_string_append (result
, casefold_table
[half
].data
);
906 else if (half
== start
)
908 else if (ch
> casefold_table
[half
].ch
)
915 g_string_append_unichar (result
, g_unichar_tolower (ch
));
918 p
= g_utf8_next_char (p
);
921 return g_string_free (result
, FALSE
);