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