Document that GUnicodeScript is interchangeable with PangoScript.
[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_ismark:
293  * @c: a Unicode character
294  *
295  * Determines whether a character is a mark (non-spacing mark,
296  * combining mark, or enclosing mark in Unicode speak).
297  * Given some UTF-8 text, obtain a character value
298  * with g_utf8_get_char().
299  *
300  * Note: in most cases where isalpha characters are allowed,
301  * ismark characters should be allowed to as they are essential
302  * for writing most European languages as well as many non-Latin
303  * scripts.
304  *
305  * Return value: %TRUE if @c is a mark character
306  *
307  * Since: 2.14
308  **/
309 gboolean
310 g_unichar_ismark (gunichar c)
311 {
312   return ISMARK (TYPE (c));
313 }
314
315 /**
316  * g_unichar_isupper:
317  * @c: a Unicode character
318  * 
319  * Determines if a character is uppercase.
320  * 
321  * Return value: %TRUE if @c is an uppercase character
322  **/
323 gboolean
324 g_unichar_isupper (gunichar c)
325 {
326   return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
327 }
328
329 /**
330  * g_unichar_istitle:
331  * @c: a Unicode character
332  * 
333  * Determines if a character is titlecase. Some characters in
334  * Unicode which are composites, such as the DZ digraph
335  * have three case variants instead of just two. The titlecase
336  * form is used at the beginning of a word where only the
337  * first letter is capitalized. The titlecase form of the DZ
338  * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z.
339  * 
340  * Return value: %TRUE if the character is titlecase
341  **/
342 gboolean
343 g_unichar_istitle (gunichar c)
344 {
345   unsigned int i;
346   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
347     if (title_table[i][0] == c)
348       return TRUE;
349   return FALSE;
350 }
351
352 /**
353  * g_unichar_isxdigit:
354  * @c: a Unicode character.
355  * 
356  * Determines if a character is a hexidecimal digit.
357  * 
358  * Return value: %TRUE if the character is a hexadecimal digit
359  **/
360 gboolean
361 g_unichar_isxdigit (gunichar c)
362 {
363   return ((c >= 'a' && c <= 'f')
364           || (c >= 'A' && c <= 'F')
365           || (TYPE (c) == G_UNICODE_DECIMAL_NUMBER));
366 }
367
368 /**
369  * g_unichar_isdefined:
370  * @c: a Unicode character
371  * 
372  * Determines if a given character is assigned in the Unicode
373  * standard.
374  *
375  * Return value: %TRUE if the character has an assigned value
376  **/
377 gboolean
378 g_unichar_isdefined (gunichar c)
379 {
380   return TYPE (c) != G_UNICODE_UNASSIGNED;
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 /**
418  * g_unichar_iswide:
419  * @c: a Unicode character
420  * 
421  * Determines if a character is typically rendered in a double-width
422  * cell.
423  * 
424  * Return value: %TRUE if the character is wide
425  **/
426 gboolean
427 g_unichar_iswide (gunichar c)
428 {
429   gunichar ucs = c;
430
431   /* The following block is stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>'s
432    * wcwidth implementation.  */
433   return
434     (ucs >= 0x1100 &&
435      (ucs <= 0x115f ||                    /* Hangul Jamo init. consonants */
436       ucs == 0x2329 || ucs == 0x232a ||
437       (ucs >= 0x2e80 && ucs <= 0xa4cf &&
438        ucs != 0x303f) ||                  /* CJK ... Yi */
439       (ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */
440       (ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */
441       (ucs >= 0xfe10 && ucs <= 0xfe19) || /* Vertical forms */
442       (ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
443       (ucs >= 0xff00 && ucs <= 0xff60) || /* Fullwidth Forms */
444       (ucs >= 0xffe0 && ucs <= 0xffe6) ||
445       (ucs >= 0x20000 && ucs <= 0x2fffd) ||
446       (ucs >= 0x30000 && ucs <= 0x3fffd)));
447 }
448
449
450 /* The following block is stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>'s
451  * wcwidth implementation.  */
452 struct Interval
453 {
454   gunichar start, end;
455 };
456
457 /* The following block is stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>'s
458  * wcwidth implementation.  */
459 static int
460 interval_compare (const void *key, const void *elt)
461 {
462   gunichar c = GPOINTER_TO_UINT (key);
463   struct Interval *interval = (struct Interval *)elt;
464
465   if (c < interval->start)
466     return -1;
467   if (c > interval->end)
468     return +1;
469
470   return 0;
471 }
472
473 /**
474  * g_unichar_iswide_cjk:
475  * @c: a Unicode character
476  * 
477  * Determines if a character is typically rendered in a double-width
478  * cell under legacy East Asian locales.  If a character is wide according to
479  * g_unichar_iswide(), then it is also reported wide with this function, but
480  * the converse is not necessarily true.  See the
481  * <ulink url="http://www.unicode.org/reports/tr11/">Unicode Standard
482  * Annex #11</ulink> for details.
483  * 
484  * Return value: %TRUE if the character is wide in legacy East Asian locales
485  *
486  * Since: 2.12
487  */
488 gboolean
489 g_unichar_iswide_cjk (gunichar c)
490 {
491   /* The following block is stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>'s
492    * wcwidth implementation.  */
493   /* sorted list of non-overlapping intervals of East Asian Ambiguous
494    * characters, generated by "uniset +WIDTH-A -cat=Me -cat=Mn -cat=Cf c" */
495   static const struct Interval ambiguous[] = {
496     { 0x0300, 0x036F }, { 0x0483, 0x0486 }, { 0x0488, 0x0489 },
497     { 0x0591, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 },
498     { 0x05C4, 0x05C5 }, { 0x05C7, 0x05C7 }, { 0x0600, 0x0603 },
499     { 0x0610, 0x0615 }, { 0x064B, 0x065E }, { 0x0670, 0x0670 },
500     { 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED },
501     { 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A },
502     { 0x07A6, 0x07B0 }, { 0x07EB, 0x07F3 }, { 0x0901, 0x0902 },
503     { 0x093C, 0x093C }, { 0x0941, 0x0948 }, { 0x094D, 0x094D },
504     { 0x0951, 0x0954 }, { 0x0962, 0x0963 }, { 0x0981, 0x0981 },
505     { 0x09BC, 0x09BC }, { 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD },
506     { 0x09E2, 0x09E3 }, { 0x0A01, 0x0A02 }, { 0x0A3C, 0x0A3C },
507     { 0x0A41, 0x0A42 }, { 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D },
508     { 0x0A70, 0x0A71 }, { 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC },
509     { 0x0AC1, 0x0AC5 }, { 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD },
510     { 0x0AE2, 0x0AE3 }, { 0x0B01, 0x0B01 }, { 0x0B3C, 0x0B3C },
511     { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 }, { 0x0B4D, 0x0B4D },
512     { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 }, { 0x0BC0, 0x0BC0 },
513     { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 }, { 0x0C46, 0x0C48 },
514     { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 }, { 0x0CBC, 0x0CBC },
515     { 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD },
516     { 0x0CE2, 0x0CE3 }, { 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D },
517     { 0x0DCA, 0x0DCA }, { 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 },
518     { 0x0E31, 0x0E31 }, { 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E },
519     { 0x0EB1, 0x0EB1 }, { 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC },
520     { 0x0EC8, 0x0ECD }, { 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 },
521     { 0x0F37, 0x0F37 }, { 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E },
522     { 0x0F80, 0x0F84 }, { 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 },
523     { 0x0F99, 0x0FBC }, { 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 },
524     { 0x1032, 0x1032 }, { 0x1036, 0x1037 }, { 0x1039, 0x1039 },
525     { 0x1058, 0x1059 }, { 0x1160, 0x11FF }, { 0x135F, 0x135F },
526     { 0x1712, 0x1714 }, { 0x1732, 0x1734 }, { 0x1752, 0x1753 },
527     { 0x1772, 0x1773 }, { 0x17B4, 0x17B5 }, { 0x17B7, 0x17BD },
528     { 0x17C6, 0x17C6 }, { 0x17C9, 0x17D3 }, { 0x17DD, 0x17DD },
529     { 0x180B, 0x180D }, { 0x18A9, 0x18A9 }, { 0x1920, 0x1922 },
530     { 0x1927, 0x1928 }, { 0x1932, 0x1932 }, { 0x1939, 0x193B },
531     { 0x1A17, 0x1A18 }, { 0x1B00, 0x1B03 }, { 0x1B34, 0x1B34 },
532     { 0x1B36, 0x1B3A }, { 0x1B3C, 0x1B3C }, { 0x1B42, 0x1B42 },
533     { 0x1B6B, 0x1B73 }, { 0x1DC0, 0x1DCA }, { 0x1DFE, 0x1DFF },
534     { 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x2060, 0x2063 },
535     { 0x206A, 0x206F }, { 0x20D0, 0x20EF }, { 0x302A, 0x302F },
536     { 0x3099, 0x309A }, { 0xA806, 0xA806 }, { 0xA80B, 0xA80B },
537     { 0xA825, 0xA826 }, { 0xFB1E, 0xFB1E }, { 0xFE00, 0xFE0F },
538     { 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF }, { 0xFFF9, 0xFFFB },
539     { 0x10A01, 0x10A03 }, { 0x10A05, 0x10A06 }, { 0x10A0C, 0x10A0F },
540     { 0x10A38, 0x10A3A }, { 0x10A3F, 0x10A3F }, { 0x1D167, 0x1D169 },
541     { 0x1D173, 0x1D182 }, { 0x1D185, 0x1D18B }, { 0x1D1AA, 0x1D1AD },
542     { 0x1D242, 0x1D244 }, { 0xE0001, 0xE0001 }, { 0xE0020, 0xE007F },
543     { 0xE0100, 0xE01EF }
544   };
545
546   if (g_unichar_iswide (c))
547     return TRUE;
548
549   if (bsearch (GUINT_TO_POINTER (c), ambiguous, G_N_ELEMENTS (ambiguous), sizeof ambiguous[0],
550                interval_compare))
551     return TRUE;
552
553   return FALSE;
554 }
555
556
557 /**
558  * g_unichar_toupper:
559  * @c: a Unicode character
560  * 
561  * Converts a character to uppercase.
562  * 
563  * Return value: the result of converting @c to uppercase.
564  *               If @c is not an lowercase or titlecase character,
565  *               or has no upper case equivalent @c is returned unchanged.
566  **/
567 gunichar
568 g_unichar_toupper (gunichar c)
569 {
570   int t = TYPE (c);
571   if (t == G_UNICODE_LOWERCASE_LETTER)
572     {
573       gunichar val = ATTTABLE (c >> 8, c & 0xff);
574       if (val >= 0x1000000)
575         {
576           const gchar *p = special_case_table + val - 0x1000000;
577           val = g_utf8_get_char (p);
578         }
579       /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
580        * do not have an uppercase equivalent, in which case val will be
581        * zero. 
582        */
583       return val ? val : c;
584     }
585   else if (t == G_UNICODE_TITLECASE_LETTER)
586     {
587       unsigned int i;
588       for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
589         {
590           if (title_table[i][0] == c)
591             return title_table[i][1];
592         }
593     }
594   return c;
595 }
596
597 /**
598  * g_unichar_tolower:
599  * @c: a Unicode character.
600  * 
601  * Converts a character to lower case.
602  * 
603  * Return value: the result of converting @c to lower case.
604  *               If @c is not an upperlower or titlecase character,
605  *               or has no lowercase equivalent @c is returned unchanged.
606  **/
607 gunichar
608 g_unichar_tolower (gunichar c)
609 {
610   int t = TYPE (c);
611   if (t == G_UNICODE_UPPERCASE_LETTER)
612     {
613       gunichar val = ATTTABLE (c >> 8, c & 0xff);
614       if (val >= 0x1000000)
615         {
616           const gchar *p = special_case_table + val - 0x1000000;
617           return g_utf8_get_char (p);
618         }
619       else
620         {
621           /* Not all uppercase letters are guaranteed to have a lowercase
622            * equivalent.  If this is the case, val will be zero. */
623           return val ? val : c;
624         }
625     }
626   else if (t == G_UNICODE_TITLECASE_LETTER)
627     {
628       unsigned int i;
629       for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
630         {
631           if (title_table[i][0] == c)
632             return title_table[i][2];
633         }
634     }
635   return c;
636 }
637
638 /**
639  * g_unichar_totitle:
640  * @c: a Unicode character
641  * 
642  * Converts a character to the titlecase.
643  * 
644  * Return value: the result of converting @c to titlecase.
645  *               If @c is not an uppercase or lowercase character,
646  *               @c is returned unchanged.
647  **/
648 gunichar
649 g_unichar_totitle (gunichar c)
650 {
651   unsigned int i;
652   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
653     {
654       if (title_table[i][0] == c || title_table[i][1] == c
655           || title_table[i][2] == c)
656         return title_table[i][0];
657     }
658     
659   if (TYPE (c) == G_UNICODE_LOWERCASE_LETTER)
660     return g_unichar_toupper (c);
661
662   return c;
663 }
664
665 /**
666  * g_unichar_digit_value:
667  * @c: a Unicode character
668  *
669  * Determines the numeric value of a character as a decimal
670  * digit.
671  *
672  * Return value: If @c is a decimal digit (according to
673  * g_unichar_isdigit()), its numeric value. Otherwise, -1.
674  **/
675 int
676 g_unichar_digit_value (gunichar c)
677 {
678   if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
679     return ATTTABLE (c >> 8, c & 0xff);
680   return -1;
681 }
682
683 /**
684  * g_unichar_xdigit_value:
685  * @c: a Unicode character
686  *
687  * Determines the numeric value of a character as a hexidecimal
688  * digit.
689  *
690  * Return value: If @c is a hex digit (according to
691  * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
692  **/
693 int
694 g_unichar_xdigit_value (gunichar c)
695 {
696   if (c >= 'A' && c <= 'F')
697     return c - 'A' + 10;
698   if (c >= 'a' && c <= 'f')
699     return c - 'a' + 10;
700   if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
701     return ATTTABLE (c >> 8, c & 0xff);
702   return -1;
703 }
704
705 /**
706  * g_unichar_type:
707  * @c: a Unicode character
708  * 
709  * Classifies a Unicode character by type.
710  * 
711  * Return value: the type of the character.
712  **/
713 GUnicodeType
714 g_unichar_type (gunichar c)
715 {
716   return TYPE (c);
717 }
718
719 /*
720  * Case mapping functions
721  */
722
723 typedef enum {
724   LOCALE_NORMAL,
725   LOCALE_TURKIC,
726   LOCALE_LITHUANIAN
727 } LocaleType;
728
729 static LocaleType
730 get_locale_type (void)
731 {
732 #ifdef G_OS_WIN32
733   char *tem = g_win32_getlocale ();
734   char locale[2];
735
736   locale[0] = tem[0];
737   locale[1] = tem[1];
738   g_free (tem);
739 #else
740   const char *locale = setlocale (LC_CTYPE, NULL);
741 #endif
742
743   switch (locale[0])
744     {
745    case 'a':
746       if (locale[1] == 'z')
747         return LOCALE_TURKIC;
748       break;
749     case 'l':
750       if (locale[1] == 't')
751         return LOCALE_LITHUANIAN;
752       break;
753     case 't':
754       if (locale[1] == 'r')
755         return LOCALE_TURKIC;
756       break;
757     }
758
759   return LOCALE_NORMAL;
760 }
761
762 static gint
763 output_marks (const char **p_inout,
764               char        *out_buffer,
765               gboolean     remove_dot)
766 {
767   const char *p = *p_inout;
768   gint len = 0;
769   
770   while (*p)
771     {
772       gunichar c = g_utf8_get_char (p);
773       
774       if (ISMARK (TYPE (c)))
775         {
776           if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
777             len += g_unichar_to_utf8 (c, out_buffer ? out_buffer + len : NULL);
778           p = g_utf8_next_char (p);
779         }
780       else
781         break;
782     }
783
784   *p_inout = p;
785   return len;
786 }
787
788 static gint
789 output_special_case (gchar *out_buffer,
790                      int    offset,
791                      int    type,
792                      int    which)
793 {
794   const gchar *p = special_case_table + offset;
795   gint len;
796
797   if (type != G_UNICODE_TITLECASE_LETTER)
798     p = g_utf8_next_char (p);
799
800   if (which == 1)
801     p += strlen (p) + 1;
802
803   len = strlen (p);
804   if (out_buffer)
805     memcpy (out_buffer, p, len);
806
807   return len;
808 }
809
810 static gsize
811 real_toupper (const gchar *str,
812               gssize       max_len,
813               gchar       *out_buffer,
814               LocaleType   locale_type)
815 {
816   const gchar *p = str;
817   const char *last = NULL;
818   gsize len = 0;
819   gboolean last_was_i = FALSE;
820
821   while ((max_len < 0 || p < str + max_len) && *p)
822     {
823       gunichar c = g_utf8_get_char (p);
824       int t = TYPE (c);
825       gunichar val;
826
827       last = p;
828       p = g_utf8_next_char (p);
829
830       if (locale_type == LOCALE_LITHUANIAN)
831         {
832           if (c == 'i')
833             last_was_i = TRUE;
834           else 
835             {
836               if (last_was_i)
837                 {
838                   /* Nasty, need to remove any dot above. Though
839                    * I think only E WITH DOT ABOVE occurs in practice
840                    * which could simplify this considerably.
841                    */
842                   gsize decomp_len, i;
843                   gunichar *decomp;
844
845                   decomp = g_unicode_canonical_decomposition (c, &decomp_len);
846                   for (i=0; i < decomp_len; i++)
847                     {
848                       if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
849                         len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
850                     }
851                   g_free (decomp);
852                   
853                   len += output_marks (&p, out_buffer ? out_buffer + len : NULL, TRUE);
854
855                   continue;
856                 }
857
858               if (!ISMARK (t))
859                 last_was_i = FALSE;
860             }
861         }
862       
863       if (locale_type == LOCALE_TURKIC && c == 'i')
864         {
865           /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
866           len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL); 
867         }
868       else if (c == 0x0345)     /* COMBINING GREEK YPOGEGRAMMENI */
869         {
870           /* Nasty, need to move it after other combining marks .. this would go away if
871            * we normalized first.
872            */
873           len += output_marks (&p, out_buffer ? out_buffer + len : NULL, FALSE);
874
875           /* And output as GREEK CAPITAL LETTER IOTA */
876           len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);         
877         }
878       else if (IS (t,
879                    OR (G_UNICODE_LOWERCASE_LETTER,
880                    OR (G_UNICODE_TITLECASE_LETTER,
881                   0))))
882         {
883           val = ATTTABLE (c >> 8, c & 0xff);
884
885           if (val >= 0x1000000)
886             {
887               len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t,
888                                           t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
889             }
890           else
891             {
892               if (t == G_UNICODE_TITLECASE_LETTER)
893                 {
894                   unsigned int i;
895                   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
896                     {
897                       if (title_table[i][0] == c)
898                         {
899                           val = title_table[i][1];
900                           break;
901                         }
902                     }
903                 }
904
905               /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
906                * do not have an uppercase equivalent, in which case val will be
907                * zero. */
908               len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
909             }
910         }
911       else
912         {
913           gsize char_len = g_utf8_skip[*(guchar *)last];
914
915           if (out_buffer)
916             memcpy (out_buffer + len, last, char_len);
917
918           len += char_len;
919         }
920
921     }
922
923   return len;
924 }
925
926 /**
927  * g_utf8_strup:
928  * @str: a UTF-8 encoded string
929  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
930  * 
931  * Converts all Unicode characters in the string that have a case
932  * to uppercase. The exact manner that this is done depends
933  * on the current locale, and may result in the number of
934  * characters in the string increasing. (For instance, the
935  * German ess-zet will be changed to SS.)
936  * 
937  * Return value: a newly allocated string, with all characters
938  *    converted to uppercase.  
939  **/
940 gchar *
941 g_utf8_strup (const gchar *str,
942               gssize       len)
943 {
944   gsize result_len;
945   LocaleType locale_type;
946   gchar *result;
947
948   g_return_val_if_fail (str != NULL, NULL);
949
950   locale_type = get_locale_type ();
951   
952   /*
953    * We use a two pass approach to keep memory management simple
954    */
955   result_len = real_toupper (str, len, NULL, locale_type);
956   result = g_malloc (result_len + 1);
957   real_toupper (str, len, result, locale_type);
958   result[result_len] = '\0';
959
960   return result;
961 }
962
963 /* traverses the string checking for characters with combining class == 230
964  * until a base character is found */
965 static gboolean
966 has_more_above (const gchar *str)
967 {
968   const gchar *p = str;
969   gint combining_class;
970
971   while (*p)
972     {
973       combining_class = g_unichar_combining_class (g_utf8_get_char (p));
974       if (combining_class == 230)
975         return TRUE;
976       else if (combining_class == 0)
977         break;
978
979       p = g_utf8_next_char (p);
980     }
981
982   return FALSE;
983 }
984
985 static gsize
986 real_tolower (const gchar *str,
987               gssize       max_len,
988               gchar       *out_buffer,
989               LocaleType   locale_type)
990 {
991   const gchar *p = str;
992   const char *last = NULL;
993   gsize len = 0;
994
995   while ((max_len < 0 || p < str + max_len) && *p)
996     {
997       gunichar c = g_utf8_get_char (p);
998       int t = TYPE (c);
999       gunichar val;
1000
1001       last = p;
1002       p = g_utf8_next_char (p);
1003
1004       if (locale_type == LOCALE_TURKIC && c == 'I')
1005         {
1006           if (g_utf8_get_char (p) == 0x0307)
1007             {
1008               /* I + COMBINING DOT ABOVE => i (U+0069) */
1009               len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL); 
1010               p = g_utf8_next_char (p);
1011             }
1012           else
1013             {
1014               /* I => LATIN SMALL LETTER DOTLESS I */
1015               len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL); 
1016             }
1017         }
1018       /* Introduce an explicit dot above when lowercasing capital I's and J's
1019        * whenever there are more accents above. [SpecialCasing.txt] */
1020       else if (locale_type == LOCALE_LITHUANIAN && 
1021                (c == 0x00cc || c == 0x00cd || c == 0x0128))
1022         {
1023           len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL); 
1024           len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL); 
1025
1026           switch (c)
1027             {
1028             case 0x00cc: 
1029               len += g_unichar_to_utf8 (0x0300, out_buffer ? out_buffer + len : NULL); 
1030               break;
1031             case 0x00cd: 
1032               len += g_unichar_to_utf8 (0x0301, out_buffer ? out_buffer + len : NULL); 
1033               break;
1034             case 0x0128: 
1035               len += g_unichar_to_utf8 (0x0303, out_buffer ? out_buffer + len : NULL); 
1036               break;
1037             }
1038         }
1039       else if (locale_type == LOCALE_LITHUANIAN && 
1040                (c == 'I' || c == 'J' || c == 0x012e) && 
1041                has_more_above (p))
1042         {
1043           len += g_unichar_to_utf8 (g_unichar_tolower (c), out_buffer ? out_buffer + len : NULL); 
1044           len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL); 
1045         }
1046       else if (c == 0x03A3)     /* GREEK CAPITAL LETTER SIGMA */
1047         {
1048           if ((max_len < 0 || p < str + max_len) && *p)
1049             {
1050               gunichar next_c = g_utf8_get_char (p);
1051               int next_type = TYPE(next_c);
1052
1053               /* SIGMA mapps differently depending on whether it is
1054                * final or not. The following simplified test would
1055                * fail in the case of combining marks following the
1056                * sigma, but I don't think that occurs in real text.
1057                * The test here matches that in ICU.
1058                */
1059               if (ISALPHA (next_type)) /* Lu,Ll,Lt,Lm,Lo */
1060                 val = 0x3c3;    /* GREEK SMALL SIGMA */
1061               else
1062                 val = 0x3c2;    /* GREEK SMALL FINAL SIGMA */
1063             }
1064           else
1065             val = 0x3c2;        /* GREEK SMALL FINAL SIGMA */
1066
1067           len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
1068         }
1069       else if (IS (t,
1070                    OR (G_UNICODE_UPPERCASE_LETTER,
1071                    OR (G_UNICODE_TITLECASE_LETTER,
1072                   0))))
1073         {
1074           val = ATTTABLE (c >> 8, c & 0xff);
1075
1076           if (val >= 0x1000000)
1077             {
1078               len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t, 0);
1079             }
1080           else
1081             {
1082               if (t == G_UNICODE_TITLECASE_LETTER)
1083                 {
1084                   unsigned int i;
1085                   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
1086                     {
1087                       if (title_table[i][0] == c)
1088                         {
1089                           val = title_table[i][2];
1090                           break;
1091                         }
1092                     }
1093                 }
1094
1095               /* Not all uppercase letters are guaranteed to have a lowercase
1096                * equivalent.  If this is the case, val will be zero. */
1097               len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
1098             }
1099         }
1100       else
1101         {
1102           gsize char_len = g_utf8_skip[*(guchar *)last];
1103
1104           if (out_buffer)
1105             memcpy (out_buffer + len, last, char_len);
1106
1107           len += char_len;
1108         }
1109
1110     }
1111
1112   return len;
1113 }
1114
1115 /**
1116  * g_utf8_strdown:
1117  * @str: a UTF-8 encoded string
1118  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1119  * 
1120  * Converts all Unicode characters in the string that have a case
1121  * to lowercase. The exact manner that this is done depends
1122  * on the current locale, and may result in the number of
1123  * characters in the string changing.
1124  * 
1125  * Return value: a newly allocated string, with all characters
1126  *    converted to lowercase.  
1127  **/
1128 gchar *
1129 g_utf8_strdown (const gchar *str,
1130                 gssize       len)
1131 {
1132   gsize result_len;
1133   LocaleType locale_type;
1134   gchar *result;
1135
1136   g_return_val_if_fail (str != NULL, NULL);
1137
1138   locale_type = get_locale_type ();
1139   
1140   /*
1141    * We use a two pass approach to keep memory management simple
1142    */
1143   result_len = real_tolower (str, len, NULL, locale_type);
1144   result = g_malloc (result_len + 1);
1145   real_tolower (str, len, result, locale_type);
1146   result[result_len] = '\0';
1147
1148   return result;
1149 }
1150
1151 /**
1152  * g_utf8_casefold:
1153  * @str: a UTF-8 encoded string
1154  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
1155  * 
1156  * Converts a string into a form that is independent of case. The
1157  * result will not correspond to any particular case, but can be
1158  * compared for equality or ordered with the results of calling
1159  * g_utf8_casefold() on other strings.
1160  * 
1161  * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
1162  * only an approximation to the correct linguistic case insensitive
1163  * ordering, though it is a fairly good one. Getting this exactly
1164  * right would require a more sophisticated collation function that
1165  * takes case sensitivity into account. GLib does not currently
1166  * provide such a function.
1167  * 
1168  * Return value: a newly allocated string, that is a
1169  *   case independent form of @str.
1170  **/
1171 gchar *
1172 g_utf8_casefold (const gchar *str,
1173                  gssize       len)
1174 {
1175   GString *result;
1176   const char *p;
1177
1178   g_return_val_if_fail (str != NULL, NULL);
1179
1180   result = g_string_new (NULL);
1181   p = str;
1182   while ((len < 0 || p < str + len) && *p)
1183     {
1184       gunichar ch = g_utf8_get_char (p);
1185
1186       int start = 0;
1187       int end = G_N_ELEMENTS (casefold_table);
1188
1189       if (ch >= casefold_table[start].ch &&
1190           ch <= casefold_table[end - 1].ch)
1191         {
1192           while (TRUE)
1193             {
1194               int half = (start + end) / 2;
1195               if (ch == casefold_table[half].ch)
1196                 {
1197                   g_string_append (result, casefold_table[half].data);
1198                   goto next;
1199                 }
1200               else if (half == start)
1201                 break;
1202               else if (ch > casefold_table[half].ch)
1203                 start = half;
1204               else
1205                 end = half;
1206             }
1207         }
1208
1209       g_string_append_unichar (result, g_unichar_tolower (ch));
1210       
1211     next:
1212       p = g_utf8_next_char (p);
1213     }
1214
1215   return g_string_free (result, FALSE); 
1216 }
1217
1218 /**
1219  * g_unichar_get_mirror_char:
1220  * @ch: a Unicode character
1221  * @mirrored_ch: location to store the mirrored character
1222  * 
1223  * In Unicode, some characters are <firstterm>mirrored</firstterm>. This
1224  * means that their images are mirrored horizontally in text that is laid
1225  * out from right to left. For instance, "(" would become its mirror image,
1226  * ")", in right-to-left text.
1227  *
1228  * If @ch has the Unicode mirrored property and there is another unicode
1229  * character that typically has a glyph that is the mirror image of @ch's
1230  * glyph and @mirrored_ch is set, it puts that character in the address
1231  * pointed to by @mirrored_ch.  Otherwise the original character is put.
1232  *
1233  * Return value: %TRUE if @ch has a mirrored character, %FALSE otherwise
1234  *
1235  * Since: 2.4
1236  **/
1237 gboolean
1238 g_unichar_get_mirror_char (gunichar ch,
1239                            gunichar *mirrored_ch)
1240 {
1241   gboolean found;
1242   gunichar mirrored;
1243
1244   mirrored = GLIB_GET_MIRRORING(ch);
1245
1246   found = ch != mirrored;
1247   if (mirrored_ch)
1248     *mirrored_ch = mirrored;
1249
1250   return found;
1251
1252 }
1253
1254 #define G_SCRIPT_TABLE_MIDPOINT (G_N_ELEMENTS (g_script_table) / 2)
1255
1256 static inline GUnicodeScript
1257 g_unichar_get_script_bsearch (gunichar ch)
1258 {
1259   int lower = 0;
1260   int upper = G_N_ELEMENTS (g_script_table) - 1;
1261   static int saved_mid = G_SCRIPT_TABLE_MIDPOINT;
1262   int mid = saved_mid;
1263
1264
1265   do 
1266     {
1267       if (ch < g_script_table[mid].start)
1268         upper = mid - 1;
1269       else if (ch >= g_script_table[mid].start + g_script_table[mid].chars)
1270         lower = mid + 1;
1271       else
1272         return g_script_table[saved_mid = mid].script;
1273
1274       mid = (lower + upper) / 2;
1275     }
1276   while (lower <= upper);
1277
1278   return G_UNICODE_SCRIPT_UNKNOWN;
1279 }
1280
1281 /**
1282  * g_unichar_get_script:
1283  * @ch: a Unicode character
1284  * 
1285  * Looks up the #GUnicodeScript for a particular character (as defined 
1286  * by Unicode Standard Annex #24). No check is made for @ch being a
1287  * valid Unicode character; if you pass in invalid character, the
1288  * result is undefined.
1289  *
1290  * This function is equivalent to pango_script_for_unichar() and the
1291  * two are interchangeable.
1292  * 
1293  * Return value: the #GUnicodeScript for the character.
1294  *
1295  * Since: 2.14
1296  */
1297 GUnicodeScript
1298 g_unichar_get_script (gunichar ch)
1299 {
1300   if (ch < G_EASY_SCRIPTS_RANGE)
1301     return g_script_easy_table[ch];
1302   else 
1303     return g_unichar_get_script_bsearch (ch); 
1304 }
1305
1306
1307 #define __G_UNIPROP_C__
1308 #include "galiasdef.c"