os/ossrv/glib/glib/guniprop.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* guniprop.c - Unicode character properties.
     2  *
     3  * Copyright (C) 1999 Tom Tromey
     4  * Copyright (C) 2000 Red Hat, Inc.
     5  * Portions copyright (c) 2006-2009 Nokia Corporation.  All rights reserved.
     6  *
     7  * This library is free software; you can redistribute it and/or
     8  * modify it under the terms of the GNU Lesser General Public
     9  * License as published by the Free Software Foundation; either
    10  * version 2 of the License, or (at your option) any later version.
    11  *
    12  * This library is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
    15  * Lesser General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU Lesser General Public
    18  * License along with this library; if not, write to the
    19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    20  * Boston, MA 02111-1307, USA.
    21  */
    22 
    23 #include "config.h"
    24 
    25 #include <stdlib.h>
    26 #include <stddef.h>
    27 #include <string.h>
    28 #include <locale.h>
    29 
    30 #include "glib.h"
    31 #include "gunichartables.h"
    32 #include "gmirroringtable.h"
    33 #include "gscripttable.h"
    34 #include "gunicodeprivate.h"
    35 #include "galias.h"
    36 
    37 #ifdef __SYMBIAN32__
    38 #include "glib_wsd.h"
    39 #endif /* __SYMBIAN32__ */
    40 
    41 #define ATTR_TABLE(Page) (((Page) <= G_UNICODE_LAST_PAGE_PART1) \
    42                           ? attr_table_part1[Page] \
    43                           : attr_table_part2[(Page) - 0xe00])
    44 
    45 #define ATTTABLE(Page, Char) \
    46   ((ATTR_TABLE(Page) == G_UNICODE_MAX_TABLE_INDEX) ? 0 : (attr_data[ATTR_TABLE(Page)][Char]))
    47 
    48 #define TTYPE_PART1(Page, Char) \
    49   ((type_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
    50    ? (type_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
    51    : (type_data[type_table_part1[Page]][Char]))
    52 
    53 #define TTYPE_PART2(Page, Char) \
    54   ((type_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
    55    ? (type_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
    56    : (type_data[type_table_part2[Page]][Char]))
    57 
    58 #define TYPE(Char) \
    59   (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
    60    ? TTYPE_PART1 ((Char) >> 8, (Char) & 0xff) \
    61    : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
    62       ? TTYPE_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
    63       : G_UNICODE_UNASSIGNED))
    64 
    65 
    66 #define IS(Type, Class)	(((guint)1 << (Type)) & (Class))
    67 #define OR(Type, Rest)	(((guint)1 << (Type)) | (Rest))
    68 
    69 
    70 
    71 #define ISALPHA(Type)	IS ((Type),				\
    72 			    OR (G_UNICODE_LOWERCASE_LETTER,	\
    73 			    OR (G_UNICODE_UPPERCASE_LETTER,	\
    74 			    OR (G_UNICODE_TITLECASE_LETTER,	\
    75 			    OR (G_UNICODE_MODIFIER_LETTER,	\
    76 			    OR (G_UNICODE_OTHER_LETTER,		0))))))
    77 
    78 #define ISALDIGIT(Type)	IS ((Type),				\
    79 			    OR (G_UNICODE_DECIMAL_NUMBER,	\
    80 			    OR (G_UNICODE_LETTER_NUMBER,	\
    81 			    OR (G_UNICODE_OTHER_NUMBER,		\
    82 			    OR (G_UNICODE_LOWERCASE_LETTER,	\
    83 			    OR (G_UNICODE_UPPERCASE_LETTER,	\
    84 			    OR (G_UNICODE_TITLECASE_LETTER,	\
    85 			    OR (G_UNICODE_MODIFIER_LETTER,	\
    86 			    OR (G_UNICODE_OTHER_LETTER,		0)))))))))
    87 
    88 #define ISMARK(Type)	IS ((Type),				\
    89 			    OR (G_UNICODE_NON_SPACING_MARK,	\
    90 			    OR (G_UNICODE_COMBINING_MARK,	\
    91 			    OR (G_UNICODE_ENCLOSING_MARK,	0))))
    92 
    93 #define ISZEROWIDTHTYPE(Type)	IS ((Type),			\
    94 			    OR (G_UNICODE_NON_SPACING_MARK,	\
    95 			    OR (G_UNICODE_ENCLOSING_MARK,	\
    96 			    OR (G_UNICODE_FORMAT,		0))))
    97 
    98 /**
    99  * g_unichar_isalnum:
   100  * @c: a Unicode character
   101  * 
   102  * Determines whether a character is alphanumeric.
   103  * Given some UTF-8 text, obtain a character value
   104  * with g_utf8_get_char().
   105  * 
   106  * Return value: %TRUE if @c is an alphanumeric character
   107  **/
   108 EXPORT_C gboolean
   109 g_unichar_isalnum (gunichar c)
   110 {
   111   return ISALDIGIT (TYPE (c)) ? TRUE : FALSE;
   112 }
   113 
   114 /**
   115  * g_unichar_isalpha:
   116  * @c: a Unicode character
   117  * 
   118  * Determines whether a character is alphabetic (i.e. a letter).
   119  * Given some UTF-8 text, obtain a character value with
   120  * g_utf8_get_char().
   121  * 
   122  * Return value: %TRUE if @c is an alphabetic character
   123  **/
   124 EXPORT_C gboolean
   125 g_unichar_isalpha (gunichar c)
   126 {
   127   return ISALPHA (TYPE (c)) ? TRUE : FALSE;
   128 }
   129 
   130 
   131 /**
   132  * g_unichar_iscntrl:
   133  * @c: a Unicode character
   134  * 
   135  * Determines whether a character is a control character.
   136  * Given some UTF-8 text, obtain a character value with
   137  * g_utf8_get_char().
   138  * 
   139  * Return value: %TRUE if @c is a control character
   140  **/
   141 EXPORT_C gboolean
   142 g_unichar_iscntrl (gunichar c)
   143 {
   144   return TYPE (c) == G_UNICODE_CONTROL;
   145 }
   146 
   147 /**
   148  * g_unichar_isdigit:
   149  * @c: a Unicode character
   150  * 
   151  * Determines whether a character is numeric (i.e. a digit).  This
   152  * covers ASCII 0-9 and also digits in other languages/scripts.  Given
   153  * some UTF-8 text, obtain a character value with g_utf8_get_char().
   154  * 
   155  * Return value: %TRUE if @c is a digit
   156  **/
   157 EXPORT_C gboolean
   158 g_unichar_isdigit (gunichar c)
   159 {
   160   return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
   161 }
   162 
   163 
   164 /**
   165  * g_unichar_isgraph:
   166  * @c: a Unicode character
   167  * 
   168  * Determines whether a character is printable and not a space
   169  * (returns %FALSE for control characters, format characters, and
   170  * spaces). g_unichar_isprint() is similar, but returns %TRUE for
   171  * spaces. Given some UTF-8 text, obtain a character value with
   172  * g_utf8_get_char().
   173  * 
   174  * Return value: %TRUE if @c is printable unless it's a space
   175  **/
   176 EXPORT_C gboolean
   177 g_unichar_isgraph (gunichar c)
   178 {
   179   return !IS (TYPE(c),
   180 	      OR (G_UNICODE_CONTROL,
   181 	      OR (G_UNICODE_FORMAT,
   182 	      OR (G_UNICODE_UNASSIGNED,
   183 	      OR (G_UNICODE_SURROGATE,
   184 	      OR (G_UNICODE_SPACE_SEPARATOR,
   185 	     0))))));
   186 }
   187 
   188 /**
   189  * g_unichar_islower:
   190  * @c: a Unicode character
   191  * 
   192  * Determines whether a character is a lowercase letter.
   193  * Given some UTF-8 text, obtain a character value with
   194  * g_utf8_get_char().
   195  * 
   196  * Return value: %TRUE if @c is a lowercase letter
   197  **/
   198 EXPORT_C gboolean
   199 g_unichar_islower (gunichar c)
   200 {
   201   return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
   202 }
   203 
   204 
   205 /**
   206  * g_unichar_isprint:
   207  * @c: a Unicode character
   208  * 
   209  * Determines whether a character is printable.
   210  * Unlike g_unichar_isgraph(), returns %TRUE for spaces.
   211  * Given some UTF-8 text, obtain a character value with
   212  * g_utf8_get_char().
   213  * 
   214  * Return value: %TRUE if @c is printable
   215  **/
   216 EXPORT_C gboolean
   217 g_unichar_isprint (gunichar c)
   218 {
   219   return !IS (TYPE(c),
   220 	      OR (G_UNICODE_CONTROL,
   221 	      OR (G_UNICODE_FORMAT,
   222 	      OR (G_UNICODE_UNASSIGNED,
   223 	      OR (G_UNICODE_SURROGATE,
   224 	     0)))));
   225 }
   226 
   227 /**
   228  * g_unichar_ispunct:
   229  * @c: a Unicode character
   230  * 
   231  * Determines whether a character is punctuation or a symbol.
   232  * Given some UTF-8 text, obtain a character value with
   233  * g_utf8_get_char().
   234  * 
   235  * Return value: %TRUE if @c is a punctuation or symbol character
   236  **/
   237 EXPORT_C gboolean
   238 g_unichar_ispunct (gunichar c)
   239 {
   240   return IS (TYPE(c),
   241 	     OR (G_UNICODE_CONNECT_PUNCTUATION,
   242 	     OR (G_UNICODE_DASH_PUNCTUATION,
   243 	     OR (G_UNICODE_CLOSE_PUNCTUATION,
   244 	     OR (G_UNICODE_FINAL_PUNCTUATION,
   245 	     OR (G_UNICODE_INITIAL_PUNCTUATION,
   246 	     OR (G_UNICODE_OTHER_PUNCTUATION,
   247 	     OR (G_UNICODE_OPEN_PUNCTUATION,
   248 	     OR (G_UNICODE_CURRENCY_SYMBOL,
   249 	     OR (G_UNICODE_MODIFIER_SYMBOL,
   250 	     OR (G_UNICODE_MATH_SYMBOL,
   251 	     OR (G_UNICODE_OTHER_SYMBOL,
   252 	    0)))))))))))) ? TRUE : FALSE;
   253 }
   254 
   255 /**
   256  * g_unichar_isspace:
   257  * @c: a Unicode character
   258  * 
   259  * Determines whether a character is a space, tab, or line separator
   260  * (newline, carriage return, etc.).  Given some UTF-8 text, obtain a
   261  * character value with g_utf8_get_char().
   262  *
   263  * (Note: don't use this to do word breaking; you have to use
   264  * Pango or equivalent to get word breaking right, the algorithm
   265  * is fairly complex.)
   266  *  
   267  * Return value: %TRUE if @c is a space character
   268  **/
   269 EXPORT_C gboolean
   270 g_unichar_isspace (gunichar c)
   271 {
   272   switch (c)
   273     {
   274       /* special-case these since Unicode thinks they are not spaces */
   275     case '\t':
   276     case '\n':
   277     case '\r':
   278     case '\f':
   279       return TRUE;
   280       break;
   281       
   282     default:
   283       {
   284 	return IS (TYPE(c),
   285 	           OR (G_UNICODE_SPACE_SEPARATOR,
   286 	           OR (G_UNICODE_LINE_SEPARATOR,
   287                    OR (G_UNICODE_PARAGRAPH_SEPARATOR,
   288 		  0)))) ? TRUE : FALSE;
   289       }
   290       break;
   291     }
   292 }
   293 
   294 /**
   295  * g_unichar_ismark:
   296  * @c: a Unicode character
   297  *
   298  * Determines whether a character is a mark (non-spacing mark,
   299  * combining mark, or enclosing mark in Unicode speak).
   300  * Given some UTF-8 text, obtain a character value
   301  * with g_utf8_get_char().
   302  *
   303  * Note: in most cases where isalpha characters are allowed,
   304  * ismark characters should be allowed to as they are essential
   305  * for writing most European languages as well as many non-Latin
   306  * scripts.
   307  *
   308  * Return value: %TRUE if @c is a mark character
   309  *
   310  * Since: 2.14
   311  **/
   312 EXPORT_C gboolean
   313 g_unichar_ismark (gunichar c)
   314 {
   315   return ISMARK (TYPE (c));
   316 }
   317 
   318 /**
   319  * g_unichar_isupper:
   320  * @c: a Unicode character
   321  * 
   322  * Determines if a character is uppercase.
   323  * 
   324  * Return value: %TRUE if @c is an uppercase character
   325  **/
   326 EXPORT_C gboolean
   327 g_unichar_isupper (gunichar c)
   328 {
   329   return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
   330 }
   331 
   332 /**
   333  * g_unichar_istitle:
   334  * @c: a Unicode character
   335  * 
   336  * Determines if a character is titlecase. Some characters in
   337  * Unicode which are composites, such as the DZ digraph
   338  * have three case variants instead of just two. The titlecase
   339  * form is used at the beginning of a word where only the
   340  * first letter is capitalized. The titlecase form of the DZ
   341  * digraph is U+01F2 LATIN CAPITAL LETTTER D WITH SMALL LETTER Z.
   342  * 
   343  * Return value: %TRUE if the character is titlecase
   344  **/
   345 EXPORT_C gboolean
   346 g_unichar_istitle (gunichar c)
   347 {
   348   unsigned int i;
   349   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
   350     if (title_table[i][0] == c)
   351       return TRUE;
   352   return FALSE;
   353 }
   354 
   355 /**
   356  * g_unichar_isxdigit:
   357  * @c: a Unicode character.
   358  * 
   359  * Determines if a character is a hexidecimal digit.
   360  * 
   361  * Return value: %TRUE if the character is a hexadecimal digit
   362  **/
   363 EXPORT_C gboolean
   364 g_unichar_isxdigit (gunichar c)
   365 {
   366   return ((c >= 'a' && c <= 'f')
   367 	  || (c >= 'A' && c <= 'F')
   368 	  || (TYPE (c) == G_UNICODE_DECIMAL_NUMBER));
   369 }
   370 
   371 /**
   372  * g_unichar_isdefined:
   373  * @c: a Unicode character
   374  * 
   375  * Determines if a given character is assigned in the Unicode
   376  * standard.
   377  *
   378  * Return value: %TRUE if the character has an assigned value
   379  **/
   380 EXPORT_C gboolean
   381 g_unichar_isdefined (gunichar c)
   382 {
   383   return !IS (TYPE(c),
   384 	      OR (G_UNICODE_UNASSIGNED,
   385 	      OR (G_UNICODE_SURROGATE,
   386 	     0)));
   387 }
   388 
   389 /**
   390  * g_unichar_iszerowidth:
   391  * @c: a Unicode character
   392  * 
   393  * Determines if a given character typically takes zero width when rendered.
   394  * The return value is %TRUE for all non-spacing and enclosing marks
   395  * (e.g., combining accents), format characters, zero-width
   396  * space, but not U+00AD SOFT HYPHEN.
   397  *
   398  * A typical use of this function is with one of g_unichar_iswide() or
   399  * g_unichar_iswide_cjk() to determine the number of cells a string occupies
   400  * when displayed on a grid display (terminals).  However, note that not all
   401  * terminals support zero-width rendering of zero-width marks.
   402  *
   403  * Return value: %TRUE if the character has zero width
   404  *
   405  * Since: 2.14
   406  **/
   407 EXPORT_C gboolean
   408 g_unichar_iszerowidth (gunichar c)
   409 {
   410   if (G_UNLIKELY (c == 0x00AD))
   411     return FALSE;
   412 
   413   if (G_UNLIKELY (ISZEROWIDTHTYPE (TYPE (c))))
   414     return TRUE;
   415 
   416   if (G_UNLIKELY ((c >= 0x1160 && c < 0x1200) ||
   417 		  c == 0x200B))
   418     return TRUE;
   419 
   420   return FALSE;
   421 }
   422 
   423 struct Interval
   424 {
   425   gunichar start, end;
   426 };
   427 
   428 static int
   429 interval_compare (const void *key, const void *elt)
   430 {
   431   gunichar c = GPOINTER_TO_UINT (key);
   432   struct Interval *interval = (struct Interval *)elt;
   433 
   434   if (c < interval->start)
   435     return -1;
   436   if (c > interval->end)
   437     return +1;
   438 
   439   return 0;
   440 }
   441 
   442 /**
   443  * g_unichar_iswide:
   444  * @c: a Unicode character
   445  * 
   446  * Determines if a character is typically rendered in a double-width
   447  * cell.
   448  * 
   449  * Return value: %TRUE if the character is wide
   450  **/
   451 EXPORT_C gboolean
   452 g_unichar_iswide (gunichar c)
   453 {
   454   /* sorted list of intervals of East_Asian_Width = W and F characters
   455    * from Unicode 5.1.0.  produced by mungling output of:
   456    * grep ';[FW]\>' EastAsianWidth.txt */
   457   static const struct Interval wide[] = {
   458     {0x1100, 0x1159}, {0x115F, 0x115F}, {0x2329, 0x232A}, {0x2E80, 0x2E99},
   459     {0x2E9B, 0x2EF3}, {0x2F00, 0x2FD5}, {0x2FF0, 0x2FFB}, {0x3000, 0x303E},
   460     {0x3041, 0x3096}, {0x3099, 0x30FF}, {0x3105, 0x312D}, {0x3131, 0x318E},
   461     {0x3190, 0x31B7}, {0x31C0, 0x31E3}, {0x31F0, 0x321E}, {0x3220, 0x3243},
   462     {0x3250, 0x32FE}, {0x3300, 0x4DB5}, {0x4E00, 0x9FC3}, {0xA000, 0xA48C},
   463     {0xA490, 0xA4C6}, {0xAC00, 0xD7A3}, {0xF900, 0xFA2D}, {0xFA30, 0xFA6A},
   464     {0xFA70, 0xFAD9}, {0xFE10, 0xFE19}, {0xFE30, 0xFE52}, {0xFE54, 0xFE66},
   465     {0xFE68, 0xFE6B}, {0xFF01, 0xFF60}, {0xFFE0, 0xFFE6}, {0x20000, 0x2FFFD},
   466     {0x30000, 0x3FFFD}
   467   };
   468 
   469   if (bsearch (GUINT_TO_POINTER (c), wide, G_N_ELEMENTS (wide), sizeof wide[0],
   470 	       interval_compare))
   471     return TRUE;
   472 
   473   return FALSE;
   474 }
   475 
   476 
   477 /**
   478  * g_unichar_iswide_cjk:
   479  * @c: a Unicode character
   480  * 
   481  * Determines if a character is typically rendered in a double-width
   482  * cell under legacy East Asian locales.  If a character is wide according to
   483  * g_unichar_iswide(), then it is also reported wide with this function, but
   484  * the converse is not necessarily true.  See the
   485  * <ulink url="http://www.unicode.org/reports/tr11/">Unicode Standard
   486  * Annex #11</ulink> for details.
   487  *
   488  * If a character passes the g_unichar_iswide() test then it will also pass
   489  * this test, but not the other way around.  Note that some characters may
   490  * pas both this test and g_unichar_iszerowidth().
   491  * 
   492  * Return value: %TRUE if the character is wide in legacy East Asian locales
   493  *
   494  * Since: 2.12
   495  */
   496 EXPORT_C gboolean
   497 g_unichar_iswide_cjk (gunichar c)
   498 {
   499   /* sorted list of intervals of East_Asian_Width = A and F characters
   500    * from Unicode 5.1.0.  produced by mungling output of:
   501    * grep ';[A]\>' EastAsianWidth.txt */
   502   static const struct Interval ambiguous[] = {
   503     {0x00A1, 0x00A1}, {0x00A4, 0x00A4}, {0x00A7, 0x00A8}, {0x00AA, 0x00AA},
   504     {0x00AD, 0x00AE}, {0x00B0, 0x00B4}, {0x00B6, 0x00BA}, {0x00BC, 0x00BF},
   505     {0x00C6, 0x00C6}, {0x00D0, 0x00D0}, {0x00D7, 0x00D8}, {0x00DE, 0x00E1},
   506     {0x00E6, 0x00E6}, {0x00E8, 0x00EA}, {0x00EC, 0x00ED}, {0x00F0, 0x00F0},
   507     {0x00F2, 0x00F3}, {0x00F7, 0x00FA}, {0x00FC, 0x00FC}, {0x00FE, 0x00FE},
   508     {0x0101, 0x0101}, {0x0111, 0x0111}, {0x0113, 0x0113}, {0x011B, 0x011B},
   509     {0x0126, 0x0127}, {0x012B, 0x012B}, {0x0131, 0x0133}, {0x0138, 0x0138},
   510     {0x013F, 0x0142}, {0x0144, 0x0144}, {0x0148, 0x014B}, {0x014D, 0x014D},
   511     {0x0152, 0x0153}, {0x0166, 0x0167}, {0x016B, 0x016B}, {0x01CE, 0x01CE},
   512     {0x01D0, 0x01D0}, {0x01D2, 0x01D2}, {0x01D4, 0x01D4}, {0x01D6, 0x01D6},
   513     {0x01D8, 0x01D8}, {0x01DA, 0x01DA}, {0x01DC, 0x01DC}, {0x0251, 0x0251},
   514     {0x0261, 0x0261}, {0x02C4, 0x02C4}, {0x02C7, 0x02C7}, {0x02C9, 0x02CB},
   515     {0x02CD, 0x02CD}, {0x02D0, 0x02D0}, {0x02D8, 0x02DB}, {0x02DD, 0x02DD},
   516     {0x02DF, 0x02DF}, {0x0300, 0x036F}, {0x0391, 0x03A1}, {0x03A3, 0x03A9},
   517     {0x03B1, 0x03C1}, {0x03C3, 0x03C9}, {0x0401, 0x0401}, {0x0410, 0x044F},
   518     {0x0451, 0x0451}, {0x2010, 0x2010}, {0x2013, 0x2016}, {0x2018, 0x2019},
   519     {0x201C, 0x201D}, {0x2020, 0x2022}, {0x2024, 0x2027}, {0x2030, 0x2030},
   520     {0x2032, 0x2033}, {0x2035, 0x2035}, {0x203B, 0x203B}, {0x203E, 0x203E},
   521     {0x2074, 0x2074}, {0x207F, 0x207F}, {0x2081, 0x2084}, {0x20AC, 0x20AC},
   522     {0x2103, 0x2103}, {0x2105, 0x2105}, {0x2109, 0x2109}, {0x2113, 0x2113},
   523     {0x2116, 0x2116}, {0x2121, 0x2122}, {0x2126, 0x2126}, {0x212B, 0x212B},
   524     {0x2153, 0x2154}, {0x215B, 0x215E}, {0x2160, 0x216B}, {0x2170, 0x2179},
   525     {0x2190, 0x2199}, {0x21B8, 0x21B9}, {0x21D2, 0x21D2}, {0x21D4, 0x21D4},
   526     {0x21E7, 0x21E7}, {0x2200, 0x2200}, {0x2202, 0x2203}, {0x2207, 0x2208},
   527     {0x220B, 0x220B}, {0x220F, 0x220F}, {0x2211, 0x2211}, {0x2215, 0x2215},
   528     {0x221A, 0x221A}, {0x221D, 0x2220}, {0x2223, 0x2223}, {0x2225, 0x2225},
   529     {0x2227, 0x222C}, {0x222E, 0x222E}, {0x2234, 0x2237}, {0x223C, 0x223D},
   530     {0x2248, 0x2248}, {0x224C, 0x224C}, {0x2252, 0x2252}, {0x2260, 0x2261},
   531     {0x2264, 0x2267}, {0x226A, 0x226B}, {0x226E, 0x226F}, {0x2282, 0x2283},
   532     {0x2286, 0x2287}, {0x2295, 0x2295}, {0x2299, 0x2299}, {0x22A5, 0x22A5},
   533     {0x22BF, 0x22BF}, {0x2312, 0x2312}, {0x2460, 0x24E9}, {0x24EB, 0x254B},
   534     {0x2550, 0x2573}, {0x2580, 0x258F}, {0x2592, 0x2595}, {0x25A0, 0x25A1},
   535     {0x25A3, 0x25A9}, {0x25B2, 0x25B3}, {0x25B6, 0x25B7}, {0x25BC, 0x25BD},
   536     {0x25C0, 0x25C1}, {0x25C6, 0x25C8}, {0x25CB, 0x25CB}, {0x25CE, 0x25D1},
   537     {0x25E2, 0x25E5}, {0x25EF, 0x25EF}, {0x2605, 0x2606}, {0x2609, 0x2609},
   538     {0x260E, 0x260F}, {0x2614, 0x2615}, {0x261C, 0x261C}, {0x261E, 0x261E},
   539     {0x2640, 0x2640}, {0x2642, 0x2642}, {0x2660, 0x2661}, {0x2663, 0x2665},
   540     {0x2667, 0x266A}, {0x266C, 0x266D}, {0x266F, 0x266F}, {0x273D, 0x273D},
   541     {0x2776, 0x277F}, {0xE000, 0xF8FF}, {0xFE00, 0xFE0F}, {0xFFFD, 0xFFFD},
   542     {0xE0100, 0xE01EF}, {0xF0000, 0xFFFFD}, {0x100000, 0x10FFFD}
   543   };
   544 
   545   if (g_unichar_iswide (c))
   546     return TRUE;
   547 
   548   if (bsearch (GUINT_TO_POINTER (c), ambiguous, G_N_ELEMENTS (ambiguous), sizeof ambiguous[0],
   549 	       interval_compare))
   550     return TRUE;
   551 
   552   return FALSE;
   553 }
   554 
   555 
   556 /**
   557  * g_unichar_toupper:
   558  * @c: a Unicode character
   559  * 
   560  * Converts a character to uppercase.
   561  * 
   562  * Return value: the result of converting @c to uppercase.
   563  *               If @c is not an lowercase or titlecase character,
   564  *               or has no upper case equivalent @c is returned unchanged.
   565  **/
   566 EXPORT_C gunichar
   567 g_unichar_toupper (gunichar c)
   568 {
   569   int t = TYPE (c);
   570   if (t == G_UNICODE_LOWERCASE_LETTER)
   571     {
   572       gunichar val = ATTTABLE (c >> 8, c & 0xff);
   573       if (val >= 0x1000000)
   574 	{
   575 	  const gchar *p = special_case_table + val - 0x1000000;
   576           val = g_utf8_get_char (p);
   577 	}
   578       /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
   579        * do not have an uppercase equivalent, in which case val will be
   580        * zero. 
   581        */
   582       return val ? val : c;
   583     }
   584   else if (t == G_UNICODE_TITLECASE_LETTER)
   585     {
   586       unsigned int i;
   587       for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
   588 	{
   589 	  if (title_table[i][0] == c)
   590 	    return title_table[i][1];
   591 	}
   592     }
   593   return c;
   594 }
   595 
   596 /**
   597  * g_unichar_tolower:
   598  * @c: a Unicode character.
   599  * 
   600  * Converts a character to lower case.
   601  * 
   602  * Return value: the result of converting @c to lower case.
   603  *               If @c is not an upperlower or titlecase character,
   604  *               or has no lowercase equivalent @c is returned unchanged.
   605  **/
   606 EXPORT_C gunichar
   607 g_unichar_tolower (gunichar c)
   608 {
   609   int t = TYPE (c);
   610   if (t == G_UNICODE_UPPERCASE_LETTER)
   611     {
   612       gunichar val = ATTTABLE (c >> 8, c & 0xff);
   613       if (val >= 0x1000000)
   614 	{
   615 	  const gchar *p = special_case_table + val - 0x1000000;
   616 	  return g_utf8_get_char (p);
   617 	}
   618       else
   619 	{
   620 	  /* Not all uppercase letters are guaranteed to have a lowercase
   621 	   * equivalent.  If this is the case, val will be zero. */
   622 	  return val ? val : c;
   623 	}
   624     }
   625   else if (t == G_UNICODE_TITLECASE_LETTER)
   626     {
   627       unsigned int i;
   628       for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
   629 	{
   630 	  if (title_table[i][0] == c)
   631 	    return title_table[i][2];
   632 	}
   633     }
   634   return c;
   635 }
   636 
   637 /**
   638  * g_unichar_totitle:
   639  * @c: a Unicode character
   640  * 
   641  * Converts a character to the titlecase.
   642  * 
   643  * Return value: the result of converting @c to titlecase.
   644  *               If @c is not an uppercase or lowercase character,
   645  *               @c is returned unchanged.
   646  **/
   647 EXPORT_C gunichar
   648 g_unichar_totitle (gunichar c)
   649 {
   650   unsigned int i;
   651   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
   652     {
   653       if (title_table[i][0] == c || title_table[i][1] == c
   654 	  || title_table[i][2] == c)
   655 	return title_table[i][0];
   656     }
   657     
   658   if (TYPE (c) == G_UNICODE_LOWERCASE_LETTER)
   659     return g_unichar_toupper (c);
   660 
   661   return c;
   662 }
   663 
   664 /**
   665  * g_unichar_digit_value:
   666  * @c: a Unicode character
   667  *
   668  * Determines the numeric value of a character as a decimal
   669  * digit.
   670  *
   671  * Return value: If @c is a decimal digit (according to
   672  * g_unichar_isdigit()), its numeric value. Otherwise, -1.
   673  **/
   674 EXPORT_C int
   675 g_unichar_digit_value (gunichar c)
   676 {
   677   if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
   678     return ATTTABLE (c >> 8, c & 0xff);
   679   return -1;
   680 }
   681 
   682 /**
   683  * g_unichar_xdigit_value:
   684  * @c: a Unicode character
   685  *
   686  * Determines the numeric value of a character as a hexidecimal
   687  * digit.
   688  *
   689  * Return value: If @c is a hex digit (according to
   690  * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
   691  **/
   692 EXPORT_C int
   693 g_unichar_xdigit_value (gunichar c)
   694 {
   695   if (c >= 'A' && c <= 'F')
   696     return c - 'A' + 10;
   697   if (c >= 'a' && c <= 'f')
   698     return c - 'a' + 10;
   699   if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
   700     return ATTTABLE (c >> 8, c & 0xff);
   701   return -1;
   702 }
   703 
   704 /**
   705  * g_unichar_type:
   706  * @c: a Unicode character
   707  * 
   708  * Classifies a Unicode character by type.
   709  * 
   710  * Return value: the type of the character.
   711  **/
   712 EXPORT_C GUnicodeType
   713 g_unichar_type (gunichar c)
   714 {
   715   return TYPE (c);
   716 }
   717 
   718 /*
   719  * Case mapping functions
   720  */
   721 
   722 typedef enum {
   723   LOCALE_NORMAL,
   724   LOCALE_TURKIC,
   725   LOCALE_LITHUANIAN
   726 } LocaleType;
   727 
   728 static LocaleType
   729 get_locale_type (void)
   730 {
   731 #ifdef G_OS_WIN32
   732   char *tem = g_win32_getlocale ();
   733   char locale[2];
   734 
   735   locale[0] = tem[0];
   736   locale[1] = tem[1];
   737   g_free (tem);
   738 #else
   739   const char *locale = setlocale (LC_CTYPE, NULL);
   740 #endif
   741 
   742   switch (locale[0])
   743     {
   744    case 'a':
   745       if (locale[1] == 'z')
   746 	return LOCALE_TURKIC;
   747       break;
   748     case 'l':
   749       if (locale[1] == 't')
   750 	return LOCALE_LITHUANIAN;
   751       break;
   752     case 't':
   753       if (locale[1] == 'r')
   754 	return LOCALE_TURKIC;
   755       break;
   756     }
   757 
   758   return LOCALE_NORMAL;
   759 }
   760 
   761 static gint
   762 output_marks (const char **p_inout,
   763 	      char        *out_buffer,
   764 	      gboolean     remove_dot)
   765 {
   766   const char *p = *p_inout;
   767   gint len = 0;
   768   
   769   while (*p)
   770     {
   771       gunichar c = g_utf8_get_char (p);
   772       
   773       if (ISMARK (TYPE (c)))
   774 	{
   775 	  if (!remove_dot || c != 0x307 /* COMBINING DOT ABOVE */)
   776 	    len += g_unichar_to_utf8 (c, out_buffer ? out_buffer + len : NULL);
   777 	  p = g_utf8_next_char (p);
   778 	}
   779       else
   780 	break;
   781     }
   782 
   783   *p_inout = p;
   784   return len;
   785 }
   786 
   787 static gint
   788 output_special_case (gchar *out_buffer,
   789 		     int    offset,
   790 		     int    type,
   791 		     int    which)
   792 {
   793   const gchar *p = special_case_table + offset;
   794   gint len;
   795 
   796   if (type != G_UNICODE_TITLECASE_LETTER)
   797     p = g_utf8_next_char (p);
   798 
   799   if (which == 1)
   800     p += strlen (p) + 1;
   801 
   802   len = strlen (p);
   803   if (out_buffer)
   804     memcpy (out_buffer, p, len);
   805 
   806   return len;
   807 }
   808 
   809 static gsize
   810 real_toupper (const gchar *str,
   811 	      gssize       max_len,
   812 	      gchar       *out_buffer,
   813 	      LocaleType   locale_type)
   814 {
   815   const gchar *p = str;
   816   const char *last = NULL;
   817   gsize len = 0;
   818   gboolean last_was_i = FALSE;
   819 
   820   while ((max_len < 0 || p < str + max_len) && *p)
   821     {
   822       gunichar c = g_utf8_get_char (p);
   823       int t = TYPE (c);
   824       gunichar val;
   825 
   826       last = p;
   827       p = g_utf8_next_char (p);
   828 
   829       if (locale_type == LOCALE_LITHUANIAN)
   830 	{
   831 	  if (c == 'i')
   832 	    last_was_i = TRUE;
   833 	  else 
   834 	    {
   835 	      if (last_was_i)
   836 		{
   837 		  /* Nasty, need to remove any dot above. Though
   838 		   * I think only E WITH DOT ABOVE occurs in practice
   839 		   * which could simplify this considerably.
   840 		   */
   841 		  gsize decomp_len, i;
   842 		  gunichar *decomp;
   843 
   844 		  decomp = g_unicode_canonical_decomposition (c, &decomp_len);
   845 		  for (i=0; i < decomp_len; i++)
   846 		    {
   847 		      if (decomp[i] != 0x307 /* COMBINING DOT ABOVE */)
   848 			len += g_unichar_to_utf8 (g_unichar_toupper (decomp[i]), out_buffer ? out_buffer + len : NULL);
   849 		    }
   850 		  g_free (decomp);
   851 		  
   852 		  len += output_marks (&p, out_buffer ? out_buffer + len : NULL, TRUE);
   853 
   854 		  continue;
   855 		}
   856 
   857 	      if (!ISMARK (t))
   858 		last_was_i = FALSE;
   859 	    }
   860 	}
   861       
   862       if (locale_type == LOCALE_TURKIC && c == 'i')
   863 	{
   864 	  /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
   865 	  len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL); 
   866 	}
   867       else if (c == 0x0345)	/* COMBINING GREEK YPOGEGRAMMENI */
   868 	{
   869 	  /* Nasty, need to move it after other combining marks .. this would go away if
   870 	   * we normalized first.
   871 	   */
   872 	  len += output_marks (&p, out_buffer ? out_buffer + len : NULL, FALSE);
   873 
   874 	  /* And output as GREEK CAPITAL LETTER IOTA */
   875 	  len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL); 	  
   876 	}
   877       else if (IS (t,
   878 		   OR (G_UNICODE_LOWERCASE_LETTER,
   879 		   OR (G_UNICODE_TITLECASE_LETTER,
   880 		  0))))
   881 	{
   882 	  val = ATTTABLE (c >> 8, c & 0xff);
   883 
   884 	  if (val >= 0x1000000)
   885 	    {
   886 	      len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t,
   887 					  t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
   888 	    }
   889 	  else
   890 	    {
   891 	      if (t == G_UNICODE_TITLECASE_LETTER)
   892 		{
   893 		  unsigned int i;
   894 		  for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
   895 		    {
   896 		      if (title_table[i][0] == c)
   897 			{
   898 			  val = title_table[i][1];
   899 			  break;
   900 			}
   901 		    }
   902 		}
   903 
   904 	      /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
   905 	       * do not have an uppercase equivalent, in which case val will be
   906 	       * zero. */
   907 	      len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
   908 	    }
   909 	}
   910       else
   911 	{
   912 	  gsize char_len = g_utf8_skip[*(guchar *)last];
   913 
   914 	  if (out_buffer)
   915 	    memcpy (out_buffer + len, last, char_len);
   916 
   917 	  len += char_len;
   918 	}
   919 
   920     }
   921 
   922   return len;
   923 }
   924 
   925 /**
   926  * g_utf8_strup:
   927  * @str: a UTF-8 encoded string
   928  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
   929  * 
   930  * Converts all Unicode characters in the string that have a case
   931  * to uppercase. The exact manner that this is done depends
   932  * on the current locale, and may result in the number of
   933  * characters in the string increasing. (For instance, the
   934  * German ess-zet will be changed to SS.)
   935  * 
   936  * Return value: a newly allocated string, with all characters
   937  *    converted to uppercase.  
   938  **/
   939 EXPORT_C gchar *
   940 g_utf8_strup (const gchar *str,
   941 	      gssize       len)
   942 {
   943   gsize result_len;
   944   LocaleType locale_type;
   945   gchar *result;
   946 
   947   g_return_val_if_fail (str != NULL, NULL);
   948 
   949   locale_type = get_locale_type ();
   950   
   951   /*
   952    * We use a two pass approach to keep memory management simple
   953    */
   954   result_len = real_toupper (str, len, NULL, locale_type);
   955   result = g_malloc (result_len + 1);
   956   real_toupper (str, len, result, locale_type);
   957   result[result_len] = '\0';
   958 
   959   return result;
   960 }
   961 
   962 /* traverses the string checking for characters with combining class == 230
   963  * until a base character is found */
   964 static gboolean
   965 has_more_above (const gchar *str)
   966 {
   967   const gchar *p = str;
   968   gint combining_class;
   969 
   970   while (*p)
   971     {
   972       combining_class = g_unichar_combining_class (g_utf8_get_char (p));
   973       if (combining_class == 230)
   974         return TRUE;
   975       else if (combining_class == 0)
   976         break;
   977 
   978       p = g_utf8_next_char (p);
   979     }
   980 
   981   return FALSE;
   982 }
   983 
   984 static gsize
   985 real_tolower (const gchar *str,
   986 	      gssize       max_len,
   987 	      gchar       *out_buffer,
   988 	      LocaleType   locale_type)
   989 {
   990   const gchar *p = str;
   991   const char *last = NULL;
   992   gsize len = 0;
   993 
   994   while ((max_len < 0 || p < str + max_len) && *p)
   995     {
   996       gunichar c = g_utf8_get_char (p);
   997       int t = TYPE (c);
   998       gunichar val;
   999 
  1000       last = p;
  1001       p = g_utf8_next_char (p);
  1002 
  1003       if (locale_type == LOCALE_TURKIC && c == 'I')
  1004 	{
  1005           if (g_utf8_get_char (p) == 0x0307)
  1006             {
  1007               /* I + COMBINING DOT ABOVE => i (U+0069) */
  1008               len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL); 
  1009               p = g_utf8_next_char (p);
  1010             }
  1011           else
  1012             {
  1013               /* I => LATIN SMALL LETTER DOTLESS I */
  1014               len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL); 
  1015             }
  1016         }
  1017       /* Introduce an explicit dot above when lowercasing capital I's and J's
  1018        * whenever there are more accents above. [SpecialCasing.txt] */
  1019       else if (locale_type == LOCALE_LITHUANIAN && 
  1020                (c == 0x00cc || c == 0x00cd || c == 0x0128))
  1021         {
  1022           len += g_unichar_to_utf8 (0x0069, out_buffer ? out_buffer + len : NULL); 
  1023           len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL); 
  1024 
  1025           switch (c)
  1026             {
  1027             case 0x00cc: 
  1028               len += g_unichar_to_utf8 (0x0300, out_buffer ? out_buffer + len : NULL); 
  1029               break;
  1030             case 0x00cd: 
  1031               len += g_unichar_to_utf8 (0x0301, out_buffer ? out_buffer + len : NULL); 
  1032               break;
  1033             case 0x0128: 
  1034               len += g_unichar_to_utf8 (0x0303, out_buffer ? out_buffer + len : NULL); 
  1035               break;
  1036             }
  1037         }
  1038       else if (locale_type == LOCALE_LITHUANIAN && 
  1039                (c == 'I' || c == 'J' || c == 0x012e) && 
  1040                has_more_above (p))
  1041         {
  1042           len += g_unichar_to_utf8 (g_unichar_tolower (c), out_buffer ? out_buffer + len : NULL); 
  1043           len += g_unichar_to_utf8 (0x0307, out_buffer ? out_buffer + len : NULL); 
  1044         }
  1045       else if (c == 0x03A3)	/* GREEK CAPITAL LETTER SIGMA */
  1046 	{
  1047 	  if ((max_len < 0 || p < str + max_len) && *p)
  1048 	    {
  1049 	      gunichar next_c = g_utf8_get_char (p);
  1050 	      int next_type = TYPE(next_c);
  1051 
  1052 	      /* SIGMA mapps differently depending on whether it is
  1053 	       * final or not. The following simplified test would
  1054 	       * fail in the case of combining marks following the
  1055 	       * sigma, but I don't think that occurs in real text.
  1056 	       * The test here matches that in ICU.
  1057 	       */
  1058 	      if (ISALPHA (next_type)) /* Lu,Ll,Lt,Lm,Lo */
  1059 		val = 0x3c3;	/* GREEK SMALL SIGMA */
  1060 	      else
  1061 		val = 0x3c2;	/* GREEK SMALL FINAL SIGMA */
  1062 	    }
  1063 	  else
  1064 	    val = 0x3c2;	/* GREEK SMALL FINAL SIGMA */
  1065 
  1066 	  len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
  1067 	}
  1068       else if (IS (t,
  1069 		   OR (G_UNICODE_UPPERCASE_LETTER,
  1070 		   OR (G_UNICODE_TITLECASE_LETTER,
  1071 		  0))))
  1072 	{
  1073 	  val = ATTTABLE (c >> 8, c & 0xff);
  1074 
  1075 	  if (val >= 0x1000000)
  1076 	    {
  1077 	      len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t, 0);
  1078 	    }
  1079 	  else
  1080 	    {
  1081 	      if (t == G_UNICODE_TITLECASE_LETTER)
  1082 		{
  1083 		  unsigned int i;
  1084 		  for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
  1085 		    {
  1086 		      if (title_table[i][0] == c)
  1087 			{
  1088 			  val = title_table[i][2];
  1089 			  break;
  1090 			}
  1091 		    }
  1092 		}
  1093 
  1094 	      /* Not all uppercase letters are guaranteed to have a lowercase
  1095 	       * equivalent.  If this is the case, val will be zero. */
  1096 	      len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
  1097 	    }
  1098 	}
  1099       else
  1100 	{
  1101 	  gsize char_len = g_utf8_skip[*(guchar *)last];
  1102 
  1103 	  if (out_buffer)
  1104 	    memcpy (out_buffer + len, last, char_len);
  1105 
  1106 	  len += char_len;
  1107 	}
  1108 
  1109     }
  1110 
  1111   return len;
  1112 }
  1113 
  1114 /**
  1115  * g_utf8_strdown:
  1116  * @str: a UTF-8 encoded string
  1117  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
  1118  * 
  1119  * Converts all Unicode characters in the string that have a case
  1120  * to lowercase. The exact manner that this is done depends
  1121  * on the current locale, and may result in the number of
  1122  * characters in the string changing.
  1123  * 
  1124  * Return value: a newly allocated string, with all characters
  1125  *    converted to lowercase.  
  1126  **/
  1127 EXPORT_C gchar *
  1128 g_utf8_strdown (const gchar *str,
  1129 		gssize       len)
  1130 {
  1131   gsize result_len;
  1132   LocaleType locale_type;
  1133   gchar *result;
  1134 
  1135   g_return_val_if_fail (str != NULL, NULL);
  1136 
  1137   locale_type = get_locale_type ();
  1138   
  1139   /*
  1140    * We use a two pass approach to keep memory management simple
  1141    */
  1142   result_len = real_tolower (str, len, NULL, locale_type);
  1143   result = g_malloc (result_len + 1);
  1144   real_tolower (str, len, result, locale_type);
  1145   result[result_len] = '\0';
  1146 
  1147   return result;
  1148 }
  1149 
  1150 /**
  1151  * g_utf8_casefold:
  1152  * @str: a UTF-8 encoded string
  1153  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
  1154  * 
  1155  * Converts a string into a form that is independent of case. The
  1156  * result will not correspond to any particular case, but can be
  1157  * compared for equality or ordered with the results of calling
  1158  * g_utf8_casefold() on other strings.
  1159  * 
  1160  * Note that calling g_utf8_casefold() followed by g_utf8_collate() is
  1161  * only an approximation to the correct linguistic case insensitive
  1162  * ordering, though it is a fairly good one. Getting this exactly
  1163  * right would require a more sophisticated collation function that
  1164  * takes case sensitivity into account. GLib does not currently
  1165  * provide such a function.
  1166  * 
  1167  * Return value: a newly allocated string, that is a
  1168  *   case independent form of @str.
  1169  **/
  1170 EXPORT_C gchar *
  1171 g_utf8_casefold (const gchar *str,
  1172 		 gssize       len)
  1173 {
  1174   GString *result;
  1175   const char *p;
  1176 
  1177   g_return_val_if_fail (str != NULL, NULL);
  1178 
  1179   result = g_string_new (NULL);
  1180   p = str;
  1181   while ((len < 0 || p < str + len) && *p)
  1182     {
  1183       gunichar ch = g_utf8_get_char (p);
  1184 
  1185       int start = 0;
  1186       int end = G_N_ELEMENTS (casefold_table);
  1187 
  1188       if (ch >= casefold_table[start].ch &&
  1189           ch <= casefold_table[end - 1].ch)
  1190 	{
  1191 	  while (TRUE)
  1192 	    {
  1193 	      int half = (start + end) / 2;
  1194 	      if (ch == casefold_table[half].ch)
  1195 		{
  1196 		  g_string_append (result, casefold_table[half].data);
  1197 		  goto next;
  1198 		}
  1199 	      else if (half == start)
  1200 		break;
  1201 	      else if (ch > casefold_table[half].ch)
  1202 		start = half;
  1203 	      else
  1204 		end = half;
  1205 	    }
  1206 	}
  1207 
  1208       g_string_append_unichar (result, g_unichar_tolower (ch));
  1209       
  1210     next:
  1211       p = g_utf8_next_char (p);
  1212     }
  1213 
  1214   return g_string_free (result, FALSE); 
  1215 }
  1216 
  1217 /**
  1218  * g_unichar_get_mirror_char:
  1219  * @ch: a Unicode character
  1220  * @mirrored_ch: location to store the mirrored character
  1221  * 
  1222  * In Unicode, some characters are <firstterm>mirrored</firstterm>. This
  1223  * means that their images are mirrored horizontally in text that is laid
  1224  * out from right to left. For instance, "(" would become its mirror image,
  1225  * ")", in right-to-left text.
  1226  *
  1227  * If @ch has the Unicode mirrored property and there is another unicode
  1228  * character that typically has a glyph that is the mirror image of @ch's
  1229  * glyph and @mirrored_ch is set, it puts that character in the address
  1230  * pointed to by @mirrored_ch.  Otherwise the original character is put.
  1231  *
  1232  * Return value: %TRUE if @ch has a mirrored character, %FALSE otherwise
  1233  *
  1234  * Since: 2.4
  1235  **/
  1236 EXPORT_C gboolean
  1237 g_unichar_get_mirror_char (gunichar ch,
  1238                            gunichar *mirrored_ch)
  1239 {
  1240   gboolean found;
  1241   gunichar mirrored;
  1242 
  1243   mirrored = GLIB_GET_MIRRORING(ch);
  1244 
  1245   found = ch != mirrored;
  1246   if (mirrored_ch)
  1247     *mirrored_ch = mirrored;
  1248 
  1249   return found;
  1250 
  1251 }
  1252 
  1253 #if (!EMULATOR)
  1254 #define G_SCRIPT_TABLE_MIDPOINT (G_N_ELEMENTS (g_script_table) / 2)
  1255 #endif//!EMULATOR
  1256 
  1257 #if EMULATOR
  1258 PLS(saved_mid ,g_unichar_get_script_bsearch,int)
  1259 #define saved_mid (*FUNCTION_NAME(saved_mid,g_unichar_get_script_bsearch)())
  1260 #endif /* EMULATOR */
  1261 
  1262 static inline GUnicodeScript
  1263 g_unichar_get_script_bsearch (gunichar ch)
  1264 {
  1265   int lower = 0;
  1266   int upper = G_N_ELEMENTS (g_script_table) - 1;
  1267 #if (!EMULATOR)   
  1268   static int saved_mid = G_SCRIPT_TABLE_MIDPOINT;
  1269 #endif//!EMULATOR 
  1270   int mid = saved_mid;
  1271 
  1272 
  1273   do 
  1274     {
  1275       if (ch < g_script_table[mid].start)
  1276 	upper = mid - 1;
  1277       else if (ch >= g_script_table[mid].start + g_script_table[mid].chars)
  1278 	lower = mid + 1;
  1279       else
  1280 	return g_script_table[saved_mid = mid].script;
  1281 
  1282       mid = (lower + upper) / 2;
  1283     }
  1284   while (lower <= upper);
  1285 
  1286   return G_UNICODE_SCRIPT_UNKNOWN;
  1287 }
  1288 
  1289 #if (EMULATOR)
  1290 #undef saved_mid
  1291 #endif//EMULATOR
  1292 
  1293 /**
  1294  * g_unichar_get_script:
  1295  * @ch: a Unicode character
  1296  * 
  1297  * Looks up the #GUnicodeScript for a particular character (as defined 
  1298  * by Unicode Standard Annex #24). No check is made for @ch being a
  1299  * valid Unicode character; if you pass in invalid character, the
  1300  * result is undefined.
  1301  *
  1302  * This function is equivalent to pango_script_for_unichar() and the
  1303  * two are interchangeable.
  1304  * 
  1305  * Return value: the #GUnicodeScript for the character.
  1306  *
  1307  * Since: 2.14
  1308  */
  1309 EXPORT_C GUnicodeScript
  1310 g_unichar_get_script (gunichar ch)
  1311 {
  1312   if (ch < G_EASY_SCRIPTS_RANGE)
  1313     return g_script_easy_table[ch];
  1314   else 
  1315     return g_unichar_get_script_bsearch (ch); 
  1316 }
  1317 
  1318 
  1319 #define __G_UNIPROP_C__
  1320 #include "galiasdef.c"