Mention 64bit integer types.
[glib.git] / glib / guniprop.c
blob6c88a27c40764cea7ceb5bdd919df2f47143cba1
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"
30 #include "gunicodeprivate.h"
31 #include "galias.h"
33 #define ATTR_TABLE(Page) (((Page) <= G_UNICODE_LAST_PAGE_PART1) \
34 ? attr_table_part1[Page] \
35 : attr_table_part2[(Page) - 0xe00])
37 #define ATTTABLE(Page, Char) \
38 ((ATTR_TABLE(Page) == G_UNICODE_MAX_TABLE_INDEX) ? 0 : (attr_data[ATTR_TABLE(Page)][Char]))
40 #define TTYPE_PART1(Page, Char) \
41 ((type_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
42 ? (type_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
43 : (type_data[type_table_part1[Page]][Char]))
45 #define TTYPE_PART2(Page, Char) \
46 ((type_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
47 ? (type_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
48 : (type_data[type_table_part2[Page]][Char]))
50 #define TYPE(Char) \
51 (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
52 ? TTYPE_PART1 ((Char) >> 8, (Char) & 0xff) \
53 : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
54 ? TTYPE_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
55 : G_UNICODE_UNASSIGNED))
58 #define ISDIGIT(Type) ((Type) == G_UNICODE_DECIMAL_NUMBER \
59 || (Type) == G_UNICODE_LETTER_NUMBER \
60 || (Type) == G_UNICODE_OTHER_NUMBER)
62 #define ISALPHA(Type) ((Type) == G_UNICODE_LOWERCASE_LETTER \
63 || (Type) == G_UNICODE_UPPERCASE_LETTER \
64 || (Type) == G_UNICODE_TITLECASE_LETTER \
65 || (Type) == G_UNICODE_MODIFIER_LETTER \
66 || (Type) == G_UNICODE_OTHER_LETTER)
68 #define ISMARK(Type) ((Type) == G_UNICODE_NON_SPACING_MARK || \
69 (Type) == G_UNICODE_COMBINING_MARK || \
70 (Type) == G_UNICODE_ENCLOSING_MARK)
73 /**
74 * g_unichar_isalnum:
75 * @c: a Unicode character
77 * Determines whether a character is alphanumeric.
78 * Given some UTF-8 text, obtain a character value
79 * with g_utf8_get_char().
81 * Return value: %TRUE if @c is an alphanumeric character
82 **/
83 gboolean
84 g_unichar_isalnum (gunichar c)
86 int t = TYPE (c);
87 return ISDIGIT (t) || ISALPHA (t);
90 /**
91 * g_unichar_isalpha:
92 * @c: a Unicode character
94 * Determines whether a character is alphabetic (i.e. a letter).
95 * Given some UTF-8 text, obtain a character value with
96 * g_utf8_get_char().
98 * Return value: %TRUE if @c is an alphabetic character
99 **/
100 gboolean
101 g_unichar_isalpha (gunichar c)
103 int t = TYPE (c);
104 return ISALPHA (t);
109 * g_unichar_iscntrl:
110 * @c: a Unicode character
112 * Determines whether a character is a control character.
113 * Given some UTF-8 text, obtain a character value with
114 * g_utf8_get_char().
116 * Return value: %TRUE if @c is a control character
118 gboolean
119 g_unichar_iscntrl (gunichar c)
121 return TYPE (c) == G_UNICODE_CONTROL;
125 * g_unichar_isdigit:
126 * @c: a Unicode character
128 * Determines whether a character is numeric (i.e. a digit). This
129 * covers ASCII 0-9 and also digits in other languages/scripts. Given
130 * some UTF-8 text, obtain a character value with g_utf8_get_char().
132 * Return value: %TRUE if @c is a digit
134 gboolean
135 g_unichar_isdigit (gunichar c)
137 return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
142 * g_unichar_isgraph:
143 * @c: a Unicode character
145 * Determines whether a character is printable and not a space
146 * (returns %FALSE for control characters, format characters, and
147 * spaces). g_unichar_isprint() is similar, but returns %TRUE for
148 * spaces. Given some UTF-8 text, obtain a character value with
149 * g_utf8_get_char().
151 * Return value: %TRUE if @c is printable unless it's a space
153 gboolean
154 g_unichar_isgraph (gunichar c)
156 int t = TYPE (c);
157 return (t != G_UNICODE_CONTROL
158 && t != G_UNICODE_FORMAT
159 && t != G_UNICODE_UNASSIGNED
160 && t != G_UNICODE_PRIVATE_USE
161 && t != G_UNICODE_SURROGATE
162 && t != G_UNICODE_SPACE_SEPARATOR);
166 * g_unichar_islower:
167 * @c: a Unicode character
169 * Determines whether a character is a lowercase letter.
170 * Given some UTF-8 text, obtain a character value with
171 * g_utf8_get_char().
173 * Return value: %TRUE if @c is a lowercase letter
175 gboolean
176 g_unichar_islower (gunichar c)
178 return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
183 * g_unichar_isprint:
184 * @c: a Unicode character
186 * Determines whether a character is printable.
187 * Unlike g_unichar_isgraph(), returns %TRUE for spaces.
188 * Given some UTF-8 text, obtain a character value with
189 * g_utf8_get_char().
191 * Return value: %TRUE if @c is printable
193 gboolean
194 g_unichar_isprint (gunichar c)
196 int t = TYPE (c);
197 return (t != G_UNICODE_CONTROL
198 && t != G_UNICODE_FORMAT
199 && t != G_UNICODE_UNASSIGNED
200 && t != G_UNICODE_PRIVATE_USE
201 && t != G_UNICODE_SURROGATE);
205 * g_unichar_ispunct:
206 * @c: a Unicode character
208 * Determines whether a character is punctuation or a symbol.
209 * Given some UTF-8 text, obtain a character value with
210 * g_utf8_get_char().
212 * Return value: %TRUE if @c is a punctuation or symbol character
214 gboolean
215 g_unichar_ispunct (gunichar c)
217 int t = TYPE (c);
218 return (t == G_UNICODE_CONNECT_PUNCTUATION || t == G_UNICODE_DASH_PUNCTUATION
219 || t == G_UNICODE_CLOSE_PUNCTUATION || t == G_UNICODE_FINAL_PUNCTUATION
220 || t == G_UNICODE_INITIAL_PUNCTUATION || t == G_UNICODE_OTHER_PUNCTUATION
221 || t == G_UNICODE_OPEN_PUNCTUATION || t == G_UNICODE_CURRENCY_SYMBOL
222 || t == G_UNICODE_MODIFIER_SYMBOL || t == G_UNICODE_MATH_SYMBOL
223 || t == G_UNICODE_OTHER_SYMBOL);
227 * g_unichar_isspace:
228 * @c: a Unicode character
230 * Determines whether a character is a space, tab, or line separator
231 * (newline, carriage return, etc.). Given some UTF-8 text, obtain a
232 * character value with g_utf8_get_char().
234 * (Note: don't use this to do word breaking; you have to use
235 * Pango or equivalent to get word breaking right, the algorithm
236 * is fairly complex.)
238 * Return value: %TRUE if @c is a punctuation character
240 gboolean
241 g_unichar_isspace (gunichar c)
243 switch (c)
245 /* special-case these since Unicode thinks they are not spaces */
246 case '\t':
247 case '\n':
248 case '\r':
249 case '\f':
250 return TRUE;
251 break;
253 default:
255 int t = TYPE (c);
256 return (t == G_UNICODE_SPACE_SEPARATOR || t == G_UNICODE_LINE_SEPARATOR
257 || t == G_UNICODE_PARAGRAPH_SEPARATOR);
259 break;
264 * g_unichar_isupper:
265 * @c: a Unicode character
267 * Determines if a character is uppercase.
269 * Return value: %TRUE if @c is an uppercase character
271 gboolean
272 g_unichar_isupper (gunichar c)
274 return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
278 * g_unichar_istitle:
279 * @c: a Unicode character
281 * Determines if a character is titlecase. Some characters in
282 * Unicode which are composites, such as the DZ digraph
283 * have three case variants instead of just two. The titlecase
284 * form is used at the beginning of a word where only the
285 * first letter is capitalized. The titlecase form of the DZ
286 * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z.
288 * Return value: %TRUE if the character is titlecase
290 gboolean
291 g_unichar_istitle (gunichar c)
293 unsigned int i;
294 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
295 if (title_table[i][0] == c)
296 return 1;
297 return 0;
301 * g_unichar_isxdigit:
302 * @c: a Unicode character.
304 * Determines if a character is a hexidecimal digit.
306 * Return value: %TRUE if the character is a hexadecimal digit
308 gboolean
309 g_unichar_isxdigit (gunichar c)
311 int t = TYPE (c);
312 return ((c >= 'a' && c <= 'f')
313 || (c >= 'A' && c <= 'F')
314 || ISDIGIT (t));
318 * g_unichar_isdefined:
319 * @c: a Unicode character
321 * Determines if a given character is assigned in the Unicode
322 * standard.
324 * Return value: %TRUE if the character has an assigned value
326 gboolean
327 g_unichar_isdefined (gunichar c)
329 int t = TYPE (c);
330 return t != G_UNICODE_UNASSIGNED;
334 * g_unichar_iswide:
335 * @c: a Unicode character
337 * Determines if a character is typically rendered in a double-width
338 * cell.
340 * Return value: %TRUE if the character is wide
342 /* This function stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>. */
343 gboolean
344 g_unichar_iswide (gunichar c)
346 if (c < 0x1100)
347 return FALSE;
349 return (c <= 0x115f /* Hangul Jamo init. consonants */
350 || c == 0x2329 || c == 0x232a /* angle brackets */
351 || (c >= 0x2e80 && c <= 0xa4cf && (c < 0x302a || c > 0x302f)
352 && c != 0x303f && c != 0x3099 && c!= 0x309a) /* CJK ... Yi */
353 || (c >= 0xac00 && c <= 0xd7a3) /* Hangul Syllables */
354 || (c >= 0xf900 && c <= 0xfaff) /* CJK Compatibility Ideographs */
355 || (c >= 0xfe30 && c <= 0xfe6f) /* CJK Compatibility Forms */
356 || (c >= 0xff00 && c <= 0xff60) /* Fullwidth Forms */
357 || (c >= 0xffe0 && c <= 0xffe6) /* Fullwidth Forms */
358 || (c >= 0x20000 && c <= 0x2fffd) /* CJK extra stuff */
359 || (c >= 0x30000 && c <= 0x3fffd));
363 * g_unichar_toupper:
364 * @c: a Unicode character
366 * Converts a character to uppercase.
368 * Return value: the result of converting @c to uppercase.
369 * If @c is not an lowercase or titlecase character,
370 * or has no upper case equivalent @c is returned unchanged.
372 gunichar
373 g_unichar_toupper (gunichar c)
375 int t = TYPE (c);
376 if (t == G_UNICODE_LOWERCASE_LETTER)
378 gunichar val = ATTTABLE (c >> 8, c & 0xff);
379 if (val >= 0x1000000)
381 const gchar *p = special_case_table + val - 0x1000000;
382 return g_utf8_get_char (p);
384 else
385 return val ? val : c;
387 else if (t == G_UNICODE_TITLECASE_LETTER)
389 unsigned int i;
390 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
392 if (title_table[i][0] == c)
393 return title_table[i][1];
396 return c;
400 * g_unichar_tolower:
401 * @c: a Unicode character.
403 * Converts a character to lower case.
405 * Return value: the result of converting @c to lower case.
406 * If @c is not an upperlower or titlecase character,
407 * or has no lowercase equivalent @c is returned unchanged.
409 gunichar
410 g_unichar_tolower (gunichar c)
412 int t = TYPE (c);
413 if (t == G_UNICODE_UPPERCASE_LETTER)
415 gunichar val = ATTTABLE (c >> 8, c & 0xff);
416 if (val >= 0x1000000)
418 const gchar *p = special_case_table + val - 0x1000000;
419 return g_utf8_get_char (p);
421 else
422 return val ? val : c;
424 else if (t == G_UNICODE_TITLECASE_LETTER)
426 unsigned int i;
427 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
429 if (title_table[i][0] == c)
430 return title_table[i][2];
433 return c;
437 * g_unichar_totitle:
438 * @c: a Unicode character
440 * Converts a character to the titlecase.
442 * Return value: the result of converting @c to titlecase.
443 * If @c is not an uppercase or lowercase character,
444 * @c is returned unchanged.
446 gunichar
447 g_unichar_totitle (gunichar c)
449 unsigned int i;
450 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
452 if (title_table[i][0] == c || title_table[i][1] == c
453 || title_table[i][2] == c)
454 return title_table[i][0];
456 return (TYPE (c) == G_UNICODE_LOWERCASE_LETTER
457 ? ATTTABLE (c >> 8, c & 0xff)
458 : c);
462 * g_unichar_digit_value:
463 * @c: a Unicode character
465 * Determines the numeric value of a character as a decimal
466 * digit.
468 * Return value: If @c is a decimal digit (according to
469 * g_unichar_isdigit()), its numeric value. Otherwise, -1.
472 g_unichar_digit_value (gunichar c)
474 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
475 return ATTTABLE (c >> 8, c & 0xff);
476 return -1;
480 * g_unichar_xdigit_value:
481 * @c: a Unicode character
483 * Determines the numeric value of a character as a hexidecimal
484 * digit.
486 * Return value: If @c is a hex digit (according to
487 * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
490 g_unichar_xdigit_value (gunichar c)
492 if (c >= 'A' && c <= 'F')
493 return c - 'A' + 10;
494 if (c >= 'a' && c <= 'f')
495 return c - 'a' + 10;
496 if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
497 return ATTTABLE (c >> 8, c & 0xff);
498 return -1;
502 * g_unichar_type:
503 * @c: a Unicode character
505 * Classifies a Unicode character by type.
507 * Return value: the type of the character.
509 GUnicodeType
510 g_unichar_type (gunichar c)
512 return TYPE (c);
516 * Case mapping functions
519 typedef enum {
520 LOCALE_NORMAL,
521 LOCALE_TURKIC,
522 LOCALE_LITHUANIAN
523 } LocaleType;
525 static LocaleType
526 get_locale_type (void)
528 #ifdef G_OS_WIN32
529 char *tem = g_win32_getlocale ();
530 char locale[2];
532 locale[0] = tem[0];
533 locale[1] = tem[1];
534 g_free (tem);
535 #else
536 const char *locale = setlocale (LC_CTYPE, NULL);
537 #endif
539 switch (locale[0])
541 case 'a':
542 if (locale[1] == 'z')
543 return LOCALE_TURKIC;
544 break;
545 case 'l':
546 if (locale[1] == 't')
547 return LOCALE_LITHUANIAN;
548 break;
549 case 't':
550 if (locale[1] == 'r')
551 return LOCALE_TURKIC;
552 break;
555 return LOCALE_NORMAL;
558 static gint
559 output_marks (const char **p_inout,
560 char *out_buffer,
561 gboolean remove_dot)
563 const char *p = *p_inout;
564 gint len = 0;
566 while (*p)
568 gunichar c = g_utf8_get_char (p);
569 int t = TYPE(c);
571 if (ISMARK(t))
573 if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
574 len += g_unichar_to_utf8 (c, out_buffer ? out_buffer + len : NULL);
575 p = g_utf8_next_char (p);
577 else
578 break;
581 *p_inout = p;
582 return len;
585 static gint
586 output_special_case (gchar *out_buffer,
587 int offset,
588 int type,
589 int which)
591 const gchar *p = special_case_table + offset;
592 gint len;
594 if (type != G_UNICODE_TITLECASE_LETTER)
595 p = g_utf8_next_char (p);
597 if (which == 1)
598 p += strlen (p) + 1;
600 len = strlen (p);
601 if (out_buffer)
602 memcpy (out_buffer, p, len);
604 return len;
607 static gsize
608 real_toupper (const gchar *str,
609 gssize max_len,
610 gchar *out_buffer,
611 LocaleType locale_type)
613 const gchar *p = str;
614 const char *last = NULL;
615 gsize len = 0;
616 gboolean last_was_i = FALSE;
618 while ((max_len < 0 || p < str + max_len) && *p)
620 gunichar c = g_utf8_get_char (p);
621 int t = TYPE (c);
622 gunichar val;
624 last = p;
625 p = g_utf8_next_char (p);
627 if (locale_type == LOCALE_LITHUANIAN)
629 if (c == 'i')
630 last_was_i = TRUE;
631 else
633 if (last_was_i)
635 /* Nasty, need to remove any dot above. Though
636 * I think only E WITH DOT ABOVE occurs in practice
637 * which could simplify this considerably.
639 gsize decomp_len, i;
640 gunichar *decomp;
642 decomp = g_unicode_canonical_decomposition (c, &decomp_len);
643 for (i=0; i < decomp_len; i++)
645 if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
646 len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
648 g_free (decomp);
650 len += output_marks (&p, out_buffer ? out_buffer + len : NULL, TRUE);
652 continue;
655 if (!ISMARK(t))
656 last_was_i = FALSE;
660 if (locale_type == LOCALE_TURKIC && c == 'i')
662 /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
663 len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL);
665 else if (c == 0x0345) /* COMBINING GREEK YPOGEGRAMMENI */
667 /* Nasty, need to move it after other combining marks .. this would go away if
668 * we normalized first.
670 len += output_marks (&p, out_buffer ? out_buffer + len : NULL, FALSE);
672 /* And output as GREEK CAPITAL LETTER IOTA */
673 len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL);
675 else if (t == G_UNICODE_LOWERCASE_LETTER || t == G_UNICODE_TITLECASE_LETTER)
677 val = ATTTABLE (c >> 8, c & 0xff);
679 if (val >= 0x1000000)
681 len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t,
682 t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
684 else
686 if (t == G_UNICODE_TITLECASE_LETTER)
688 unsigned int i;
689 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
691 if (title_table[i][0] == c)
692 val = title_table[i][1];
696 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
699 else
701 gsize char_len = g_utf8_skip[*(guchar *)last];
703 if (out_buffer)
704 memcpy (out_buffer + len, last, char_len);
706 len += char_len;
711 return len;
715 * g_utf8_strup:
716 * @str: a UTF-8 encoded string
717 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
719 * Converts all Unicode characters in the string that have a case
720 * to uppercase. The exact manner that this is done depends
721 * on the current locale, and may result in the number of
722 * characters in the string increasing. (For instance, the
723 * German ess-zet will be changed to SS.)
725 * Return value: a newly allocated string, with all characters
726 * converted to uppercase.
728 gchar *
729 g_utf8_strup (const gchar *str,
730 gssize len)
732 gsize result_len;
733 LocaleType locale_type;
734 gchar *result;
736 g_return_val_if_fail (str != NULL, NULL);
738 locale_type = get_locale_type ();
741 * We use a two pass approach to keep memory management simple
743 result_len = real_toupper (str, len, NULL, locale_type);
744 result = g_malloc (result_len + 1);
745 real_toupper (str, len, result, locale_type);
746 result[result_len] = '\0';
748 return result;
751 /* traverses the string checking for characters with combining class == 230
752 * until a base character is found */
753 static gboolean
754 has_more_above (const gchar *str)
756 const gchar *p = str;
757 gint combining_class;
759 while (*p)
761 combining_class = _g_unichar_combining_class (g_utf8_get_char (p));
762 if (combining_class == 230)
763 return TRUE;
764 else if (combining_class == 0)
765 break;
767 p = g_utf8_next_char (p);
770 return FALSE;
773 static gsize
774 real_tolower (const gchar *str,
775 gssize max_len,
776 gchar *out_buffer,
777 LocaleType locale_type)
779 const gchar *p = str;
780 const char *last = NULL;
781 gsize len = 0;
783 while ((max_len < 0 || p < str + max_len) && *p)
785 gunichar c = g_utf8_get_char (p);
786 int t = TYPE (c);
787 gunichar val;
789 last = p;
790 p = g_utf8_next_char (p);
792 if (locale_type == LOCALE_TURKIC && c == 'I')
794 if (g_utf8_get_char (p) == 0x0307)
796 /* I + COMBINING DOT ABOVE => i (U+0069) */
797 len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL);
798 p = g_utf8_next_char (p);
800 else
802 /* I => LATIN SMALL LETTER DOTLESS I */
803 len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL);
806 /* Introduce an explicit dot above when lowercasing capital I's and J's
807 * whenever there are more accents above. [SpecialCasing.txt] */
808 else if (locale_type == LOCALE_LITHUANIAN &&
809 (c == 0x00cc || c == 0x00cd || c == 0x0128))
811 len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL);
812 len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL);
814 switch (c)
816 case 0x00cc:
817 len += g_unichar_to_utf8 (0x0300, out_buffer ? out_buffer + len : NULL);
818 break;
819 case 0x00cd:
820 len += g_unichar_to_utf8 (0x0301, out_buffer ? out_buffer + len : NULL);
821 break;
822 case 0x0128:
823 len += g_unichar_to_utf8 (0x0303, out_buffer ? out_buffer + len : NULL);
824 break;
827 else if (locale_type == LOCALE_LITHUANIAN &&
828 (c == 'I' || c == 'J' || c == 0x012e) &&
829 has_more_above (p))
831 len += g_unichar_to_utf8 (g_unichar_tolower (c), out_buffer ? out_buffer + len : NULL);
832 len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL);
834 else if (c == 0x03A3) /* GREEK CAPITAL LETTER SIGMA */
836 if ((max_len < 0 || p < str + max_len) && *p)
838 gunichar next_c = g_utf8_get_char (p);
839 int next_type = TYPE(next_c);
841 /* SIGMA mapps differently depending on whether it is
842 * final or not. The following simplified test would
843 * fail in the case of combining marks following the
844 * sigma, but I don't think that occurs in real text.
845 * The test here matches that in ICU.
847 if (ISALPHA(next_type)) /* Lu,Ll,Lt,Lm,Lo */
848 val = 0x3c3; /* GREEK SMALL SIGMA */
849 else
850 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
852 else
853 val = 0x3c2; /* GREEK SMALL FINAL SIGMA */
855 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
857 else if (t == G_UNICODE_UPPERCASE_LETTER || t == G_UNICODE_TITLECASE_LETTER)
859 val = ATTTABLE (c >> 8, c & 0xff);
861 if (val >= 0x1000000)
863 len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t, 0);
865 else
867 if (t == G_UNICODE_TITLECASE_LETTER)
869 unsigned int i;
870 for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
872 if (title_table[i][0] == c)
873 val = title_table[i][2];
877 len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
880 else
882 gsize char_len = g_utf8_skip[*(guchar *)last];
884 if (out_buffer)
885 memcpy (out_buffer + len, last, char_len);
887 len += char_len;
892 return len;
896 * g_utf8_strdown:
897 * @str: a UTF-8 encoded string
898 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
900 * Converts all Unicode characters in the string that have a case
901 * to lowercase. The exact manner that this is done depends
902 * on the current locale, and may result in the number of
903 * characters in the string changing.
905 * Return value: a newly allocated string, with all characters
906 * converted to lowercase.
908 gchar *
909 g_utf8_strdown (const gchar *str,
910 gssize len)
912 gsize result_len;
913 LocaleType locale_type;
914 gchar *result;
916 g_return_val_if_fail (str != NULL, NULL);
918 locale_type = get_locale_type ();
921 * We use a two pass approach to keep memory management simple
923 result_len = real_tolower (str, len, NULL, locale_type);
924 result = g_malloc (result_len + 1);
925 real_tolower (str, len, result, locale_type);
926 result[result_len] = '\0';
928 return result;
932 * g_utf8_casefold:
933 * @str: a UTF-8 encoded string
934 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
936 * Converts a string into a form that is independent of case. The
937 * result will not correspond to any particular case, but can be
938 * compared for equality or ordered with the results of calling
939 * g_utf8_casefold() on other strings.
941 * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
942 * only an approximation to the correct linguistic case insensitive
943 * ordering, though it is a fairly good one. Getting this exactly
944 * right would require a more sophisticated collation function that
945 * takes case sensitivity into account. GLib does not currently
946 * provide such a function.
948 * Return value: a newly allocated string, that is a
949 * case independent form of @str.
951 gchar *
952 g_utf8_casefold (const gchar *str,
953 gssize len)
955 GString *result;
956 const char *p;
958 g_return_val_if_fail (str != NULL, NULL);
960 result = g_string_new (NULL);
961 p = str;
962 while ((len < 0 || p < str + len) && *p)
964 gunichar ch = g_utf8_get_char (p);
966 int start = 0;
967 int end = G_N_ELEMENTS (casefold_table);
969 if (ch >= casefold_table[start].ch &&
970 ch <= casefold_table[end - 1].ch)
972 while (TRUE)
974 int half = (start + end) / 2;
975 if (ch == casefold_table[half].ch)
977 g_string_append (result, casefold_table[half].data);
978 goto next;
980 else if (half == start)
981 break;
982 else if (ch > casefold_table[half].ch)
983 start = half;
984 else
985 end = half;
989 g_string_append_unichar (result, g_unichar_tolower (ch));
991 next:
992 p = g_utf8_next_char (p);
995 return g_string_free (result, FALSE);
999 * g_unichar_get_mirror_char:
1000 * @ch: a unicode character
1001 * @mirrored_ch: location to store the mirrored character
1003 * In Unicode, some characters are <firstterm>mirrored</firstterm>. This
1004 * means that their images are mirrored horizontally in text that is laid
1005 * out from right to left. For instance, "(" would become its mirror image,
1006 * ")", in right-to-left text.
1008 * If @ch has the Unicode mirrored property and there is another unicode
1009 * character that typically has a glyph that is the mirror image of @ch's
1010 * glyph, puts that character in the address pointed to by @mirrored_ch.
1012 * Return value: %TRUE if @ch has a mirrored character and @mirrored_ch is
1013 * filled in, %FALSE otherwise
1015 * Since: 2.4
1017 /* This code is adapted from FriBidi (http://fribidi.sourceforge.net/).
1018 * FriBidi is: Copyright (C) 1999,2000 Dov Grobgeld, and
1019 * Copyright (C) 2001,2002 Behdad Esfahbod.
1021 gboolean
1022 g_unichar_get_mirror_char (gunichar ch,
1023 gunichar *mirrored_ch)
1025 gint pos, step, size;
1026 gboolean found;
1028 size = G_N_ELEMENTS (bidi_mirroring_table);
1029 pos = step = (size / 2) + 1;
1031 while (step > 1)
1033 gunichar cmp_ch = bidi_mirroring_table[pos].ch;
1034 step = (step + 1) / 2;
1036 if (cmp_ch < ch)
1038 pos += step;
1039 if (pos > size - 1)
1040 pos = size - 1;
1042 else if (cmp_ch > ch)
1044 pos -= step;
1045 if (pos < 0)
1046 pos = 0;
1048 else
1049 break;
1051 found = bidi_mirroring_table[pos].ch == ch;
1052 if (mirrored_ch)
1053 *mirrored_ch = found ? bidi_mirroring_table[pos].mirrored_ch : ch;
1055 return found;
1059 #define __G_UNIPROP_C__
1060 #include "galiasdef.c"