Remove unused variables. (#321984, Andrew Paprocki)
[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 <stddef.h>
25 #include <string.h>
26 #include <locale.h>
27
28 #include "glib.h"
29 #include "gunichartables.h"
30 #include "gmirroringtable.h"
31 #include "gunicodeprivate.h"
32 #include "galias.h"
33
34 #define ATTR_TABLE(Page) (((Page) <= G_UNICODE_LAST_PAGE_PART1) \
35                           ? attr_table_part1[Page] \
36                           : attr_table_part2[(Page) - 0xe00])
37
38 #define ATTTABLE(Page, Char) \
39   ((ATTR_TABLE(Page) == G_UNICODE_MAX_TABLE_INDEX) ? 0 : (attr_data[ATTR_TABLE(Page)][Char]))
40
41 #define TTYPE_PART1(Page, Char) \
42   ((type_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
43    ? (type_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
44    : (type_data[type_table_part1[Page]][Char]))
45
46 #define TTYPE_PART2(Page, Char) \
47   ((type_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
48    ? (type_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
49    : (type_data[type_table_part2[Page]][Char]))
50
51 #define TYPE(Char) \
52   (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
53    ? TTYPE_PART1 ((Char) >> 8, (Char) & 0xff) \
54    : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
55       ? TTYPE_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
56       : G_UNICODE_UNASSIGNED))
57
58
59 #define IS(Type, Class) (((guint)1 << (Type)) & (Class))
60 #define OR(Type, Rest)  (((guint)1 << (Type)) | (Rest))
61
62
63
64 #define ISDIGIT(Type)   IS ((Type),                             \
65                             OR (G_UNICODE_DECIMAL_NUMBER,       \
66                             OR (G_UNICODE_LETTER_NUMBER,        \
67                             OR (G_UNICODE_OTHER_NUMBER,         0))))
68
69 #define ISALPHA(Type)   IS ((Type),                             \
70                             OR (G_UNICODE_LOWERCASE_LETTER,     \
71                             OR (G_UNICODE_UPPERCASE_LETTER,     \
72                             OR (G_UNICODE_TITLECASE_LETTER,     \
73                             OR (G_UNICODE_MODIFIER_LETTER,      \
74                             OR (G_UNICODE_OTHER_LETTER,         0))))))
75
76 #define ISALDIGIT(Type) IS ((Type),                             \
77                             OR (G_UNICODE_DECIMAL_NUMBER,       \
78                             OR (G_UNICODE_LETTER_NUMBER,        \
79                             OR (G_UNICODE_OTHER_NUMBER,         \
80                             OR (G_UNICODE_LOWERCASE_LETTER,     \
81                             OR (G_UNICODE_UPPERCASE_LETTER,     \
82                             OR (G_UNICODE_TITLECASE_LETTER,     \
83                             OR (G_UNICODE_MODIFIER_LETTER,      \
84                             OR (G_UNICODE_OTHER_LETTER,         0)))))))))
85
86 #define ISMARK(Type)    IS ((Type),                             \
87                             OR (G_UNICODE_NON_SPACING_MARK,     \
88                             OR (G_UNICODE_COMBINING_MARK,       \
89                             OR (G_UNICODE_ENCLOSING_MARK,       0))))
90
91 /**
92  * g_unichar_isalnum:
93  * @c: a Unicode character
94  * 
95  * Determines whether a character is alphanumeric.
96  * Given some UTF-8 text, obtain a character value
97  * with g_utf8_get_char().
98  * 
99  * Return value: %TRUE if @c is an alphanumeric character
100  **/
101 gboolean
102 g_unichar_isalnum (gunichar c)
103 {
104   return ISALDIGIT (TYPE (c)) ? TRUE : FALSE;
105 }
106
107 /**
108  * g_unichar_isalpha:
109  * @c: a Unicode character
110  * 
111  * Determines whether a character is alphabetic (i.e. a letter).
112  * Given some UTF-8 text, obtain a character value with
113  * g_utf8_get_char().
114  * 
115  * Return value: %TRUE if @c is an alphabetic character
116  **/
117 gboolean
118 g_unichar_isalpha (gunichar c)
119 {
120   return ISALPHA (TYPE (c)) ? TRUE : FALSE;
121 }
122
123
124 /**
125  * g_unichar_iscntrl:
126  * @c: a Unicode character
127  * 
128  * Determines whether a character is a control character.
129  * Given some UTF-8 text, obtain a character value with
130  * g_utf8_get_char().
131  * 
132  * Return value: %TRUE if @c is a control character
133  **/
134 gboolean
135 g_unichar_iscntrl (gunichar c)
136 {
137   return TYPE (c) == G_UNICODE_CONTROL;
138 }
139
140 /**
141  * g_unichar_isdigit:
142  * @c: a Unicode character
143  * 
144  * Determines whether a character is numeric (i.e. a digit).  This
145  * covers ASCII 0-9 and also digits in other languages/scripts.  Given
146  * some UTF-8 text, obtain a character value with g_utf8_get_char().
147  * 
148  * Return value: %TRUE if @c is a digit
149  **/
150 gboolean
151 g_unichar_isdigit (gunichar c)
152 {
153   return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
154 }
155
156
157 /**
158  * g_unichar_isgraph:
159  * @c: a Unicode character
160  * 
161  * Determines whether a character is printable and not a space
162  * (returns %FALSE for control characters, format characters, and
163  * spaces). g_unichar_isprint() is similar, but returns %TRUE for
164  * spaces. Given some UTF-8 text, obtain a character value with
165  * g_utf8_get_char().
166  * 
167  * Return value: %TRUE if @c is printable unless it's a space
168  **/
169 gboolean
170 g_unichar_isgraph (gunichar c)
171 {
172   return !IS (TYPE(c),
173               OR (G_UNICODE_CONTROL,
174               OR (G_UNICODE_FORMAT,
175               OR (G_UNICODE_UNASSIGNED,
176               OR (G_UNICODE_PRIVATE_USE,
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_PRIVATE_USE,
218               OR (G_UNICODE_SURROGATE,
219              0))))));
220 }
221
222 /**
223  * g_unichar_ispunct:
224  * @c: a Unicode character
225  * 
226  * Determines whether a character is punctuation or a symbol.
227  * Given some UTF-8 text, obtain a character value with
228  * g_utf8_get_char().
229  * 
230  * Return value: %TRUE if @c is a punctuation or symbol character
231  **/
232 gboolean
233 g_unichar_ispunct (gunichar c)
234 {
235   return IS (TYPE(c),
236              OR (G_UNICODE_CONNECT_PUNCTUATION,
237              OR (G_UNICODE_DASH_PUNCTUATION,
238              OR (G_UNICODE_CLOSE_PUNCTUATION,
239              OR (G_UNICODE_FINAL_PUNCTUATION,
240              OR (G_UNICODE_INITIAL_PUNCTUATION,
241              OR (G_UNICODE_OTHER_PUNCTUATION,
242              OR (G_UNICODE_OPEN_PUNCTUATION,
243              OR (G_UNICODE_CURRENCY_SYMBOL,
244              OR (G_UNICODE_MODIFIER_SYMBOL,
245              OR (G_UNICODE_MATH_SYMBOL,
246              OR (G_UNICODE_OTHER_SYMBOL,
247             0)))))))))))) ? TRUE : FALSE;
248 }
249
250 /**
251  * g_unichar_isspace:
252  * @c: a Unicode character
253  * 
254  * Determines whether a character is a space, tab, or line separator
255  * (newline, carriage return, etc.).  Given some UTF-8 text, obtain a
256  * character value with g_utf8_get_char().
257  *
258  * (Note: don't use this to do word breaking; you have to use
259  * Pango or equivalent to get word breaking right, the algorithm
260  * is fairly complex.)
261  *  
262  * Return value: %TRUE if @c is a space character
263  **/
264 gboolean
265 g_unichar_isspace (gunichar c)
266 {
267   switch (c)
268     {
269       /* special-case these since Unicode thinks they are not spaces */
270     case '\t':
271     case '\n':
272     case '\r':
273     case '\f':
274       return TRUE;
275       break;
276       
277     default:
278       {
279         return IS (TYPE(c),
280                    OR (G_UNICODE_SPACE_SEPARATOR,
281                    OR (G_UNICODE_LINE_SEPARATOR,
282                    OR (G_UNICODE_PARAGRAPH_SEPARATOR,
283                   0)))) ? TRUE : FALSE;
284       }
285       break;
286     }
287 }
288
289 /**
290  * g_unichar_isupper:
291  * @c: a Unicode character
292  * 
293  * Determines if a character is uppercase.
294  * 
295  * Return value: %TRUE if @c is an uppercase character
296  **/
297 gboolean
298 g_unichar_isupper (gunichar c)
299 {
300   return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
301 }
302
303 /**
304  * g_unichar_istitle:
305  * @c: a Unicode character
306  * 
307  * Determines if a character is titlecase. Some characters in
308  * Unicode which are composites, such as the DZ digraph
309  * have three case variants instead of just two. The titlecase
310  * form is used at the beginning of a word where only the
311  * first letter is capitalized. The titlecase form of the DZ
312  * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z.
313  * 
314  * Return value: %TRUE if the character is titlecase
315  **/
316 gboolean
317 g_unichar_istitle (gunichar c)
318 {
319   unsigned int i;
320   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
321     if (title_table[i][0] == c)
322       return 1;
323   return 0;
324 }
325
326 /**
327  * g_unichar_isxdigit:
328  * @c: a Unicode character.
329  * 
330  * Determines if a character is a hexidecimal digit.
331  * 
332  * Return value: %TRUE if the character is a hexadecimal digit
333  **/
334 gboolean
335 g_unichar_isxdigit (gunichar c)
336 {
337   return ((c >= 'a' && c <= 'f')
338           || (c >= 'A' && c <= 'F')
339           || ISDIGIT (TYPE (c)));
340 }
341
342 /**
343  * g_unichar_isdefined:
344  * @c: a Unicode character
345  * 
346  * Determines if a given character is assigned in the Unicode
347  * standard.
348  *
349  * Return value: %TRUE if the character has an assigned value
350  **/
351 gboolean
352 g_unichar_isdefined (gunichar c)
353 {
354   return TYPE (c) != G_UNICODE_UNASSIGNED;
355 }
356
357 /**
358  * g_unichar_iswide:
359  * @c: a Unicode character
360  * 
361  * Determines if a character is typically rendered in a double-width
362  * cell.
363  * 
364  * Return value: %TRUE if the character is wide
365  **/
366 /* This function stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>.  */
367 gboolean
368 g_unichar_iswide (gunichar c)
369 {
370   if (c < 0x1100)
371     return FALSE;
372
373   return (c <= 0x115f  /* Hangul Jamo init. consonants */ 
374           || c == 0x2329 || c == 0x232a     /* angle brackets */
375           || (c >= 0x2e80 && c <= 0xa4cf && (c < 0x302a || c > 0x302f) 
376               && c != 0x303f && c != 0x3099 && c!= 0x309a) /* CJK ... Yi */
377           || (c >= 0xac00 && c <= 0xd7a3)   /* Hangul Syllables */
378           || (c >= 0xf900 && c <= 0xfaff)   /* CJK Compatibility Ideographs */
379           || (c >= 0xfe30 && c <= 0xfe6f)   /* CJK Compatibility Forms */
380           || (c >= 0xff00 && c <= 0xff60)   /* Fullwidth Forms */
381           || (c >= 0xffe0 && c <= 0xffe6)   /* Fullwidth Forms */
382           || (c >= 0x20000 && c <= 0x2fffd) /* CJK extra stuff */
383           || (c >= 0x30000 && c <= 0x3fffd));
384 }
385
386 /**
387  * g_unichar_toupper:
388  * @c: a Unicode character
389  * 
390  * Converts a character to uppercase.
391  * 
392  * Return value: the result of converting @c to uppercase.
393  *               If @c is not an lowercase or titlecase character,
394  *               or has no upper case equivalent @c is returned unchanged.
395  **/
396 gunichar
397 g_unichar_toupper (gunichar c)
398 {
399   int t = TYPE (c);
400   if (t == G_UNICODE_LOWERCASE_LETTER)
401     {
402       gunichar val = ATTTABLE (c >> 8, c & 0xff);
403       if (val >= 0x1000000)
404         {
405           const gchar *p = special_case_table + val - 0x1000000;
406           return g_utf8_get_char (p);
407         }
408       else
409         return val ? val : c;
410     }
411   else if (t == G_UNICODE_TITLECASE_LETTER)
412     {
413       unsigned int i;
414       for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
415         {
416           if (title_table[i][0] == c)
417             return title_table[i][1];
418         }
419     }
420   return c;
421 }
422
423 /**
424  * g_unichar_tolower:
425  * @c: a Unicode character.
426  * 
427  * Converts a character to lower case.
428  * 
429  * Return value: the result of converting @c to lower case.
430  *               If @c is not an upperlower or titlecase character,
431  *               or has no lowercase equivalent @c is returned unchanged.
432  **/
433 gunichar
434 g_unichar_tolower (gunichar c)
435 {
436   int t = TYPE (c);
437   if (t == G_UNICODE_UPPERCASE_LETTER)
438     {
439       gunichar val = ATTTABLE (c >> 8, c & 0xff);
440       if (val >= 0x1000000)
441         {
442           const gchar *p = special_case_table + val - 0x1000000;
443           return g_utf8_get_char (p);
444         }
445       else
446         return val ? val : c;
447     }
448   else if (t == G_UNICODE_TITLECASE_LETTER)
449     {
450       unsigned int i;
451       for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
452         {
453           if (title_table[i][0] == c)
454             return title_table[i][2];
455         }
456     }
457   return c;
458 }
459
460 /**
461  * g_unichar_totitle:
462  * @c: a Unicode character
463  * 
464  * Converts a character to the titlecase.
465  * 
466  * Return value: the result of converting @c to titlecase.
467  *               If @c is not an uppercase or lowercase character,
468  *               @c is returned unchanged.
469  **/
470 gunichar
471 g_unichar_totitle (gunichar c)
472 {
473   unsigned int i;
474   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
475     {
476       if (title_table[i][0] == c || title_table[i][1] == c
477           || title_table[i][2] == c)
478         return title_table[i][0];
479     }
480   return (TYPE (c) == G_UNICODE_LOWERCASE_LETTER
481           ? ATTTABLE (c >> 8, c & 0xff)
482           : c);
483 }
484
485 /**
486  * g_unichar_digit_value:
487  * @c: a Unicode character
488  *
489  * Determines the numeric value of a character as a decimal
490  * digit.
491  *
492  * Return value: If @c is a decimal digit (according to
493  * g_unichar_isdigit()), its numeric value. Otherwise, -1.
494  **/
495 int
496 g_unichar_digit_value (gunichar c)
497 {
498   if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
499     return ATTTABLE (c >> 8, c & 0xff);
500   return -1;
501 }
502
503 /**
504  * g_unichar_xdigit_value:
505  * @c: a Unicode character
506  *
507  * Determines the numeric value of a character as a hexidecimal
508  * digit.
509  *
510  * Return value: If @c is a hex digit (according to
511  * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
512  **/
513 int
514 g_unichar_xdigit_value (gunichar c)
515 {
516   if (c >= 'A' && c <= 'F')
517     return c - 'A' + 10;
518   if (c >= 'a' && c <= 'f')
519     return c - 'a' + 10;
520   if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
521     return ATTTABLE (c >> 8, c & 0xff);
522   return -1;
523 }
524
525 /**
526  * g_unichar_type:
527  * @c: a Unicode character
528  * 
529  * Classifies a Unicode character by type.
530  * 
531  * Return value: the type of the character.
532  **/
533 GUnicodeType
534 g_unichar_type (gunichar c)
535 {
536   return TYPE (c);
537 }
538
539 /*
540  * Case mapping functions
541  */
542
543 typedef enum {
544   LOCALE_NORMAL,
545   LOCALE_TURKIC,
546   LOCALE_LITHUANIAN
547 } LocaleType;
548
549 static LocaleType
550 get_locale_type (void)
551 {
552 #ifdef G_OS_WIN32
553   char *tem = g_win32_getlocale ();
554   char locale[2];
555
556   locale[0] = tem[0];
557   locale[1] = tem[1];
558   g_free (tem);
559 #else
560   const char *locale = setlocale (LC_CTYPE, NULL);
561 #endif
562
563   switch (locale[0])
564     {
565    case 'a':
566       if (locale[1] == 'z')
567         return LOCALE_TURKIC;
568       break;
569     case 'l':
570       if (locale[1] == 't')
571         return LOCALE_LITHUANIAN;
572       break;
573     case 't':
574       if (locale[1] == 'r')
575         return LOCALE_TURKIC;
576       break;
577     }
578
579   return LOCALE_NORMAL;
580 }
581
582 static gint
583 output_marks (const char **p_inout,
584               char        *out_buffer,
585               gboolean     remove_dot)
586 {
587   const char *p = *p_inout;
588   gint len = 0;
589   
590   while (*p)
591     {
592       gunichar c = g_utf8_get_char (p);
593       
594       if (ISMARK (TYPE (c)))
595         {
596           if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
597             len += g_unichar_to_utf8 (c, out_buffer ? out_buffer + len : NULL);
598           p = g_utf8_next_char (p);
599         }
600       else
601         break;
602     }
603
604   *p_inout = p;
605   return len;
606 }
607
608 static gint
609 output_special_case (gchar *out_buffer,
610                      int    offset,
611                      int    type,
612                      int    which)
613 {
614   const gchar *p = special_case_table + offset;
615   gint len;
616
617   if (type != G_UNICODE_TITLECASE_LETTER)
618     p = g_utf8_next_char (p);
619
620   if (which == 1)
621     p += strlen (p) + 1;
622
623   len = strlen (p);
624   if (out_buffer)
625     memcpy (out_buffer, p, len);
626
627   return len;
628 }
629
630 static gsize
631 real_toupper (const gchar *str,
632               gssize       max_len,
633               gchar       *out_buffer,
634               LocaleType   locale_type)
635 {
636   const gchar *p = str;
637   const char *last = NULL;
638   gsize len = 0;
639   gboolean last_was_i = FALSE;
640
641   while ((max_len < 0 || p < str + max_len) && *p)
642     {
643       gunichar c = g_utf8_get_char (p);
644       int t = TYPE (c);
645       gunichar val;
646
647       last = p;
648       p = g_utf8_next_char (p);
649
650       if (locale_type == LOCALE_LITHUANIAN)
651         {
652           if (c == 'i')
653             last_was_i = TRUE;
654           else 
655             {
656               if (last_was_i)
657                 {
658                   /* Nasty, need to remove any dot above. Though
659                    * I think only E WITH DOT ABOVE occurs in practice
660                    * which could simplify this considerably.
661                    */
662                   gsize decomp_len, i;
663                   gunichar *decomp;
664
665                   decomp = g_unicode_canonical_decomposition (c, &decomp_len);
666                   for (i=0; i < decomp_len; i++)
667                     {
668                       if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
669                         len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
670                     }
671                   g_free (decomp);
672                   
673                   len += output_marks (&p, out_buffer ? out_buffer + len : NULL, TRUE);
674
675                   continue;
676                 }
677
678               if (!ISMARK (t))
679                 last_was_i = FALSE;
680             }
681         }
682       
683       if (locale_type == LOCALE_TURKIC && c == 'i')
684         {
685           /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
686           len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL); 
687         }
688       else if (c == 0x0345)     /* COMBINING GREEK YPOGEGRAMMENI */
689         {
690           /* Nasty, need to move it after other combining marks .. this would go away if
691            * we normalized first.
692            */
693           len += output_marks (&p, out_buffer ? out_buffer + len : NULL, FALSE);
694
695           /* And output as GREEK CAPITAL LETTER IOTA */
696           len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);         
697         }
698       else if (IS (t,
699                    OR (G_UNICODE_LOWERCASE_LETTER,
700                    OR (G_UNICODE_TITLECASE_LETTER,
701                   0))))
702         {
703           val = ATTTABLE (c >> 8, c & 0xff);
704
705           if (val >= 0x1000000)
706             {
707               len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t,
708                                           t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
709             }
710           else
711             {
712               if (t == G_UNICODE_TITLECASE_LETTER)
713                 {
714                   unsigned int i;
715                   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
716                     {
717                       if (title_table[i][0] == c)
718                         val = title_table[i][1];
719                     }
720                 }
721
722               len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
723             }
724         }
725       else
726         {
727           gsize char_len = g_utf8_skip[*(guchar *)last];
728
729           if (out_buffer)
730             memcpy (out_buffer + len, last, char_len);
731
732           len += char_len;
733         }
734
735     }
736
737   return len;
738 }
739
740 /**
741  * g_utf8_strup:
742  * @str: a UTF-8 encoded string
743  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
744  * 
745  * Converts all Unicode characters in the string that have a case
746  * to uppercase. The exact manner that this is done depends
747  * on the current locale, and may result in the number of
748  * characters in the string increasing. (For instance, the
749  * German ess-zet will be changed to SS.)
750  * 
751  * Return value: a newly allocated string, with all characters
752  *    converted to uppercase.  
753  **/
754 gchar *
755 g_utf8_strup (const gchar *str,
756               gssize       len)
757 {
758   gsize result_len;
759   LocaleType locale_type;
760   gchar *result;
761
762   g_return_val_if_fail (str != NULL, NULL);
763
764   locale_type = get_locale_type ();
765   
766   /*
767    * We use a two pass approach to keep memory management simple
768    */
769   result_len = real_toupper (str, len, NULL, locale_type);
770   result = g_malloc (result_len + 1);
771   real_toupper (str, len, result, locale_type);
772   result[result_len] = '\0';
773
774   return result;
775 }
776
777 /* traverses the string checking for characters with combining class == 230
778  * until a base character is found */
779 static gboolean
780 has_more_above (const gchar *str)
781 {
782   const gchar *p = str;
783   gint combining_class;
784
785   while (*p)
786     {
787       combining_class = _g_unichar_combining_class (g_utf8_get_char (p));
788       if (combining_class == 230)
789         return TRUE;
790       else if (combining_class == 0)
791         break;
792
793       p = g_utf8_next_char (p);
794     }
795
796   return FALSE;
797 }
798
799 static gsize
800 real_tolower (const gchar *str,
801               gssize       max_len,
802               gchar       *out_buffer,
803               LocaleType   locale_type)
804 {
805   const gchar *p = str;
806   const char *last = NULL;
807   gsize len = 0;
808
809   while ((max_len < 0 || p < str + max_len) && *p)
810     {
811       gunichar c = g_utf8_get_char (p);
812       int t = TYPE (c);
813       gunichar val;
814
815       last = p;
816       p = g_utf8_next_char (p);
817
818       if (locale_type == LOCALE_TURKIC && c == 'I')
819         {
820           if (g_utf8_get_char (p) == 0x0307)
821             {
822               /* I + COMBINING DOT ABOVE => i (U+0069) */
823               len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL); 
824               p = g_utf8_next_char (p);
825             }
826           else
827             {
828               /* I => LATIN SMALL LETTER DOTLESS I */
829               len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL); 
830             }
831         }
832       /* Introduce an explicit dot above when lowercasing capital I's and J's
833        * whenever there are more accents above. [SpecialCasing.txt] */
834       else if (locale_type == LOCALE_LITHUANIAN && 
835                (c == 0x00cc || c == 0x00cd || c == 0x0128))
836         {
837           len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL); 
838           len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL); 
839
840           switch (c)
841             {
842             case 0x00cc: 
843               len += g_unichar_to_utf8 (0x0300, out_buffer ? out_buffer + len : NULL); 
844               break;
845             case 0x00cd: 
846               len += g_unichar_to_utf8 (0x0301, out_buffer ? out_buffer + len : NULL); 
847               break;
848             case 0x0128: 
849               len += g_unichar_to_utf8 (0x0303, out_buffer ? out_buffer + len : NULL); 
850               break;
851             }
852         }
853       else if (locale_type == LOCALE_LITHUANIAN && 
854                (c == 'I' || c == 'J' || c == 0x012e) && 
855                has_more_above (p))
856         {
857           len += g_unichar_to_utf8 (g_unichar_tolower (c), out_buffer ? out_buffer + len : NULL); 
858           len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL); 
859         }
860       else if (c == 0x03A3)     /* GREEK CAPITAL LETTER SIGMA */
861         {
862           if ((max_len < 0 || p < str + max_len) && *p)
863             {
864               gunichar next_c = g_utf8_get_char (p);
865               int next_type = TYPE(next_c);
866
867               /* SIGMA mapps differently depending on whether it is
868                * final or not. The following simplified test would
869                * fail in the case of combining marks following the
870                * sigma, but I don't think that occurs in real text.
871                * The test here matches that in ICU.
872                */
873               if (ISALPHA (next_type)) /* Lu,Ll,Lt,Lm,Lo */
874                 val = 0x3c3;    /* GREEK SMALL SIGMA */
875               else
876                 val = 0x3c2;    /* GREEK SMALL FINAL SIGMA */
877             }
878           else
879             val = 0x3c2;        /* GREEK SMALL FINAL SIGMA */
880
881           len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
882         }
883       else if (IS (t,
884                    OR (G_UNICODE_UPPERCASE_LETTER,
885                    OR (G_UNICODE_TITLECASE_LETTER,
886                   0))))
887         {
888           val = ATTTABLE (c >> 8, c & 0xff);
889
890           if (val >= 0x1000000)
891             {
892               len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t, 0);
893             }
894           else
895             {
896               if (t == G_UNICODE_TITLECASE_LETTER)
897                 {
898                   unsigned int i;
899                   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
900                     {
901                       if (title_table[i][0] == c)
902                         val = title_table[i][2];
903                     }
904                 }
905
906               len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
907             }
908         }
909       else
910         {
911           gsize char_len = g_utf8_skip[*(guchar *)last];
912
913           if (out_buffer)
914             memcpy (out_buffer + len, last, char_len);
915
916           len += char_len;
917         }
918
919     }
920
921   return len;
922 }
923
924 /**
925  * g_utf8_strdown:
926  * @str: a UTF-8 encoded string
927  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
928  * 
929  * Converts all Unicode characters in the string that have a case
930  * to lowercase. The exact manner that this is done depends
931  * on the current locale, and may result in the number of
932  * characters in the string changing.
933  * 
934  * Return value: a newly allocated string, with all characters
935  *    converted to lowercase.  
936  **/
937 gchar *
938 g_utf8_strdown (const gchar *str,
939                 gssize       len)
940 {
941   gsize result_len;
942   LocaleType locale_type;
943   gchar *result;
944
945   g_return_val_if_fail (str != NULL, NULL);
946
947   locale_type = get_locale_type ();
948   
949   /*
950    * We use a two pass approach to keep memory management simple
951    */
952   result_len = real_tolower (str, len, NULL, locale_type);
953   result = g_malloc (result_len + 1);
954   real_tolower (str, len, result, locale_type);
955   result[result_len] = '\0';
956
957   return result;
958 }
959
960 /**
961  * g_utf8_casefold:
962  * @str: a UTF-8 encoded string
963  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
964  * 
965  * Converts a string into a form that is independent of case. The
966  * result will not correspond to any particular case, but can be
967  * compared for equality or ordered with the results of calling
968  * g_utf8_casefold() on other strings.
969  * 
970  * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
971  * only an approximation to the correct linguistic case insensitive
972  * ordering, though it is a fairly good one. Getting this exactly
973  * right would require a more sophisticated collation function that
974  * takes case sensitivity into account. GLib does not currently
975  * provide such a function.
976  * 
977  * Return value: a newly allocated string, that is a
978  *   case independent form of @str.
979  **/
980 gchar *
981 g_utf8_casefold (const gchar *str,
982                  gssize       len)
983 {
984   GString *result;
985   const char *p;
986
987   g_return_val_if_fail (str != NULL, NULL);
988
989   result = g_string_new (NULL);
990   p = str;
991   while ((len < 0 || p < str + len) && *p)
992     {
993       gunichar ch = g_utf8_get_char (p);
994
995       int start = 0;
996       int end = G_N_ELEMENTS (casefold_table);
997
998       if (ch >= casefold_table[start].ch &&
999           ch <= casefold_table[end - 1].ch)
1000         {
1001           while (TRUE)
1002             {
1003               int half = (start + end) / 2;
1004               if (ch == casefold_table[half].ch)
1005                 {
1006                   g_string_append (result, casefold_table[half].data);
1007                   goto next;
1008                 }
1009               else if (half == start)
1010                 break;
1011               else if (ch > casefold_table[half].ch)
1012                 start = half;
1013               else
1014                 end = half;
1015             }
1016         }
1017
1018       g_string_append_unichar (result, g_unichar_tolower (ch));
1019       
1020     next:
1021       p = g_utf8_next_char (p);
1022     }
1023
1024   return g_string_free (result, FALSE); 
1025 }
1026
1027 /**
1028  * g_unichar_get_mirror_char:
1029  * @ch: a Unicode character
1030  * @mirrored_ch: location to store the mirrored character
1031  * 
1032  * In Unicode, some characters are <firstterm>mirrored</firstterm>. This
1033  * means that their images are mirrored horizontally in text that is laid
1034  * out from right to left. For instance, "(" would become its mirror image,
1035  * ")", in right-to-left text.
1036  *
1037  * If @ch has the Unicode mirrored property and there is another unicode
1038  * character that typically has a glyph that is the mirror image of @ch's
1039  * glyph and @mirrored_ch is set, it puts that character in the address
1040  * pointed to by @mirrored_ch.  Otherwise the original character is put.
1041  *
1042  * Return value: %TRUE if @ch has a mirrored character, %FALSE otherwise
1043  *
1044  * Since: 2.4
1045  **/
1046 gboolean
1047 g_unichar_get_mirror_char (gunichar ch,
1048                            gunichar *mirrored_ch)
1049 {
1050   gboolean found;
1051   gunichar mirrored;
1052
1053   mirrored = GLIB_GET_MIRRORING(ch);
1054
1055   found = ch != mirrored;
1056   if (mirrored_ch)
1057     *mirrored_ch = mirrored;
1058
1059   return found;
1060
1061 }
1062
1063 #define __G_UNIPROP_C__
1064 #include "galiasdef.c"