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