use _GL_ATTRIBUTE_CONST and _GL_ATTRIBUTE_PURE
[gnulib.git] / lib / propername.c
1 /* Localization of proper names.
2    Copyright (C) 2006-2011 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2006.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    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
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include <config.h>
19
20 /* Specification.  */
21 #include "propername.h"
22
23 #include <ctype.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #if HAVE_ICONV
29 # include <iconv.h>
30 #endif
31
32 #include "trim.h"
33 #include "mbchar.h"
34 #include "mbuiter.h"
35 #include "localcharset.h"
36 #include "c-strcase.h"
37 #include "xstriconv.h"
38 #include "xalloc.h"
39 #include "gettext.h"
40
41 /* The attribute __const__ was added in gcc 2.95.  */
42 #undef _GL_ATTRIBUTE_CONST
43 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
44 # define _GL_ATTRIBUTE_CONST __attribute__ ((__const__))
45 #else
46 # define _GL_ATTRIBUTE_CONST /* empty */
47 #endif
48
49
50 /* Tests whether STRING contains trim (SUB), starting and ending at word
51    boundaries.
52    Here, instead of implementing Unicode Standard Annex #29 for determining
53    word boundaries, we assume that trim (SUB) starts and ends with words and
54    only test whether the part before it ends with a non-word and the part
55    after it starts with a non-word.  */
56 static bool
57 mbsstr_trimmed_wordbounded (const char *string, const char *sub)
58 {
59   char *tsub = trim (sub);
60   bool found = false;
61
62   for (; *string != '\0';)
63     {
64       const char *tsub_in_string = mbsstr (string, tsub);
65       if (tsub_in_string == NULL)
66         break;
67       else
68         {
69           if (MB_CUR_MAX > 1)
70             {
71               mbui_iterator_t string_iter;
72               bool word_boundary_before;
73               bool word_boundary_after;
74
75               mbui_init (string_iter, string);
76               word_boundary_before = true;
77               if (mbui_cur_ptr (string_iter) < tsub_in_string)
78                 {
79                   mbchar_t last_char_before_tsub;
80                   do
81                     {
82                       if (!mbui_avail (string_iter))
83                         abort ();
84                       last_char_before_tsub = mbui_cur (string_iter);
85                       mbui_advance (string_iter);
86                     }
87                   while (mbui_cur_ptr (string_iter) < tsub_in_string);
88                   if (mb_isalnum (last_char_before_tsub))
89                     word_boundary_before = false;
90                 }
91
92               mbui_init (string_iter, tsub_in_string);
93               {
94                 mbui_iterator_t tsub_iter;
95
96                 for (mbui_init (tsub_iter, tsub);
97                      mbui_avail (tsub_iter);
98                      mbui_advance (tsub_iter))
99                   {
100                     if (!mbui_avail (string_iter))
101                       abort ();
102                     mbui_advance (string_iter);
103                   }
104               }
105               word_boundary_after = true;
106               if (mbui_avail (string_iter))
107                 {
108                   mbchar_t first_char_after_tsub = mbui_cur (string_iter);
109                   if (mb_isalnum (first_char_after_tsub))
110                     word_boundary_after = false;
111                 }
112
113               if (word_boundary_before && word_boundary_after)
114                 {
115                   found = true;
116                   break;
117                 }
118
119               mbui_init (string_iter, tsub_in_string);
120               if (!mbui_avail (string_iter))
121                 break;
122               string = tsub_in_string + mb_len (mbui_cur (string_iter));
123             }
124           else
125             {
126               bool word_boundary_before;
127               const char *p;
128               bool word_boundary_after;
129
130               word_boundary_before = true;
131               if (string < tsub_in_string)
132                 if (isalnum ((unsigned char) tsub_in_string[-1]))
133                   word_boundary_before = false;
134
135               p = tsub_in_string + strlen (tsub);
136               word_boundary_after = true;
137               if (*p != '\0')
138                 if (isalnum ((unsigned char) *p))
139                   word_boundary_after = false;
140
141               if (word_boundary_before && word_boundary_after)
142                 {
143                   found = true;
144                   break;
145                 }
146
147               if (*tsub_in_string == '\0')
148                 break;
149               string = tsub_in_string + 1;
150             }
151         }
152     }
153   free (tsub);
154   return found;
155 }
156
157 /* Return the localization of NAME.  NAME is written in ASCII.  */
158
159 const char * _GL_ATTRIBUTE_CONST
160 proper_name (const char *name)
161 {
162   /* See whether there is a translation.   */
163   const char *translation = gettext (name);
164
165   if (translation != name)
166     {
167       /* See whether the translation contains the original name.  */
168       if (mbsstr_trimmed_wordbounded (translation, name))
169         return translation;
170       else
171         {
172           /* Return "TRANSLATION (NAME)".  */
173           char *result =
174             XNMALLOC (strlen (translation) + 2 + strlen (name) + 1 + 1, char);
175
176           sprintf (result, "%s (%s)", translation, name);
177           return result;
178         }
179     }
180   else
181     return name;
182 }
183
184 /* Return the localization of a name whose original writing is not ASCII.
185    NAME_UTF8 is the real name, written in UTF-8 with octal or hexadecimal
186    escape sequences.  NAME_ASCII is a fallback written only with ASCII
187    characters.  */
188
189 const char *
190 proper_name_utf8 (const char *name_ascii, const char *name_utf8)
191 {
192   /* See whether there is a translation.   */
193   const char *translation = gettext (name_ascii);
194
195   /* Try to convert NAME_UTF8 to the locale encoding.  */
196   const char *locale_code = locale_charset ();
197   char *alloc_name_converted = NULL;
198   char *alloc_name_converted_translit = NULL;
199   const char *name_converted = NULL;
200   const char *name_converted_translit = NULL;
201   const char *name;
202
203   if (c_strcasecmp (locale_code, "UTF-8") != 0)
204     {
205 #if HAVE_ICONV
206       name_converted = alloc_name_converted =
207         xstr_iconv (name_utf8, "UTF-8", locale_code);
208
209 # if (((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2) \
210       && !defined __UCLIBC__) \
211      || _LIBICONV_VERSION >= 0x0105
212       {
213         char *converted_translit;
214
215         size_t len = strlen (locale_code);
216         char *locale_code_translit = XNMALLOC (len + 10 + 1, char);
217         memcpy (locale_code_translit, locale_code, len);
218         memcpy (locale_code_translit + len, "//TRANSLIT", 10 + 1);
219
220         converted_translit =
221           xstr_iconv (name_utf8, "UTF-8", locale_code_translit);
222
223         free (locale_code_translit);
224
225         if (converted_translit != NULL)
226           {
227 #  if !_LIBICONV_VERSION
228             /* Don't use the transliteration if it added question marks.
229                glibc's transliteration falls back to question marks; libiconv's
230                transliteration does not.
231                mbschr is equivalent to strchr in this case.  */
232             if (strchr (converted_translit, '?') != NULL)
233               free (converted_translit);
234             else
235 #  endif
236               name_converted_translit = alloc_name_converted_translit =
237                 converted_translit;
238           }
239       }
240 # endif
241 #endif
242     }
243   else
244     {
245       name_converted = name_utf8;
246       name_converted_translit = name_utf8;
247     }
248
249   /* The name in locale encoding.  */
250   name = (name_converted != NULL ? name_converted :
251           name_converted_translit != NULL ? name_converted_translit :
252           name_ascii);
253
254   /* See whether we have a translation.  Some translators have not understood
255      that they should use the UTF-8 form of the name, if possible.  So if the
256      translator provided a no-op translation, we ignore it.  */
257   if (strcmp (translation, name_ascii) != 0)
258     {
259       /* See whether the translation contains the original name.  */
260       if (mbsstr_trimmed_wordbounded (translation, name_ascii)
261           || (name_converted != NULL
262               && mbsstr_trimmed_wordbounded (translation, name_converted))
263           || (name_converted_translit != NULL
264               && mbsstr_trimmed_wordbounded (translation, name_converted_translit)))
265         {
266           if (alloc_name_converted != NULL)
267             free (alloc_name_converted);
268           if (alloc_name_converted_translit != NULL)
269             free (alloc_name_converted_translit);
270           return translation;
271         }
272       else
273         {
274           /* Return "TRANSLATION (NAME)".  */
275           char *result =
276             XNMALLOC (strlen (translation) + 2 + strlen (name) + 1 + 1, char);
277
278           sprintf (result, "%s (%s)", translation, name);
279
280           if (alloc_name_converted != NULL)
281             free (alloc_name_converted);
282           if (alloc_name_converted_translit != NULL)
283             free (alloc_name_converted_translit);
284           return result;
285         }
286     }
287   else
288     {
289       if (alloc_name_converted != NULL && alloc_name_converted != name)
290         free (alloc_name_converted);
291       if (alloc_name_converted_translit != NULL
292           && alloc_name_converted_translit != name)
293         free (alloc_name_converted_translit);
294       return name;
295     }
296 }
297
298 #ifdef TEST1
299 # include <locale.h>
300 int
301 main (int argc, char *argv[])
302 {
303   setlocale (LC_ALL, "");
304   if (mbsstr_trimmed_wordbounded (argv[1], argv[2]))
305     printf("found\n");
306   return 0;
307 }
308 #endif
309
310 #ifdef TEST2
311 # include <locale.h>
312 # include <stdio.h>
313 int
314 main (int argc, char *argv[])
315 {
316   setlocale (LC_ALL, "");
317   printf ("%s\n", proper_name_utf8 ("Franc,ois Pinard", "Fran\303\247ois Pinard"));
318   return 0;
319 }
320 #endif