This source file includes following definitions.
- mbsstr_trimmed_wordbounded
- proper_name
- proper_name_utf8
- main
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
49
50
51
52
53
54 static bool
55 mbsstr_trimmed_wordbounded (const char *string, const char *sub)
56 {
57 char *tsub = trim (sub);
58 bool found = false;
59
60 for (; *string != '\0';)
61 {
62 const char *tsub_in_string = mbsstr (string, tsub);
63 if (tsub_in_string == NULL)
64 break;
65 else
66 {
67 if (MB_CUR_MAX > 1)
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)
76 {
77 mbchar_t last_char_before_tsub;
78 do
79 {
80 if (!mbui_avail (string_iter))
81 abort ();
82 last_char_before_tsub = mbui_cur (string_iter);
83 mbui_advance (string_iter);
84 }
85 while (mbui_cur_ptr (string_iter) < tsub_in_string);
86 if (mb_isalnum (last_char_before_tsub))
87 word_boundary_before = false;
88 }
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);
96 mbui_advance (tsub_iter))
97 {
98 if (!mbui_avail (string_iter))
99 abort ();
100 mbui_advance (string_iter);
101 }
102 }
103 word_boundary_after = true;
104 if (mbui_avail (string_iter))
105 {
106 mbchar_t first_char_after_tsub = mbui_cur (string_iter);
107 if (mb_isalnum (first_char_after_tsub))
108 word_boundary_after = false;
109 }
110
111 if (word_boundary_before && word_boundary_after)
112 {
113 found = true;
114 break;
115 }
116
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));
121 }
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)
130 if (isalnum ((unsigned char) tsub_in_string[-1]))
131 word_boundary_before = false;
132
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;
138
139 if (word_boundary_before && word_boundary_after)
140 {
141 found = true;
142 break;
143 }
144
145 if (*tsub_in_string == '\0')
146 break;
147 string = tsub_in_string + 1;
148 }
149 }
150 }
151 free (tsub);
152 return found;
153 }
154
155
156
157 const char *
158 proper_name (const char *name)
159 {
160
161 const char *translation = gettext (name);
162
163 if (translation != name)
164 {
165
166 if (mbsstr_trimmed_wordbounded (translation, name))
167 return translation;
168 else
169 {
170
171 char *result =
172 XNMALLOC (strlen (translation) + 2 + strlen (name) + 1 + 1, char);
173
174 sprintf (result, "%s (%s)", translation, name);
175 return result;
176 }
177 }
178 else
179 return name;
180 }
181
182
183
184
185
186
187 const char *
188 proper_name_utf8 (const char *name_ascii, const char *name_utf8)
189 {
190
191 const char *translation = gettext (name_ascii);
192
193
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)
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)
224 {
225 # if !_LIBICONV_VERSION
226
227
228
229
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;
236 }
237 }
238 # endif
239 #endif
240 }
241 else
242 {
243 name_converted = name_utf8;
244 name_converted_translit = name_utf8;
245 }
246
247
248 name = (name_converted != NULL ? name_converted :
249 name_converted_translit != NULL ? name_converted_translit :
250 name_ascii);
251
252
253
254
255 if (strcmp (translation, name_ascii) != 0)
256 {
257
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)))
263 {
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;
269 }
270 else
271 {
272
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)
279 free (alloc_name_converted);
280 if (alloc_name_converted_translit != NULL)
281 free (alloc_name_converted_translit);
282 return result;
283 }
284 }
285 else
286 {
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;
293 }
294 }
295
296 #ifdef TEST1
297 # include <locale.h>
298 int
299 main (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>
311 int
312 main (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