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 (((GPOINTER_TO_INT(type_table[Page]) & 0xff) == GPOINTER_TO_INT(type_table[Page])) \
38 ? GPOINTER_TO_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)
55 * @c: a Unicode character
57 * Determines whether a character is alphanumeric.
58 * Given some UTF-8 text, obtain a character value
59 * with g_utf8_get_char().
61 * Return value: %TRUE if @c is an alphanumeric character
64 g_unichar_isalnum (gunichar c)
67 return ISDIGIT (t) || ISALPHA (t);
72 * @c: a Unicode character
74 * Determines whether a character is alphabetic (i.e. a letter).
75 * Given some UTF-8 text, obtain a character value with
78 * Return value: %TRUE if @c is an alphabetic character
81 g_unichar_isalpha (gunichar c)
90 * @c: a Unicode character
92 * Determines whether a character is a control character.
93 * Given some UTF-8 text, obtain a character value with
96 * Return value: %TRUE if @c is a control character
99 g_unichar_iscntrl (gunichar c)
101 return TYPE (c) == G_UNICODE_CONTROL;
106 * @c: a Unicode character
108 * Determines whether a character is numeric (i.e. a digit). This
109 * covers ASCII 0-9 and also digits in other languages/scripts. Given
110 * some UTF-8 text, obtain a character value with g_utf8_get_char().
112 * Return value: %TRUE if @c is a digit
115 g_unichar_isdigit (gunichar c)
117 return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
123 * @c: a Unicode character
125 * Determines whether a character is printable and not a space
126 * (returns %FALSE for control characters, format characters, and
127 * spaces). g_unichar_isprint() is similar, but returns %TRUE for
128 * spaces. Given some UTF-8 text, obtain a character value with
131 * Return value: %TRUE if @c is printable unless it's a space
134 g_unichar_isgraph (gunichar c)
137 return (t != G_UNICODE_CONTROL
138 && t != G_UNICODE_FORMAT
139 && t != G_UNICODE_UNASSIGNED
140 && t != G_UNICODE_PRIVATE_USE
141 && t != G_UNICODE_SURROGATE
142 && t != G_UNICODE_SPACE_SEPARATOR);
147 * @c: a Unicode character
149 * Determines whether a character is a lowercase letter.
150 * Given some UTF-8 text, obtain a character value with
153 * Return value: %TRUE if @c is a lowercase letter
156 g_unichar_islower (gunichar c)
158 return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
164 * @c: a Unicode character
166 * Determines whether a character is printable.
167 * Unlike g_unichar_isgraph(), returns %TRUE for spaces.
168 * Given some UTF-8 text, obtain a character value with
171 * Return value: %TRUE if @c is printable
174 g_unichar_isprint (gunichar c)
177 return (t != G_UNICODE_CONTROL
178 && t != G_UNICODE_FORMAT
179 && t != G_UNICODE_UNASSIGNED
180 && t != G_UNICODE_PRIVATE_USE
181 && t != G_UNICODE_SURROGATE);
186 * @c: a Unicode character
188 * Determines whether a character is punctuation.
189 * Given some UTF-8 text, obtain a character value with
192 * Return value: %TRUE if @c is a punctuation character
195 g_unichar_ispunct (gunichar c)
198 return (t == G_UNICODE_CONNECT_PUNCTUATION || t == G_UNICODE_DASH_PUNCTUATION
199 || t == G_UNICODE_CLOSE_PUNCTUATION || t == G_UNICODE_FINAL_PUNCTUATION
200 || t == G_UNICODE_INITIAL_PUNCTUATION || t == G_UNICODE_OTHER_PUNCTUATION
201 || t == G_UNICODE_OPEN_PUNCTUATION);
206 * @c: a Unicode character
208 * Determines whether a character is a space, tab, or line separator
209 * (newline, carriage return, etc.). Given some UTF-8 text, obtain a
210 * character value with g_utf8_get_char().
212 * (Note: don't use this to do word breaking; you have to use
213 * Pango or equivalent to get word breaking right, the algorithm
214 * is fairly complex.)
216 * Return value: %TRUE if @c is a punctuation character
219 g_unichar_isspace (gunichar c)
223 /* special-case these since Unicode thinks they are not spaces */
228 case '\v': /* vertical tab - as if anyone has ever used this... */
235 return (t == G_UNICODE_SPACE_SEPARATOR || t == G_UNICODE_LINE_SEPARATOR
236 || t == G_UNICODE_PARAGRAPH_SEPARATOR);
244 * @c: a unicode character
246 * Determines if a character is uppercase.
248 * Return value: %TRUE if @c is an uppercase character.
251 g_unichar_isupper (gunichar c)
253 return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
258 * @c: a unicode character
260 * Determines if a character is titlecase. Some characters in
261 * Unicode which are composites, such as the DZ digraph
262 * have three case variants instead of just two. The titlecase
263 * form is used at the beginning of a word where only the
264 * first letter is capitalized. The titlecase form of the DZ
265 * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z
267 * Return value: %TRUE if the character is titlecase.
270 g_unichar_istitle (gunichar c)
273 for (i = 0; i < asize (title_table); ++i)
274 if (title_table[i][0] == c)
280 * g_unichar_isxdigit:
281 * @c: a unicode character.
283 * Determines if a characters is a hexidecimal digit
285 * Return value: %TRUE if the character is a hexadecimal digit.
288 g_unichar_isxdigit (gunichar c)
291 return ((c >= 'a' && c <= 'f')
292 || (c >= 'A' && c <= 'F')
297 * g_unichar_isdefined:
298 * @c: a unicode character
300 * Determines if a given character is assigned in the Unicode
303 * Return value: %TRUE if the character has an assigned value.
306 g_unichar_isdefined (gunichar c)
309 return t != G_UNICODE_UNASSIGNED;
314 * @c: a unicode character
316 * Determines if a character is typically rendered in a double-width
319 * Return value: %TRUE if the character is wide.
321 /* This function stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>. */
323 g_unichar_iswide (gunichar c)
328 return ((c >= 0x1100 && c <= 0x115f) /* Hangul Jamo */
329 || (c >= 0x2e80 && c <= 0xa4cf && (c & ~0x0011) != 0x300a &&
330 c != 0x303f) /* CJK ... Yi */
331 || (c >= 0xac00 && c <= 0xd7a3) /* Hangul Syllables */
332 || (c >= 0xf900 && c <= 0xfaff) /* CJK Compatibility Ideographs */
333 || (c >= 0xfe30 && c <= 0xfe6f) /* CJK Compatibility Forms */
334 || (c >= 0xff00 && c <= 0xff5f) /* Fullwidth Forms */
335 || (c >= 0xffe0 && c <= 0xffe6));
340 * @c: a unicode character
342 * Convert a character to uppercase.
344 * Return value: the result of converting @c to uppercase.
345 * If @c is not an lowercase or titlecase character,
346 * @c is returned unchanged.
349 g_unichar_toupper (gunichar c)
352 if (t == G_UNICODE_LOWERCASE_LETTER)
353 return ATTTABLE (c >> 8, c & 0xff);
354 else if (t == G_UNICODE_TITLECASE_LETTER)
357 for (i = 0; i < asize (title_table); ++i)
359 if (title_table[i][0] == c)
360 return title_table[i][1];
368 * @c: a unicode character.
370 * Convert a character to lower case
372 * Return value: the result of converting @c to lower case.
373 * If @c is not an upperlower or titlecase character,
374 * @c is returned unchanged.
377 g_unichar_tolower (gunichar c)
380 if (t == G_UNICODE_UPPERCASE_LETTER)
381 return ATTTABLE (c >> 8, c & 0xff);
382 else if (t == G_UNICODE_TITLECASE_LETTER)
385 for (i = 0; i < asize (title_table); ++i)
387 if (title_table[i][0] == c)
388 return title_table[i][2];
396 * @c: a unicode character
398 * Convert a character to the titlecase
400 * Return value: the result of converting @c to titlecase.
401 * If @c is not an uppercase or lowercase character,
402 * @c is returned unchanged.
405 g_unichar_totitle (gunichar c)
408 for (i = 0; i < asize (title_table); ++i)
410 if (title_table[i][0] == c || title_table[i][1] == c
411 || title_table[i][2] == c)
412 return title_table[i][0];
414 return (TYPE (c) == G_UNICODE_LOWERCASE_LETTER
415 ? ATTTABLE (c >> 8, c & 0xff)
420 * g_unichar_digit_value:
421 * @c: a unicode character
423 * Determines the numeric value of a character as a decimal
426 * Return value: If @c is a decimal digit (according to
427 * `g_unichar_isdigit'), its numeric value. Otherwise, -1.
430 g_unichar_digit_value (gunichar c)
432 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
433 return ATTTABLE (c >> 8, c & 0xff);
438 * g_unichar_xdigit_value:
439 * @c: a unicode character
441 * Determines the numeric value of a character as a hexidecimal
444 * Return value: If @c is a hex digit (according to
445 * `g_unichar_isxdigit'), its numeric value. Otherwise, -1.
448 g_unichar_xdigit_value (gunichar c)
450 if (c >= 'A' && c <= 'F')
452 if (c >= 'a' && c <= 'f')
454 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
455 return ATTTABLE (c >> 8, c & 0xff);
461 * @c: a unicode character
463 * Classifies a unicode character by type.
465 * Return value: the type of the character.
468 g_unichar_type (gunichar c)