#include <stdlib.h>
[platform/upstream/glib.git] / glib / guniprop.c
1 /* guniprop.c - Unicode character properties.
2  *
3  * Copyright (C) 1999 Tom Tromey
4  * Copyright (C) 2000 Red Hat, Inc.
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <stddef.h>
26 #include <string.h>
27 #include <locale.h>
28
29 #include "glib.h"
30 #include "gunichartables.h"
31 #include "gmirroringtable.h"
32 #include "gunicodeprivate.h"
33 #include "galias.h"
34
35 #define ATTR_TABLE(Page) (((Page) <= G_UNICODE_LAST_PAGE_PART1) \
36                           ? attr_table_part1[Page] \
37                           : attr_table_part2[(Page) - 0xe00])
38
39 #define ATTTABLE(Page, Char) \
40   ((ATTR_TABLE(Page) == G_UNICODE_MAX_TABLE_INDEX) ? 0 : (attr_data[ATTR_TABLE(Page)][Char]))
41
42 #define TTYPE_PART1(Page, Char) \
43   ((type_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
44    ? (type_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
45    : (type_data[type_table_part1[Page]][Char]))
46
47 #define TTYPE_PART2(Page, Char) \
48   ((type_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
49    ? (type_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
50    : (type_data[type_table_part2[Page]][Char]))
51
52 #define TYPE(Char) \
53   (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
54    ? TTYPE_PART1 ((Char) >> 8, (Char) & 0xff) \
55    : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
56       ? TTYPE_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
57       : G_UNICODE_UNASSIGNED))
58
59
60 #define IS(Type, Class) (((guint)1 << (Type)) & (Class))
61 #define OR(Type, Rest)  (((guint)1 << (Type)) | (Rest))
62
63
64
65 #define ISDIGIT(Type)   IS ((Type),                             \
66                             OR (G_UNICODE_DECIMAL_NUMBER,       \
67                             OR (G_UNICODE_LETTER_NUMBER,        \
68                             OR (G_UNICODE_OTHER_NUMBER,         0))))
69
70 #define ISALPHA(Type)   IS ((Type),                             \
71                             OR (G_UNICODE_LOWERCASE_LETTER,     \
72                             OR (G_UNICODE_UPPERCASE_LETTER,     \
73                             OR (G_UNICODE_TITLECASE_LETTER,     \
74                             OR (G_UNICODE_MODIFIER_LETTER,      \
75                             OR (G_UNICODE_OTHER_LETTER,         0))))))
76
77 #define ISALDIGIT(Type) IS ((Type),                             \
78                             OR (G_UNICODE_DECIMAL_NUMBER,       \
79                             OR (G_UNICODE_LETTER_NUMBER,        \
80                             OR (G_UNICODE_OTHER_NUMBER,         \
81                             OR (G_UNICODE_LOWERCASE_LETTER,     \
82                             OR (G_UNICODE_UPPERCASE_LETTER,     \
83                             OR (G_UNICODE_TITLECASE_LETTER,     \
84                             OR (G_UNICODE_MODIFIER_LETTER,      \
85                             OR (G_UNICODE_OTHER_LETTER,         0)))))))))
86
87 #define ISMARK(Type)    IS ((Type),                             \
88                             OR (G_UNICODE_NON_SPACING_MARK,     \
89                             OR (G_UNICODE_COMBINING_MARK,       \
90                             OR (G_UNICODE_ENCLOSING_MARK,       0))))
91
92 /**
93  * g_unichar_isalnum:
94  * @c: a Unicode character
95  * 
96  * Determines whether a character is alphanumeric.
97  * Given some UTF-8 text, obtain a character value
98  * with g_utf8_get_char().
99  * 
100  * Return value: %TRUE if @c is an alphanumeric character
101  **/
102 gboolean
103 g_unichar_isalnum (gunichar c)
104 {
105   return ISALDIGIT (TYPE (c)) ? TRUE : FALSE;
106 }
107
108 /**
109  * g_unichar_isalpha:
110  * @c: a Unicode character
111  * 
112  * Determines whether a character is alphabetic (i.e. a letter).
113  * Given some UTF-8 text, obtain a character value with
114  * g_utf8_get_char().
115  * 
116  * Return value: %TRUE if @c is an alphabetic character
117  **/
118 gboolean
119 g_unichar_isalpha (gunichar c)
120 {
121   return ISALPHA (TYPE (c)) ? TRUE : FALSE;
122 }
123
124
125 /**
126  * g_unichar_iscntrl:
127  * @c: a Unicode character
128  * 
129  * Determines whether a character is a control character.
130  * Given some UTF-8 text, obtain a character value with
131  * g_utf8_get_char().
132  * 
133  * Return value: %TRUE if @c is a control character
134  **/
135 gboolean
136 g_unichar_iscntrl (gunichar c)
137 {
138   return TYPE (c) == G_UNICODE_CONTROL;
139 }
140
141 /**
142  * g_unichar_isdigit:
143  * @c: a Unicode character
144  * 
145  * Determines whether a character is numeric (i.e. a digit).  This
146  * covers ASCII 0-9 and also digits in other languages/scripts.  Given
147  * some UTF-8 text, obtain a character value with g_utf8_get_char().
148  * 
149  * Return value: %TRUE if @c is a digit
150  **/
151 gboolean
152 g_unichar_isdigit (gunichar c)
153 {
154   return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
155 }
156
157
158 /**
159  * g_unichar_isgraph:
160  * @c: a Unicode character
161  * 
162  * Determines whether a character is printable and not a space
163  * (returns %FALSE for control characters, format characters, and
164  * spaces). g_unichar_isprint() is similar, but returns %TRUE for
165  * spaces. Given some UTF-8 text, obtain a character value with
166  * g_utf8_get_char().
167  * 
168  * Return value: %TRUE if @c is printable unless it's a space
169  **/
170 gboolean
171 g_unichar_isgraph (gunichar c)
172 {
173   return !IS (TYPE(c),
174               OR (G_UNICODE_CONTROL,
175               OR (G_UNICODE_FORMAT,
176               OR (G_UNICODE_UNASSIGNED,
177               OR (G_UNICODE_PRIVATE_USE,
178               OR (G_UNICODE_SURROGATE,
179               OR (G_UNICODE_SPACE_SEPARATOR,
180              0)))))));
181 }
182
183 /**
184  * g_unichar_islower:
185  * @c: a Unicode character
186  * 
187  * Determines whether a character is a lowercase letter.
188  * Given some UTF-8 text, obtain a character value with
189  * g_utf8_get_char().
190  * 
191  * Return value: %TRUE if @c is a lowercase letter
192  **/
193 gboolean
194 g_unichar_islower (gunichar c)
195 {
196   return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
197 }
198
199
200 /**
201  * g_unichar_isprint:
202  * @c: a Unicode character
203  * 
204  * Determines whether a character is printable.
205  * Unlike g_unichar_isgraph(), returns %TRUE for spaces.
206  * Given some UTF-8 text, obtain a character value with
207  * g_utf8_get_char().
208  * 
209  * Return value: %TRUE if @c is printable
210  **/
211 gboolean
212 g_unichar_isprint (gunichar c)
213 {
214   return !IS (TYPE(c),
215               OR (G_UNICODE_CONTROL,
216               OR (G_UNICODE_FORMAT,
217               OR (G_UNICODE_UNASSIGNED,
218               OR (G_UNICODE_PRIVATE_USE,
219               OR (G_UNICODE_SURROGATE,
220              0))))));
221 }
222
223 /**
224  * g_unichar_ispunct:
225  * @c: a Unicode character
226  * 
227  * Determines whether a character is punctuation or a symbol.
228  * Given some UTF-8 text, obtain a character value with
229  * g_utf8_get_char().
230  * 
231  * Return value: %TRUE if @c is a punctuation or symbol character
232  **/
233 gboolean
234 g_unichar_ispunct (gunichar c)
235 {
236   return IS (TYPE(c),
237              OR (G_UNICODE_CONNECT_PUNCTUATION,
238              OR (G_UNICODE_DASH_PUNCTUATION,
239              OR (G_UNICODE_CLOSE_PUNCTUATION,
240              OR (G_UNICODE_FINAL_PUNCTUATION,
241              OR (G_UNICODE_INITIAL_PUNCTUATION,
242              OR (G_UNICODE_OTHER_PUNCTUATION,
243              OR (G_UNICODE_OPEN_PUNCTUATION,
244              OR (G_UNICODE_CURRENCY_SYMBOL,
245              OR (G_UNICODE_MODIFIER_SYMBOL,
246              OR (G_UNICODE_MATH_SYMBOL,
247              OR (G_UNICODE_OTHER_SYMBOL,
248             0)))))))))))) ? TRUE : FALSE;
249 }
250
251 /**
252  * g_unichar_isspace:
253  * @c: a Unicode character
254  * 
255  * Determines whether a character is a space, tab, or line separator
256  * (newline, carriage return, etc.).  Given some UTF-8 text, obtain a
257  * character value with g_utf8_get_char().
258  *
259  * (Note: don't use this to do word breaking; you have to use
260  * Pango or equivalent to get word breaking right, the algorithm
261  * is fairly complex.)
262  *  
263  * Return value: %TRUE if @c is a space character
264  **/
265 gboolean
266 g_unichar_isspace (gunichar c)
267 {
268   switch (c)
269     {
270       /* special-case these since Unicode thinks they are not spaces */
271     case '\t':
272     case '\n':
273     case '\r':
274     case '\f':
275       return TRUE;
276       break;
277       
278     default:
279       {
280         return IS (TYPE(c),
281                    OR (G_UNICODE_SPACE_SEPARATOR,
282                    OR (G_UNICODE_LINE_SEPARATOR,
283                    OR (G_UNICODE_PARAGRAPH_SEPARATOR,
284                   0)))) ? TRUE : FALSE;
285       }
286       break;
287     }
288 }
289
290 /**
291  * g_unichar_isupper:
292  * @c: a Unicode character
293  * 
294  * Determines if a character is uppercase.
295  * 
296  * Return value: %TRUE if @c is an uppercase character
297  **/
298 gboolean
299 g_unichar_isupper (gunichar c)
300 {
301   return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
302 }
303
304 /**
305  * g_unichar_istitle:
306  * @c: a Unicode character
307  * 
308  * Determines if a character is titlecase. Some characters in
309  * Unicode which are composites, such as the DZ digraph
310  * have three case variants instead of just two. The titlecase
311  * form is used at the beginning of a word where only the
312  * first letter is capitalized. The titlecase form of the DZ
313  * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z.
314  * 
315  * Return value: %TRUE if the character is titlecase
316  **/
317 gboolean
318 g_unichar_istitle (gunichar c)
319 {
320   unsigned int i;
321   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
322     if (title_table[i][0] == c)
323       return 1;
324   return 0;
325 }
326
327 /**
328  * g_unichar_isxdigit:
329  * @c: a Unicode character.
330  * 
331  * Determines if a character is a hexidecimal digit.
332  * 
333  * Return value: %TRUE if the character is a hexadecimal digit
334  **/
335 gboolean
336 g_unichar_isxdigit (gunichar c)
337 {
338   return ((c >= 'a' && c <= 'f')
339           || (c >= 'A' && c <= 'F')
340           || ISDIGIT (TYPE (c)));
341 }
342
343 /**
344  * g_unichar_isdefined:
345  * @c: a Unicode character
346  * 
347  * Determines if a given character is assigned in the Unicode
348  * standard.
349  *
350  * Return value: %TRUE if the character has an assigned value
351  **/
352 gboolean
353 g_unichar_isdefined (gunichar c)
354 {
355   return TYPE (c) != G_UNICODE_UNASSIGNED;
356 }
357
358 /**
359  * g_unichar_iswide:
360  * @c: a Unicode character
361  * 
362  * Determines if a character is typically rendered in a double-width
363  * cell.
364  * 
365  * Return value: %TRUE if the character is wide
366  **/
367 /* This function stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>.  */
368 gboolean
369 g_unichar_iswide (gunichar c)
370 {
371   if (c < 0x1100)
372     return FALSE;
373
374   return (c <= 0x115f  /* Hangul Jamo init. consonants */ 
375           || c == 0x2329 || c == 0x232a     /* angle brackets */
376           || (c >= 0x2e80 && c <= 0xa4cf && (c < 0x302a || c > 0x302f) 
377               && c != 0x303f && c != 0x3099 && c!= 0x309a) /* CJK ... Yi */
378           || (c >= 0xac00 && c <= 0xd7a3)   /* Hangul Syllables */
379           || (c >= 0xf900 && c <= 0xfaff)   /* CJK Compatibility Ideographs */
380           || (c >= 0xfe30 && c <= 0xfe6f)   /* CJK Compatibility Forms */
381           || (c >= 0xff00 && c <= 0xff60)   /* Fullwidth Forms */
382           || (c >= 0xffe0 && c <= 0xffe6)   /* Fullwidth Forms */
383           || (c >= 0x20000 && c <= 0x2fffd) /* CJK extra stuff */
384           || (c >= 0x30000 && c <= 0x3fffd));
385 }
386
387
388 struct Interval
389 {
390   gunichar start, end;
391 };
392
393 int
394 interval_compare (const void *key, const void *elt)
395 {
396   gunichar c = GPOINTER_TO_UINT (key);
397   struct Interval *interval = elt;
398
399   if (c < interval->start)
400     return -1;
401   if (c > interval->end)
402     return +1;
403
404   return 0;
405 }
406
407 /**
408  * g_unichar_iswide_cjk:
409  * @c: a Unicode character
410  * 
411  * Determines if a character is typically rendered in a double-width
412  * cell under legacy East Asian locales.  If a character is wide according to
413  * g_unichar_iswide(), then it is also reported wide with this function, but
414  * the converse is not necessarily true.  See the
415  * <ulink url="http://www.unicode.org/reports/tr11/">Unicode Standard
416  * Annex #11</ulink> for details.
417  * 
418  * Return value: %TRUE if the character is wide in legacy East Asian locales
419  *
420  * Since: 2.12
421  */
422 /* This function stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>.  */
423 gboolean
424 g_unichar_iswide_cjk (gunichar c)
425 {
426   /* sorted list of non-overlapping intervals of East Asian Ambiguous
427    * characters, generated by "uniset +WIDTH-A -cat=Me -cat=Mn -cat=Cf c" */
428   static const struct Interval ambiguous[] = {
429     { 0x00A1, 0x00A1 }, { 0x00A4, 0x00A4 }, { 0x00A7, 0x00A8 },
430     { 0x00AA, 0x00AA }, { 0x00AE, 0x00AE }, { 0x00B0, 0x00B4 },
431     { 0x00B6, 0x00BA }, { 0x00BC, 0x00BF }, { 0x00C6, 0x00C6 },
432     { 0x00D0, 0x00D0 }, { 0x00D7, 0x00D8 }, { 0x00DE, 0x00E1 },
433     { 0x00E6, 0x00E6 }, { 0x00E8, 0x00EA }, { 0x00EC, 0x00ED },
434     { 0x00F0, 0x00F0 }, { 0x00F2, 0x00F3 }, { 0x00F7, 0x00FA },
435     { 0x00FC, 0x00FC }, { 0x00FE, 0x00FE }, { 0x0101, 0x0101 },
436     { 0x0111, 0x0111 }, { 0x0113, 0x0113 }, { 0x011B, 0x011B },
437     { 0x0126, 0x0127 }, { 0x012B, 0x012B }, { 0x0131, 0x0133 },
438     { 0x0138, 0x0138 }, { 0x013F, 0x0142 }, { 0x0144, 0x0144 },
439     { 0x0148, 0x014B }, { 0x014D, 0x014D }, { 0x0152, 0x0153 },
440     { 0x0166, 0x0167 }, { 0x016B, 0x016B }, { 0x01CE, 0x01CE },
441     { 0x01D0, 0x01D0 }, { 0x01D2, 0x01D2 }, { 0x01D4, 0x01D4 },
442     { 0x01D6, 0x01D6 }, { 0x01D8, 0x01D8 }, { 0x01DA, 0x01DA },
443     { 0x01DC, 0x01DC }, { 0x0251, 0x0251 }, { 0x0261, 0x0261 },
444     { 0x02C4, 0x02C4 }, { 0x02C7, 0x02C7 }, { 0x02C9, 0x02CB },
445     { 0x02CD, 0x02CD }, { 0x02D0, 0x02D0 }, { 0x02D8, 0x02DB },
446     { 0x02DD, 0x02DD }, { 0x02DF, 0x02DF }, { 0x0391, 0x03A1 },
447     { 0x03A3, 0x03A9 }, { 0x03B1, 0x03C1 }, { 0x03C3, 0x03C9 },
448     { 0x0401, 0x0401 }, { 0x0410, 0x044F }, { 0x0451, 0x0451 },
449     { 0x2010, 0x2010 }, { 0x2013, 0x2016 }, { 0x2018, 0x2019 },
450     { 0x201C, 0x201D }, { 0x2020, 0x2022 }, { 0x2024, 0x2027 },
451     { 0x2030, 0x2030 }, { 0x2032, 0x2033 }, { 0x2035, 0x2035 },
452     { 0x203B, 0x203B }, { 0x203E, 0x203E }, { 0x2074, 0x2074 },
453     { 0x207F, 0x207F }, { 0x2081, 0x2084 }, { 0x20AC, 0x20AC },
454     { 0x2103, 0x2103 }, { 0x2105, 0x2105 }, { 0x2109, 0x2109 },
455     { 0x2113, 0x2113 }, { 0x2116, 0x2116 }, { 0x2121, 0x2122 },
456     { 0x2126, 0x2126 }, { 0x212B, 0x212B }, { 0x2153, 0x2154 },
457     { 0x215B, 0x215E }, { 0x2160, 0x216B }, { 0x2170, 0x2179 },
458     { 0x2190, 0x2199 }, { 0x21B8, 0x21B9 }, { 0x21D2, 0x21D2 },
459     { 0x21D4, 0x21D4 }, { 0x21E7, 0x21E7 }, { 0x2200, 0x2200 },
460     { 0x2202, 0x2203 }, { 0x2207, 0x2208 }, { 0x220B, 0x220B },
461     { 0x220F, 0x220F }, { 0x2211, 0x2211 }, { 0x2215, 0x2215 },
462     { 0x221A, 0x221A }, { 0x221D, 0x2220 }, { 0x2223, 0x2223 },
463     { 0x2225, 0x2225 }, { 0x2227, 0x222C }, { 0x222E, 0x222E },
464     { 0x2234, 0x2237 }, { 0x223C, 0x223D }, { 0x2248, 0x2248 },
465     { 0x224C, 0x224C }, { 0x2252, 0x2252 }, { 0x2260, 0x2261 },
466     { 0x2264, 0x2267 }, { 0x226A, 0x226B }, { 0x226E, 0x226F },
467     { 0x2282, 0x2283 }, { 0x2286, 0x2287 }, { 0x2295, 0x2295 },
468     { 0x2299, 0x2299 }, { 0x22A5, 0x22A5 }, { 0x22BF, 0x22BF },
469     { 0x2312, 0x2312 }, { 0x2460, 0x24E9 }, { 0x24EB, 0x254B },
470     { 0x2550, 0x2573 }, { 0x2580, 0x258F }, { 0x2592, 0x2595 },
471     { 0x25A0, 0x25A1 }, { 0x25A3, 0x25A9 }, { 0x25B2, 0x25B3 },
472     { 0x25B6, 0x25B7 }, { 0x25BC, 0x25BD }, { 0x25C0, 0x25C1 },
473     { 0x25C6, 0x25C8 }, { 0x25CB, 0x25CB }, { 0x25CE, 0x25D1 },
474     { 0x25E2, 0x25E5 }, { 0x25EF, 0x25EF }, { 0x2605, 0x2606 },
475     { 0x2609, 0x2609 }, { 0x260E, 0x260F }, { 0x2614, 0x2615 },
476     { 0x261C, 0x261C }, { 0x261E, 0x261E }, { 0x2640, 0x2640 },
477     { 0x2642, 0x2642 }, { 0x2660, 0x2661 }, { 0x2663, 0x2665 },
478     { 0x2667, 0x266A }, { 0x266C, 0x266D }, { 0x266F, 0x266F },
479     { 0x273D, 0x273D }, { 0x2776, 0x277F }, { 0xE000, 0xF8FF },
480     { 0xFFFD, 0xFFFD }, { 0xF0000, 0xFFFFD }, { 0x100000, 0x10FFFD }
481   };
482
483   if (g_unichar_iswide (c))
484     return TRUE;
485
486   if (bsearch (GUINT_TO_POINTER (c), ambiguous, G_N_ELEMENTS (ambiguous), sizeof ambiguous[0],
487                interval_compare))
488     return TRUE;
489
490   return FALSE;
491 }
492
493
494 /**
495  * g_unichar_toupper:
496  * @c: a Unicode character
497  * 
498  * Converts a character to uppercase.
499  * 
500  * Return value: the result of converting @c to uppercase.
501  *               If @c is not an lowercase or titlecase character,
502  *               or has no upper case equivalent @c is returned unchanged.
503  **/
504 gunichar
505 g_unichar_toupper (gunichar c)
506 {
507   int t = TYPE (c);
508   if (t == G_UNICODE_LOWERCASE_LETTER)
509     {
510       gunichar val = ATTTABLE (c >> 8, c & 0xff);
511       if (val >= 0x1000000)
512         {
513           const gchar *p = special_case_table + val - 0x1000000;
514           return g_utf8_get_char (p);
515         }
516       else
517         return val ? val : c;
518     }
519   else if (t == G_UNICODE_TITLECASE_LETTER)
520     {
521       unsigned int i;
522       for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
523         {
524           if (title_table[i][0] == c)
525             return title_table[i][1];
526         }
527     }
528   return c;
529 }
530
531 /**
532  * g_unichar_tolower:
533  * @c: a Unicode character.
534  * 
535  * Converts a character to lower case.
536  * 
537  * Return value: the result of converting @c to lower case.
538  *               If @c is not an upperlower or titlecase character,
539  *               or has no lowercase equivalent @c is returned unchanged.
540  **/
541 gunichar
542 g_unichar_tolower (gunichar c)
543 {
544   int t = TYPE (c);
545   if (t == G_UNICODE_UPPERCASE_LETTER)
546     {
547       gunichar val = ATTTABLE (c >> 8, c & 0xff);
548       if (val >= 0x1000000)
549         {
550           const gchar *p = special_case_table + val - 0x1000000;
551           return g_utf8_get_char (p);
552         }
553       else
554         return val ? val : c;
555     }
556   else if (t == G_UNICODE_TITLECASE_LETTER)
557     {
558       unsigned int i;
559       for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
560         {
561           if (title_table[i][0] == c)
562             return title_table[i][2];
563         }
564     }
565   return c;
566 }
567
568 /**
569  * g_unichar_totitle:
570  * @c: a Unicode character
571  * 
572  * Converts a character to the titlecase.
573  * 
574  * Return value: the result of converting @c to titlecase.
575  *               If @c is not an uppercase or lowercase character,
576  *               @c is returned unchanged.
577  **/
578 gunichar
579 g_unichar_totitle (gunichar c)
580 {
581   unsigned int i;
582   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
583     {
584       if (title_table[i][0] == c || title_table[i][1] == c
585           || title_table[i][2] == c)
586         return title_table[i][0];
587     }
588   return (TYPE (c) == G_UNICODE_LOWERCASE_LETTER
589           ? ATTTABLE (c >> 8, c & 0xff)
590           : c);
591 }
592
593 /**
594  * g_unichar_digit_value:
595  * @c: a Unicode character
596  *
597  * Determines the numeric value of a character as a decimal
598  * digit.
599  *
600  * Return value: If @c is a decimal digit (according to
601  * g_unichar_isdigit()), its numeric value. Otherwise, -1.
602  **/
603 int
604 g_unichar_digit_value (gunichar c)
605 {
606   if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
607     return ATTTABLE (c >> 8, c & 0xff);
608   return -1;
609 }
610
611 /**
612  * g_unichar_xdigit_value:
613  * @c: a Unicode character
614  *
615  * Determines the numeric value of a character as a hexidecimal
616  * digit.
617  *
618  * Return value: If @c is a hex digit (according to
619  * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
620  **/
621 int
622 g_unichar_xdigit_value (gunichar c)
623 {
624   if (c >= 'A' && c <= 'F')
625     return c - 'A' + 10;
626   if (c >= 'a' && c <= 'f')
627     return c - 'a' + 10;
628   if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
629     return ATTTABLE (c >> 8, c & 0xff);
630   return -1;
631 }
632
633 /**
634  * g_unichar_type:
635  * @c: a Unicode character
636  * 
637  * Classifies a Unicode character by type.
638  * 
639  * Return value: the type of the character.
640  **/
641 GUnicodeType
642 g_unichar_type (gunichar c)
643 {
644   return TYPE (c);
645 }
646
647 /*
648  * Case mapping functions
649  */
650
651 typedef enum {
652   LOCALE_NORMAL,
653   LOCALE_TURKIC,
654   LOCALE_LITHUANIAN
655 } LocaleType;
656
657 static LocaleType
658 get_locale_type (void)
659 {
660 #ifdef G_OS_WIN32
661   char *tem = g_win32_getlocale ();
662   char locale[2];
663
664   locale[0] = tem[0];
665   locale[1] = tem[1];
666   g_free (tem);
667 #else
668   const char *locale = setlocale (LC_CTYPE, NULL);
669 #endif
670
671   switch (locale[0])
672     {
673    case 'a':
674       if (locale[1] == 'z')
675         return LOCALE_TURKIC;
676       break;
677     case 'l':
678       if (locale[1] == 't')
679         return LOCALE_LITHUANIAN;
680       break;
681     case 't':
682       if (locale[1] == 'r')
683         return LOCALE_TURKIC;
684       break;
685     }
686
687   return LOCALE_NORMAL;
688 }
689
690 static gint
691 output_marks (const char **p_inout,
692               char        *out_buffer,
693               gboolean     remove_dot)
694 {
695   const char *p = *p_inout;
696   gint len = 0;
697   
698   while (*p)
699     {
700       gunichar c = g_utf8_get_char (p);
701       
702       if (ISMARK (TYPE (c)))
703         {
704           if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
705             len += g_unichar_to_utf8 (c, out_buffer ? out_buffer + len : NULL);
706           p = g_utf8_next_char (p);
707         }
708       else
709         break;
710     }
711
712   *p_inout = p;
713   return len;
714 }
715
716 static gint
717 output_special_case (gchar *out_buffer,
718                      int    offset,
719                      int    type,
720                      int    which)
721 {
722   const gchar *p = special_case_table + offset;
723   gint len;
724
725   if (type != G_UNICODE_TITLECASE_LETTER)
726     p = g_utf8_next_char (p);
727
728   if (which == 1)
729     p += strlen (p) + 1;
730
731   len = strlen (p);
732   if (out_buffer)
733     memcpy (out_buffer, p, len);
734
735   return len;
736 }
737
738 static gsize
739 real_toupper (const gchar *str,
740               gssize       max_len,
741               gchar       *out_buffer,
742               LocaleType   locale_type)
743 {
744   const gchar *p = str;
745   const char *last = NULL;
746   gsize len = 0;
747   gboolean last_was_i = FALSE;
748
749   while ((max_len < 0 || p < str + max_len) && *p)
750     {
751       gunichar c = g_utf8_get_char (p);
752       int t = TYPE (c);
753       gunichar val;
754
755       last = p;
756       p = g_utf8_next_char (p);
757
758       if (locale_type == LOCALE_LITHUANIAN)
759         {
760           if (c == 'i')
761             last_was_i = TRUE;
762           else 
763             {
764               if (last_was_i)
765                 {
766                   /* Nasty, need to remove any dot above. Though
767                    * I think only E WITH DOT ABOVE occurs in practice
768                    * which could simplify this considerably.
769                    */
770                   gsize decomp_len, i;
771                   gunichar *decomp;
772
773                   decomp = g_unicode_canonical_decomposition (c, &decomp_len);
774                   for (i=0; i < decomp_len; i++)
775                     {
776                       if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
777                         len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
778                     }
779                   g_free (decomp);
780                   
781                   len += output_marks (&p, out_buffer ? out_buffer + len : NULL, TRUE);
782
783                   continue;
784                 }
785
786               if (!ISMARK (t))
787                 last_was_i = FALSE;
788             }
789         }
790       
791       if (locale_type == LOCALE_TURKIC && c == 'i')
792         {
793           /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
794           len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL); 
795         }
796       else if (c == 0x0345)     /* COMBINING GREEK YPOGEGRAMMENI */
797         {
798           /* Nasty, need to move it after other combining marks .. this would go away if
799            * we normalized first.
800            */
801           len += output_marks (&p, out_buffer ? out_buffer + len : NULL, FALSE);
802
803           /* And output as GREEK CAPITAL LETTER IOTA */
804           len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);         
805         }
806       else if (IS (t,
807                    OR (G_UNICODE_LOWERCASE_LETTER,
808                    OR (G_UNICODE_TITLECASE_LETTER,
809                   0))))
810         {
811           val = ATTTABLE (c >> 8, c & 0xff);
812
813           if (val >= 0x1000000)
814             {
815               len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t,
816                                           t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
817             }
818           else
819             {
820               if (t == G_UNICODE_TITLECASE_LETTER)
821                 {
822                   unsigned int i;
823                   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
824                     {
825                       if (title_table[i][0] == c)
826                         val = title_table[i][1];
827                     }
828                 }
829
830               len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
831             }
832         }
833       else
834         {
835           gsize char_len = g_utf8_skip[*(guchar *)last];
836
837           if (out_buffer)
838             memcpy (out_buffer + len, last, char_len);
839
840           len += char_len;
841         }
842
843     }
844
845   return len;
846 }
847
848 /**
849  * g_utf8_strup:
850  * @str: a UTF-8 encoded string
851  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
852  * 
853  * Converts all Unicode characters in the string that have a case
854  * to uppercase. The exact manner that this is done depends
855  * on the current locale, and may result in the number of
856  * characters in the string increasing. (For instance, the
857  * German ess-zet will be changed to SS.)
858  * 
859  * Return value: a newly allocated string, with all characters
860  *    converted to uppercase.  
861  **/
862 gchar *
863 g_utf8_strup (const gchar *str,
864               gssize       len)
865 {
866   gsize result_len;
867   LocaleType locale_type;
868   gchar *result;
869
870   g_return_val_if_fail (str != NULL, NULL);
871
872   locale_type = get_locale_type ();
873   
874   /*
875    * We use a two pass approach to keep memory management simple
876    */
877   result_len = real_toupper (str, len, NULL, locale_type);
878   result = g_malloc (result_len + 1);
879   real_toupper (str, len, result, locale_type);
880   result[result_len] = '\0';
881
882   return result;
883 }
884
885 /* traverses the string checking for characters with combining class == 230
886  * until a base character is found */
887 static gboolean
888 has_more_above (const gchar *str)
889 {
890   const gchar *p = str;
891   gint combining_class;
892
893   while (*p)
894     {
895       combining_class = _g_unichar_combining_class (g_utf8_get_char (p));
896       if (combining_class == 230)
897         return TRUE;
898       else if (combining_class == 0)
899         break;
900
901       p = g_utf8_next_char (p);
902     }
903
904   return FALSE;
905 }
906
907 static gsize
908 real_tolower (const gchar *str,
909               gssize       max_len,
910               gchar       *out_buffer,
911               LocaleType   locale_type)
912 {
913   const gchar *p = str;
914   const char *last = NULL;
915   gsize len = 0;
916
917   while ((max_len < 0 || p < str + max_len) && *p)
918     {
919       gunichar c = g_utf8_get_char (p);
920       int t = TYPE (c);
921       gunichar val;
922
923       last = p;
924       p = g_utf8_next_char (p);
925
926       if (locale_type == LOCALE_TURKIC && c == 'I')
927         {
928           if (g_utf8_get_char (p) == 0x0307)
929             {
930               /* I + COMBINING DOT ABOVE => i (U+0069) */
931               len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL); 
932               p = g_utf8_next_char (p);
933             }
934           else
935             {
936               /* I => LATIN SMALL LETTER DOTLESS I */
937               len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL); 
938             }
939         }
940       /* Introduce an explicit dot above when lowercasing capital I's and J's
941        * whenever there are more accents above. [SpecialCasing.txt] */
942       else if (locale_type == LOCALE_LITHUANIAN && 
943                (c == 0x00cc || c == 0x00cd || c == 0x0128))
944         {
945           len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL); 
946           len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL); 
947
948           switch (c)
949             {
950             case 0x00cc: 
951               len += g_unichar_to_utf8 (0x0300, out_buffer ? out_buffer + len : NULL); 
952               break;
953             case 0x00cd: 
954               len += g_unichar_to_utf8 (0x0301, out_buffer ? out_buffer + len : NULL); 
955               break;
956             case 0x0128: 
957               len += g_unichar_to_utf8 (0x0303, out_buffer ? out_buffer + len : NULL); 
958               break;
959             }
960         }
961       else if (locale_type == LOCALE_LITHUANIAN && 
962                (c == 'I' || c == 'J' || c == 0x012e) && 
963                has_more_above (p))
964         {
965           len += g_unichar_to_utf8 (g_unichar_tolower (c), out_buffer ? out_buffer + len : NULL); 
966           len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL); 
967         }
968       else if (c == 0x03A3)     /* GREEK CAPITAL LETTER SIGMA */
969         {
970           if ((max_len < 0 || p < str + max_len) && *p)
971             {
972               gunichar next_c = g_utf8_get_char (p);
973               int next_type = TYPE(next_c);
974
975               /* SIGMA mapps differently depending on whether it is
976                * final or not. The following simplified test would
977                * fail in the case of combining marks following the
978                * sigma, but I don't think that occurs in real text.
979                * The test here matches that in ICU.
980                */
981               if (ISALPHA (next_type)) /* Lu,Ll,Lt,Lm,Lo */
982                 val = 0x3c3;    /* GREEK SMALL SIGMA */
983               else
984                 val = 0x3c2;    /* GREEK SMALL FINAL SIGMA */
985             }
986           else
987             val = 0x3c2;        /* GREEK SMALL FINAL SIGMA */
988
989           len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
990         }
991       else if (IS (t,
992                    OR (G_UNICODE_UPPERCASE_LETTER,
993                    OR (G_UNICODE_TITLECASE_LETTER,
994                   0))))
995         {
996           val = ATTTABLE (c >> 8, c & 0xff);
997
998           if (val >= 0x1000000)
999             {
1000               len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t, 0);
1001             }
1002           else
1003             {
1004               if (t == G_UNICODE_TITLECASE_LETTER)
1005                 {
1006                   unsigned int i;
1007                   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
1008                     {
1009                       if (title_table[i][0] == c)
1010                         val = title_table[i][2];
1011                     }
1012                 }
1013
1014               len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
1015             }
1016         }
1017       else
1018         {
1019           gsize char_len = g_utf8_skip[*(guchar *)last];
1020
1021           if (out_buffer)
1022             memcpy (out_buffer + len, last, char_len);
1023
1024           len += char_len;
1025         }
1026
1027     }
1028
1029   return len;
1030 }
1031
1032 /**
1033  * g_utf8_strdown:
1034  * @str: a UTF-8 encoded string
1035  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1036  * 
1037  * Converts all Unicode characters in the string that have a case
1038  * to lowercase. The exact manner that this is done depends
1039  * on the current locale, and may result in the number of
1040  * characters in the string changing.
1041  * 
1042  * Return value: a newly allocated string, with all characters
1043  *    converted to lowercase.  
1044  **/
1045 gchar *
1046 g_utf8_strdown (const gchar *str,
1047                 gssize       len)
1048 {
1049   gsize result_len;
1050   LocaleType locale_type;
1051   gchar *result;
1052
1053   g_return_val_if_fail (str != NULL, NULL);
1054
1055   locale_type = get_locale_type ();
1056   
1057   /*
1058    * We use a two pass approach to keep memory management simple
1059    */
1060   result_len = real_tolower (str, len, NULL, locale_type);
1061   result = g_malloc (result_len + 1);
1062   real_tolower (str, len, result, locale_type);
1063   result[result_len] = '\0';
1064
1065   return result;
1066 }
1067
1068 /**
1069  * g_utf8_casefold:
1070  * @str: a UTF-8 encoded string
1071  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1072  * 
1073  * Converts a string into a form that is independent of case. The
1074  * result will not correspond to any particular case, but can be
1075  * compared for equality or ordered with the results of calling
1076  * g_utf8_casefold() on other strings.
1077  * 
1078  * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
1079  * only an approximation to the correct linguistic case insensitive
1080  * ordering, though it is a fairly good one. Getting this exactly
1081  * right would require a more sophisticated collation function that
1082  * takes case sensitivity into account. GLib does not currently
1083  * provide such a function.
1084  * 
1085  * Return value: a newly allocated string, that is a
1086  *   case independent form of @str.
1087  **/
1088 gchar *
1089 g_utf8_casefold (const gchar *str,
1090                  gssize       len)
1091 {
1092   GString *result;
1093   const char *p;
1094
1095   g_return_val_if_fail (str != NULL, NULL);
1096
1097   result = g_string_new (NULL);
1098   p = str;
1099   while ((len < 0 || p < str + len) && *p)
1100     {
1101       gunichar ch = g_utf8_get_char (p);
1102
1103       int start = 0;
1104       int end = G_N_ELEMENTS (casefold_table);
1105
1106       if (ch >= casefold_table[start].ch &&
1107           ch <= casefold_table[end - 1].ch)
1108         {
1109           while (TRUE)
1110             {
1111               int half = (start + end) / 2;
1112               if (ch == casefold_table[half].ch)
1113                 {
1114                   g_string_append (result, casefold_table[half].data);
1115                   goto next;
1116                 }
1117               else if (half == start)
1118                 break;
1119               else if (ch > casefold_table[half].ch)
1120                 start = half;
1121               else
1122                 end = half;
1123             }
1124         }
1125
1126       g_string_append_unichar (result, g_unichar_tolower (ch));
1127       
1128     next:
1129       p = g_utf8_next_char (p);
1130     }
1131
1132   return g_string_free (result, FALSE); 
1133 }
1134
1135 /**
1136  * g_unichar_get_mirror_char:
1137  * @ch: a Unicode character
1138  * @mirrored_ch: location to store the mirrored character
1139  * 
1140  * In Unicode, some characters are <firstterm>mirrored</firstterm>. This
1141  * means that their images are mirrored horizontally in text that is laid
1142  * out from right to left. For instance, "(" would become its mirror image,
1143  * ")", in right-to-left text.
1144  *
1145  * If @ch has the Unicode mirrored property and there is another unicode
1146  * character that typically has a glyph that is the mirror image of @ch's
1147  * glyph and @mirrored_ch is set, it puts that character in the address
1148  * pointed to by @mirrored_ch.  Otherwise the original character is put.
1149  *
1150  * Return value: %TRUE if @ch has a mirrored character, %FALSE otherwise
1151  *
1152  * Since: 2.4
1153  **/
1154 gboolean
1155 g_unichar_get_mirror_char (gunichar ch,
1156                            gunichar *mirrored_ch)
1157 {
1158   gboolean found;
1159   gunichar mirrored;
1160
1161   mirrored = GLIB_GET_MIRRORING(ch);
1162
1163   found = ch != mirrored;
1164   if (mirrored_ch)
1165     *mirrored_ch = mirrored;
1166
1167   return found;
1168
1169 }
1170
1171 #define __G_UNIPROP_C__
1172 #include "galiasdef.c"