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