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