1 /* decomp.c - Character decomposition.
3 * Copyright (C) 1999, 2000 Tom Tromey
4 * Copyright 2000 Red Hat, Inc.
6 * The Gnome Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * The Gnome Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with the Gnome Library; see the file COPYING.LIB. If not,
18 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 #include "gunidecomp.h"
30 /* We cheat a bit and cast type values to (char *). We detect these
31 using the &0xff trick. */
32 #define CC(Page, Char) \
33 ((((GPOINTER_TO_INT(combining_class_table[Page])) & 0xff) \
34 == GPOINTER_TO_INT(combining_class_table[Page])) \
35 ? GPOINTER_TO_INT(combining_class_table[Page]) \
36 : (combining_class_table[Page][Char]))
38 #define COMBINING_CLASS(Char) \
39 (((Char) > (G_UNICODE_LAST_CHAR)) ? 0 : CC((Char) >> 8, (Char) & 0xff))
41 /* Compute the canonical ordering of a string in-place. */
43 g_unicode_canonical_ordering (gunichar *string,
53 last = COMBINING_CLASS (string[0]);
54 for (i = 0; i < len - 1; ++i)
56 int next = COMBINING_CLASS (string[i + 1]);
57 if (next != 0 && last > next)
60 /* Percolate item leftward through string. */
61 for (j = i; j > 0; --j)
64 if (COMBINING_CLASS (string[j]) <= next)
67 string[j + 1] = string[j];
71 /* We're re-entering the loop looking at the old
81 find_decomposition (gunichar ch,
85 int end = G_N_ELEMENTS (decomp_table);
87 if (ch >= decomp_table[start].ch &&
88 ch <= decomp_table[end - 1].ch)
92 int half = (start + end) / 2;
93 if (ch == decomp_table[half].ch)
99 offset = decomp_table[half].compat_offset;
101 offset = decomp_table[half].canon_offset;
105 offset = decomp_table[half].canon_offset;
110 return decomp_table[half].expansion + offset;
112 else if (half == start)
114 else if (ch > decomp_table[half].ch)
125 g_unicode_canonical_decomposition (gunichar ch,
128 guchar *decomp = find_decomposition (ch, FALSE);
135 /* We store as a double-nul terminated string. */
136 for (len = 0; (decomp[len] || decomp[len + 1]);
140 /* We've counted twice as many bytes as there are
142 *result_len = len / 2;
143 r = malloc (len / 2 * sizeof (gunichar));
145 for (i = 0; i < len; i += 2)
147 r[i / 2] = (decomp[i] << 8 | decomp[i + 1]);
152 /* Not in our table. */
153 r = malloc (sizeof (gunichar));
158 /* Supposedly following the Unicode 2.1.9 table means that the
159 decompositions come out in canonical order. I haven't tested
160 this, but we rely on it here. */
164 #define CI(Page, Char) \
165 ((((GPOINTER_TO_INT(compose_table[Page])) & 0xff) \
166 == GPOINTER_TO_INT(compose_table[Page])) \
167 ? GPOINTER_TO_INT(compose_table[Page]) \
168 : (compose_table[Page][Char]))
170 #define COMPOSE_INDEX(Char) \
171 (((Char) > (G_UNICODE_LAST_CHAR)) ? 0 : CI((Char) >> 8, (Char) & 0xff))
178 gushort index_a, index_b;
180 index_a = COMPOSE_INDEX(a);
181 if (index_a >= COMPOSE_FIRST_SINGLE_START && index_a < COMPOSE_SECOND_START)
183 if (b == compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][0])
185 *result = compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][1];
192 index_b = COMPOSE_INDEX(b);
193 if (index_b >= COMPOSE_SECOND_SINGLE_START)
195 if (a == compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][0])
197 *result = compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][1];
204 if (index_a >= COMPOSE_FIRST_START && index_a < COMPOSE_FIRST_SINGLE_START &&
205 index_b >= COMPOSE_SECOND_START && index_a < COMPOSE_SECOND_SINGLE_START)
207 gunichar res = compose_array[index_a - COMPOSE_FIRST_START][index_b - COMPOSE_SECOND_START];
220 _g_utf8_normalize_wc (const gchar *str,
227 gboolean do_compat = (mode == G_NORMALIZE_NFKC ||
228 mode == G_NORMALIZE_NFKD);
229 gboolean do_compose = (mode == G_NORMALIZE_NFC ||
230 mode == G_NORMALIZE_NFKC);
236 gunichar wc = g_utf8_get_char (p);
238 guchar *decomp = find_decomposition (wc, do_compat);
243 /* We store as a double-nul terminated string. */
244 for (len = 0; (decomp[len] || decomp[len + 1]);
252 p = g_utf8_next_char (p);
255 wc_buffer = g_new (gunichar, n_wc + 1);
262 gunichar wc = g_utf8_get_char (p);
265 size_t old_n_wc = n_wc;
267 decomp = find_decomposition (wc, do_compat);
272 /* We store as a double-nul terminated string. */
273 for (len = 0; (decomp[len] || decomp[len + 1]);
275 wc_buffer[n_wc++] = (decomp[len] << 8 | decomp[len + 1]);
278 wc_buffer[n_wc++] = wc;
282 cc = COMBINING_CLASS (wc_buffer[old_n_wc]);
286 g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
287 last_start = old_n_wc;
291 p = g_utf8_next_char (p);
296 g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
302 /* All decomposed and reordered */
305 if (do_compose && n_wc > 0)
311 for (i = 0; i < n_wc; i++)
313 int cc = COMBINING_CLASS (wc_buffer[i]);
316 (last_cc == 0 || last_cc != cc) &&
317 combine (wc_buffer[last_start], wc_buffer[i],
318 &wc_buffer[last_start]))
320 for (j = i + 1; j < n_wc; j++)
321 wc_buffer[j-1] = wc_buffer[j];
328 last_cc = COMBINING_CLASS (wc_buffer[i-1]);
347 * @str: a UTF-8 encoded string.
348 * @mode: the type of normalization to perform.
350 * Convert a string into canonical form, standardizing
351 * such issues as whether a character with an accent
352 * is represented as a base character and combining
353 * accent or as a single precomposed characters. You
354 * should generally call g_utf8_normalize before
355 * comparing two Unicode strings.
357 * The normalization mode %G_NORMALIZE_DEFAULT only
358 * standardizes differences that do not affect the
359 * text content, such as the above-mentioned accent
360 * representation. %G_NORMALIZE_ALL also standardizes
361 * the "compatibility" characters in Unicode, such
362 * as SUPERSCRIPT THREE to the standard forms
363 * (in this case DIGIT THREE). Formatting information
364 * may be lost but for most text operations such
365 * characters should be considered the same.
366 * For example, g_utf8_collate() normalizes
367 * with %G_NORMALIZE_ALL as its first step.
369 * %G_NORMALIZE_DEFAULT_COMPOSE and %G_NORMALIZE_ALL_COMPOSE
370 * are like %G_NORMALIZE_DEFAULT and %G_NORMALIZE_ALL,
371 * but returned a result with composed forms rather
372 * than a maximally decomposed form. This is often
373 * useful if you intend to convert the string to
374 * a legacy encoding or pass it to a system with
375 * less capable Unicode handling.
377 * Return value: the string in normalized form
380 g_utf8_normalize (const gchar *str,
383 gunichar *result_wc = _g_utf8_normalize_wc (str, mode);
386 result = g_ucs4_to_utf8 (result_wc, -1, NULL, NULL, NULL);