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