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