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"
29 #define asize(x) ((sizeof (x)) / sizeof (x[0]))
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 (((((int) type_table[Page]) & 0xff) == ((int) type_table[Page])) \
38 ? ((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)
54 g_unichar_isalnum (gunichar c)
57 return ISDIGIT (t) || ISALPHA (t);
61 g_unichar_isalpha (gunichar c)
68 g_unichar_iscntrl (gunichar c)
70 return TYPE (c) == G_UNICODE_CONTROL;
74 g_unichar_isdigit (gunichar c)
76 return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
80 g_unichar_isgraph (gunichar c)
83 return (t != G_UNICODE_CONTROL
84 && t != G_UNICODE_FORMAT
85 && t != G_UNICODE_UNASSIGNED
86 && t != G_UNICODE_PRIVATE_USE
87 && t != G_UNICODE_SURROGATE
88 && t != G_UNICODE_SPACE_SEPARATOR);
92 g_unichar_islower (gunichar c)
94 return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
98 g_unichar_isprint (gunichar c)
101 return (t != G_UNICODE_CONTROL
102 && t != G_UNICODE_FORMAT
103 && t != G_UNICODE_UNASSIGNED
104 && t != G_UNICODE_PRIVATE_USE
105 && t != G_UNICODE_SURROGATE);
109 g_unichar_ispunct (gunichar c)
112 return (t == G_UNICODE_CONNECT_PUNCTUATION || t == G_UNICODE_DASH_PUNCTUATION
113 || t == G_UNICODE_CLOSE_PUNCTUATION || t == G_UNICODE_FINAL_PUNCTUATION
114 || t == G_UNICODE_INITIAL_PUNCTUATION || t == G_UNICODE_OTHER_PUNCTUATION
115 || t == G_UNICODE_OPEN_PUNCTUATION);
119 g_unichar_isspace (gunichar c)
122 return (t == G_UNICODE_SPACE_SEPARATOR || t == G_UNICODE_LINE_SEPARATOR
123 || t == G_UNICODE_PARAGRAPH_SEPARATOR);
128 * @c: a unicode character
130 * Determines if a character is uppercase.
135 g_unichar_isupper (gunichar c)
137 return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
142 * @c: a unicode character
144 * Determines if a character is titlecase. Some characters in
145 * Unicode which are composites, such as the DZ digraph
146 * have three case variants instead of just two. The titlecase
147 * form is used at the beginning of a word where only the
148 * first letter is capitalized. The titlecase form of the DZ
149 * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z
151 * Return value: %TRUE if the character is titlecase.
154 g_unichar_istitle (gunichar c)
157 for (i = 0; i < asize (title_table); ++i)
158 if (title_table[i][0] == c)
164 * g_unichar_isxdigit:
165 * @c: a unicode character.
167 * Determines if a characters is a hexidecimal digit
169 * Return value: %TRUE if the character is a hexidecimal digit.
172 g_unichar_isxdigit (gunichar c)
175 return ((c >= 'a' && c <= 'f')
176 || (c >= 'A' && c <= 'F')
181 * g_unichar_isdefined:
182 * @c: a unicode character
184 * Determines if a given character is assigned in the Unicode
187 * Return value: %TRUE if the character has an assigned value.
190 g_unichar_isdefined (gunichar c)
193 return t != G_UNICODE_UNASSIGNED;
198 * @c: a unicode character
200 * Determines if a character is typically rendered in a double-width
203 * Return value: %TRUE if the character is wide.
205 /* This function stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>. */
207 g_unichar_iswide (gunichar c)
212 return ((c >= 0x1100 && c <= 0x115f) /* Hangul Jamo */
213 || (c >= 0x2e80 && c <= 0xa4cf && (c & ~0x0011) != 0x300a &&
214 c != 0x303f) /* CJK ... Yi */
215 || (c >= 0xac00 && c <= 0xd7a3) /* Hangul Syllables */
216 || (c >= 0xf900 && c <= 0xfaff) /* CJK Compatibility Ideographs */
217 || (c >= 0xfe30 && c <= 0xfe6f) /* CJK Compatibility Forms */
218 || (c >= 0xff00 && c <= 0xff5f) /* Fullwidth Forms */
219 || (c >= 0xffe0 && c <= 0xffe6));
224 * @c: a unicode character
226 * Convert a character to uppercase.
228 * Return value: the result of converting @c to uppercase.
229 * If @c is not an lowercase or titlecase character,
230 * @c is returned unchanged.
233 g_unichar_toupper (gunichar c)
236 if (t == G_UNICODE_LOWERCASE_LETTER)
237 return ATTTABLE (c >> 8, c & 0xff);
238 else if (t == G_UNICODE_TITLECASE_LETTER)
241 for (i = 0; i < asize (title_table); ++i)
243 if (title_table[i][0] == c)
244 return title_table[i][1];
252 * @c: a unicode character.
254 * Convert a character to lower case
256 e * Return value: the result of converting @c to lower case.
257 * If @c is not an upperlower or titlecase character,
258 * @c is returned unchanged.
261 g_unichar_tolower (gunichar c)
264 if (t == G_UNICODE_UPPERCASE_LETTER)
265 return ATTTABLE (c >> 8, c & 0xff);
266 else if (t == G_UNICODE_TITLECASE_LETTER)
269 for (i = 0; i < asize (title_table); ++i)
271 if (title_table[i][0] == c)
272 return title_table[i][2];
280 * @c: a unicode character
282 * Convert a character to the titlecase
284 * Return value: the result of converting @c to titlecase.
285 * If @c is not an uppercase or lowercase character,
286 * @c is returned unchanged.
289 g_unichar_totitle (gunichar c)
292 for (i = 0; i < asize (title_table); ++i)
294 if (title_table[i][0] == c || title_table[i][1] == c
295 || title_table[i][2] == c)
296 return title_table[i][0];
298 return (TYPE (c) == G_UNICODE_LOWERCASE_LETTER
299 ? ATTTABLE (c >> 8, c & 0xff)
304 * g_unichar_xdigit_value:
305 * @c: a unicode character
307 * Determines the numeric value of a character as a decimal
310 * Return value: If @c is a decimal digit (according to
311 * `g_unichar_isdigit'), its numeric value. Otherwise, -1.
314 g_unichar_digit_value (gunichar c)
316 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
317 return ATTTABLE (c >> 8, c & 0xff);
322 * g_unichar_xdigit_value:
323 * @c: a unicode character
325 * Determines the numeric value of a character as a hexidecimal
328 * Return value: If @c is a hex digit (according to
329 * `g_unichar_isxdigit'), its numeric value. Otherwise, -1.
332 g_unichar_xdigit_value (gunichar c)
334 if (c >= 'A' && c <= 'F')
336 if (c >= 'a' && c <= 'f')
338 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
339 return ATTTABLE (c >> 8, c & 0xff);
345 * @c: a unicode character
347 * Classifies a unicode character by type.
349 * Return value: the typ of the character.
352 g_unichar_type (gunichar c)