add a comment
[glib.git] / glib / guniprop.c
blobf2f64172a64ca5f45c280bbedfec9394211d611a
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.
22 #include "config.h"
24 #include <stdlib.h>
25 #include <stddef.h>
26 #include <string.h>
27 #include <locale.h>
29 #include "glib.h"
30 #include "gunichartables.h"
31 #include "gmirroringtable.h"
32 #include "gscripttable.h"
33 #include "gunicodeprivate.h"
34 #include "galias.h"
36 #define ATTR_TABLE(Page) (((Page) <= G_UNICODE_LAST_PAGE_PART1) \
37 ? attr_table_part1[Page] \
38 : attr_table_part2[(Page) - 0xe00])
40 #define ATTTABLE(Page, Char) \
41 ((ATTR_TABLE(Page) == G_UNICODE_MAX_TABLE_INDEX) ? 0 : (attr_data[ATTR_TABLE(Page)][Char]))
43 #define TTYPE_PART1(Page, Char) \
44 ((type_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
45 ? (type_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
46 : (type_data[type_table_part1[Page]][Char]))
48 #define TTYPE_PART2(Page, Char) \
49 ((type_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
50 ? (type_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
51 : (type_data[type_table_part2[Page]][Char]))
53 #define TYPE(Char) \
54 (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
55 ? TTYPE_PART1 ((Char) >> 8, (Char) & 0xff) \
56 : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
57 ? TTYPE_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
58 : G_UNICODE_UNASSIGNED))
61 #define IS(Type, Class) (((guint)1 << (Type)) & (Class))
62 #define OR(Type, Rest) (((guint)1 << (Type)) | (Rest))
66 #define ISALPHA(Type) IS ((Type), \
67 OR (G_UNICODE_LOWERCASE_LETTER, \
68 OR (G_UNICODE_UPPERCASE_LETTER, \
69 OR (G_UNICODE_TITLECASE_LETTER, \
70 OR (G_UNICODE_MODIFIER_LETTER, \
71 OR (G_UNICODE_OTHER_LETTER, 0))))))
73 #define ISALDIGIT(Type) IS ((Type), \
74 OR (G_UNICODE_DECIMAL_NUMBER, \
75 OR (G_UNICODE_LETTER_NUMBER, \
76 OR (G_UNICODE_OTHER_NUMBER, \
77 OR (G_UNICODE_LOWERCASE_LETTER, \
78 OR (G_UNICODE_UPPERCASE_LETTER, \
79 OR (G_UNICODE_TITLECASE_LETTER, \
80 OR (G_UNICODE_MODIFIER_LETTER, \
81 OR (G_UNICODE_OTHER_LETTER, 0)))))))))
83 #define ISMARK(Type) IS ((Type), \
84 OR (G_UNICODE_NON_SPACING_MARK, \
85 OR (G_UNICODE_COMBINING_MARK, \
86 OR (G_UNICODE_ENCLOSING_MARK, 0))))
88 /**
89 * g_unichar_isalnum:
90 * @c: a Unicode character
92 * Determines whether a character is alphanumeric.
93 * Given some UTF-8 text, obtain a character value
94 * with g_utf8_get_char().
96 * Return value: %TRUE if @c is an alphanumeric character
97 **/
98 gboolean
99 g_unichar_isalnum (gunichar c)
101 return ISALDIGIT (TYPE (c)) ? TRUE : FALSE;
105 * g_unichar_isalpha:
106 * @c: a Unicode character
108 * Determines whether a character is alphabetic (i.e. a letter).
109 * Given some UTF-8 text, obtain a character value with
110 * g_utf8_get_char().
112 * Return value: %TRUE if @c is an alphabetic character
114 gboolean
115 g_unichar_isalpha (gunichar c)
117 return ISALPHA (TYPE (c)) ? TRUE : FALSE;
122 * g_unichar_iscntrl:
123 * @c: a Unicode character
125 * Determines whether a character is a control character.
126 * Given some UTF-8 text, obtain a character value with
127 * g_utf8_get_char().
129 * Return value: %TRUE if @c is a control character
131 gboolean
132 g_unichar_iscntrl (gunichar c)
134 return TYPE (c) == G_UNICODE_CONTROL;
138 * g_unichar_isdigit:
139 * @c: a Unicode character
141 * Determines whether a character is numeric (i.e. a digit). This
142 * covers ASCII 0-9 and also digits in other languages/scripts. Given
143 * some UTF-8 text, obtain a character value with g_utf8_get_char().
145 * Return value: %TRUE if @c is a digit
147 gboolean
148 g_unichar_isdigit (gunichar c)
150 return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
155 * g_unichar_isgraph:
156 * @c: a Unicode character
158 * Determines whether a character is printable and not a space
159 * (returns %FALSE for control characters, format characters, and
160 * spaces). g_unichar_isprint() is similar, but returns %TRUE for
161 * spaces. Given some UTF-8 text, obtain a character value with
162 * g_utf8_get_char().
164 * Return value: %TRUE if @c is printable unless it's a space
166 gboolean
167 g_unichar_isgraph (gunichar c)
169 return !IS (TYPE(c),
170 OR (G_UNICODE_CONTROL,
171 OR (G_UNICODE_FORMAT,
172 OR (G_UNICODE_UNASSIGNED,
173 OR (G_UNICODE_PRIVATE_USE,
174 OR (G_UNICODE_SURROGATE,
175 OR (G_UNICODE_SPACE_SEPARATOR,
176 0)))))));
180 * g_unichar_islower:
181 * @c: a Unicode character
183 * Determines whether a character is a lowercase letter.
184 * Given some UTF-8 text, obtain a character value with
185 * g_utf8_get_char().
187 * Return value: %TRUE if @c is a lowercase letter
189 gboolean
190 g_unichar_islower (gunichar c)
192 return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
197 * g_unichar_isprint:
198 * @c: a Unicode character
200 * Determines whether a character is printable.
201 * Unlike g_unichar_isgraph(), returns %TRUE for spaces.
202 * Given some UTF-8 text, obtain a character value with
203 * g_utf8_get_char().
205 * Return value: %TRUE if @c is printable
207 gboolean
208 g_unichar_isprint (gunichar c)
210 return !IS (TYPE(c),
211 OR (G_UNICODE_CONTROL,
212 OR (G_UNICODE_FORMAT,
213 OR (G_UNICODE_UNASSIGNED,
214 OR (G_UNICODE_PRIVATE_USE,
215 OR (G_UNICODE_SURROGATE,
216 0))))));
220 * g_unichar_ispunct:
221 * @c: a Unicode character
223 * Determines whether a character is punctuation or a symbol.
224 * Given some UTF-8 text, obtain a character value with
225 * g_utf8_get_char().
227 * Return value: %TRUE if @c is a punctuation or symbol character
229 gboolean
230 g_unichar_ispunct (gunichar c)
232 return IS (TYPE(c),
233 OR (G_UNICODE_CONNECT_PUNCTUATION,
234 OR (G_UNICODE_DASH_PUNCTUATION,
235 OR (G_UNICODE_CLOSE_PUNCTUATION,
236 OR (G_UNICODE_FINAL_PUNCTUATION,
237 OR (G_UNICODE_INITIAL_PUNCTUATION,
238 OR (G_UNICODE_OTHER_PUNCTUATION,
239 OR (G_UNICODE_OPEN_PUNCTUATION,
240 OR (G_UNICODE_CURRENCY_SYMBOL,
241 OR (G_UNICODE_MODIFIER_SYMBOL,
242 OR (G_UNICODE_MATH_SYMBOL,
243 OR (G_UNICODE_OTHER_SYMBOL,
244 0)))))))))))) ? TRUE : FALSE;
248 * g_unichar_isspace:
249 * @c: a Unicode character
251 * Determines whether a character is a space, tab, or line separator
252 * (newline, carriage return, etc.). Given some UTF-8 text, obtain a
253 * character value with g_utf8_get_char().
255 * (Note: don't use this to do word breaking; you have to use
256 * Pango or equivalent to get word breaking right, the algorithm
257 * is fairly complex.)
259 * Return value: %TRUE if @c is a space character
261 gboolean
262 g_unichar_isspace (gunichar c)
264 switch (c)
266 /* special-case these since Unicode thinks they are not spaces */
267 case '\t':
268 case '\n':
269 case '\r':
270 case '\f':
271 return TRUE;
272 break;
274 default:
276 return IS (TYPE(c),
277 OR (G_UNICODE_SPACE_SEPARATOR,
278 OR (G_UNICODE_LINE_SEPARATOR,
279 OR (G_UNICODE_PARAGRAPH_SEPARATOR,
280 0)))) ? TRUE : FALSE;
282 break;
287 * g_unichar_isupper:
288 * @c: a Unicode character
290 * Determines if a character is uppercase.
292 * Return value: %TRUE if @c is an uppercase character
294 gboolean
295 g_unichar_isupper (gunichar c)
297 return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
301 * g_unichar_istitle:
302 * @c: a Unicode character
304 * Determines if a character is titlecase. Some characters in
305 * Unicode which are composites, such as the DZ digraph
306 * have three case variants instead of just two. The titlecase
307 * form is used at the beginning of a word where only the
308 * first letter is capitalized. The titlecase form of the DZ
309 * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z.
311 * Return value: %TRUE if the character is titlecase
313 gboolean
314 g_unichar_istitle (gunichar c)
316 unsigned int i;
317 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
318 if (title_table[i][0] == c)
319 return TRUE;
320 return FALSE;
324 * g_unichar_isxdigit:
325 * @c: a Unicode character.
327 * Determines if a character is a hexidecimal digit.
329 * Return value: %TRUE if the character is a hexadecimal digit
331 gboolean
332 g_unichar_isxdigit (gunichar c)
334 return ((c >= 'a' && c <= 'f')
335 || (c >= 'A' && c <= 'F')
336 || (TYPE (c) == G_UNICODE_DECIMAL_NUMBER));
340 * g_unichar_isdefined:
341 * @c: a Unicode character
343 * Determines if a given character is assigned in the Unicode
344 * standard.
346 * Return value: %TRUE if the character has an assigned value
348 gboolean
349 g_unichar_isdefined (gunichar c)
351 return TYPE (c) != G_UNICODE_UNASSIGNED;
355 * g_unichar_iswide:
356 * @c: a Unicode character
358 * Determines if a character is typically rendered in a double-width
359 * cell.
361 * Return value: %TRUE if the character is wide
363 /* This function stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>. */
364 gboolean
365 g_unichar_iswide (gunichar c)
367 if (c < 0x1100)
368 return FALSE;
370 return (c <= 0x115f /* Hangul Jamo init. consonants */
371 || c == 0x2329 || c == 0x232a /* angle brackets */
372 || (c >= 0x2e80 && c <= 0xa4cf && (c < 0x302a || c > 0x302f)
373 && c != 0x303f && c != 0x3099 && c!= 0x309a) /* CJK ... Yi */
374 || (c >= 0xac00 && c <= 0xd7a3) /* Hangul Syllables */
375 || (c >= 0xf900 && c <= 0xfaff) /* CJK Compatibility Ideographs */
376 || (c >= 0xfe30 && c <= 0xfe6f) /* CJK Compatibility Forms */
377 || (c >= 0xff00 && c <= 0xff60) /* Fullwidth Forms */
378 || (c >= 0xffe0 && c <= 0xffe6) /* Fullwidth Forms */
379 || (c >= 0x20000 && c <= 0x2fffd) /* CJK extra stuff */
380 || (c >= 0x30000 && c <= 0x3fffd));
384 struct Interval
386 gunichar start, end;
389 static int
390 interval_compare (const void *key, const void *elt)
392 gunichar c = GPOINTER_TO_UINT (key);
393 struct Interval *interval = (struct Interval *)elt;
395 if (c < interval->start)
396 return -1;
397 if (c > interval->end)
398 return +1;
400 return 0;
404 * g_unichar_iswide_cjk:
405 * @c: a Unicode character
407 * Determines if a character is typically rendered in a double-width
408 * cell under legacy East Asian locales. If a character is wide according to
409 * g_unichar_iswide(), then it is also reported wide with this function, but
410 * the converse is not necessarily true. See the
411 * <ulink url="http://www.unicode.org/reports/tr11/">Unicode Standard
412 * Annex #11</ulink> for details.
414 * Return value: %TRUE if the character is wide in legacy East Asian locales
416 * Since: 2.12
418 /* This function stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>. */
419 gboolean
420 g_unichar_iswide_cjk (gunichar c)
422 /* sorted list of non-overlapping intervals of East Asian Ambiguous
423 * characters, generated by "uniset +WIDTH-A -cat=Me -cat=Mn -cat=Cf c" */
424 static const struct Interval ambiguous[] = {
425 { 0x00A1, 0x00A1 }, { 0x00A4, 0x00A4 }, { 0x00A7, 0x00A8 },
426 { 0x00AA, 0x00AA }, { 0x00AE, 0x00AE }, { 0x00B0, 0x00B4 },
427 { 0x00B6, 0x00BA }, { 0x00BC, 0x00BF }, { 0x00C6, 0x00C6 },
428 { 0x00D0, 0x00D0 }, { 0x00D7, 0x00D8 }, { 0x00DE, 0x00E1 },
429 { 0x00E6, 0x00E6 }, { 0x00E8, 0x00EA }, { 0x00EC, 0x00ED },
430 { 0x00F0, 0x00F0 }, { 0x00F2, 0x00F3 }, { 0x00F7, 0x00FA },
431 { 0x00FC, 0x00FC }, { 0x00FE, 0x00FE }, { 0x0101, 0x0101 },
432 { 0x0111, 0x0111 }, { 0x0113, 0x0113 }, { 0x011B, 0x011B },
433 { 0x0126, 0x0127 }, { 0x012B, 0x012B }, { 0x0131, 0x0133 },
434 { 0x0138, 0x0138 }, { 0x013F, 0x0142 }, { 0x0144, 0x0144 },
435 { 0x0148, 0x014B }, { 0x014D, 0x014D }, { 0x0152, 0x0153 },
436 { 0x0166, 0x0167 }, { 0x016B, 0x016B }, { 0x01CE, 0x01CE },
437 { 0x01D0, 0x01D0 }, { 0x01D2, 0x01D2 }, { 0x01D4, 0x01D4 },
438 { 0x01D6, 0x01D6 }, { 0x01D8, 0x01D8 }, { 0x01DA, 0x01DA },
439 { 0x01DC, 0x01DC }, { 0x0251, 0x0251 }, { 0x0261, 0x0261 },
440 { 0x02C4, 0x02C4 }, { 0x02C7, 0x02C7 }, { 0x02C9, 0x02CB },
441 { 0x02CD, 0x02CD }, { 0x02D0, 0x02D0 }, { 0x02D8, 0x02DB },
442 { 0x02DD, 0x02DD }, { 0x02DF, 0x02DF }, { 0x0391, 0x03A1 },
443 { 0x03A3, 0x03A9 }, { 0x03B1, 0x03C1 }, { 0x03C3, 0x03C9 },
444 { 0x0401, 0x0401 }, { 0x0410, 0x044F }, { 0x0451, 0x0451 },
445 { 0x2010, 0x2010 }, { 0x2013, 0x2016 }, { 0x2018, 0x2019 },
446 { 0x201C, 0x201D }, { 0x2020, 0x2022 }, { 0x2024, 0x2027 },
447 { 0x2030, 0x2030 }, { 0x2032, 0x2033 }, { 0x2035, 0x2035 },
448 { 0x203B, 0x203B }, { 0x203E, 0x203E }, { 0x2074, 0x2074 },
449 { 0x207F, 0x207F }, { 0x2081, 0x2084 }, { 0x20AC, 0x20AC },
450 { 0x2103, 0x2103 }, { 0x2105, 0x2105 }, { 0x2109, 0x2109 },
451 { 0x2113, 0x2113 }, { 0x2116, 0x2116 }, { 0x2121, 0x2122 },
452 { 0x2126, 0x2126 }, { 0x212B, 0x212B }, { 0x2153, 0x2154 },
453 { 0x215B, 0x215E }, { 0x2160, 0x216B }, { 0x2170, 0x2179 },
454 { 0x2190, 0x2199 }, { 0x21B8, 0x21B9 }, { 0x21D2, 0x21D2 },
455 { 0x21D4, 0x21D4 }, { 0x21E7, 0x21E7 }, { 0x2200, 0x2200 },
456 { 0x2202, 0x2203 }, { 0x2207, 0x2208 }, { 0x220B, 0x220B },
457 { 0x220F, 0x220F }, { 0x2211, 0x2211 }, { 0x2215, 0x2215 },
458 { 0x221A, 0x221A }, { 0x221D, 0x2220 }, { 0x2223, 0x2223 },
459 { 0x2225, 0x2225 }, { 0x2227, 0x222C }, { 0x222E, 0x222E },
460 { 0x2234, 0x2237 }, { 0x223C, 0x223D }, { 0x2248, 0x2248 },
461 { 0x224C, 0x224C }, { 0x2252, 0x2252 }, { 0x2260, 0x2261 },
462 { 0x2264, 0x2267 }, { 0x226A, 0x226B }, { 0x226E, 0x226F },
463 { 0x2282, 0x2283 }, { 0x2286, 0x2287 }, { 0x2295, 0x2295 },
464 { 0x2299, 0x2299 }, { 0x22A5, 0x22A5 }, { 0x22BF, 0x22BF },
465 { 0x2312, 0x2312 }, { 0x2460, 0x24E9 }, { 0x24EB, 0x254B },
466 { 0x2550, 0x2573 }, { 0x2580, 0x258F }, { 0x2592, 0x2595 },
467 { 0x25A0, 0x25A1 }, { 0x25A3, 0x25A9 }, { 0x25B2, 0x25B3 },
468 { 0x25B6, 0x25B7 }, { 0x25BC, 0x25BD }, { 0x25C0, 0x25C1 },
469 { 0x25C6, 0x25C8 }, { 0x25CB, 0x25CB }, { 0x25CE, 0x25D1 },
470 { 0x25E2, 0x25E5 }, { 0x25EF, 0x25EF }, { 0x2605, 0x2606 },
471 { 0x2609, 0x2609 }, { 0x260E, 0x260F }, { 0x2614, 0x2615 },
472 { 0x261C, 0x261C }, { 0x261E, 0x261E }, { 0x2640, 0x2640 },
473 { 0x2642, 0x2642 }, { 0x2660, 0x2661 }, { 0x2663, 0x2665 },
474 { 0x2667, 0x266A }, { 0x266C, 0x266D }, { 0x266F, 0x266F },
475 { 0x273D, 0x273D }, { 0x2776, 0x277F }, { 0xE000, 0xF8FF },
476 { 0xFFFD, 0xFFFD }, { 0xF0000, 0xFFFFD }, { 0x100000, 0x10FFFD }
479 if (g_unichar_iswide (c))
480 return TRUE;
482 if (bsearch (GUINT_TO_POINTER (c), ambiguous, G_N_ELEMENTS (ambiguous), sizeof ambiguous[0],
483 interval_compare))
484 return TRUE;
486 return FALSE;
491 * g_unichar_toupper:
492 * @c: a Unicode character
494 * Converts a character to uppercase.
496 * Return value: the result of converting @c to uppercase.
497 * If @c is not an lowercase or titlecase character,
498 * or has no upper case equivalent @c is returned unchanged.
500 gunichar
501 g_unichar_toupper (gunichar c)
503 int t = TYPE (c);
504 if (t == G_UNICODE_LOWERCASE_LETTER)
506 gunichar val = ATTTABLE (c >> 8, c & 0xff);
507 if (val >= 0x1000000)
509 const gchar *p = special_case_table + val - 0x1000000;
510 return g_utf8_get_char (p);
512 else
514 /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
515 * do not have an uppercase equivalent, in which case val will be
516 * zero. */
517 return val ? val : c;
520 else if (t == G_UNICODE_TITLECASE_LETTER)
522 unsigned int i;
523 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
525 if (title_table[i][0] == c)
526 return title_table[i][1];
529 return c;
533 * g_unichar_tolower:
534 * @c: a Unicode character.
536 * Converts a character to lower case.
538 * Return value: the result of converting @c to lower case.
539 * If @c is not an upperlower or titlecase character,
540 * or has no lowercase equivalent @c is returned unchanged.
542 gunichar
543 g_unichar_tolower (gunichar c)
545 int t = TYPE (c);
546 if (t == G_UNICODE_UPPERCASE_LETTER)
548 gunichar val = ATTTABLE (c >> 8, c & 0xff);
549 if (val >= 0x1000000)
551 const gchar *p = special_case_table + val - 0x1000000;
552 return g_utf8_get_char (p);
554 else
556 /* Not all uppercase letters are guaranteed to have a lowercase
557 * equivalent. If this is the case, val will be zero. */
558 return val ? val : c;
561 else if (t == G_UNICODE_TITLECASE_LETTER)
563 unsigned int i;
564 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
566 if (title_table[i][0] == c)
567 return title_table[i][2];
570 return c;
574 * g_unichar_totitle:
575 * @c: a Unicode character
577 * Converts a character to the titlecase.
579 * Return value: the result of converting @c to titlecase.
580 * If @c is not an uppercase or lowercase character,
581 * @c is returned unchanged.
583 gunichar
584 g_unichar_totitle (gunichar c)
586 unsigned int i;
587 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
589 if (title_table[i][0] == c || title_table[i][1] == c
590 || title_table[i][2] == c)
591 return title_table[i][0];
593 return (TYPE (c) == G_UNICODE_LOWERCASE_LETTER
594 ? ATTTABLE (c >> 8, c & 0xff)
595 : c);
599 * g_unichar_digit_value:
600 * @c: a Unicode character
602 * Determines the numeric value of a character as a decimal
603 * digit.
605 * Return value: If @c is a decimal digit (according to
606 * g_unichar_isdigit()), its numeric value. Otherwise, -1.
609 g_unichar_digit_value (gunichar c)
611 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
612 return ATTTABLE (c >> 8, c & 0xff);
613 return -1;
617 * g_unichar_xdigit_value:
618 * @c: a Unicode character
620 * Determines the numeric value of a character as a hexidecimal
621 * digit.
623 * Return value: If @c is a hex digit (according to
624 * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
627 g_unichar_xdigit_value (gunichar c)
629 if (c >= 'A' && c <= 'F')
630 return c - 'A' + 10;
631 if (c >= 'a' && c <= 'f')
632 return c - 'a' + 10;
633 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
634 return ATTTABLE (c >> 8, c & 0xff);
635 return -1;
639 * g_unichar_type:
640 * @c: a Unicode character
642 * Classifies a Unicode character by type.
644 * Return value: the type of the character.
646 GUnicodeType
647 g_unichar_type (gunichar c)
649 return TYPE (c);
653 * Case mapping functions
656 typedef enum {
657 LOCALE_NORMAL,
658 LOCALE_TURKIC,
659 LOCALE_LITHUANIAN
660 } LocaleType;
662 static LocaleType
663 get_locale_type (void)
665 #ifdef G_OS_WIN32
666 char *tem = g_win32_getlocale ();
667 char locale[2];
669 locale[0] = tem[0];
670 locale[1] = tem[1];
671 g_free (tem);
672 #else
673 const char *locale = setlocale (LC_CTYPE, NULL);
674 #endif
676 switch (locale[0])
678 case 'a':
679 if (locale[1] == 'z')
680 return LOCALE_TURKIC;
681 break;
682 case 'l':
683 if (locale[1] == 't')
684 return LOCALE_LITHUANIAN;
685 break;
686 case 't':
687 if (locale[1] == 'r')
688 return LOCALE_TURKIC;
689 break;
692 return LOCALE_NORMAL;
695 static gint
696 output_marks (const char **p_inout,
697 char *out_buffer,
698 gboolean remove_dot)
700 const char *p = *p_inout;
701 gint len = 0;
703 while (*p)
705 gunichar c = g_utf8_get_char (p);
707 if (ISMARK (TYPE (c)))
709 if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
710 len += g_unichar_to_utf8 (c, out_buffer ? out_buffer + len : NULL);
711 p = g_utf8_next_char (p);
713 else
714 break;
717 *p_inout = p;
718 return len;
721 static gint
722 output_special_case (gchar *out_buffer,
723 int offset,
724 int type,
725 int which)
727 const gchar *p = special_case_table + offset;
728 gint len;
730 if (type != G_UNICODE_TITLECASE_LETTER)
731 p = g_utf8_next_char (p);
733 if (which == 1)
734 p += strlen (p) + 1;
736 len = strlen (p);
737 if (out_buffer)
738 memcpy (out_buffer, p, len);
740 return len;
743 static gsize
744 real_toupper (const gchar *str,
745 gssize max_len,
746 gchar *out_buffer,
747 LocaleType locale_type)
749 const gchar *p = str;
750 const char *last = NULL;
751 gsize len = 0;
752 gboolean last_was_i = FALSE;
754 while ((max_len < 0 || p < str + max_len) && *p)
756 gunichar c = g_utf8_get_char (p);
757 int t = TYPE (c);
758 gunichar val;
760 last = p;
761 p = g_utf8_next_char (p);
763 if (locale_type == LOCALE_LITHUANIAN)
765 if (c == 'i')
766 last_was_i = TRUE;
767 else
769 if (last_was_i)
771 /* Nasty, need to remove any dot above. Though
772 * I think only E WITH DOT ABOVE occurs in practice
773 * which could simplify this considerably.
775 gsize decomp_len, i;
776 gunichar *decomp;
778 decomp = g_unicode_canonical_decomposition (c, &decomp_len);
779 for (i=0; i < decomp_len; i++)
781 if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
782 len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
784 g_free (decomp);
786 len += output_marks (&p, out_buffer ? out_buffer + len : NULL, TRUE);
788 continue;
791 if (!ISMARK (t))
792 last_was_i = FALSE;
796 if (locale_type == LOCALE_TURKIC && c == 'i')
798 /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
799 len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL);
801 else if (c == 0x0345) /* COMBINING GREEK YPOGEGRAMMENI */
803 /* Nasty, need to move it after other combining marks .. this would go away if
804 * we normalized first.
806 len += output_marks (&p, out_buffer ? out_buffer + len : NULL, FALSE);
808 /* And output as GREEK CAPITAL LETTER IOTA */
809 len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);
811 else if (IS (t,
812 OR (G_UNICODE_LOWERCASE_LETTER,
813 OR (G_UNICODE_TITLECASE_LETTER,
814 0))))
816 val = ATTTABLE (c >> 8, c & 0xff);
818 if (val >= 0x1000000)
820 len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t,
821 t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
823 else
825 if (t == G_UNICODE_TITLECASE_LETTER)
827 unsigned int i;
828 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
830 if (title_table[i][0] == c)
832 val = title_table[i][1];
833 break;
838 /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
839 * do not have an uppercase equivalent, in which case val will be
840 * zero. */
841 len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
844 else
846 gsize char_len = g_utf8_skip[*(guchar *)last];
848 if (out_buffer)
849 memcpy (out_buffer + len, last, char_len);
851 len += char_len;
856 return len;
860 * g_utf8_strup:
861 * @str: a UTF-8 encoded string
862 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
864 * Converts all Unicode characters in the string that have a case
865 * to uppercase. The exact manner that this is done depends
866 * on the current locale, and may result in the number of
867 * characters in the string increasing. (For instance, the
868 * German ess-zet will be changed to SS.)
870 * Return value: a newly allocated string, with all characters
871 * converted to uppercase.
873 gchar *
874 g_utf8_strup (const gchar *str,
875 gssize len)
877 gsize result_len;
878 LocaleType locale_type;
879 gchar *result;
881 g_return_val_if_fail (str != NULL, NULL);
883 locale_type = get_locale_type ();
886 * We use a two pass approach to keep memory management simple
888 result_len = real_toupper (str, len, NULL, locale_type);
889 result = g_malloc (result_len + 1);
890 real_toupper (str, len, result, locale_type);
891 result[result_len] = '\0';
893 return result;
896 /* traverses the string checking for characters with combining class == 230
897 * until a base character is found */
898 static gboolean
899 has_more_above (const gchar *str)
901 const gchar *p = str;
902 gint combining_class;
904 while (*p)
906 combining_class = _g_unichar_combining_class (g_utf8_get_char (p));
907 if (combining_class == 230)
908 return TRUE;
909 else if (combining_class == 0)
910 break;
912 p = g_utf8_next_char (p);
915 return FALSE;
918 static gsize
919 real_tolower (const gchar *str,
920 gssize max_len,
921 gchar *out_buffer,
922 LocaleType locale_type)
924 const gchar *p = str;
925 const char *last = NULL;
926 gsize len = 0;
928 while ((max_len < 0 || p < str + max_len) && *p)
930 gunichar c = g_utf8_get_char (p);
931 int t = TYPE (c);
932 gunichar val;
934 last = p;
935 p = g_utf8_next_char (p);
937 if (locale_type == LOCALE_TURKIC && c == 'I')
939 if (g_utf8_get_char (p) == 0x0307)
941 /* I + COMBINING DOT ABOVE => i (U+0069) */
942 len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL);
943 p = g_utf8_next_char (p);
945 else
947 /* I => LATIN SMALL LETTER DOTLESS I */
948 len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL);
951 /* Introduce an explicit dot above when lowercasing capital I's and J's
952 * whenever there are more accents above. [SpecialCasing.txt] */
953 else if (locale_type == LOCALE_LITHUANIAN &&
954 (c == 0x00cc || c == 0x00cd || c == 0x0128))
956 len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL);
957 len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL);
959 switch (c)
961 case 0x00cc:
962 len += g_unichar_to_utf8 (0x0300, out_buffer ? out_buffer + len : NULL);
963 break;
964 case 0x00cd:
965 len += g_unichar_to_utf8 (0x0301, out_buffer ? out_buffer + len : NULL);
966 break;
967 case 0x0128:
968 len += g_unichar_to_utf8 (0x0303, out_buffer ? out_buffer + len : NULL);
969 break;
972 else if (locale_type == LOCALE_LITHUANIAN &&
973 (c == 'I' || c == 'J' || c == 0x012e) &&
974 has_more_above (p))
976 len += g_unichar_to_utf8 (g_unichar_tolower (c), out_buffer ? out_buffer + len : NULL);
977 len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL);
979 else if (c == 0x03A3) /* GREEK CAPITAL LETTER SIGMA */
981 if ((max_len < 0 || p < str + max_len) && *p)
983 gunichar next_c = g_utf8_get_char (p);
984 int next_type = TYPE(next_c);
986 /* SIGMA mapps differently depending on whether it is
987 * final or not. The following simplified test would
988 * fail in the case of combining marks following the
989 * sigma, but I don't think that occurs in real text.
990 * The test here matches that in ICU.
992 if (ISALPHA (next_type)) /* Lu,Ll,Lt,Lm,Lo */
993 val = 0x3c3; /* GREEK SMALL SIGMA */
994 else
995 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
997 else
998 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
1000 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
1002 else if (IS (t,
1003 OR (G_UNICODE_UPPERCASE_LETTER,
1004 OR (G_UNICODE_TITLECASE_LETTER,
1005 0))))
1007 val = ATTTABLE (c >> 8, c & 0xff);
1009 if (val >= 0x1000000)
1011 len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t, 0);
1013 else
1015 if (t == G_UNICODE_TITLECASE_LETTER)
1017 unsigned int i;
1018 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
1020 if (title_table[i][0] == c)
1022 val = title_table[i][2];
1023 break;
1028 /* Not all uppercase letters are guaranteed to have a lowercase
1029 * equivalent. If this is the case, val will be zero. */
1030 len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
1033 else
1035 gsize char_len = g_utf8_skip[*(guchar *)last];
1037 if (out_buffer)
1038 memcpy (out_buffer + len, last, char_len);
1040 len += char_len;
1045 return len;
1049 * g_utf8_strdown:
1050 * @str: a UTF-8 encoded string
1051 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1053 * Converts all Unicode characters in the string that have a case
1054 * to lowercase. The exact manner that this is done depends
1055 * on the current locale, and may result in the number of
1056 * characters in the string changing.
1058 * Return value: a newly allocated string, with all characters
1059 * converted to lowercase.
1061 gchar *
1062 g_utf8_strdown (const gchar *str,
1063 gssize len)
1065 gsize result_len;
1066 LocaleType locale_type;
1067 gchar *result;
1069 g_return_val_if_fail (str != NULL, NULL);
1071 locale_type = get_locale_type ();
1074 * We use a two pass approach to keep memory management simple
1076 result_len = real_tolower (str, len, NULL, locale_type);
1077 result = g_malloc (result_len + 1);
1078 real_tolower (str, len, result, locale_type);
1079 result[result_len] = '\0';
1081 return result;
1085 * g_utf8_casefold:
1086 * @str: a UTF-8 encoded string
1087 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1089 * Converts a string into a form that is independent of case. The
1090 * result will not correspond to any particular case, but can be
1091 * compared for equality or ordered with the results of calling
1092 * g_utf8_casefold() on other strings.
1094 * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
1095 * only an approximation to the correct linguistic case insensitive
1096 * ordering, though it is a fairly good one. Getting this exactly
1097 * right would require a more sophisticated collation function that
1098 * takes case sensitivity into account. GLib does not currently
1099 * provide such a function.
1101 * Return value: a newly allocated string, that is a
1102 * case independent form of @str.
1104 gchar *
1105 g_utf8_casefold (const gchar *str,
1106 gssize len)
1108 GString *result;
1109 const char *p;
1111 g_return_val_if_fail (str != NULL, NULL);
1113 result = g_string_new (NULL);
1114 p = str;
1115 while ((len < 0 || p < str + len) && *p)
1117 gunichar ch = g_utf8_get_char (p);
1119 int start = 0;
1120 int end = G_N_ELEMENTS (casefold_table);
1122 if (ch >= casefold_table[start].ch &&
1123 ch <= casefold_table[end - 1].ch)
1125 while (TRUE)
1127 int half = (start + end) / 2;
1128 if (ch == casefold_table[half].ch)
1130 g_string_append (result, casefold_table[half].data);
1131 goto next;
1133 else if (half == start)
1134 break;
1135 else if (ch > casefold_table[half].ch)
1136 start = half;
1137 else
1138 end = half;
1142 g_string_append_unichar (result, g_unichar_tolower (ch));
1144 next:
1145 p = g_utf8_next_char (p);
1148 return g_string_free (result, FALSE);
1152 * g_unichar_get_mirror_char:
1153 * @ch: a Unicode character
1154 * @mirrored_ch: location to store the mirrored character
1156 * In Unicode, some characters are <firstterm>mirrored</firstterm>. This
1157 * means that their images are mirrored horizontally in text that is laid
1158 * out from right to left. For instance, "(" would become its mirror image,
1159 * ")", in right-to-left text.
1161 * If @ch has the Unicode mirrored property and there is another unicode
1162 * character that typically has a glyph that is the mirror image of @ch's
1163 * glyph and @mirrored_ch is set, it puts that character in the address
1164 * pointed to by @mirrored_ch. Otherwise the original character is put.
1166 * Return value: %TRUE if @ch has a mirrored character, %FALSE otherwise
1168 * Since: 2.4
1170 gboolean
1171 g_unichar_get_mirror_char (gunichar ch,
1172 gunichar *mirrored_ch)
1174 gboolean found;
1175 gunichar mirrored;
1177 mirrored = GLIB_GET_MIRRORING(ch);
1179 found = ch != mirrored;
1180 if (mirrored_ch)
1181 *mirrored_ch = mirrored;
1183 return found;
1187 #define G_SCRIPT_TABLE_MIDPOINT (G_N_ELEMENTS (g_script_table) / 2)
1189 static inline GUnicodeScript
1190 g_unichar_get_script_bsearch (gunichar ch)
1192 int lower = 0;
1193 int upper = G_N_ELEMENTS (g_script_table) - 1;
1194 static int saved_mid = G_SCRIPT_TABLE_MIDPOINT;
1195 int mid = saved_mid;
1200 if (ch < g_script_table[mid].start)
1201 upper = mid - 1;
1202 else if (ch >= g_script_table[mid].start + g_script_table[mid].chars)
1203 lower = mid + 1;
1204 else
1205 return g_script_table[saved_mid = mid].script;
1207 mid = (lower + upper) / 2;
1209 while (lower <= upper);
1211 return G_UNICODE_SCRIPT_UNKNOWN;
1215 * g_unichar_get_script:
1216 * @ch: a Unicode character
1218 * Looks up the #GUnicodeScript for a particular character (as defined
1219 * by Unicode Standard Annex #24). No check is made for @ch being a
1220 * valid Unicode character; if you pass in invalid character, the
1221 * result is undefined.
1223 * Return value: the #GUnicodeScript for the character.
1225 * Since: 2.14
1227 GUnicodeScript
1228 g_unichar_get_script (gunichar ch)
1230 if (ch < G_EASY_SCRIPTS_RANGE)
1231 return g_script_easy_table[ch];
1232 else
1233 return g_unichar_get_script_bsearch (ch);
1237 #define __G_UNIPROP_C__
1238 #include "galiasdef.c"