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.
29 #include "gunichartables.h"
30 #include "gunicodeprivate.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]))
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)
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
84 g_unichar_isalnum (gunichar c
)
87 return ISDIGIT (t
) || ISALPHA (t
);
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
98 * Return value: %TRUE if @c is an alphabetic character
101 g_unichar_isalpha (gunichar c
)
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
116 * Return value: %TRUE if @c is a control character
119 g_unichar_iscntrl (gunichar c
)
121 return TYPE (c
) == G_UNICODE_CONTROL
;
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
135 g_unichar_isdigit (gunichar c
)
137 return TYPE (c
) == G_UNICODE_DECIMAL_NUMBER
;
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
151 * Return value: %TRUE if @c is printable unless it's a space
154 g_unichar_isgraph (gunichar 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
);
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
173 * Return value: %TRUE if @c is a lowercase letter
176 g_unichar_islower (gunichar c
)
178 return TYPE (c
) == G_UNICODE_LOWERCASE_LETTER
;
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
191 * Return value: %TRUE if @c is printable
194 g_unichar_isprint (gunichar 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
);
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
212 * Return value: %TRUE if @c is a punctuation or symbol character
215 g_unichar_ispunct (gunichar 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
);
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
241 g_unichar_isspace (gunichar c
)
245 /* special-case these since Unicode thinks they are not spaces */
256 return (t
== G_UNICODE_SPACE_SEPARATOR
|| t
== G_UNICODE_LINE_SEPARATOR
257 || t
== G_UNICODE_PARAGRAPH_SEPARATOR
);
265 * @c: a Unicode character
267 * Determines if a character is uppercase.
269 * Return value: %TRUE if @c is an uppercase character
272 g_unichar_isupper (gunichar c
)
274 return TYPE (c
) == G_UNICODE_UPPERCASE_LETTER
;
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
291 g_unichar_istitle (gunichar c
)
294 for (i
= 0; i
< G_N_ELEMENTS (title_table
); ++i
)
295 if (title_table
[i
][0] == c
)
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
309 g_unichar_isxdigit (gunichar c
)
312 return ((c
>= 'a' && c
<= 'f')
313 || (c
>= 'A' && c
<= 'F')
318 * g_unichar_isdefined:
319 * @c: a Unicode character
321 * Determines if a given character is assigned in the Unicode
324 * Return value: %TRUE if the character has an assigned value
327 g_unichar_isdefined (gunichar c
)
330 return t
!= G_UNICODE_UNASSIGNED
;
335 * @c: a Unicode character
337 * Determines if a character is typically rendered in a double-width
340 * Return value: %TRUE if the character is wide
342 /* This function stolen from Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk>. */
344 g_unichar_iswide (gunichar c
)
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));
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.
373 g_unichar_toupper (gunichar 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
);
385 return val
? val
: c
;
387 else if (t
== G_UNICODE_TITLECASE_LETTER
)
390 for (i
= 0; i
< G_N_ELEMENTS (title_table
); ++i
)
392 if (title_table
[i
][0] == c
)
393 return title_table
[i
][1];
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.
410 g_unichar_tolower (gunichar 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
);
422 return val
? val
: c
;
424 else if (t
== G_UNICODE_TITLECASE_LETTER
)
427 for (i
= 0; i
< G_N_ELEMENTS (title_table
); ++i
)
429 if (title_table
[i
][0] == c
)
430 return title_table
[i
][2];
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.
447 g_unichar_totitle (gunichar c
)
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)
462 * g_unichar_digit_value:
463 * @c: a Unicode character
465 * Determines the numeric value of a character as a decimal
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);
480 * g_unichar_xdigit_value:
481 * @c: a Unicode character
483 * Determines the numeric value of a character as a hexidecimal
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')
494 if (c
>= 'a' && c
<= 'f')
496 if (TYPE (c
) == G_UNICODE_DECIMAL_NUMBER
)
497 return ATTTABLE (c
>> 8, c
& 0xff);
503 * @c: a Unicode character
505 * Classifies a Unicode character by type.
507 * Return value: the type of the character.
510 g_unichar_type (gunichar c
)
516 * Case mapping functions
526 get_locale_type (void)
529 char *tem
= g_win32_getlocale ();
536 const char *locale
= setlocale (LC_CTYPE
, NULL
);
542 if (locale
[1] == 'z')
543 return LOCALE_TURKIC
;
546 if (locale
[1] == 't')
547 return LOCALE_LITHUANIAN
;
550 if (locale
[1] == 'r')
551 return LOCALE_TURKIC
;
555 return LOCALE_NORMAL
;
559 output_marks (const char **p_inout
,
563 const char *p
= *p_inout
;
568 gunichar c
= g_utf8_get_char (p
);
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
);
586 output_special_case (gchar
*out_buffer
,
591 const gchar
*p
= special_case_table
+ offset
;
594 if (type
!= G_UNICODE_TITLECASE_LETTER
)
595 p
= g_utf8_next_char (p
);
602 memcpy (out_buffer
, p
, len
);
608 real_toupper (const gchar
*str
,
611 LocaleType locale_type
)
613 const gchar
*p
= str
;
614 const char *last
= NULL
;
616 gboolean last_was_i
= FALSE
;
618 while ((max_len
< 0 || p
< str
+ max_len
) && *p
)
620 gunichar c
= g_utf8_get_char (p
);
625 p
= g_utf8_next_char (p
);
627 if (locale_type
== LOCALE_LITHUANIAN
)
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.
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
);
650 len
+= output_marks (&p
, out_buffer
? out_buffer
+ len
: NULL
, TRUE
);
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);
686 if (t
== G_UNICODE_TITLECASE_LETTER
)
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
);
701 gsize char_len
= g_utf8_skip
[*(guchar
*)last
];
704 memcpy (out_buffer
+ len
, last
, char_len
);
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.
729 g_utf8_strup (const gchar
*str
,
733 LocaleType locale_type
;
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';
751 /* traverses the string checking for characters with combining class == 230
752 * until a base character is found */
754 has_more_above (const gchar
*str
)
756 const gchar
*p
= str
;
757 gint combining_class
;
761 combining_class
= _g_unichar_combining_class (g_utf8_get_char (p
));
762 if (combining_class
== 230)
764 else if (combining_class
== 0)
767 p
= g_utf8_next_char (p
);
774 real_tolower (const gchar
*str
,
777 LocaleType locale_type
)
779 const gchar
*p
= str
;
780 const char *last
= NULL
;
783 while ((max_len
< 0 || p
< str
+ max_len
) && *p
)
785 gunichar c
= g_utf8_get_char (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
);
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
);
817 len
+= g_unichar_to_utf8 (0x0300, out_buffer
? out_buffer
+ len
: NULL
);
820 len
+= g_unichar_to_utf8 (0x0301, out_buffer
? out_buffer
+ len
: NULL
);
823 len
+= g_unichar_to_utf8 (0x0303, out_buffer
? out_buffer
+ len
: NULL
);
827 else if (locale_type
== LOCALE_LITHUANIAN
&&
828 (c
== 'I' || c
== 'J' || c
== 0x012e) &&
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 */
850 val
= 0x3c2; /* GREEK SMALL FINAL SIGMA */
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);
867 if (t
== G_UNICODE_TITLECASE_LETTER
)
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
);
882 gsize char_len
= g_utf8_skip
[*(guchar
*)last
];
885 memcpy (out_buffer
+ len
, last
, char_len
);
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.
909 g_utf8_strdown (const gchar
*str
,
913 LocaleType locale_type
;
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';
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.
952 g_utf8_casefold (const gchar
*str
,
958 g_return_val_if_fail (str
!= NULL
, NULL
);
960 result
= g_string_new (NULL
);
962 while ((len
< 0 || p
< str
+ len
) && *p
)
964 gunichar ch
= g_utf8_get_char (p
);
967 int end
= G_N_ELEMENTS (casefold_table
);
969 if (ch
>= casefold_table
[start
].ch
&&
970 ch
<= casefold_table
[end
- 1].ch
)
974 int half
= (start
+ end
) / 2;
975 if (ch
== casefold_table
[half
].ch
)
977 g_string_append (result
, casefold_table
[half
].data
);
980 else if (half
== start
)
982 else if (ch
> casefold_table
[half
].ch
)
989 g_string_append_unichar (result
, g_unichar_tolower (ch
));
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
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.
1022 g_unichar_get_mirror_char (gunichar ch
,
1023 gunichar
*mirrored_ch
)
1025 gint pos
, step
, size
;
1028 size
= G_N_ELEMENTS (bidi_mirroring_table
);
1029 pos
= step
= (size
/ 2) + 1;
1033 gunichar cmp_ch
= bidi_mirroring_table
[pos
].ch
;
1034 step
= (step
+ 1) / 2;
1042 else if (cmp_ch
> ch
)
1051 found
= bidi_mirroring_table
[pos
].ch
== ch
;
1053 *mirrored_ch
= found
? bidi_mirroring_table
[pos
].mirrored_ch
: ch
;
1059 #define __G_UNIPROP_C__
1060 #include "galiasdef.c"