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