malloc-h: New module.
[gnulib.git] / lib / propername.c
blob4acefa5131b8b03e4f5c8d31fcde63637a39f95f
1 /* Localization of proper names.
2 Copyright (C) 2006-2020 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Without this pragma, gcc 4.7.0 20111124 mistakenly suggests that
19 the proper_name function might be candidate for attribute 'const' */
20 #if (__GNUC__ == 4 && 6 <= __GNUC_MINOR__) || 4 < __GNUC__
21 # pragma GCC diagnostic ignored "-Wsuggest-attribute=const"
22 #endif
24 #include <config.h>
26 /* Specification. */
27 #include "propername.h"
29 #include <ctype.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #if HAVE_ICONV
35 # include <iconv.h>
36 #endif
38 #include "trim.h"
39 #include "mbchar.h"
40 #include "mbuiter.h"
41 #include "localcharset.h"
42 #include "c-strcase.h"
43 #include "xstriconv.h"
44 #include "xalloc.h"
45 #include "gettext.h"
48 /* Tests whether STRING contains trim (SUB), starting and ending at word
49 boundaries.
50 Here, instead of implementing Unicode Standard Annex #29 for determining
51 word boundaries, we assume that trim (SUB) starts and ends with words and
52 only test whether the part before it ends with a non-word and the part
53 after it starts with a non-word. */
54 static bool
55 mbsstr_trimmed_wordbounded (const char *string, const char *sub)
57 char *tsub = trim (sub);
58 bool found = false;
60 for (; *string != '\0';)
62 const char *tsub_in_string = mbsstr (string, tsub);
63 if (tsub_in_string == NULL)
64 break;
65 else
67 if (MB_CUR_MAX > 1)
69 mbui_iterator_t string_iter;
70 bool word_boundary_before;
71 bool word_boundary_after;
73 mbui_init (string_iter, string);
74 word_boundary_before = true;
75 if (mbui_cur_ptr (string_iter) < tsub_in_string)
77 mbchar_t last_char_before_tsub;
80 if (!mbui_avail (string_iter))
81 abort ();
82 last_char_before_tsub = mbui_cur (string_iter);
83 mbui_advance (string_iter);
85 while (mbui_cur_ptr (string_iter) < tsub_in_string);
86 if (mb_isalnum (last_char_before_tsub))
87 word_boundary_before = false;
90 mbui_init (string_iter, tsub_in_string);
92 mbui_iterator_t tsub_iter;
94 for (mbui_init (tsub_iter, tsub);
95 mbui_avail (tsub_iter);
96 mbui_advance (tsub_iter))
98 if (!mbui_avail (string_iter))
99 abort ();
100 mbui_advance (string_iter);
103 word_boundary_after = true;
104 if (mbui_avail (string_iter))
106 mbchar_t first_char_after_tsub = mbui_cur (string_iter);
107 if (mb_isalnum (first_char_after_tsub))
108 word_boundary_after = false;
111 if (word_boundary_before && word_boundary_after)
113 found = true;
114 break;
117 mbui_init (string_iter, tsub_in_string);
118 if (!mbui_avail (string_iter))
119 break;
120 string = tsub_in_string + mb_len (mbui_cur (string_iter));
122 else
124 bool word_boundary_before;
125 const char *p;
126 bool word_boundary_after;
128 word_boundary_before = true;
129 if (string < tsub_in_string)
130 if (isalnum ((unsigned char) tsub_in_string[-1]))
131 word_boundary_before = false;
133 p = tsub_in_string + strlen (tsub);
134 word_boundary_after = true;
135 if (*p != '\0')
136 if (isalnum ((unsigned char) *p))
137 word_boundary_after = false;
139 if (word_boundary_before && word_boundary_after)
141 found = true;
142 break;
145 if (*tsub_in_string == '\0')
146 break;
147 string = tsub_in_string + 1;
151 free (tsub);
152 return found;
155 /* Return the localization of NAME. NAME is written in ASCII. */
157 const char *
158 proper_name (const char *name)
160 /* See whether there is a translation. */
161 const char *translation = gettext (name);
163 if (translation != name)
165 /* See whether the translation contains the original name. */
166 if (mbsstr_trimmed_wordbounded (translation, name))
167 return translation;
168 else
170 /* Return "TRANSLATION (NAME)". */
171 char *result =
172 XNMALLOC (strlen (translation) + 2 + strlen (name) + 1 + 1, char);
174 sprintf (result, "%s (%s)", translation, name);
175 return result;
178 else
179 return name;
182 /* Return the localization of a name whose original writing is not ASCII.
183 NAME_UTF8 is the real name, written in UTF-8 with octal or hexadecimal
184 escape sequences. NAME_ASCII is a fallback written only with ASCII
185 characters. */
187 const char *
188 proper_name_utf8 (const char *name_ascii, const char *name_utf8)
190 /* See whether there is a translation. */
191 const char *translation = gettext (name_ascii);
193 /* Try to convert NAME_UTF8 to the locale encoding. */
194 const char *locale_code = locale_charset ();
195 char *alloc_name_converted = NULL;
196 char *alloc_name_converted_translit = NULL;
197 const char *name_converted = NULL;
198 const char *name_converted_translit = NULL;
199 const char *name;
201 if (c_strcasecmp (locale_code, "UTF-8") != 0)
203 #if HAVE_ICONV
204 name_converted = alloc_name_converted =
205 xstr_iconv (name_utf8, "UTF-8", locale_code);
207 # if (((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2) \
208 && !defined __UCLIBC__) \
209 || _LIBICONV_VERSION >= 0x0105
211 char *converted_translit;
213 size_t len = strlen (locale_code);
214 char *locale_code_translit = XNMALLOC (len + 10 + 1, char);
215 memcpy (locale_code_translit, locale_code, len);
216 memcpy (locale_code_translit + len, "//TRANSLIT", 10 + 1);
218 converted_translit =
219 xstr_iconv (name_utf8, "UTF-8", locale_code_translit);
221 free (locale_code_translit);
223 if (converted_translit != NULL)
225 # if !_LIBICONV_VERSION
226 /* Don't use the transliteration if it added question marks.
227 glibc's transliteration falls back to question marks; libiconv's
228 transliteration does not.
229 mbschr is equivalent to strchr in this case. */
230 if (strchr (converted_translit, '?') != NULL)
231 free (converted_translit);
232 else
233 # endif
234 name_converted_translit = alloc_name_converted_translit =
235 converted_translit;
238 # endif
239 #endif
241 else
243 name_converted = name_utf8;
244 name_converted_translit = name_utf8;
247 /* The name in locale encoding. */
248 name = (name_converted != NULL ? name_converted :
249 name_converted_translit != NULL ? name_converted_translit :
250 name_ascii);
252 /* See whether we have a translation. Some translators have not understood
253 that they should use the UTF-8 form of the name, if possible. So if the
254 translator provided a no-op translation, we ignore it. */
255 if (strcmp (translation, name_ascii) != 0)
257 /* See whether the translation contains the original name. */
258 if (mbsstr_trimmed_wordbounded (translation, name_ascii)
259 || (name_converted != NULL
260 && mbsstr_trimmed_wordbounded (translation, name_converted))
261 || (name_converted_translit != NULL
262 && mbsstr_trimmed_wordbounded (translation, name_converted_translit)))
264 if (alloc_name_converted != NULL)
265 free (alloc_name_converted);
266 if (alloc_name_converted_translit != NULL)
267 free (alloc_name_converted_translit);
268 return translation;
270 else
272 /* Return "TRANSLATION (NAME)". */
273 char *result =
274 XNMALLOC (strlen (translation) + 2 + strlen (name) + 1 + 1, char);
276 sprintf (result, "%s (%s)", translation, name);
278 if (alloc_name_converted != NULL)
279 free (alloc_name_converted);
280 if (alloc_name_converted_translit != NULL)
281 free (alloc_name_converted_translit);
282 return result;
285 else
287 if (alloc_name_converted != NULL && alloc_name_converted != name)
288 free (alloc_name_converted);
289 if (alloc_name_converted_translit != NULL
290 && alloc_name_converted_translit != name)
291 free (alloc_name_converted_translit);
292 return name;
296 #ifdef TEST1
297 # include <locale.h>
299 main (int argc, char *argv[])
301 setlocale (LC_ALL, "");
302 if (mbsstr_trimmed_wordbounded (argv[1], argv[2]))
303 printf("found\n");
304 return 0;
306 #endif
308 #ifdef TEST2
309 # include <locale.h>
310 # include <stdio.h>
312 main (int argc, char *argv[])
314 setlocale (LC_ALL, "");
315 printf ("%s\n", proper_name_utf8 ("Franc,ois Pinard", "Fran\303\247ois Pinard"));
316 return 0;
318 #endif