If a character can't be converted, don't replace it with a NUL byte, but
[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         {
513           /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
514            * do not have an uppercase equivalent, in which case val will be
515            * zero. */
516           return val ? val : c;
517         }
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         {
555           /* Not all uppercase letters are guaranteed to have a lowercase
556            * equivalent.  If this is the case, val will be zero. */
557           return val ? val : c;
558         }
559     }
560   else if (t == G_UNICODE_TITLECASE_LETTER)
561     {
562       unsigned int i;
563       for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
564         {
565           if (title_table[i][0] == c)
566             return title_table[i][2];
567         }
568     }
569   return c;
570 }
571
572 /**
573  * g_unichar_totitle:
574  * @c: a Unicode character
575  * 
576  * Converts a character to the titlecase.
577  * 
578  * Return value: the result of converting @c to titlecase.
579  *               If @c is not an uppercase or lowercase character,
580  *               @c is returned unchanged.
581  **/
582 gunichar
583 g_unichar_totitle (gunichar c)
584 {
585   unsigned int i;
586   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
587     {
588       if (title_table[i][0] == c || title_table[i][1] == c
589           || title_table[i][2] == c)
590         return title_table[i][0];
591     }
592   return (TYPE (c) == G_UNICODE_LOWERCASE_LETTER
593           ? ATTTABLE (c >> 8, c & 0xff)
594           : c);
595 }
596
597 /**
598  * g_unichar_digit_value:
599  * @c: a Unicode character
600  *
601  * Determines the numeric value of a character as a decimal
602  * digit.
603  *
604  * Return value: If @c is a decimal digit (according to
605  * g_unichar_isdigit()), its numeric value. Otherwise, -1.
606  **/
607 int
608 g_unichar_digit_value (gunichar c)
609 {
610   if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
611     return ATTTABLE (c >> 8, c & 0xff);
612   return -1;
613 }
614
615 /**
616  * g_unichar_xdigit_value:
617  * @c: a Unicode character
618  *
619  * Determines the numeric value of a character as a hexidecimal
620  * digit.
621  *
622  * Return value: If @c is a hex digit (according to
623  * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
624  **/
625 int
626 g_unichar_xdigit_value (gunichar c)
627 {
628   if (c >= 'A' && c <= 'F')
629     return c - 'A' + 10;
630   if (c >= 'a' && c <= 'f')
631     return c - 'a' + 10;
632   if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
633     return ATTTABLE (c >> 8, c & 0xff);
634   return -1;
635 }
636
637 /**
638  * g_unichar_type:
639  * @c: a Unicode character
640  * 
641  * Classifies a Unicode character by type.
642  * 
643  * Return value: the type of the character.
644  **/
645 GUnicodeType
646 g_unichar_type (gunichar c)
647 {
648   return TYPE (c);
649 }
650
651 /*
652  * Case mapping functions
653  */
654
655 typedef enum {
656   LOCALE_NORMAL,
657   LOCALE_TURKIC,
658   LOCALE_LITHUANIAN
659 } LocaleType;
660
661 static LocaleType
662 get_locale_type (void)
663 {
664 #ifdef G_OS_WIN32
665   char *tem = g_win32_getlocale ();
666   char locale[2];
667
668   locale[0] = tem[0];
669   locale[1] = tem[1];
670   g_free (tem);
671 #else
672   const char *locale = setlocale (LC_CTYPE, NULL);
673 #endif
674
675   switch (locale[0])
676     {
677    case 'a':
678       if (locale[1] == 'z')
679         return LOCALE_TURKIC;
680       break;
681     case 'l':
682       if (locale[1] == 't')
683         return LOCALE_LITHUANIAN;
684       break;
685     case 't':
686       if (locale[1] == 'r')
687         return LOCALE_TURKIC;
688       break;
689     }
690
691   return LOCALE_NORMAL;
692 }
693
694 static gint
695 output_marks (const char **p_inout,
696               char        *out_buffer,
697               gboolean     remove_dot)
698 {
699   const char *p = *p_inout;
700   gint len = 0;
701   
702   while (*p)
703     {
704       gunichar c = g_utf8_get_char (p);
705       
706       if (ISMARK (TYPE (c)))
707         {
708           if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
709             len += g_unichar_to_utf8 (c, out_buffer ? out_buffer + len : NULL);
710           p = g_utf8_next_char (p);
711         }
712       else
713         break;
714     }
715
716   *p_inout = p;
717   return len;
718 }
719
720 static gint
721 output_special_case (gchar *out_buffer,
722                      int    offset,
723                      int    type,
724                      int    which)
725 {
726   const gchar *p = special_case_table + offset;
727   gint len;
728
729   if (type != G_UNICODE_TITLECASE_LETTER)
730     p = g_utf8_next_char (p);
731
732   if (which == 1)
733     p += strlen (p) + 1;
734
735   len = strlen (p);
736   if (out_buffer)
737     memcpy (out_buffer, p, len);
738
739   return len;
740 }
741
742 static gsize
743 real_toupper (const gchar *str,
744               gssize       max_len,
745               gchar       *out_buffer,
746               LocaleType   locale_type)
747 {
748   const gchar *p = str;
749   const char *last = NULL;
750   gsize len = 0;
751   gboolean last_was_i = FALSE;
752
753   while ((max_len < 0 || p < str + max_len) && *p)
754     {
755       gunichar c = g_utf8_get_char (p);
756       int t = TYPE (c);
757       gunichar val;
758
759       last = p;
760       p = g_utf8_next_char (p);
761
762       if (locale_type == LOCALE_LITHUANIAN)
763         {
764           if (c == 'i')
765             last_was_i = TRUE;
766           else 
767             {
768               if (last_was_i)
769                 {
770                   /* Nasty, need to remove any dot above. Though
771                    * I think only E WITH DOT ABOVE occurs in practice
772                    * which could simplify this considerably.
773                    */
774                   gsize decomp_len, i;
775                   gunichar *decomp;
776
777                   decomp = g_unicode_canonical_decomposition (c, &decomp_len);
778                   for (i=0; i < decomp_len; i++)
779                     {
780                       if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
781                         len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
782                     }
783                   g_free (decomp);
784                   
785                   len += output_marks (&p, out_buffer ? out_buffer + len : NULL, TRUE);
786
787                   continue;
788                 }
789
790               if (!ISMARK (t))
791                 last_was_i = FALSE;
792             }
793         }
794       
795       if (locale_type == LOCALE_TURKIC && c == 'i')
796         {
797           /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
798           len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL); 
799         }
800       else if (c == 0x0345)     /* COMBINING GREEK YPOGEGRAMMENI */
801         {
802           /* Nasty, need to move it after other combining marks .. this would go away if
803            * we normalized first.
804            */
805           len += output_marks (&p, out_buffer ? out_buffer + len : NULL, FALSE);
806
807           /* And output as GREEK CAPITAL LETTER IOTA */
808           len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);         
809         }
810       else if (IS (t,
811                    OR (G_UNICODE_LOWERCASE_LETTER,
812                    OR (G_UNICODE_TITLECASE_LETTER,
813                   0))))
814         {
815           val = ATTTABLE (c >> 8, c & 0xff);
816
817           if (val >= 0x1000000)
818             {
819               len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t,
820                                           t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
821             }
822           else
823             {
824               if (t == G_UNICODE_TITLECASE_LETTER)
825                 {
826                   unsigned int i;
827                   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
828                     {
829                       if (title_table[i][0] == c)
830                         {
831                           val = title_table[i][1];
832                           break;
833                         }
834                     }
835                 }
836
837               /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
838                * do not have an uppercase equivalent, in which case val will be
839                * zero. */
840               len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
841             }
842         }
843       else
844         {
845           gsize char_len = g_utf8_skip[*(guchar *)last];
846
847           if (out_buffer)
848             memcpy (out_buffer + len, last, char_len);
849
850           len += char_len;
851         }
852
853     }
854
855   return len;
856 }
857
858 /**
859  * g_utf8_strup:
860  * @str: a UTF-8 encoded string
861  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
862  * 
863  * Converts all Unicode characters in the string that have a case
864  * to uppercase. The exact manner that this is done depends
865  * on the current locale, and may result in the number of
866  * characters in the string increasing. (For instance, the
867  * German ess-zet will be changed to SS.)
868  * 
869  * Return value: a newly allocated string, with all characters
870  *    converted to uppercase.  
871  **/
872 gchar *
873 g_utf8_strup (const gchar *str,
874               gssize       len)
875 {
876   gsize result_len;
877   LocaleType locale_type;
878   gchar *result;
879
880   g_return_val_if_fail (str != NULL, NULL);
881
882   locale_type = get_locale_type ();
883   
884   /*
885    * We use a two pass approach to keep memory management simple
886    */
887   result_len = real_toupper (str, len, NULL, locale_type);
888   result = g_malloc (result_len + 1);
889   real_toupper (str, len, result, locale_type);
890   result[result_len] = '\0';
891
892   return result;
893 }
894
895 /* traverses the string checking for characters with combining class == 230
896  * until a base character is found */
897 static gboolean
898 has_more_above (const gchar *str)
899 {
900   const gchar *p = str;
901   gint combining_class;
902
903   while (*p)
904     {
905       combining_class = _g_unichar_combining_class (g_utf8_get_char (p));
906       if (combining_class == 230)
907         return TRUE;
908       else if (combining_class == 0)
909         break;
910
911       p = g_utf8_next_char (p);
912     }
913
914   return FALSE;
915 }
916
917 static gsize
918 real_tolower (const gchar *str,
919               gssize       max_len,
920               gchar       *out_buffer,
921               LocaleType   locale_type)
922 {
923   const gchar *p = str;
924   const char *last = NULL;
925   gsize len = 0;
926
927   while ((max_len < 0 || p < str + max_len) && *p)
928     {
929       gunichar c = g_utf8_get_char (p);
930       int t = TYPE (c);
931       gunichar val;
932
933       last = p;
934       p = g_utf8_next_char (p);
935
936       if (locale_type == LOCALE_TURKIC && c == 'I')
937         {
938           if (g_utf8_get_char (p) == 0x0307)
939             {
940               /* I + COMBINING DOT ABOVE => i (U+0069) */
941               len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL); 
942               p = g_utf8_next_char (p);
943             }
944           else
945             {
946               /* I => LATIN SMALL LETTER DOTLESS I */
947               len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL); 
948             }
949         }
950       /* Introduce an explicit dot above when lowercasing capital I's and J's
951        * whenever there are more accents above. [SpecialCasing.txt] */
952       else if (locale_type == LOCALE_LITHUANIAN && 
953                (c == 0x00cc || c == 0x00cd || c == 0x0128))
954         {
955           len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL); 
956           len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL); 
957
958           switch (c)
959             {
960             case 0x00cc: 
961               len += g_unichar_to_utf8 (0x0300, out_buffer ? out_buffer + len : NULL); 
962               break;
963             case 0x00cd: 
964               len += g_unichar_to_utf8 (0x0301, out_buffer ? out_buffer + len : NULL); 
965               break;
966             case 0x0128: 
967               len += g_unichar_to_utf8 (0x0303, out_buffer ? out_buffer + len : NULL); 
968               break;
969             }
970         }
971       else if (locale_type == LOCALE_LITHUANIAN && 
972                (c == 'I' || c == 'J' || c == 0x012e) && 
973                has_more_above (p))
974         {
975           len += g_unichar_to_utf8 (g_unichar_tolower (c), out_buffer ? out_buffer + len : NULL); 
976           len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL); 
977         }
978       else if (c == 0x03A3)     /* GREEK CAPITAL LETTER SIGMA */
979         {
980           if ((max_len < 0 || p < str + max_len) && *p)
981             {
982               gunichar next_c = g_utf8_get_char (p);
983               int next_type = TYPE(next_c);
984
985               /* SIGMA mapps differently depending on whether it is
986                * final or not. The following simplified test would
987                * fail in the case of combining marks following the
988                * sigma, but I don't think that occurs in real text.
989                * The test here matches that in ICU.
990                */
991               if (ISALPHA (next_type)) /* Lu,Ll,Lt,Lm,Lo */
992                 val = 0x3c3;    /* GREEK SMALL SIGMA */
993               else
994                 val = 0x3c2;    /* GREEK SMALL FINAL SIGMA */
995             }
996           else
997             val = 0x3c2;        /* GREEK SMALL FINAL SIGMA */
998
999           len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
1000         }
1001       else if (IS (t,
1002                    OR (G_UNICODE_UPPERCASE_LETTER,
1003                    OR (G_UNICODE_TITLECASE_LETTER,
1004                   0))))
1005         {
1006           val = ATTTABLE (c >> 8, c & 0xff);
1007
1008           if (val >= 0x1000000)
1009             {
1010               len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t, 0);
1011             }
1012           else
1013             {
1014               if (t == G_UNICODE_TITLECASE_LETTER)
1015                 {
1016                   unsigned int i;
1017                   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
1018                     {
1019                       if (title_table[i][0] == c)
1020                         {
1021                           val = title_table[i][2];
1022                           break;
1023                         }
1024                     }
1025                 }
1026
1027               /* Not all uppercase letters are guaranteed to have a lowercase
1028                * equivalent.  If this is the case, val will be zero. */
1029               len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
1030             }
1031         }
1032       else
1033         {
1034           gsize char_len = g_utf8_skip[*(guchar *)last];
1035
1036           if (out_buffer)
1037             memcpy (out_buffer + len, last, char_len);
1038
1039           len += char_len;
1040         }
1041
1042     }
1043
1044   return len;
1045 }
1046
1047 /**
1048  * g_utf8_strdown:
1049  * @str: a UTF-8 encoded string
1050  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1051  * 
1052  * Converts all Unicode characters in the string that have a case
1053  * to lowercase. The exact manner that this is done depends
1054  * on the current locale, and may result in the number of
1055  * characters in the string changing.
1056  * 
1057  * Return value: a newly allocated string, with all characters
1058  *    converted to lowercase.  
1059  **/
1060 gchar *
1061 g_utf8_strdown (const gchar *str,
1062                 gssize       len)
1063 {
1064   gsize result_len;
1065   LocaleType locale_type;
1066   gchar *result;
1067
1068   g_return_val_if_fail (str != NULL, NULL);
1069
1070   locale_type = get_locale_type ();
1071   
1072   /*
1073    * We use a two pass approach to keep memory management simple
1074    */
1075   result_len = real_tolower (str, len, NULL, locale_type);
1076   result = g_malloc (result_len + 1);
1077   real_tolower (str, len, result, locale_type);
1078   result[result_len] = '\0';
1079
1080   return result;
1081 }
1082
1083 /**
1084  * g_utf8_casefold:
1085  * @str: a UTF-8 encoded string
1086  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1087  * 
1088  * Converts a string into a form that is independent of case. The
1089  * result will not correspond to any particular case, but can be
1090  * compared for equality or ordered with the results of calling
1091  * g_utf8_casefold() on other strings.
1092  * 
1093  * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
1094  * only an approximation to the correct linguistic case insensitive
1095  * ordering, though it is a fairly good one. Getting this exactly
1096  * right would require a more sophisticated collation function that
1097  * takes case sensitivity into account. GLib does not currently
1098  * provide such a function.
1099  * 
1100  * Return value: a newly allocated string, that is a
1101  *   case independent form of @str.
1102  **/
1103 gchar *
1104 g_utf8_casefold (const gchar *str,
1105                  gssize       len)
1106 {
1107   GString *result;
1108   const char *p;
1109
1110   g_return_val_if_fail (str != NULL, NULL);
1111
1112   result = g_string_new (NULL);
1113   p = str;
1114   while ((len < 0 || p < str + len) && *p)
1115     {
1116       gunichar ch = g_utf8_get_char (p);
1117
1118       int start = 0;
1119       int end = G_N_ELEMENTS (casefold_table);
1120
1121       if (ch >= casefold_table[start].ch &&
1122           ch <= casefold_table[end - 1].ch)
1123         {
1124           while (TRUE)
1125             {
1126               int half = (start + end) / 2;
1127               if (ch == casefold_table[half].ch)
1128                 {
1129                   g_string_append (result, casefold_table[half].data);
1130                   goto next;
1131                 }
1132               else if (half == start)
1133                 break;
1134               else if (ch > casefold_table[half].ch)
1135                 start = half;
1136               else
1137                 end = half;
1138             }
1139         }
1140
1141       g_string_append_unichar (result, g_unichar_tolower (ch));
1142       
1143     next:
1144       p = g_utf8_next_char (p);
1145     }
1146
1147   return g_string_free (result, FALSE); 
1148 }
1149
1150 /**
1151  * g_unichar_get_mirror_char:
1152  * @ch: a Unicode character
1153  * @mirrored_ch: location to store the mirrored character
1154  * 
1155  * In Unicode, some characters are <firstterm>mirrored</firstterm>. This
1156  * means that their images are mirrored horizontally in text that is laid
1157  * out from right to left. For instance, "(" would become its mirror image,
1158  * ")", in right-to-left text.
1159  *
1160  * If @ch has the Unicode mirrored property and there is another unicode
1161  * character that typically has a glyph that is the mirror image of @ch's
1162  * glyph and @mirrored_ch is set, it puts that character in the address
1163  * pointed to by @mirrored_ch.  Otherwise the original character is put.
1164  *
1165  * Return value: %TRUE if @ch has a mirrored character, %FALSE otherwise
1166  *
1167  * Since: 2.4
1168  **/
1169 gboolean
1170 g_unichar_get_mirror_char (gunichar ch,
1171                            gunichar *mirrored_ch)
1172 {
1173   gboolean found;
1174   gunichar mirrored;
1175
1176   mirrored = GLIB_GET_MIRRORING(ch);
1177
1178   found = ch != mirrored;
1179   if (mirrored_ch)
1180     *mirrored_ch = mirrored;
1181
1182   return found;
1183
1184 }
1185
1186 #define __G_UNIPROP_C__
1187 #include "galiasdef.c"