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