Put the g_return_val_if_fail() in the right place.
[glib.git] / glib / guniprop.c
blob0740b3cc6f03faaacb8536ce6fc96241ed5fb19e
1 /* guniprop.c - Unicode character properties.
3 * Copyright (C) 1999 Tom Tromey
4 * Copyright (C) 2000 Red Hat, Inc.
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.
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.
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.
22 #include "config.h"
24 #include <stddef.h>
25 #include <string.h>
26 #include <locale.h>
28 #include "glib.h"
29 #include "gunichartables.h"
32 #define ATTTABLE(Page, Char) \
33 ((attr_table[Page] == G_UNICODE_MAX_TABLE_INDEX) ? 0 : (attr_data[attr_table[Page]][Char]))
35 /* We cheat a bit and cast type values to (char *). We detect these
36 using the &0xff trick. */
37 #define TTYPE(Page, Char) \
38 ((type_table[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
39 ? (type_table[Page] - G_UNICODE_MAX_TABLE_INDEX) \
40 : (type_data[type_table[Page]][Char]))
43 #define TYPE(Char) (((Char) > (G_UNICODE_LAST_CHAR)) ? G_UNICODE_UNASSIGNED : TTYPE ((Char) >> 8, (Char) & 0xff))
45 #define ISDIGIT(Type) ((Type) == G_UNICODE_DECIMAL_NUMBER \
46 || (Type) == G_UNICODE_LETTER_NUMBER \
47 || (Type) == G_UNICODE_OTHER_NUMBER)
49 #define ISALPHA(Type) ((Type) == G_UNICODE_LOWERCASE_LETTER \
50 || (Type) == G_UNICODE_UPPERCASE_LETTER \
51 || (Type) == G_UNICODE_TITLECASE_LETTER \
52 || (Type) == G_UNICODE_MODIFIER_LETTER \
53 || (Type) == G_UNICODE_OTHER_LETTER)
55 #define ISMARK(Type) ((Type) == G_UNICODE_NON_SPACING_MARK || \
56 (Type) == G_UNICODE_COMBINING_MARK || \
57 (Type) == G_UNICODE_ENCLOSING_MARK)
60 /**
61 * g_unichar_isalnum:
62 * @c: a Unicode character
64 * Determines whether a character is alphanumeric.
65 * Given some UTF-8 text, obtain a character value
66 * with g_utf8_get_char().
68 * Return value: %TRUE if @c is an alphanumeric character
69 **/
70 gboolean
71 g_unichar_isalnum (gunichar c)
73 int t = TYPE (c);
74 return ISDIGIT (t) || ISALPHA (t);
77 /**
78 * g_unichar_isalpha:
79 * @c: a Unicode character
81 * Determines whether a character is alphabetic (i.e. a letter).
82 * Given some UTF-8 text, obtain a character value with
83 * g_utf8_get_char().
85 * Return value: %TRUE if @c is an alphabetic character
86 **/
87 gboolean
88 g_unichar_isalpha (gunichar c)
90 int t = TYPE (c);
91 return ISALPHA (t);
95 /**
96 * g_unichar_iscntrl:
97 * @c: a Unicode character
99 * Determines whether a character is a control character.
100 * Given some UTF-8 text, obtain a character value with
101 * g_utf8_get_char().
103 * Return value: %TRUE if @c is a control character
105 gboolean
106 g_unichar_iscntrl (gunichar c)
108 return TYPE (c) == G_UNICODE_CONTROL;
112 * g_unichar_isdigit:
113 * @c: a Unicode character
115 * Determines whether a character is numeric (i.e. a digit). This
116 * covers ASCII 0-9 and also digits in other languages/scripts. Given
117 * some UTF-8 text, obtain a character value with g_utf8_get_char().
119 * Return value: %TRUE if @c is a digit
121 gboolean
122 g_unichar_isdigit (gunichar c)
124 return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
129 * g_unichar_isgraph:
130 * @c: a Unicode character
132 * Determines whether a character is printable and not a space
133 * (returns %FALSE for control characters, format characters, and
134 * spaces). g_unichar_isprint() is similar, but returns %TRUE for
135 * spaces. Given some UTF-8 text, obtain a character value with
136 * g_utf8_get_char().
138 * Return value: %TRUE if @c is printable unless it's a space
140 gboolean
141 g_unichar_isgraph (gunichar c)
143 int t = TYPE (c);
144 return (t != G_UNICODE_CONTROL
145 && t != G_UNICODE_FORMAT
146 && t != G_UNICODE_UNASSIGNED
147 && t != G_UNICODE_PRIVATE_USE
148 && t != G_UNICODE_SURROGATE
149 && t != G_UNICODE_SPACE_SEPARATOR);
153 * g_unichar_islower:
154 * @c: a Unicode character
156 * Determines whether a character is a lowercase letter.
157 * Given some UTF-8 text, obtain a character value with
158 * g_utf8_get_char().
160 * Return value: %TRUE if @c is a lowercase letter
162 gboolean
163 g_unichar_islower (gunichar c)
165 return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
170 * g_unichar_isprint:
171 * @c: a Unicode character
173 * Determines whether a character is printable.
174 * Unlike g_unichar_isgraph(), returns %TRUE for spaces.
175 * Given some UTF-8 text, obtain a character value with
176 * g_utf8_get_char().
178 * Return value: %TRUE if @c is printable
180 gboolean
181 g_unichar_isprint (gunichar c)
183 int t = TYPE (c);
184 return (t != G_UNICODE_CONTROL
185 && t != G_UNICODE_FORMAT
186 && t != G_UNICODE_UNASSIGNED
187 && t != G_UNICODE_PRIVATE_USE
188 && t != G_UNICODE_SURROGATE);
192 * g_unichar_ispunct:
193 * @c: a Unicode character
195 * Determines whether a character is punctuation or a symbol.
196 * Given some UTF-8 text, obtain a character value with
197 * g_utf8_get_char().
199 * Return value: %TRUE if @c is a punctuation or symbol character
201 gboolean
202 g_unichar_ispunct (gunichar c)
204 int t = TYPE (c);
205 return (t == G_UNICODE_CONNECT_PUNCTUATION || t == G_UNICODE_DASH_PUNCTUATION
206 || t == G_UNICODE_CLOSE_PUNCTUATION || t == G_UNICODE_FINAL_PUNCTUATION
207 || t == G_UNICODE_INITIAL_PUNCTUATION || t == G_UNICODE_OTHER_PUNCTUATION
208 || t == G_UNICODE_OPEN_PUNCTUATION || t == G_UNICODE_CURRENCY_SYMBOL
209 || t == G_UNICODE_MODIFIER_SYMBOL || t == G_UNICODE_MATH_SYMBOL
210 || t == G_UNICODE_OTHER_SYMBOL);
214 * g_unichar_isspace:
215 * @c: a Unicode character
217 * Determines whether a character is a space, tab, or line separator
218 * (newline, carriage return, etc.). Given some UTF-8 text, obtain a
219 * character value with g_utf8_get_char().
221 * (Note: don't use this to do word breaking; you have to use
222 * Pango or equivalent to get word breaking right, the algorithm
223 * is fairly complex.)
225 * Return value: %TRUE if @c is a punctuation character
227 gboolean
228 g_unichar_isspace (gunichar c)
230 switch (c)
232 /* special-case these since Unicode thinks they are not spaces */
233 case '\t':
234 case '\n':
235 case '\r':
236 case '\f':
237 return TRUE;
238 break;
240 default:
242 int t = TYPE (c);
243 return (t == G_UNICODE_SPACE_SEPARATOR || t == G_UNICODE_LINE_SEPARATOR
244 || t == G_UNICODE_PARAGRAPH_SEPARATOR);
246 break;
251 * g_unichar_isupper:
252 * @c: a Unicode character
254 * Determines if a character is uppercase.
256 * Return value: %TRUE if @c is an uppercase character
258 gboolean
259 g_unichar_isupper (gunichar c)
261 return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
265 * g_unichar_istitle:
266 * @c: a Unicode character
268 * Determines if a character is titlecase. Some characters in
269 * Unicode which are composites, such as the DZ digraph
270 * have three case variants instead of just two. The titlecase
271 * form is used at the beginning of a word where only the
272 * first letter is capitalized. The titlecase form of the DZ
273 * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z.
275 * Return value: %TRUE if the character is titlecase
277 gboolean
278 g_unichar_istitle (gunichar c)
280 unsigned int i;
281 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
282 if (title_table[i][0] == c)
283 return 1;
284 return 0;
288 * g_unichar_isxdigit:
289 * @c: a Unicode character.
291 * Determines if a character is a hexidecimal digit.
293 * Return value: %TRUE if the character is a hexadecimal digit
295 gboolean
296 g_unichar_isxdigit (gunichar c)
298 int t = TYPE (c);
299 return ((c >= 'a' && c <= 'f')
300 || (c >= 'A' && c <= 'F')
301 || ISDIGIT (t));
305 * g_unichar_isdefined:
306 * @c: a Unicode character
308 * Determines if a given character is assigned in the Unicode
309 * standard.
311 * Return value: %TRUE if the character has an assigned value
313 gboolean
314 g_unichar_isdefined (gunichar c)
316 int t = TYPE (c);
317 return t != G_UNICODE_UNASSIGNED;
321 * g_unichar_iswide:
322 * @c: a Unicode character
324 * Determines if a character is typically rendered in a double-width
325 * cell.
327 * Return value: %TRUE if the character is wide
329 /* This function stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>. */
330 gboolean
331 g_unichar_iswide (gunichar c)
333 if (c < 0x1100)
334 return FALSE;
336 return (c <= 0x115f /* Hangul Jamo init. consonants */
337 || c == 0x2329 || c == 0x232a /* angle brackets */
338 || (c >= 0x2e80 && c <= 0xa4cf && (c < 0x302a || c > 0x302f)
339 && c != 0x303f && c != 0x3099 && c!= 0x309a) /* CJK ... Yi */
340 || (c >= 0xac00 && c <= 0xd7a3) /* Hangul Syllables */
341 || (c >= 0xf900 && c <= 0xfaff) /* CJK Compatibility Ideographs */
342 || (c >= 0xfe30 && c <= 0xfe6f) /* CJK Compatibility Forms */
343 || (c >= 0xff00 && c <= 0xff60) /* Fullwidth Forms */
344 || (c >= 0xffe0 && c <= 0xffe6) /* Fullwidth Forms */
345 || (c >= 0x20000 && c <= 0x2fffd) /* CJK extra stuff */
346 || (c >= 0x30000 && c <= 0x3fffd));
350 * g_unichar_toupper:
351 * @c: a Unicode character
353 * Converts a character to uppercase.
355 * Return value: the result of converting @c to uppercase.
356 * If @c is not an lowercase or titlecase character,
357 * or has no upper case equivalent @c is returned unchanged.
359 gunichar
360 g_unichar_toupper (gunichar c)
362 int t = TYPE (c);
363 if (t == G_UNICODE_LOWERCASE_LETTER)
365 gunichar val = ATTTABLE (c >> 8, c & 0xff);
366 if (val >= 0xd800 && val < 0xdc00)
368 const guchar *p = special_case_table[val - 0xd800];
369 return p[0] * 256 + p[1];
371 else
372 return val ? val : c;
374 else if (t == G_UNICODE_TITLECASE_LETTER)
376 unsigned int i;
377 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
379 if (title_table[i][0] == c)
380 return title_table[i][1];
383 return c;
387 * g_unichar_tolower:
388 * @c: a Unicode character.
390 * Converts a character to lower case.
392 * Return value: the result of converting @c to lower case.
393 * If @c is not an upperlower or titlecase character,
394 * or has no lowercase equivalent @c is returned unchanged.
396 gunichar
397 g_unichar_tolower (gunichar c)
399 int t = TYPE (c);
400 if (t == G_UNICODE_UPPERCASE_LETTER)
402 gunichar val = ATTTABLE (c >> 8, c & 0xff);
403 if (val >= 0xd800 && val < 0xdc00)
405 const guchar *p = special_case_table[val - 0xd800];
406 return p[0] * 256 + p[1];
408 else
409 return val ? val : c;
411 else if (t == G_UNICODE_TITLECASE_LETTER)
413 unsigned int i;
414 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
416 if (title_table[i][0] == c)
417 return title_table[i][2];
420 return c;
424 * g_unichar_totitle:
425 * @c: a Unicode character
427 * Converts a character to the titlecase.
429 * Return value: the result of converting @c to titlecase.
430 * If @c is not an uppercase or lowercase character,
431 * @c is returned unchanged.
433 gunichar
434 g_unichar_totitle (gunichar c)
436 unsigned int i;
437 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
439 if (title_table[i][0] == c || title_table[i][1] == c
440 || title_table[i][2] == c)
441 return title_table[i][0];
443 return (TYPE (c) == G_UNICODE_LOWERCASE_LETTER
444 ? ATTTABLE (c >> 8, c & 0xff)
445 : c);
449 * g_unichar_digit_value:
450 * @c: a Unicode character
452 * Determines the numeric value of a character as a decimal
453 * digit.
455 * Return value: If @c is a decimal digit (according to
456 * g_unichar_isdigit()), its numeric value. Otherwise, -1.
459 g_unichar_digit_value (gunichar c)
461 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
462 return ATTTABLE (c >> 8, c & 0xff);
463 return -1;
467 * g_unichar_xdigit_value:
468 * @c: a Unicode character
470 * Determines the numeric value of a character as a hexidecimal
471 * digit.
473 * Return value: If @c is a hex digit (according to
474 * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
477 g_unichar_xdigit_value (gunichar c)
479 if (c >= 'A' && c <= 'F')
480 return c - 'A' + 10;
481 if (c >= 'a' && c <= 'f')
482 return c - 'a' + 10;
483 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
484 return ATTTABLE (c >> 8, c & 0xff);
485 return -1;
489 * g_unichar_type:
490 * @c: a Unicode character
492 * Classifies a Unicode character by type.
494 * Return value: the type of the character.
496 GUnicodeType
497 g_unichar_type (gunichar c)
499 return TYPE (c);
503 * Case mapping functions
506 typedef enum {
507 LOCALE_NORMAL,
508 LOCALE_TURKIC,
509 LOCALE_LITHUANIAN
510 } LocaleType;
512 static LocaleType
513 get_locale_type (void)
515 const char *locale = setlocale (LC_CTYPE, NULL);
517 switch (locale[0])
519 case 'a':
520 if (locale[1] == 'z')
521 return LOCALE_TURKIC;
522 break;
523 case 'l':
524 if (locale[1] == 't')
525 return LOCALE_LITHUANIAN;
526 break;
527 case 't':
528 if (locale[1] == 'r')
529 return LOCALE_TURKIC;
530 break;
533 return LOCALE_NORMAL;
536 static int
537 output_marks (const char **p_inout,
538 char *out_buffer,
539 int len,
540 gboolean remove_dot)
542 const char *p = *p_inout;
544 while (*p)
546 gunichar c = g_utf8_get_char (p);
547 int t = TYPE(c);
549 if (ISMARK(t))
551 if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
552 len += g_unichar_to_utf8 (c, out_buffer ? out_buffer + len : NULL);
553 p = g_utf8_next_char (p);
555 else
556 break;
559 *p_inout = p;
560 return len;
563 static gsize
564 output_special_case (gchar *out_buffer,
565 gsize len,
566 int index,
567 int type,
568 int which)
570 const guchar *p = special_case_table[index];
572 if (type != G_UNICODE_TITLECASE_LETTER)
573 p += 2; /* +2 to skip over "best single match" */
575 if (which == 1)
577 while (p[0] || p[1])
578 p += 2;
579 p += 2;
582 while (TRUE)
584 gunichar ch = p[0] * 256 + p[1];
585 if (!ch)
586 break;
588 len += g_unichar_to_utf8 (ch, out_buffer ? out_buffer + len : NULL);
589 p += 2;
592 return len;
595 static gsize
596 real_toupper (const gchar *str,
597 gssize max_len,
598 gchar *out_buffer,
599 LocaleType locale_type)
601 const gchar *p = str;
602 const char *last = NULL;
603 gsize len = 0;
604 gboolean last_was_i = FALSE;
606 while ((max_len < 0 || p < str + max_len) && *p)
608 gunichar c = g_utf8_get_char (p);
609 int t = TYPE (c);
610 gunichar val;
612 last = p;
613 p = g_utf8_next_char (p);
615 if (locale_type == LOCALE_LITHUANIAN)
617 if (c == 'i')
618 last_was_i = TRUE;
619 else
621 if (last_was_i)
623 /* Nasty, need to remove any dot above. Though
624 * I think only E WITH DOT ABOVE occurs in practice
625 * which could simplify this considerably.
627 gsize decomp_len, i;
628 gunichar *decomp;
630 decomp = g_unicode_canonical_decomposition (c, &decomp_len);
631 for (i=0; i < decomp_len; i++)
633 if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
634 len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
636 g_free (decomp);
638 len = output_marks (&p, out_buffer, len, TRUE);
640 continue;
643 if (!ISMARK(t))
644 last_was_i = FALSE;
648 if (locale_type == LOCALE_TURKIC && c == 'i')
650 /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
651 len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL);
653 else if (c == 0x0345) /* COMBINING GREEK YPOGEGRAMMENI */
655 /* Nasty, need to move it after other combining marks .. this would go away if
656 * we normalized first.
658 len = output_marks (&p, out_buffer, len, FALSE);
660 /* And output as GREEK CAPITAL LETTER IOTA */
661 len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);
663 else if (t == G_UNICODE_LOWERCASE_LETTER || t == G_UNICODE_TITLECASE_LETTER)
665 val = ATTTABLE (c >> 8, c & 0xff);
667 if (val >= 0xd800 && val < 0xdc00)
669 len += output_special_case (out_buffer, len, val - 0xd800, t,
670 t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
672 else
674 if (t == G_UNICODE_TITLECASE_LETTER)
676 unsigned int i;
677 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
679 if (title_table[i][0] == c)
680 val = title_table[i][1];
684 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
687 else
689 gsize char_len = g_utf8_skip[*(guchar *)last];
691 if (out_buffer)
692 memcpy (out_buffer + len, last, char_len);
694 len += char_len;
699 return len;
703 * g_utf8_strup:
704 * @str: a UTF-8 encoded string
705 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
707 * Converts all Unicode characters in the string that have a case
708 * to uppercase. The exact manner that this is done depends
709 * on the current locale, and may result in the number of
710 * characters in the string increasing. (For instance, the
711 * German ess-zet will be changed to SS.)
713 * Return value: a newly allocated string, with all characters
714 * converted to uppercase.
716 gchar *
717 g_utf8_strup (const gchar *str,
718 gssize len)
720 gsize result_len;
721 LocaleType locale_type;
722 gchar *result;
724 g_return_val_if_fail (str != NULL, NULL);
726 locale_type = get_locale_type ();
729 * We use a two pass approach to keep memory management simple
731 result_len = real_toupper (str, len, NULL, locale_type);
732 result = g_malloc (result_len + 1);
733 real_toupper (str, len, result, locale_type);
734 result[result_len] = '\0';
736 return result;
739 static gsize
740 real_tolower (const gchar *str,
741 gssize max_len,
742 gchar *out_buffer,
743 LocaleType locale_type)
745 const gchar *p = str;
746 const char *last = NULL;
747 gsize len = 0;
749 while ((max_len < 0 || p < str + max_len) && *p)
751 gunichar c = g_utf8_get_char (p);
752 int t = TYPE (c);
753 gunichar val;
755 last = p;
756 p = g_utf8_next_char (p);
758 if (locale_type == LOCALE_TURKIC && c == 'I')
760 /* I => LATIN SMALL LETTER DOTLESS I */
761 len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL);
763 else if (c == 0x03A3) /* GREEK CAPITAL LETTER SIGMA */
765 if ((max_len < 0 || p < str + max_len) && *p)
767 gunichar next_c = g_utf8_get_char (p);
768 int next_type = TYPE(next_c);
770 /* SIGMA mapps differently depending on whether it is
771 * final or not. The following simplified test would
772 * fail in the case of combining marks following the
773 * sigma, but I don't think that occurs in real text.
774 * The test here matches that in ICU.
776 if (ISALPHA(next_type)) /* Lu,Ll,Lt,Lm,Lo */
777 val = 0x3c3; /* GREEK SMALL SIGMA */
778 else
779 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
781 else
782 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
784 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
786 else if (t == G_UNICODE_UPPERCASE_LETTER || t == G_UNICODE_TITLECASE_LETTER)
788 val = ATTTABLE (c >> 8, c & 0xff);
790 if (val >= 0xd800 && val < 0xdc00)
792 len += output_special_case (out_buffer, len, val - 0xd800, t, 0);
794 else
796 if (t == G_UNICODE_TITLECASE_LETTER)
798 unsigned int i;
799 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
801 if (title_table[i][0] == c)
802 val = title_table[i][2];
806 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
809 else
811 gsize char_len = g_utf8_skip[*(guchar *)last];
813 if (out_buffer)
814 memcpy (out_buffer + len, last, char_len);
816 len += char_len;
821 return len;
825 * g_utf8_strdown:
826 * @str: a UTF-8 encoded string
827 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
829 * Converts all Unicode characters in the string that have a case
830 * to lowercase. The exact manner that this is done depends
831 * on the current locale, and may result in the number of
832 * characters in the string changing.
834 * Return value: a newly allocated string, with all characters
835 * converted to lowercase.
837 gchar *
838 g_utf8_strdown (const gchar *str,
839 gssize len)
841 gsize result_len;
842 LocaleType locale_type;
843 gchar *result;
845 g_return_val_if_fail (str != NULL, NULL);
847 locale_type = get_locale_type ();
850 * We use a two pass approach to keep memory management simple
852 result_len = real_tolower (str, len, NULL, locale_type);
853 result = g_malloc (result_len + 1);
854 real_tolower (str, len, result, locale_type);
855 result[result_len] = '\0';
857 return result;
861 * g_utf8_casefold:
862 * @str: a UTF-8 encoded string
863 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
865 * Converts a string into a form that is independent of case. The
866 * result will not correspond to any particular case, but can be
867 * compared for equality or ordered with the results of calling
868 * g_utf8_casefold() on other strings.
870 * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
871 * only an approximation to the correct linguistic case insensitive
872 * ordering, though it is a fairly good one. Getting this exactly
873 * right would require a more sophisticated collation function that
874 * takes case sensitivity into account. GLib does not currently
875 * provide such a function.
877 * Return value: a newly allocated string, that is a
878 * case independent form of @str.
880 gchar *
881 g_utf8_casefold (const gchar *str,
882 gssize len)
884 GString *result = g_string_new (NULL);
885 const char *p;
887 p = str;
888 while ((len < 0 || p < str + len) && *p)
890 gunichar ch = g_utf8_get_char (p);
892 int start = 0;
893 int end = G_N_ELEMENTS (casefold_table);
895 if (ch >= casefold_table[start].ch &&
896 ch <= casefold_table[end - 1].ch)
898 while (TRUE)
900 int half = (start + end) / 2;
901 if (ch == casefold_table[half].ch)
903 g_string_append (result, casefold_table[half].data);
904 goto next;
906 else if (half == start)
907 break;
908 else if (ch > casefold_table[half].ch)
909 start = half;
910 else
911 end = half;
915 g_string_append_unichar (result, g_unichar_tolower (ch));
917 next:
918 p = g_utf8_next_char (p);
921 return g_string_free (result, FALSE);