New module 'unicase/ulc-casecoll'.
[gnulib.git] / lib / unicase / u-casemap.h
1 /* Case mapping for UTF-8/UTF-16/UTF-32 strings (locale dependent).
2    Copyright (C) 2009 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2009.
4
5    This program is free software: you can redistribute it and/or modify it
6    under the terms of the GNU Lesser General Public License as published
7    by the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 /* Quoting the Unicode standard:
19      Definition: A character is defined to be "cased" if it has the Lowercase or
20      Uppercase property or has a General_Category value of Titlecase_Letter.  */
21 static inline bool
22 is_cased (ucs4_t uc)
23 {
24   return (uc_is_property_lowercase (uc)
25           || uc_is_property_uppercase (uc)
26           || uc_is_general_category (uc, UC_TITLECASE_LETTER));
27 }
28
29 /* Quoting the Unicode standard:
30      Definition: A character is defined to be "case-ignorable" if it has the
31      value MidLetter {or the value MidNumLet} for the Word_Break property or
32      its General_Category is one of Nonspacing_Mark (Mn), Enclosing_Mark (Me),
33      Format (Cf), Modifier_Letter (Lm), or Modifier_Symbol (Sk).
34    The text marked in braces was added in Unicode 5.1.0, see
35    <http://www.unicode.org/versions/Unicode5.1.0/> section "Update of
36    Definition of case-ignorable".   */
37 static inline bool
38 is_case_ignorable (ucs4_t uc)
39 {
40   int wbp = uc_wordbreak_property (uc);
41
42   return (wbp == WBP_MIDLETTER || wbp == WBP_MIDNUMLET
43           || uc_is_general_category_withtable (uc, UC_CATEGORY_MASK_Mn
44                                                    | UC_CATEGORY_MASK_Me
45                                                    | UC_CATEGORY_MASK_Cf
46                                                    | UC_CATEGORY_MASK_Lm
47                                                    | UC_CATEGORY_MASK_Sk));
48 }
49
50 UNIT *
51 FUNC (const UNIT *s, size_t n, const char *iso639_language,
52       ucs4_t (*single_character_map) (ucs4_t),
53       size_t offset_in_rule, /* offset in 'struct special_casing_rule' */
54       uninorm_t nf,
55       UNIT *resultbuf, size_t *lengthp)
56 {
57   /* The result being accumulated.  */
58   UNIT *result;
59   size_t length;
60   size_t allocated;
61
62   /* Initialize the accumulator.  */
63   if (nf != NULL || resultbuf == NULL)
64     {
65       result = NULL;
66       allocated = 0;
67     }
68   else
69     {
70       result = resultbuf;
71       allocated = *lengthp;
72     }
73   length = 0;
74
75   {
76     const UNIT *s_end = s + n;
77
78     /* Helper for evaluating the FINAL_SIGMA condition:
79        Last character that was not case-ignorable.  */
80     ucs4_t last_char_except_ignorable = 0xFFFD;
81
82     /* Helper for evaluating the AFTER_SOFT_DOTTED and AFTER_I conditions:
83        Last character that was of combining class 230 ("Above") or 0.  */
84     ucs4_t last_char_normal_or_above = 0xFFFD;
85
86     while (s < s_end)
87       {
88         ucs4_t uc;
89         int count = U_MBTOUC_UNSAFE (&uc, s, s_end - s);
90
91         ucs4_t mapped_uc[3];
92         unsigned int mapped_count;
93
94         if (uc < 0x10000)
95           {
96             /* Look first in the special-casing table.  */
97             char code[3];
98
99             code[0] = (uc >> 8) & 0xff;
100             code[1] = uc & 0xff;
101
102             for (code[2] = 0; ; code[2]++)
103               {
104                 const struct special_casing_rule *rule =
105                   gl_unicase_special_lookup (code, 3);
106
107                 if (rule == NULL)
108                   break;
109
110                 /* Test if the condition applies.  */
111                 /* Does the language apply?  */
112                 if (rule->language[0] == '\0'
113                     || (iso639_language != NULL
114                         && iso639_language[0] == rule->language[0]
115                         && iso639_language[1] == rule->language[1]))
116                   {
117                     /* Does the context apply?  */
118                     int context = rule->context;
119                     bool applies;
120
121                     if (context < 0)
122                       context = - context;
123                     switch (context)
124                       {
125                       case SCC_ALWAYS:
126                         applies = true;
127                         break;
128
129                       case SCC_FINAL_SIGMA:
130                         /* "Before" condition: preceded by a sequence
131                            consisting of a cased letter and a case-ignorable
132                            sequence.
133                            "After" condition: not followed by a sequence
134                            consisting of a case-ignorable sequence and then a
135                            cased letter.  */
136                         /* Test the "before" condition.  */
137                         applies = is_cased (last_char_except_ignorable);
138                         /* Test the "after" condition.  */
139                         if (applies)
140                           {
141                             const UNIT *s2 = s + count;
142                             while (s2 < s_end)
143                               {
144                                 ucs4_t uc2;
145                                 int count2 = U_MBTOUC_UNSAFE (&uc2, s2, s_end - s2);
146                                 if (is_cased (uc2))
147                                   {
148                                     applies = false;
149                                     break;
150                                   }
151                                 if (!is_case_ignorable (uc2))
152                                   break;
153                                 s2 += count2;
154                               }
155                           }
156                         break;
157
158                       case SCC_AFTER_SOFT_DOTTED:
159                         /* "Before" condition: There is a Soft_Dotted character
160                            before it, with no intervening character of
161                            combining class 0 or 230 (Above).  */
162                         /* Test the "before" condition.  */
163                         applies = uc_is_property_soft_dotted (last_char_normal_or_above);
164                         break;
165
166                       case SCC_MORE_ABOVE:
167                         /* "After" condition: followed by a character of
168                            combining class 230 (Above) with no intervening
169                            character of combining class 0 or 230 (Above).  */
170                         /* Test the "after" condition.  */
171                         {
172                           const UNIT *s2 = s + count;
173                           applies = false;
174                           while (s2 < s_end)
175                             {
176                               ucs4_t uc2;
177                               int count2 = U_MBTOUC_UNSAFE (&uc2, s2, s_end - s2);
178                               int ccc = uc_combining_class (uc2);
179                               if (ccc == UC_CCC_A)
180                                 {
181                                   applies = true;
182                                   break;
183                                 }
184                               if (ccc == UC_CCC_NR)
185                                 break;
186                               s2 += count2;
187                             }
188                         }
189                         break;
190
191                       case SCC_BEFORE_DOT:
192                         /* "After" condition: followed by COMBINING DOT ABOVE
193                            (U+0307). Any sequence of characters with a
194                            combining class that is neither 0 nor 230 may
195                            intervene between the current character and the
196                            combining dot above.  */
197                         /* Test the "after" condition.  */
198                         {
199                           const UNIT *s2 = s + count;
200                           applies = false;
201                           while (s2 < s_end)
202                             {
203                               ucs4_t uc2;
204                               int count2 = U_MBTOUC_UNSAFE (&uc2, s2, s_end - s2);
205                               if (uc2 == 0x0307) /* COMBINING DOT ABOVE */
206                                 {
207                                   applies = true;
208                                   break;
209                                 }
210                               {
211                                 int ccc = uc_combining_class (uc2);
212                                 if (ccc == UC_CCC_A || ccc == UC_CCC_NR)
213                                   break;
214                               }
215                               s2 += count2;
216                             }
217                         }
218                         break;
219
220                       case SCC_AFTER_I:
221                         /* "Before" condition: There is an uppercase I before
222                            it, and there is no intervening character of
223                            combining class 0 or 230 (Above).  */
224                         /* Test the "before" condition.  */
225                         applies = (last_char_normal_or_above == 'I');
226                         break;
227
228                       default:
229                         abort ();
230                       }
231                     if (rule->context < 0)
232                       applies = !applies;
233
234                     if (applies)
235                       {
236                         /* The rule applies.
237                            Look up the mapping (0 to 3 characters).  */
238                         const unsigned short *mapped_in_rule =
239                           (const unsigned short *)((const char *)rule + offset_in_rule);
240
241                         if (mapped_in_rule[0] == 0)
242                           mapped_count = 0;
243                         else
244                           {
245                             mapped_uc[0] = mapped_in_rule[0];
246                             if (mapped_in_rule[1] == 0)
247                               mapped_count = 1;
248                             else
249                               {
250                                 mapped_uc[1] = mapped_in_rule[1];
251                                 if (mapped_in_rule[2] == 0)
252                                   mapped_count = 2;
253                                 else
254                                   {
255                                     mapped_uc[2] = mapped_in_rule[2];
256                                     mapped_count = 3;
257                                   }
258                               }
259                           }
260                         goto found_mapping;
261                       }
262                   }
263
264                 /* Optimization: Save a hash table lookup in the next round.  */
265                 if (!rule->has_next)
266                   break;
267               }
268           }
269
270         /* No special-cased mapping.  So use the locale and context independent
271            mapping.  */
272         mapped_uc[0] = single_character_map (uc);
273         mapped_count = 1;
274
275        found_mapping:
276         /* Found the mapping: uc maps to mapped_uc[0..mapped_count-1].  */
277         {
278           unsigned int i;
279
280           for (i = 0; i < mapped_count; i++)
281             {
282               ucs4_t muc = mapped_uc[i];
283
284               /* Append muc to the result accumulator.  */
285               if (length < allocated)
286                 {
287                   int ret = U_UCTOMB (result + length, muc, allocated - length);
288                   if (ret == -1)
289                     {
290                       errno = EINVAL;
291                       goto fail;
292                     }
293                   if (ret >= 0)
294                     {
295                       length += ret;
296                       goto done_appending;
297                     }
298                 }
299               {
300                 size_t old_allocated = allocated;
301                 size_t new_allocated = 2 * old_allocated;
302                 if (new_allocated < 64)
303                   new_allocated = 64;
304                 if (new_allocated < old_allocated) /* integer overflow? */
305                   abort ();
306                 {
307                   UNIT *larger_result;
308                   if (result == NULL)
309                     {
310                       larger_result = (UNIT *) malloc (new_allocated * sizeof (UNIT));
311                       if (larger_result == NULL)
312                         {
313                           errno = ENOMEM;
314                           goto fail;
315                         }
316                     }
317                   else if (result == resultbuf)
318                     {
319                       larger_result = (UNIT *) malloc (new_allocated * sizeof (UNIT));
320                       if (larger_result == NULL)
321                         {
322                           errno = ENOMEM;
323                           goto fail;
324                         }
325                       U_CPY (larger_result, resultbuf, length);
326                     }
327                   else
328                     {
329                       larger_result =
330                         (UNIT *) realloc (result, new_allocated * sizeof (UNIT));
331                       if (larger_result == NULL)
332                         {
333                           errno = ENOMEM;
334                           goto fail;
335                         }
336                     }
337                   result = larger_result;
338                   allocated = new_allocated;
339                   {
340                     int ret = U_UCTOMB (result + length, muc, allocated - length);
341                     if (ret == -1)
342                       {
343                         errno = EINVAL;
344                         goto fail;
345                       }
346                     if (ret < 0)
347                       abort ();
348                     length += ret;
349                     goto done_appending;
350                   }
351                 }
352               }
353              done_appending: ;
354             }
355         }
356
357         if (!is_case_ignorable (uc))
358           last_char_except_ignorable = uc;
359
360         {
361           int ccc = uc_combining_class (uc);
362           if (ccc == UC_CCC_A || ccc == UC_CCC_NR)
363             last_char_normal_or_above = uc;
364         }
365
366         s += count;
367       }
368   }
369
370   if (nf != NULL)
371     {
372       /* Finally, normalize the result.  */
373       UNIT *normalized_result;
374
375       normalized_result = U_NORMALIZE (nf, result, length, resultbuf, lengthp);
376       if (normalized_result == NULL)
377         goto fail;
378
379       free (result);
380       return normalized_result;
381     }
382
383   if (length == 0)
384     {
385       if (result == NULL)
386         {
387           /* Return a non-NULL value.  NULL means error.  */
388           result = (UNIT *) malloc (1);
389           if (result == NULL)
390             {
391               errno = ENOMEM;
392               goto fail;
393             }
394         }
395     }
396   else if (result != resultbuf && length < allocated)
397     {
398       /* Shrink the allocated memory if possible.  */
399       UNIT *memory;
400
401       memory = (UNIT *) realloc (result, length * sizeof (UNIT));
402       if (memory != NULL)
403         result = memory;
404     }
405
406   *lengthp = length;
407   return result;
408
409  fail:
410   if (result != resultbuf)
411     {
412       int saved_errno = errno;
413       free (result);
414       errno = saved_errno;
415     }
416   return NULL;
417 }