OpenCoverage

propername.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/gnulib/lib/propername.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* Localization of proper names.-
2 Copyright (C) 2006-2018 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 <https://www.gnu.org/licenses/>. */-
17-
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-
23-
24#include <config.h>-
25-
26/* Specification. */-
27#include "propername.h"-
28-
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-
37-
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"-
46-
47-
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. */-
54static bool-
55mbsstr_trimmed_wordbounded (const char *string, const char *sub)-
56{-
57 char *tsub = trim (sub);-
58 bool found = false;-
59-
60 for (; *string != '\0';)
Description
TRUEnever evaluated
FALSEnever evaluated
0
61 {-
62 const char *tsub_in_string = mbsstr (string, tsub);-
63 if (tsub_in_string == NULL)
((void *)0) )Description
TRUEnever evaluated
FALSEnever evaluated
0
64 break;
never executed: se
0
65 else-
66 {-
67 if (MB_CUR_MAX > 1)
> 1) {Description
TRUEnever evaluated
FALSEnever evaluated
0
68 {-
69 mbui_iterator_t string_iter;-
70 bool word_boundary_before;-
71 bool word_boundary_after;-
72-
73 mbui_init (string_iter, string);-
74 word_boundary_before = true;-
75 if (mbui_cur_ptr (string_iter) < tsub_in_string)
Description
TRUEnever evaluated
FALSEnever evaluated
0
76 {-
77 mbchar_t last_char_before_tsub;-
78 do-
79 {-
80 if (!mbui_avail (string_iter))
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
0
81 abort ();
never executed: st_char_b
0
82 last_char_before_tsub = mbui_cur (string_iter);-
83 mbui_advance (string_iter);-
84 }
never executed: i
0
85 while (mbui_cur_ptr (string_iter) < tsub_in_string);
ast_char_befor..._valid && iswaDescription
TRUEnever evaluated
FALSEnever evaluated
0
86 if (mb_isalnum (last_char_before_tsub))
d_boundary_before =Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
0
87 word_boundary_before = false;
never executed: 0 ; }
0
88 }
never executed
0
89-
90 mbui_init (string_iter, tsub_in_string);-
91 {-
92 mbui_iterator_t tsub_iter;-
93-
94 for (mbui_init (tsub_iter, tsub);-
95 mbui_avail (tsub_iter);
r.bytes, (tsub_iter).next_Description
TRUEnever evaluated
FALSEnever evaluated
=Description
TRUEnever evaluated
FALSEnever evaluated
0
96 mbui_advance (tsub_iter))-
97 {-
98 if (!mbui_avail (string_iter))
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
0
99 abort ();
never executed: string_it
0
100 mbui_advance (string_iter);-
101 }
never executed
0
102 }-
103 word_boundary_after = true;-
104 if (mbui_avail (string_iter))
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
0
105 {-
106 mbchar_t first_char_after_tsub = mbui_cur (string_iter);-
107 if (mb_isalnum (first_char_after_tsub))
d_boundary_after =Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
0
108 word_boundary_after = false;
never executed: 0 ; }
0
109 }
never executed
0
110-
111 if (word_boundary_before && word_boundary_after)
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
0
112 {-
113 found = true;-
114 break;
never executed
0
115 }-
116-
117 mbui_init (string_iter, tsub_in_string);-
118 if (!mbui_avail (string_iter))
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
0
119 break;
never executed: ring =
0
120 string = tsub_in_string + mb_len (mbui_cur (string_iter));-
121 }
never executed: s
0
122 else-
123 {-
124 bool word_boundary_before;-
125 const char *p;-
126 bool word_boundary_after;-
127-
128 word_boundary_before = true;-
129 if (string < tsub_in_string)
(Description
TRUEnever evaluated
FALSEnever evaluated
0
130 if (isalnum ((unsigned char) tsub_in_string[-1]))
(unsigned char...int) _ISalnum)Description
TRUEnever evaluated
FALSEnever evaluated
0
131 word_boundary_before = false;
never executed: 0 ;
0
132-
133 p = tsub_in_string + strlen (tsub);-
134 word_boundary_after = true;-
135 if (*p != '\0')
(Description
TRUEnever evaluated
FALSEnever evaluated
0
136 if (isalnum ((unsigned char) *p))
(unsigned char...t) _ISalnum) )Description
TRUEnever evaluated
FALSEnever evaluated
0
137 word_boundary_after = false;
never executed: 0 ;
0
138-
139 if (word_boundary_before && word_boundary_after)
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
0
140 {-
141 found = true;-
142 break;
never executed
0
143 }-
144-
145 if (*tsub_in_string == '\0')
eak;Description
TRUEnever evaluated
FALSEnever evaluated
0
146 break;
never executed: ring =
0
147 string = tsub_in_string + 1;-
148 }
never executed
0
149 }-
150 }-
151 free (tsub);-
152 return found;
never executed
0
153}-
154-
155/* Return the localization of NAME. NAME is written in ASCII. */-
156-
157const char *-
158proper_name (const char *name)-
159{-
160 /* See whether there is a translation. */-
161 const char *translation = gettext (name);-
162-
163 if (translation != name)
Description
TRUEnever evaluated
FALSEnever evaluated
0
164 {-
165 /* See whether the translation contains the original name. */-
166 if (mbsstr_trimmed_wordbounded (translation, name))
turn translation;Description
TRUEnever evaluated
FALSEnever evaluated
0
167 return translation;
never executed: se
0
168 else-
169 {-
170 /* Return "TRANSLATION (NAME)". */-
171 char *result =-
172 XNMALLOC (strlen (translation) + 2 + strlen (name) + 1 + 1, char);-
173-
174 sprintf (result, "%s (%s)", translation, name);-
175 return result;
never executed
0
176 }-
177 }-
178 else-
179 return name;
never executed
0
180}-
181-
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. */-
186-
187const char *-
188proper_name_utf8 (const char *name_ascii, const char *name_utf8)-
189{-
190 /* See whether there is a translation. */-
191 const char *translation = gettext (name_ascii);-
192-
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;-
200-
201 if (c_strcasecmp (locale_code, "UTF-8") != 0)
Description
TRUEevaluated 15 times by 2 tests
Evaluated by:
  • factor
  • ptx
FALSEnever evaluated
0-15
202 {-
203#if HAVE_ICONV-
204 name_converted = alloc_name_converted =-
205 xstr_iconv (name_utf8, "UTF-8", locale_code);-
206-
207# if (((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2) \-
208 && !defined __UCLIBC__) \-
209 || _LIBICONV_VERSION >= 0x0105-
210 {-
211 char *converted_translit;-
212-
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);-
217-
218 converted_translit =-
219 xstr_iconv (name_utf8, "UTF-8", locale_code_translit);-
220-
221 free (locale_code_translit);-
222-
223 if (converted_translit != NULL)
((void *)0) )Description
TRUEevaluated 15 times by 2 tests
Evaluated by:
  • factor
  • ptx
FALSEnever evaluated
0-15
224 {-
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)
'?' ) && !__bu... ((void *)0) )Description
TRUEevaluated 15 times by 2 tests
Evaluated by:
  • factor
  • ptx
FALSEnever evaluated
) && !__builtin_constant_p ( cDescription
TRUEevaluated 15 times by 2 tests
Evaluated by:
  • factor
  • ptx
FALSEnever evaluated
rted_translit ) && ( 'Description
TRUEevaluated 15 times by 2 tests
Evaluated by:
  • factor
  • ptx
FALSEnever evaluated
) == '\0' ? (c...hr ( convertedDescription
TRUEnever evaluated
FALSEevaluated 15 times by 2 tests
Evaluated by:
  • factor
  • ptx
0-15
231 free (converted_translit);
executed 15 times by 2 tests: se
Executed by:
  • factor
  • ptx
15
232 else-
233# endif-
234 name_converted_translit = alloc_name_converted_translit =
never executed: converted_translit; }
0
235 converted_translit;
never executed: converted_translit; }
0
236 }-
237 }-
238# endif-
239#endif-
240 }
executed 15 times by 2 tests: s
Executed by:
  • factor
  • ptx
15
241 else-
242 {-
243 name_converted = name_utf8;-
244 name_converted_translit = name_utf8;-
245 }
never executed
0
246-
247 /* The name in locale encoding. */-
248 name = (name_converted != NULL ? name_converted :
((void *)0) ? nameDescription
TRUEnever evaluated
FALSEevaluated 15 times by 2 tests
Evaluated by:
  • factor
  • ptx
0-15
249 name_converted_translit != NULL ? name_converted_translit :
((void *)0) ? nameDescription
TRUEnever evaluated
FALSEevaluated 15 times by 2 tests
Evaluated by:
  • factor
  • ptx
0-15
250 name_ascii);-
251-
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)
never executed: ))[3] - __s2[3]); } } __result; }))) : (__builtin_constant_p ( name_ascii
never executed
never executed
never executed: ))[3] - __s2[3]); } } __result; }))) : __builtin_strcmp ( translation
never executed
never executed
translation ) ...)); }) != 0) {Description
TRUEnever evaluated
FALSEevaluated 15 times by 2 tests
Evaluated by:
  • factor
  • ptx
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
Description
TRUEnever evaluated
FALSEnever evaluated
0-15
256 {-
257 /* See whether the translation contains the original name. */-
258 if (mbsstr_trimmed_wordbounded (translation, name_ascii)
|| (name_converted !=Description
TRUEnever evaluated
FALSEnever evaluated
0
259 || (name_converted != NULL
((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
260 && mbsstr_trimmed_wordbounded (translation, name_converted))
me_converted_translit !=Description
TRUEnever evaluated
FALSEnever evaluated
0
261 || (name_converted_translit != NULL
((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
262 && mbsstr_trimmed_wordbounded (translation, name_converted_translit)))
Description
TRUEnever evaluated
FALSEnever evaluated
0
263 {-
264 if (alloc_name_converted != NULL)
((void *)0) )Description
TRUEnever evaluated
FALSEnever evaluated
0
265 free (alloc_name_converted);
never executed: (alloc_name_converted_trans
0
266 if (alloc_name_converted_translit != NULL)
((void *)0) )Description
TRUEnever evaluated
FALSEnever evaluated
0
267 free (alloc_name_converted_translit);
never executed: turn translation;
0
268 return translation;
never executed
0
269 }-
270 else-
271 {-
272 /* Return "TRANSLATION (NAME)". */-
273 char *result =-
274 XNMALLOC (strlen (translation) + 2 + strlen (name) + 1 + 1, char);-
275-
276 sprintf (result, "%s (%s)", translation, name);-
277-
278 if (alloc_name_converted != NULL)
((void *)0) )Description
TRUEnever evaluated
FALSEnever evaluated
0
279 free (alloc_name_converted);
never executed: (alloc_name_converted_trans
0
280 if (alloc_name_converted_translit != NULL)
((void *)0) )Description
TRUEnever evaluated
FALSEnever evaluated
0
281 free (alloc_name_converted_translit);
never executed: turn result;
0
282 return result;
never executed
0
283 }-
284 }-
285 else-
286 {-
287 if (alloc_name_converted != NULL && alloc_name_converted != name)
((void *)0) && allDescription
TRUEnever evaluated
FALSEevaluated 15 times by 2 tests
Evaluated by:
  • factor
  • ptx
Description
TRUEnever evaluated
FALSEnever evaluated
0-15
288 free (alloc_name_converted);
never executed: (alloc_name_converted_trans
0
289 if (alloc_name_converted_translit != NULL
((void *)0)Description
TRUEnever evaluated
FALSEevaluated 15 times by 2 tests
Evaluated by:
  • factor
  • ptx
0-15
290 && alloc_name_converted_translit != name)
(alloc_name_co...ted_translit);Description
TRUEnever evaluated
FALSEnever evaluated
0
291 free (alloc_name_converted_translit);
never executed: turn name;
0
292 return name;
executed 15 times by 2 tests
Executed by:
  • factor
  • ptx
15
293 }-
294}-
295-
296#ifdef TEST1-
297# include <locale.h>-
298int-
299main (int argc, char *argv[])-
300{-
301 setlocale (LC_ALL, "");-
302 if (mbsstr_trimmed_wordbounded (argv[1], argv[2]))-
303 printf("found\n");-
304 return 0;-
305}-
306#endif-
307-
308#ifdef TEST2-
309# include <locale.h>-
310# include <stdio.h>-
311int-
312main (int argc, char *argv[])-
313{-
314 setlocale (LC_ALL, "");-
315 printf ("%s\n", proper_name_utf8 ("Franc,ois Pinard", "Fran\303\247ois Pinard"));-
316 return 0;-
317}-
318#endif-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2