for msvc build
[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
583   if (type != G_UNICODE_TITLECASE_LETTER)
584     p = g_utf8_next_char (p);
585
586   if (which == 1)
587     p += strlen (p) + 1;
588
589   len = strlen (p);
590   if (out_buffer)
591     memcpy (out_buffer, p, len);
592
593   return len;
594 }
595
596 static gsize
597 real_toupper (const gchar *str,
598               gssize       max_len,
599               gchar       *out_buffer,
600               LocaleType   locale_type)
601 {
602   const gchar *p = str;
603   const char *last = NULL;
604   gsize len = 0;
605   gboolean last_was_i = FALSE;
606
607   while ((max_len < 0 || p < str + max_len) && *p)
608     {
609       gunichar c = g_utf8_get_char (p);
610       int t = TYPE (c);
611       gunichar val;
612
613       last = p;
614       p = g_utf8_next_char (p);
615
616       if (locale_type == LOCALE_LITHUANIAN)
617         {
618           if (c == 'i')
619             last_was_i = TRUE;
620           else 
621             {
622               if (last_was_i)
623                 {
624                   /* Nasty, need to remove any dot above. Though
625                    * I think only E WITH DOT ABOVE occurs in practice
626                    * which could simplify this considerably.
627                    */
628                   gsize decomp_len, i;
629                   gunichar *decomp;
630
631                   decomp = g_unicode_canonical_decomposition (c, &decomp_len);
632                   for (i=0; i < decomp_len; i++)
633                     {
634                       if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
635                         len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
636                     }
637                   g_free (decomp);
638                   
639                   len = output_marks (&p, out_buffer, len, TRUE);
640
641                   continue;
642                 }
643
644               if (!ISMARK(t))
645                 last_was_i = FALSE;
646             }
647         }
648       
649       if (locale_type == LOCALE_TURKIC && c == 'i')
650         {
651           /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
652           len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL); 
653         }
654       else if (c == 0x0345)     /* COMBINING GREEK YPOGEGRAMMENI */
655         {
656           /* Nasty, need to move it after other combining marks .. this would go away if
657            * we normalized first.
658            */
659           len = output_marks (&p, out_buffer, len, FALSE);
660
661           /* And output as GREEK CAPITAL LETTER IOTA */
662           len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);         
663         }
664       else if (t == G_UNICODE_LOWERCASE_LETTER || t == G_UNICODE_TITLECASE_LETTER)
665         {
666           val = ATTTABLE (c >> 8, c & 0xff);
667
668           if (val >= 0x1000000)
669             {
670               len += output_special_case (out_buffer, len, val - 0x1000000, t,
671                                           t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
672             }
673           else
674             {
675               if (t == G_UNICODE_TITLECASE_LETTER)
676                 {
677                   unsigned int i;
678                   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
679                     {
680                       if (title_table[i][0] == c)
681                         val = title_table[i][1];
682                     }
683                 }
684
685               len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
686             }
687         }
688       else
689         {
690           gsize char_len = g_utf8_skip[*(guchar *)last];
691
692           if (out_buffer)
693             memcpy (out_buffer + len, last, char_len);
694
695           len += char_len;
696         }
697
698     }
699
700   return len;
701 }
702
703 /**
704  * g_utf8_strup:
705  * @str: a UTF-8 encoded string
706  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
707  * 
708  * Converts all Unicode characters in the string that have a case
709  * to uppercase. The exact manner that this is done depends
710  * on the current locale, and may result in the number of
711  * characters in the string increasing. (For instance, the
712  * German ess-zet will be changed to SS.)
713  * 
714  * Return value: a newly allocated string, with all characters
715  *    converted to uppercase.  
716  **/
717 gchar *
718 g_utf8_strup (const gchar *str,
719               gssize       len)
720 {
721   gsize result_len;
722   LocaleType locale_type;
723   gchar *result;
724
725   g_return_val_if_fail (str != NULL, NULL);
726
727   locale_type = get_locale_type ();
728   
729   /*
730    * We use a two pass approach to keep memory management simple
731    */
732   result_len = real_toupper (str, len, NULL, locale_type);
733   result = g_malloc (result_len + 1);
734   real_toupper (str, len, result, locale_type);
735   result[result_len] = '\0';
736
737   return result;
738 }
739
740 static gsize
741 real_tolower (const gchar *str,
742               gssize       max_len,
743               gchar       *out_buffer,
744               LocaleType   locale_type)
745 {
746   const gchar *p = str;
747   const char *last = NULL;
748   gsize len = 0;
749
750   while ((max_len < 0 || p < str + max_len) && *p)
751     {
752       gunichar c = g_utf8_get_char (p);
753       int t = TYPE (c);
754       gunichar val;
755
756       last = p;
757       p = g_utf8_next_char (p);
758
759       if (locale_type == LOCALE_TURKIC && c == 'I')
760         {
761           /* I => LATIN SMALL LETTER DOTLESS I */
762           len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL); 
763         }
764       else if (c == 0x03A3)     /* GREEK CAPITAL LETTER SIGMA */
765         {
766           if ((max_len < 0 || p < str + max_len) && *p)
767             {
768               gunichar next_c = g_utf8_get_char (p);
769               int next_type = TYPE(next_c);
770
771               /* SIGMA mapps differently depending on whether it is
772                * final or not. The following simplified test would
773                * fail in the case of combining marks following the
774                * sigma, but I don't think that occurs in real text.
775                * The test here matches that in ICU.
776                */
777               if (ISALPHA(next_type)) /* Lu,Ll,Lt,Lm,Lo */
778                 val = 0x3c3;    /* GREEK SMALL SIGMA */
779               else
780                 val = 0x3c2;    /* GREEK SMALL FINAL SIGMA */
781             }
782           else
783             val = 0x3c2;        /* GREEK SMALL FINAL SIGMA */
784
785           len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
786         }
787       else if (t == G_UNICODE_UPPERCASE_LETTER || t == G_UNICODE_TITLECASE_LETTER)
788         {
789           val = ATTTABLE (c >> 8, c & 0xff);
790
791           if (val >= 0x1000000)
792             {
793               len += output_special_case (out_buffer, len, val - 0x1000000, t, 0);
794             }
795           else
796             {
797               if (t == G_UNICODE_TITLECASE_LETTER)
798                 {
799                   unsigned int i;
800                   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
801                     {
802                       if (title_table[i][0] == c)
803                         val = title_table[i][2];
804                     }
805                 }
806
807               len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
808             }
809         }
810       else
811         {
812           gsize char_len = g_utf8_skip[*(guchar *)last];
813
814           if (out_buffer)
815             memcpy (out_buffer + len, last, char_len);
816
817           len += char_len;
818         }
819
820     }
821
822   return len;
823 }
824
825 /**
826  * g_utf8_strdown:
827  * @str: a UTF-8 encoded string
828  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
829  * 
830  * Converts all Unicode characters in the string that have a case
831  * to lowercase. The exact manner that this is done depends
832  * on the current locale, and may result in the number of
833  * characters in the string changing.
834  * 
835  * Return value: a newly allocated string, with all characters
836  *    converted to lowercase.  
837  **/
838 gchar *
839 g_utf8_strdown (const gchar *str,
840                 gssize       len)
841 {
842   gsize result_len;
843   LocaleType locale_type;
844   gchar *result;
845
846   g_return_val_if_fail (str != NULL, NULL);
847
848   locale_type = get_locale_type ();
849   
850   /*
851    * We use a two pass approach to keep memory management simple
852    */
853   result_len = real_tolower (str, len, NULL, locale_type);
854   result = g_malloc (result_len + 1);
855   real_tolower (str, len, result, locale_type);
856   result[result_len] = '\0';
857
858   return result;
859 }
860
861 /**
862  * g_utf8_casefold:
863  * @str: a UTF-8 encoded string
864  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
865  * 
866  * Converts a string into a form that is independent of case. The
867  * result will not correspond to any particular case, but can be
868  * compared for equality or ordered with the results of calling
869  * g_utf8_casefold() on other strings.
870  * 
871  * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
872  * only an approximation to the correct linguistic case insensitive
873  * ordering, though it is a fairly good one. Getting this exactly
874  * right would require a more sophisticated collation function that
875  * takes case sensitivity into account. GLib does not currently
876  * provide such a function.
877  * 
878  * Return value: a newly allocated string, that is a
879  *   case independent form of @str.
880  **/
881 gchar *
882 g_utf8_casefold (const gchar *str,
883                  gssize       len)
884 {
885   GString *result = g_string_new (NULL);
886   const char *p;
887
888   p = str;
889   while ((len < 0 || p < str + len) && *p)
890     {
891       gunichar ch = g_utf8_get_char (p);
892
893       int start = 0;
894       int end = G_N_ELEMENTS (casefold_table);
895
896       if (ch >= casefold_table[start].ch &&
897           ch <= casefold_table[end - 1].ch)
898         {
899           while (TRUE)
900             {
901               int half = (start + end) / 2;
902               if (ch == casefold_table[half].ch)
903                 {
904                   g_string_append (result, casefold_table[half].data);
905                   goto next;
906                 }
907               else if (half == start)
908                 break;
909               else if (ch > casefold_table[half].ch)
910                 start = half;
911               else
912                 end = half;
913             }
914         }
915
916       g_string_append_unichar (result, g_unichar_tolower (ch));
917       
918     next:
919       p = g_utf8_next_char (p);
920     }
921
922   return g_string_free (result, FALSE); 
923 }