os/ossrv/glib/glib/gunidecomp.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* decomp.c - Character decomposition.
     2  *
     3  *  Copyright (C) 1999, 2000 Tom Tromey
     4  *  Copyright 2000 Red Hat, Inc.
     5  * Portions copyright (c) 2006-2009 Nokia Corporation.  All rights reserved.
     6  *
     7  * The Gnome Library is free software; you can redistribute it and/or
     8  * modify it under the terms of the GNU Lesser General Public License as
     9  * published by the Free Software Foundation; either version 2 of the
    10  * License, or (at your option) any later version.
    11  *
    12  * The Gnome 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 the Gnome Library; see the file COPYING.LIB.  If not,
    19  * write to the 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 
    27 #include "glib.h"
    28 #include "gunidecomp.h"
    29 #include "gunicomp.h"
    30 #include "gunicodeprivate.h"
    31 #include "galias.h"
    32 
    33 
    34 #define CC_PART1(Page, Char) \
    35   ((combining_class_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
    36    ? (combining_class_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
    37    : (cclass_data[combining_class_table_part1[Page]][Char]))
    38 
    39 #define CC_PART2(Page, Char) \
    40   ((combining_class_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
    41    ? (combining_class_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
    42    : (cclass_data[combining_class_table_part2[Page]][Char]))
    43 
    44 #define COMBINING_CLASS(Char) \
    45   (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
    46    ? CC_PART1 ((Char) >> 8, (Char) & 0xff) \
    47    : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
    48       ? CC_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
    49       : 0))
    50 
    51 /**
    52  * g_unichar_combining_class:
    53  * @uc: a Unicode character
    54  * 
    55  * Determines the canonical combining class of a Unicode character.
    56  * 
    57  * Return value: the combining class of the character
    58  *
    59  * Since: 2.14
    60  **/
    61 EXPORT_C gint
    62 g_unichar_combining_class (gunichar uc)
    63 {
    64   return COMBINING_CLASS (uc);
    65 }
    66 
    67 /* constants for hangul syllable [de]composition */
    68 #define SBase 0xAC00 
    69 #define LBase 0x1100 
    70 #define VBase 0x1161 
    71 #define TBase 0x11A7
    72 #define LCount 19 
    73 #define VCount 21
    74 #define TCount 28
    75 #define NCount (VCount * TCount)
    76 #define SCount (LCount * NCount)
    77 
    78 /**
    79  * g_unicode_canonical_ordering:
    80  * @string: a UCS-4 encoded string.
    81  * @len: the maximum length of @string to use.
    82  *
    83  * Computes the canonical ordering of a string in-place.  
    84  * This rearranges decomposed characters in the string 
    85  * according to their combining classes.  See the Unicode 
    86  * manual for more information. 
    87  **/
    88 EXPORT_C void
    89 g_unicode_canonical_ordering (gunichar *string,
    90 			      gsize     len)
    91 {
    92   gsize i;
    93   int swap = 1;
    94 
    95   while (swap)
    96     {
    97       int last;
    98       swap = 0;
    99       last = COMBINING_CLASS (string[0]);
   100       for (i = 0; i < len - 1; ++i)
   101 	{
   102 	  int next = COMBINING_CLASS (string[i + 1]);
   103 	  if (next != 0 && last > next)
   104 	    {
   105 	      gsize j;
   106 	      /* Percolate item leftward through string.  */
   107 	      for (j = i + 1; j > 0; --j)
   108 		{
   109 		  gunichar t;
   110 		  if (COMBINING_CLASS (string[j - 1]) <= next)
   111 		    break;
   112 		  t = string[j];
   113 		  string[j] = string[j - 1];
   114 		  string[j - 1] = t;
   115 		  swap = 1;
   116 		}
   117 	      /* We're re-entering the loop looking at the old
   118 		 character again.  */
   119 	      next = last;
   120 	    }
   121 	  last = next;
   122 	}
   123     }
   124 }
   125 
   126 /* http://www.unicode.org/unicode/reports/tr15/#Hangul
   127  * r should be null or have sufficient space. Calling with r == NULL will
   128  * only calculate the result_len; however, a buffer with space for three
   129  * characters will always be big enough. */
   130 static void
   131 decompose_hangul (gunichar s, 
   132                   gunichar *r,
   133                   gsize *result_len)
   134 {
   135   gint SIndex = s - SBase;
   136 
   137   /* not a hangul syllable */
   138   if (SIndex < 0 || SIndex >= SCount)
   139     {
   140       if (r)
   141         r[0] = s;
   142       *result_len = 1;
   143     }
   144   else
   145     {
   146       gunichar L = LBase + SIndex / NCount;
   147       gunichar V = VBase + (SIndex % NCount) / TCount;
   148       gunichar T = TBase + SIndex % TCount;
   149 
   150       if (r)
   151         {
   152           r[0] = L;
   153           r[1] = V;
   154         }
   155 
   156       if (T != TBase) 
   157         {
   158           if (r)
   159             r[2] = T;
   160           *result_len = 3;
   161         }
   162       else
   163         *result_len = 2;
   164     }
   165 }
   166 
   167 /* returns a pointer to a null-terminated UTF-8 string */
   168 static const gchar *
   169 find_decomposition (gunichar ch,
   170 		    gboolean compat)
   171 {
   172   int start = 0;
   173   int end = G_N_ELEMENTS (decomp_table);
   174   
   175   if (ch >= decomp_table[start].ch &&
   176       ch <= decomp_table[end - 1].ch)
   177     {
   178       while (TRUE)
   179 	{
   180 	  int half = (start + end) / 2;
   181 	  if (ch == decomp_table[half].ch)
   182 	    {
   183 	      int offset;
   184 
   185 	      if (compat)
   186 		{
   187 		  offset = decomp_table[half].compat_offset;
   188 		  if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
   189 		    offset = decomp_table[half].canon_offset;
   190 		}
   191 	      else
   192 		{
   193 		  offset = decomp_table[half].canon_offset;
   194 		  if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
   195 		    return NULL;
   196 		}
   197 	      
   198 	      return &(decomp_expansion_string[offset]);
   199 	    }
   200 	  else if (half == start)
   201 	    break;
   202 	  else if (ch > decomp_table[half].ch)
   203 	    start = half;
   204 	  else
   205 	    end = half;
   206 	}
   207     }
   208 
   209   return NULL;
   210 }
   211 
   212 /**
   213  * g_unicode_canonical_decomposition:
   214  * @ch: a Unicode character.
   215  * @result_len: location to store the length of the return value.
   216  *
   217  * Computes the canonical decomposition of a Unicode character.  
   218  * 
   219  * Return value: a newly allocated string of Unicode characters.
   220  *   @result_len is set to the resulting length of the string.
   221  **/
   222 EXPORT_C gunichar *
   223 g_unicode_canonical_decomposition (gunichar ch,
   224 				   gsize   *result_len)
   225 {
   226   const gchar *decomp;
   227   const gchar *p;
   228   gunichar *r;
   229 
   230   /* Hangul syllable */
   231   if (ch >= 0xac00 && ch <= 0xd7a3)
   232     {
   233       decompose_hangul (ch, NULL, result_len);
   234       r = g_malloc (*result_len * sizeof (gunichar));
   235       decompose_hangul (ch, r, result_len);
   236     }
   237   else if ((decomp = find_decomposition (ch, FALSE)) != NULL)
   238     {
   239       /* Found it.  */
   240       int i;
   241       
   242       *result_len = g_utf8_strlen (decomp, -1);
   243       r = g_malloc (*result_len * sizeof (gunichar));
   244       
   245       for (p = decomp, i = 0; *p != '\0'; p = g_utf8_next_char (p), i++)
   246         r[i] = g_utf8_get_char (p);
   247     }
   248   else
   249     {
   250       /* Not in our table.  */
   251       r = g_malloc (sizeof (gunichar));
   252       *r = ch;
   253       *result_len = 1;
   254     }
   255 
   256   /* Supposedly following the Unicode 2.1.9 table means that the
   257      decompositions come out in canonical order.  I haven't tested
   258      this, but we rely on it here.  */
   259   return r;
   260 }
   261 
   262 /* L,V => LV and LV,T => LVT  */
   263 static gboolean
   264 combine_hangul (gunichar a,
   265                 gunichar b,
   266                 gunichar *result)
   267 {
   268   gint LIndex = a - LBase;
   269   gint SIndex = a - SBase;
   270 
   271   gint VIndex = b - VBase;
   272   gint TIndex = b - TBase;
   273 
   274   if (0 <= LIndex && LIndex < LCount
   275       && 0 <= VIndex && VIndex < VCount)
   276     {
   277       *result = SBase + (LIndex * VCount + VIndex) * TCount;
   278       return TRUE;
   279     }
   280   else if (0 <= SIndex && SIndex < SCount && (SIndex % TCount) == 0
   281            && 0 < TIndex && TIndex < TCount)
   282     {
   283       *result = a + TIndex;
   284       return TRUE;
   285     }
   286 
   287   return FALSE;
   288 }
   289 
   290 #define CI(Page, Char) \
   291   ((compose_table[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
   292    ? (compose_table[Page] - G_UNICODE_MAX_TABLE_INDEX) \
   293    : (compose_data[compose_table[Page]][Char]))
   294 
   295 #define COMPOSE_INDEX(Char) \
   296      (((Char >> 8) > (COMPOSE_TABLE_LAST)) ? 0 : CI((Char) >> 8, (Char) & 0xff))
   297 
   298 static gboolean
   299 combine (gunichar  a,
   300 	 gunichar  b,
   301 	 gunichar *result)
   302 {
   303   gushort index_a, index_b;
   304 
   305   if (combine_hangul (a, b, result))
   306     return TRUE;
   307 
   308   index_a = COMPOSE_INDEX(a);
   309 
   310   if (index_a >= COMPOSE_FIRST_SINGLE_START && index_a < COMPOSE_SECOND_START)
   311     {
   312       if (b == compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][0])
   313 	{
   314 	  *result = compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][1];
   315 	  return TRUE;
   316 	}
   317       else
   318         return FALSE;
   319     }
   320   
   321   index_b = COMPOSE_INDEX(b);
   322 
   323   if (index_b >= COMPOSE_SECOND_SINGLE_START)
   324     {
   325       if (a == compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][0])
   326 	{
   327 	  *result = compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][1];
   328 	  return TRUE;
   329 	}
   330       else
   331         return FALSE;
   332     }
   333 
   334   if (index_a >= COMPOSE_FIRST_START && index_a < COMPOSE_FIRST_SINGLE_START &&
   335       index_b >= COMPOSE_SECOND_START && index_b < COMPOSE_SECOND_SINGLE_START)
   336     {
   337       gunichar res = compose_array[index_a - COMPOSE_FIRST_START][index_b - COMPOSE_SECOND_START];
   338 
   339       if (res)
   340 	{
   341 	  *result = res;
   342 	  return TRUE;
   343 	}
   344     }
   345 
   346   return FALSE;
   347 }
   348 
   349 gunichar *
   350 _g_utf8_normalize_wc (const gchar    *str,
   351 		      gssize          max_len,
   352 		      GNormalizeMode  mode)
   353 {
   354   gsize n_wc;
   355   gunichar *wc_buffer;
   356   const char *p;
   357   gsize last_start;
   358   gboolean do_compat = (mode == G_NORMALIZE_NFKC ||
   359 			mode == G_NORMALIZE_NFKD);
   360   gboolean do_compose = (mode == G_NORMALIZE_NFC ||
   361 			 mode == G_NORMALIZE_NFKC);
   362 
   363   n_wc = 0;
   364   p = str;
   365   while ((max_len < 0 || p < str + max_len) && *p)
   366     {
   367       const gchar *decomp;
   368       gunichar wc = g_utf8_get_char (p);
   369 
   370       if (wc >= 0xac00 && wc <= 0xd7a3)
   371         {
   372           gsize result_len;
   373           decompose_hangul (wc, NULL, &result_len);
   374           n_wc += result_len;
   375         }
   376       else 
   377         {
   378           decomp = find_decomposition (wc, do_compat);
   379 
   380           if (decomp)
   381             n_wc += g_utf8_strlen (decomp, -1);
   382           else
   383             n_wc++;
   384         }
   385 
   386       p = g_utf8_next_char (p);
   387     }
   388 
   389   wc_buffer = g_new (gunichar, n_wc + 1);
   390 
   391   last_start = 0;
   392   n_wc = 0;
   393   p = str;
   394   while ((max_len < 0 || p < str + max_len) && *p)
   395     {
   396       gunichar wc = g_utf8_get_char (p);
   397       const gchar *decomp;
   398       int cc;
   399       gsize old_n_wc = n_wc;
   400 	  
   401       if (wc >= 0xac00 && wc <= 0xd7a3)
   402         {
   403           gsize result_len;
   404           decompose_hangul (wc, wc_buffer + n_wc, &result_len);
   405           n_wc += result_len;
   406         }
   407       else
   408         {
   409           decomp = find_decomposition (wc, do_compat);
   410           
   411           if (decomp)
   412             {
   413               const char *pd;
   414               for (pd = decomp; *pd != '\0'; pd = g_utf8_next_char (pd))
   415                 wc_buffer[n_wc++] = g_utf8_get_char (pd);
   416             }
   417           else
   418             wc_buffer[n_wc++] = wc;
   419         }
   420 
   421       if (n_wc > 0)
   422 	{
   423 	  cc = COMBINING_CLASS (wc_buffer[old_n_wc]);
   424 
   425 	  if (cc == 0)
   426 	    {
   427 	      g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
   428 	      last_start = old_n_wc;
   429 	    }
   430 	}
   431       
   432       p = g_utf8_next_char (p);
   433     }
   434 
   435   if (n_wc > 0)
   436     {
   437       g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
   438       last_start = n_wc;
   439     }
   440 	  
   441   wc_buffer[n_wc] = 0;
   442 
   443   /* All decomposed and reordered */ 
   444 
   445   if (do_compose && n_wc > 0)
   446     {
   447       gsize i, j;
   448       int last_cc = 0;
   449       last_start = 0;
   450       
   451       for (i = 0; i < n_wc; i++)
   452 	{
   453 	  int cc = COMBINING_CLASS (wc_buffer[i]);
   454 
   455 	  if (i > 0 &&
   456 	      (last_cc == 0 || last_cc < cc) &&
   457 	      combine (wc_buffer[last_start], wc_buffer[i],
   458 		       &wc_buffer[last_start]))
   459 	    {
   460 	      for (j = i + 1; j < n_wc; j++)
   461 		wc_buffer[j-1] = wc_buffer[j];
   462 	      n_wc--;
   463 	      i--;
   464 	      
   465 	      if (i == last_start)
   466 		last_cc = 0;
   467 	      else
   468 		last_cc = COMBINING_CLASS (wc_buffer[i-1]);
   469 	      
   470 	      continue;
   471 	    }
   472 
   473 	  if (cc == 0)
   474 	    last_start = i;
   475 
   476 	  last_cc = cc;
   477 	}
   478     }
   479 
   480   wc_buffer[n_wc] = 0;
   481 
   482   return wc_buffer;
   483 }
   484 
   485 /**
   486  * g_utf8_normalize:
   487  * @str: a UTF-8 encoded string.
   488  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
   489  * @mode: the type of normalization to perform.
   490  *
   491  * Converts a string into canonical form, standardizing
   492  * such issues as whether a character with an accent
   493  * is represented as a base character and combining
   494  * accent or as a single precomposed character. The
   495  * string has to be valid UTF-8, otherwise %NULL is
   496  * returned. You should generally call g_utf8_normalize()
   497  * before comparing two Unicode strings.
   498  *
   499  * The normalization mode %G_NORMALIZE_DEFAULT only
   500  * standardizes differences that do not affect the
   501  * text content, such as the above-mentioned accent
   502  * representation. %G_NORMALIZE_ALL also standardizes
   503  * the "compatibility" characters in Unicode, such
   504  * as SUPERSCRIPT THREE to the standard forms
   505  * (in this case DIGIT THREE). Formatting information
   506  * may be lost but for most text operations such
   507  * characters should be considered the same.
   508  *
   509  * %G_NORMALIZE_DEFAULT_COMPOSE and %G_NORMALIZE_ALL_COMPOSE
   510  * are like %G_NORMALIZE_DEFAULT and %G_NORMALIZE_ALL,
   511  * but returned a result with composed forms rather
   512  * than a maximally decomposed form. This is often
   513  * useful if you intend to convert the string to
   514  * a legacy encoding or pass it to a system with
   515  * less capable Unicode handling.
   516  *
   517  * Return value: a newly allocated string, that is the
   518  *   normalized form of @str, or %NULL if @str is not
   519  *   valid UTF-8.
   520  **/
   521 EXPORT_C gchar *
   522 g_utf8_normalize (const gchar    *str,
   523 		  gssize          len,
   524 		  GNormalizeMode  mode)
   525 {
   526   gunichar *result_wc = _g_utf8_normalize_wc (str, len, mode);
   527   gchar *result;
   528 
   529   result = g_ucs4_to_utf8 (result_wc, -1, NULL, NULL, NULL);
   530   g_free (result_wc);
   531 
   532   return result;
   533 }
   534 
   535 #define __G_UNIDECOMP_C__
   536 #include "galiasdef.c"