Update contrib.
     1 /* guniprop.c - Unicode character properties.
 
     3  * Copyright (C) 1999 Tom Tromey
 
     4  * Copyright (C) 2000 Red Hat, Inc.
 
     5  * Portions copyright (c) 2006-2009 Nokia Corporation.  All rights reserved.
 
     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.
 
    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.
 
    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.
 
    31 #include "gunichartables.h"
 
    32 #include "gmirroringtable.h"
 
    33 #include "gscripttable.h"
 
    34 #include "gunicodeprivate.h"
 
    39 #endif /* __SYMBIAN32__ */
 
    41 #define ATTR_TABLE(Page) (((Page) <= G_UNICODE_LAST_PAGE_PART1) \
 
    42                           ? attr_table_part1[Page] \
 
    43                           : attr_table_part2[(Page) - 0xe00])
 
    45 #define ATTTABLE(Page, Char) \
 
    46   ((ATTR_TABLE(Page) == G_UNICODE_MAX_TABLE_INDEX) ? 0 : (attr_data[ATTR_TABLE(Page)][Char]))
 
    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]))
 
    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]))
 
    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))
 
    66 #define IS(Type, Class)	(((guint)1 << (Type)) & (Class))
 
    67 #define OR(Type, Rest)	(((guint)1 << (Type)) | (Rest))
 
    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))))))
 
    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)))))))))
 
    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))))
 
    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))))
 
   100  * @c: a Unicode character
 
   102  * Determines whether a character is alphanumeric.
 
   103  * Given some UTF-8 text, obtain a character value
 
   104  * with g_utf8_get_char().
 
   106  * Return value: %TRUE if @c is an alphanumeric character
 
   109 g_unichar_isalnum (gunichar c)
 
   111   return ISALDIGIT (TYPE (c)) ? TRUE : FALSE;
 
   116  * @c: a Unicode character
 
   118  * Determines whether a character is alphabetic (i.e. a letter).
 
   119  * Given some UTF-8 text, obtain a character value with
 
   122  * Return value: %TRUE if @c is an alphabetic character
 
   125 g_unichar_isalpha (gunichar c)
 
   127   return ISALPHA (TYPE (c)) ? TRUE : FALSE;
 
   133  * @c: a Unicode character
 
   135  * Determines whether a character is a control character.
 
   136  * Given some UTF-8 text, obtain a character value with
 
   139  * Return value: %TRUE if @c is a control character
 
   142 g_unichar_iscntrl (gunichar c)
 
   144   return TYPE (c) == G_UNICODE_CONTROL;
 
   149  * @c: a Unicode character
 
   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().
 
   155  * Return value: %TRUE if @c is a digit
 
   158 g_unichar_isdigit (gunichar c)
 
   160   return TYPE (c) == G_UNICODE_DECIMAL_NUMBER;
 
   166  * @c: a Unicode character
 
   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
 
   174  * Return value: %TRUE if @c is printable unless it's a space
 
   177 g_unichar_isgraph (gunichar 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,
 
   190  * @c: a Unicode character
 
   192  * Determines whether a character is a lowercase letter.
 
   193  * Given some UTF-8 text, obtain a character value with
 
   196  * Return value: %TRUE if @c is a lowercase letter
 
   199 g_unichar_islower (gunichar c)
 
   201   return TYPE (c) == G_UNICODE_LOWERCASE_LETTER;
 
   207  * @c: a Unicode character
 
   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
 
   214  * Return value: %TRUE if @c is printable
 
   217 g_unichar_isprint (gunichar c)
 
   220 	      OR (G_UNICODE_CONTROL,
 
   221 	      OR (G_UNICODE_FORMAT,
 
   222 	      OR (G_UNICODE_UNASSIGNED,
 
   223 	      OR (G_UNICODE_SURROGATE,
 
   229  * @c: a Unicode character
 
   231  * Determines whether a character is punctuation or a symbol.
 
   232  * Given some UTF-8 text, obtain a character value with
 
   235  * Return value: %TRUE if @c is a punctuation or symbol character
 
   238 g_unichar_ispunct (gunichar 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;
 
   257  * @c: a Unicode character
 
   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().
 
   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.)
 
   267  * Return value: %TRUE if @c is a space character
 
   270 g_unichar_isspace (gunichar c)
 
   274       /* special-case these since Unicode thinks they are not spaces */
 
   285 	           OR (G_UNICODE_SPACE_SEPARATOR,
 
   286 	           OR (G_UNICODE_LINE_SEPARATOR,
 
   287                    OR (G_UNICODE_PARAGRAPH_SEPARATOR,
 
   288 		  0)))) ? TRUE : FALSE;
 
   296  * @c: a Unicode character
 
   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().
 
   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
 
   308  * Return value: %TRUE if @c is a mark character
 
   313 g_unichar_ismark (gunichar c)
 
   315   return ISMARK (TYPE (c));
 
   320  * @c: a Unicode character
 
   322  * Determines if a character is uppercase.
 
   324  * Return value: %TRUE if @c is an uppercase character
 
   327 g_unichar_isupper (gunichar c)
 
   329   return TYPE (c) == G_UNICODE_UPPERCASE_LETTER;
 
   334  * @c: a Unicode character
 
   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.
 
   343  * Return value: %TRUE if the character is titlecase
 
   346 g_unichar_istitle (gunichar c)
 
   349   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
 
   350     if (title_table[i][0] == c)
 
   356  * g_unichar_isxdigit:
 
   357  * @c: a Unicode character.
 
   359  * Determines if a character is a hexidecimal digit.
 
   361  * Return value: %TRUE if the character is a hexadecimal digit
 
   364 g_unichar_isxdigit (gunichar c)
 
   366   return ((c >= 'a' && c <= 'f')
 
   367 	  || (c >= 'A' && c <= 'F')
 
   368 	  || (TYPE (c) == G_UNICODE_DECIMAL_NUMBER));
 
   372  * g_unichar_isdefined:
 
   373  * @c: a Unicode character
 
   375  * Determines if a given character is assigned in the Unicode
 
   378  * Return value: %TRUE if the character has an assigned value
 
   381 g_unichar_isdefined (gunichar c)
 
   384 	      OR (G_UNICODE_UNASSIGNED,
 
   385 	      OR (G_UNICODE_SURROGATE,
 
   390  * g_unichar_iszerowidth:
 
   391  * @c: a Unicode character
 
   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.
 
   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.
 
   403  * Return value: %TRUE if the character has zero width
 
   408 g_unichar_iszerowidth (gunichar c)
 
   410   if (G_UNLIKELY (c == 0x00AD))
 
   413   if (G_UNLIKELY (ISZEROWIDTHTYPE (TYPE (c))))
 
   416   if (G_UNLIKELY ((c >= 0x1160 && c < 0x1200) ||
 
   429 interval_compare (const void *key, const void *elt)
 
   431   gunichar c = GPOINTER_TO_UINT (key);
 
   432   struct Interval *interval = (struct Interval *)elt;
 
   434   if (c < interval->start)
 
   436   if (c > interval->end)
 
   444  * @c: a Unicode character
 
   446  * Determines if a character is typically rendered in a double-width
 
   449  * Return value: %TRUE if the character is wide
 
   452 g_unichar_iswide (gunichar c)
 
   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},
 
   469   if (bsearch (GUINT_TO_POINTER (c), wide, G_N_ELEMENTS (wide), sizeof wide[0],
 
   478  * g_unichar_iswide_cjk:
 
   479  * @c: a Unicode character
 
   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.
 
   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().
 
   492  * Return value: %TRUE if the character is wide in legacy East Asian locales
 
   497 g_unichar_iswide_cjk (gunichar c)
 
   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}
 
   545   if (g_unichar_iswide (c))
 
   548   if (bsearch (GUINT_TO_POINTER (c), ambiguous, G_N_ELEMENTS (ambiguous), sizeof ambiguous[0],
 
   558  * @c: a Unicode character
 
   560  * Converts a character to uppercase.
 
   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.
 
   567 g_unichar_toupper (gunichar c)
 
   570   if (t == G_UNICODE_LOWERCASE_LETTER)
 
   572       gunichar val = ATTTABLE (c >> 8, c & 0xff);
 
   573       if (val >= 0x1000000)
 
   575 	  const gchar *p = special_case_table + val - 0x1000000;
 
   576           val = g_utf8_get_char (p);
 
   578       /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
 
   579        * do not have an uppercase equivalent, in which case val will be
 
   582       return val ? val : c;
 
   584   else if (t == G_UNICODE_TITLECASE_LETTER)
 
   587       for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
 
   589 	  if (title_table[i][0] == c)
 
   590 	    return title_table[i][1];
 
   598  * @c: a Unicode character.
 
   600  * Converts a character to lower case.
 
   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.
 
   607 g_unichar_tolower (gunichar c)
 
   610   if (t == G_UNICODE_UPPERCASE_LETTER)
 
   612       gunichar val = ATTTABLE (c >> 8, c & 0xff);
 
   613       if (val >= 0x1000000)
 
   615 	  const gchar *p = special_case_table + val - 0x1000000;
 
   616 	  return g_utf8_get_char (p);
 
   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;
 
   625   else if (t == G_UNICODE_TITLECASE_LETTER)
 
   628       for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
 
   630 	  if (title_table[i][0] == c)
 
   631 	    return title_table[i][2];
 
   639  * @c: a Unicode character
 
   641  * Converts a character to the titlecase.
 
   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.
 
   648 g_unichar_totitle (gunichar c)
 
   651   for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
 
   653       if (title_table[i][0] == c || title_table[i][1] == c
 
   654 	  || title_table[i][2] == c)
 
   655 	return title_table[i][0];
 
   658   if (TYPE (c) == G_UNICODE_LOWERCASE_LETTER)
 
   659     return g_unichar_toupper (c);
 
   665  * g_unichar_digit_value:
 
   666  * @c: a Unicode character
 
   668  * Determines the numeric value of a character as a decimal
 
   671  * Return value: If @c is a decimal digit (according to
 
   672  * g_unichar_isdigit()), its numeric value. Otherwise, -1.
 
   675 g_unichar_digit_value (gunichar c)
 
   677   if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
 
   678     return ATTTABLE (c >> 8, c & 0xff);
 
   683  * g_unichar_xdigit_value:
 
   684  * @c: a Unicode character
 
   686  * Determines the numeric value of a character as a hexidecimal
 
   689  * Return value: If @c is a hex digit (according to
 
   690  * g_unichar_isxdigit()), its numeric value. Otherwise, -1.
 
   693 g_unichar_xdigit_value (gunichar c)
 
   695   if (c >= 'A' && c <= 'F')
 
   697   if (c >= 'a' && c <= 'f')
 
   699   if (TYPE (c) == G_UNICODE_DECIMAL_NUMBER)
 
   700     return ATTTABLE (c >> 8, c & 0xff);
 
   706  * @c: a Unicode character
 
   708  * Classifies a Unicode character by type.
 
   710  * Return value: the type of the character.
 
   712 EXPORT_C GUnicodeType
 
   713 g_unichar_type (gunichar c)
 
   719  * Case mapping functions
 
   729 get_locale_type (void)
 
   732   char *tem = g_win32_getlocale ();
 
   739   const char *locale = setlocale (LC_CTYPE, NULL);
 
   745       if (locale[1] == 'z')
 
   746 	return LOCALE_TURKIC;
 
   749       if (locale[1] == 't')
 
   750 	return LOCALE_LITHUANIAN;
 
   753       if (locale[1] == 'r')
 
   754 	return LOCALE_TURKIC;
 
   758   return LOCALE_NORMAL;
 
   762 output_marks (const char **p_inout,
 
   766   const char *p = *p_inout;
 
   771       gunichar c = g_utf8_get_char (p);
 
   773       if (ISMARK (TYPE (c)))
 
   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);
 
   788 output_special_case (gchar *out_buffer,
 
   793   const gchar *p = special_case_table + offset;
 
   796   if (type != G_UNICODE_TITLECASE_LETTER)
 
   797     p = g_utf8_next_char (p);
 
   804     memcpy (out_buffer, p, len);
 
   810 real_toupper (const gchar *str,
 
   813 	      LocaleType   locale_type)
 
   815   const gchar *p = str;
 
   816   const char *last = NULL;
 
   818   gboolean last_was_i = FALSE;
 
   820   while ((max_len < 0 || p < str + max_len) && *p)
 
   822       gunichar c = g_utf8_get_char (p);
 
   827       p = g_utf8_next_char (p);
 
   829       if (locale_type == LOCALE_LITHUANIAN)
 
   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.
 
   844 		  decomp = g_unicode_canonical_decomposition (c, &decomp_len);
 
   845 		  for (i=0; i < decomp_len; i++)
 
   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);
 
   852 		  len += output_marks (&p, out_buffer ? out_buffer + len : NULL, TRUE);
 
   862       if (locale_type == LOCALE_TURKIC && c == 'i')
 
   864 	  /* i => LATIN CAPITAL LETTER I WITH DOT ABOVE */
 
   865 	  len += g_unichar_to_utf8 (0x130, out_buffer ? out_buffer + len : NULL); 
 
   867       else if (c == 0x0345)	/* COMBINING GREEK YPOGEGRAMMENI */
 
   869 	  /* Nasty, need to move it after other combining marks .. this would go away if
 
   870 	   * we normalized first.
 
   872 	  len += output_marks (&p, out_buffer ? out_buffer + len : NULL, FALSE);
 
   874 	  /* And output as GREEK CAPITAL LETTER IOTA */
 
   875 	  len += g_unichar_to_utf8 (0x399, out_buffer ? out_buffer + len : NULL); 	  
 
   878 		   OR (G_UNICODE_LOWERCASE_LETTER,
 
   879 		   OR (G_UNICODE_TITLECASE_LETTER,
 
   882 	  val = ATTTABLE (c >> 8, c & 0xff);
 
   884 	  if (val >= 0x1000000)
 
   886 	      len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t,
 
   887 					  t == G_UNICODE_LOWERCASE_LETTER ? 0 : 1);
 
   891 	      if (t == G_UNICODE_TITLECASE_LETTER)
 
   894 		  for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
 
   896 		      if (title_table[i][0] == c)
 
   898 			  val = title_table[i][1];
 
   904 	      /* Some lowercase letters, e.g., U+000AA, FEMININE ORDINAL INDICATOR,
 
   905 	       * do not have an uppercase equivalent, in which case val will be
 
   907 	      len += g_unichar_to_utf8 (val ? val : c, out_buffer ? out_buffer + len : NULL);
 
   912 	  gsize char_len = g_utf8_skip[*(guchar *)last];
 
   915 	    memcpy (out_buffer + len, last, char_len);
 
   927  * @str: a UTF-8 encoded string
 
   928  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
 
   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.)
 
   936  * Return value: a newly allocated string, with all characters
 
   937  *    converted to uppercase.  
 
   940 g_utf8_strup (const gchar *str,
 
   944   LocaleType locale_type;
 
   947   g_return_val_if_fail (str != NULL, NULL);
 
   949   locale_type = get_locale_type ();
 
   952    * We use a two pass approach to keep memory management simple
 
   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';
 
   962 /* traverses the string checking for characters with combining class == 230
 
   963  * until a base character is found */
 
   965 has_more_above (const gchar *str)
 
   967   const gchar *p = str;
 
   968   gint combining_class;
 
   972       combining_class = g_unichar_combining_class (g_utf8_get_char (p));
 
   973       if (combining_class == 230)
 
   975       else if (combining_class == 0)
 
   978       p = g_utf8_next_char (p);
 
   985 real_tolower (const gchar *str,
 
   988 	      LocaleType   locale_type)
 
   990   const gchar *p = str;
 
   991   const char *last = NULL;
 
   994   while ((max_len < 0 || p < str + max_len) && *p)
 
   996       gunichar c = g_utf8_get_char (p);
 
  1001       p = g_utf8_next_char (p);
 
  1003       if (locale_type == LOCALE_TURKIC && c == 'I')
 
  1005           if (g_utf8_get_char (p) == 0x0307)
 
  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);
 
  1013               /* I => LATIN SMALL LETTER DOTLESS I */
 
  1014               len += g_unichar_to_utf8 (0x131, out_buffer ? out_buffer + len : NULL); 
 
  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))
 
  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); 
 
  1028               len += g_unichar_to_utf8 (0x0300, out_buffer ? out_buffer + len : NULL); 
 
  1031               len += g_unichar_to_utf8 (0x0301, out_buffer ? out_buffer + len : NULL); 
 
  1034               len += g_unichar_to_utf8 (0x0303, out_buffer ? out_buffer + len : NULL); 
 
  1038       else if (locale_type == LOCALE_LITHUANIAN && 
 
  1039                (c == 'I' || c == 'J' || c == 0x012e) && 
 
  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); 
 
  1045       else if (c == 0x03A3)	/* GREEK CAPITAL LETTER SIGMA */
 
  1047 	  if ((max_len < 0 || p < str + max_len) && *p)
 
  1049 	      gunichar next_c = g_utf8_get_char (p);
 
  1050 	      int next_type = TYPE(next_c);
 
  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.
 
  1058 	      if (ISALPHA (next_type)) /* Lu,Ll,Lt,Lm,Lo */
 
  1059 		val = 0x3c3;	/* GREEK SMALL SIGMA */
 
  1061 		val = 0x3c2;	/* GREEK SMALL FINAL SIGMA */
 
  1064 	    val = 0x3c2;	/* GREEK SMALL FINAL SIGMA */
 
  1066 	  len += g_unichar_to_utf8 (val, out_buffer ? out_buffer + len : NULL);
 
  1069 		   OR (G_UNICODE_UPPERCASE_LETTER,
 
  1070 		   OR (G_UNICODE_TITLECASE_LETTER,
 
  1073 	  val = ATTTABLE (c >> 8, c & 0xff);
 
  1075 	  if (val >= 0x1000000)
 
  1077 	      len += output_special_case (out_buffer ? out_buffer + len : NULL, val - 0x1000000, t, 0);
 
  1081 	      if (t == G_UNICODE_TITLECASE_LETTER)
 
  1084 		  for (i = 0; i < G_N_ELEMENTS (title_table); ++i)
 
  1086 		      if (title_table[i][0] == c)
 
  1088 			  val = title_table[i][2];
 
  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);
 
  1101 	  gsize char_len = g_utf8_skip[*(guchar *)last];
 
  1104 	    memcpy (out_buffer + len, last, char_len);
 
  1116  * @str: a UTF-8 encoded string
 
  1117  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
 
  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.
 
  1124  * Return value: a newly allocated string, with all characters
 
  1125  *    converted to lowercase.  
 
  1128 g_utf8_strdown (const gchar *str,
 
  1132   LocaleType locale_type;
 
  1135   g_return_val_if_fail (str != NULL, NULL);
 
  1137   locale_type = get_locale_type ();
 
  1140    * We use a two pass approach to keep memory management simple
 
  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';
 
  1152  * @str: a UTF-8 encoded string
 
  1153  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
 
  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.
 
  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.
 
  1167  * Return value: a newly allocated string, that is a
 
  1168  *   case independent form of @str.
 
  1171 g_utf8_casefold (const gchar *str,
 
  1177   g_return_val_if_fail (str != NULL, NULL);
 
  1179   result = g_string_new (NULL);
 
  1181   while ((len < 0 || p < str + len) && *p)
 
  1183       gunichar ch = g_utf8_get_char (p);
 
  1186       int end = G_N_ELEMENTS (casefold_table);
 
  1188       if (ch >= casefold_table[start].ch &&
 
  1189           ch <= casefold_table[end - 1].ch)
 
  1193 	      int half = (start + end) / 2;
 
  1194 	      if (ch == casefold_table[half].ch)
 
  1196 		  g_string_append (result, casefold_table[half].data);
 
  1199 	      else if (half == start)
 
  1201 	      else if (ch > casefold_table[half].ch)
 
  1208       g_string_append_unichar (result, g_unichar_tolower (ch));
 
  1211       p = g_utf8_next_char (p);
 
  1214   return g_string_free (result, FALSE); 
 
  1218  * g_unichar_get_mirror_char:
 
  1219  * @ch: a Unicode character
 
  1220  * @mirrored_ch: location to store the mirrored character
 
  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.
 
  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.
 
  1232  * Return value: %TRUE if @ch has a mirrored character, %FALSE otherwise
 
  1237 g_unichar_get_mirror_char (gunichar ch,
 
  1238                            gunichar *mirrored_ch)
 
  1243   mirrored = GLIB_GET_MIRRORING(ch);
 
  1245   found = ch != mirrored;
 
  1247     *mirrored_ch = mirrored;
 
  1254 #define G_SCRIPT_TABLE_MIDPOINT (G_N_ELEMENTS (g_script_table) / 2)
 
  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 */
 
  1262 static inline GUnicodeScript
 
  1263 g_unichar_get_script_bsearch (gunichar ch)
 
  1266   int upper = G_N_ELEMENTS (g_script_table) - 1;
 
  1268   static int saved_mid = G_SCRIPT_TABLE_MIDPOINT;
 
  1270   int mid = saved_mid;
 
  1275       if (ch < g_script_table[mid].start)
 
  1277       else if (ch >= g_script_table[mid].start + g_script_table[mid].chars)
 
  1280 	return g_script_table[saved_mid = mid].script;
 
  1282       mid = (lower + upper) / 2;
 
  1284   while (lower <= upper);
 
  1286   return G_UNICODE_SCRIPT_UNKNOWN;
 
  1294  * g_unichar_get_script:
 
  1295  * @ch: a Unicode character
 
  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.
 
  1302  * This function is equivalent to pango_script_for_unichar() and the
 
  1303  * two are interchangeable.
 
  1305  * Return value: the #GUnicodeScript for the character.
 
  1309 EXPORT_C GUnicodeScript
 
  1310 g_unichar_get_script (gunichar ch)
 
  1312   if (ch < G_EASY_SCRIPTS_RANGE)
 
  1313     return g_script_easy_table[ch];
 
  1315     return g_unichar_get_script_bsearch (ch); 
 
  1319 #define __G_UNIPROP_C__
 
  1320 #include "galiasdef.c"