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.
25 #include "gunidecomp.h"
30 #define CC(Page, Char) \
31 ((combining_class_table[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
32 ? (combining_class_table[Page] - G_UNICODE_MAX_TABLE_INDEX) \
33 : (cclass_data[combining_class_table[Page]][Char]))
35 #define COMBINING_CLASS(Char) \
36 (((Char) > (G_UNICODE_LAST_CHAR)) ? 0 : CC((Char) >> 8, (Char) & 0xff))
39 * g_unicode_canonical_ordering:
40 * @string: a UCS-4 encoded string.
41 * @len: the maximum length of @string to use.
43 * Computes the canonical ordering of a string in-place.
44 * This rearranges decomposed characters in the string
45 * according to their combining classes. See the Unicode
46 * manual for more information.
49 g_unicode_canonical_ordering (gunichar *string,
59 last = COMBINING_CLASS (string[0]);
60 for (i = 0; i < len - 1; ++i)
62 int next = COMBINING_CLASS (string[i + 1]);
63 if (next != 0 && last > next)
66 /* Percolate item leftward through string. */
67 for (j = i; j > 0; --j)
70 if (COMBINING_CLASS (string[j]) <= next)
73 string[j + 1] = string[j];
77 /* We're re-entering the loop looking at the old
87 find_decomposition (gunichar ch,
91 int end = G_N_ELEMENTS (decomp_table);
93 if (ch >= decomp_table[start].ch &&
94 ch <= decomp_table[end - 1].ch)
98 int half = (start + end) / 2;
99 if (ch == decomp_table[half].ch)
105 offset = decomp_table[half].compat_offset;
107 offset = decomp_table[half].canon_offset;
111 offset = decomp_table[half].canon_offset;
116 return &(decomp_expansion_string[decomp_table[half].expansion_offset + offset]);
118 else if (half == start)
120 else if (ch > decomp_table[half].ch)
131 * g_unicode_canonical_decomposition:
132 * @ch: a Unicode character.
133 * @result_len: location to store the length of the return value.
135 * Computes the canonical decomposition of a Unicode character.
137 * Return value: a newly allocated string of Unicode characters.
138 * @result_len is set to the resulting length of the string.
141 g_unicode_canonical_decomposition (gunichar ch,
144 const guchar *decomp = find_decomposition (ch, FALSE);
151 /* We store as a double-nul terminated string. */
152 for (len = 0; (decomp[len] || decomp[len + 1]);
156 /* We've counted twice as many bytes as there are
158 *result_len = len / 2;
159 r = malloc (len / 2 * sizeof (gunichar));
161 for (i = 0; i < len; i += 2)
163 r[i / 2] = (decomp[i] << 8 | decomp[i + 1]);
168 /* Not in our table. */
169 r = malloc (sizeof (gunichar));
174 /* Supposedly following the Unicode 2.1.9 table means that the
175 decompositions come out in canonical order. I haven't tested
176 this, but we rely on it here. */
180 #define CI(Page, Char) \
181 ((compose_table[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
182 ? (compose_table[Page] - G_UNICODE_MAX_TABLE_INDEX) \
183 : (compose_data[compose_table[Page]][Char]))
185 #define COMPOSE_INDEX(Char) \
186 (((Char) > (G_UNICODE_LAST_CHAR)) ? 0 : CI((Char) >> 8, (Char) & 0xff))
193 gushort index_a, index_b;
195 index_a = COMPOSE_INDEX(a);
196 if (index_a >= COMPOSE_FIRST_SINGLE_START && index_a < COMPOSE_SECOND_START)
198 if (b == compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][0])
200 *result = compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][1];
207 index_b = COMPOSE_INDEX(b);
208 if (index_b >= COMPOSE_SECOND_SINGLE_START)
210 if (a == compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][0])
212 *result = compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][1];
219 if (index_a >= COMPOSE_FIRST_START && index_a < COMPOSE_FIRST_SINGLE_START &&
220 index_b >= COMPOSE_SECOND_START && index_a < COMPOSE_SECOND_SINGLE_START)
222 gunichar res = compose_array[index_a - COMPOSE_FIRST_START][index_b - COMPOSE_SECOND_START];
235 _g_utf8_normalize_wc (const gchar *str,
243 gboolean do_compat = (mode == G_NORMALIZE_NFKC ||
244 mode == G_NORMALIZE_NFKD);
245 gboolean do_compose = (mode == G_NORMALIZE_NFC ||
246 mode == G_NORMALIZE_NFKC);
250 while ((max_len < 0 || p < str + max_len) && *p)
252 gunichar wc = g_utf8_get_char (p);
254 const guchar *decomp = find_decomposition (wc, do_compat);
259 /* We store as a double-nul terminated string. */
260 for (len = 0; (decomp[len] || decomp[len + 1]);
268 p = g_utf8_next_char (p);
271 wc_buffer = g_new (gunichar, n_wc + 1);
276 while ((max_len < 0 || p < str + max_len) && *p)
278 gunichar wc = g_utf8_get_char (p);
279 const guchar *decomp;
281 gsize old_n_wc = n_wc;
283 decomp = find_decomposition (wc, do_compat);
288 /* We store as a double-nul terminated string. */
289 for (len = 0; (decomp[len] || decomp[len + 1]);
291 wc_buffer[n_wc++] = (decomp[len] << 8 | decomp[len + 1]);
294 wc_buffer[n_wc++] = wc;
298 cc = COMBINING_CLASS (wc_buffer[old_n_wc]);
302 g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
303 last_start = old_n_wc;
307 p = g_utf8_next_char (p);
312 g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
318 /* All decomposed and reordered */
321 if (do_compose && n_wc > 0)
327 for (i = 0; i < n_wc; i++)
329 int cc = COMBINING_CLASS (wc_buffer[i]);
332 (last_cc == 0 || last_cc != cc) &&
333 combine (wc_buffer[last_start], wc_buffer[i],
334 &wc_buffer[last_start]))
336 for (j = i + 1; j < n_wc; j++)
337 wc_buffer[j-1] = wc_buffer[j];
344 last_cc = COMBINING_CLASS (wc_buffer[i-1]);
363 * @str: a UTF-8 encoded string.
364 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
365 * @mode: the type of normalization to perform.
367 * Converts a string into canonical form, standardizing
368 * such issues as whether a character with an accent
369 * is represented as a base character and combining
370 * accent or as a single precomposed character. You
371 * should generally call g_utf8_normalize() before
372 * comparing two Unicode strings.
374 * The normalization mode %G_NORMALIZE_DEFAULT only
375 * standardizes differences that do not affect the
376 * text content, such as the above-mentioned accent
377 * representation. %G_NORMALIZE_ALL also standardizes
378 * the "compatibility" characters in Unicode, such
379 * as SUPERSCRIPT THREE to the standard forms
380 * (in this case DIGIT THREE). Formatting information
381 * may be lost but for most text operations such
382 * characters should be considered the same.
383 * For example, g_utf8_collate() normalizes
384 * with %G_NORMALIZE_ALL as its first step.
386 * %G_NORMALIZE_DEFAULT_COMPOSE and %G_NORMALIZE_ALL_COMPOSE
387 * are like %G_NORMALIZE_DEFAULT and %G_NORMALIZE_ALL,
388 * but returned a result with composed forms rather
389 * than a maximally decomposed form. This is often
390 * useful if you intend to convert the string to
391 * a legacy encoding or pass it to a system with
392 * less capable Unicode handling.
394 * Return value: a newly allocated string, that is the
395 * normalized form of @str.
398 g_utf8_normalize (const gchar *str,
402 gunichar *result_wc = _g_utf8_normalize_wc (str, len, mode);
405 result = g_ucs4_to_utf8 (result_wc, -1, NULL, NULL, NULL);