Add SSE4.2 support for strcasecmp and strncasecmp on x86-32
[glibc.git] / intl / l10nflist.c
blobdf91f3decaf91161803fe0837537703e28122220
1 /* Copyright (C) 1995-2002, 2004, 2005, 2011 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 /* Tell glibc's <string.h> to provide a prototype for stpcpy().
21 This must come before <config.h> because <config.h> may include
22 <features.h>, and once <features.h> has been included, it's too late. */
23 #ifndef _GNU_SOURCE
24 # define _GNU_SOURCE 1
25 #endif
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
31 #include <string.h>
33 #if defined _LIBC || defined HAVE_ARGZ_H
34 # include <argz.h>
35 #endif
36 #include <ctype.h>
37 #include <sys/types.h>
38 #include <stdlib.h>
40 #include "loadinfo.h"
42 /* On some strange systems still no definition of NULL is found. Sigh! */
43 #ifndef NULL
44 # if defined __STDC__ && __STDC__
45 # define NULL ((void *) 0)
46 # else
47 # define NULL 0
48 # endif
49 #endif
51 /* @@ end of prolog @@ */
53 #ifdef _LIBC
54 /* Rename the non ANSI C functions. This is required by the standard
55 because some ANSI C functions will require linking with this object
56 file and the name space must not be polluted. */
57 # ifndef stpcpy
58 # define stpcpy(dest, src) __stpcpy(dest, src)
59 # endif
60 #else
61 # ifndef HAVE_STPCPY
62 static char *stpcpy PARAMS ((char *dest, const char *src));
63 # endif
64 #endif
66 /* Define function which are usually not available. */
68 #if !defined _LIBC && !defined HAVE___ARGZ_COUNT
69 /* Returns the number of strings in ARGZ. */
70 static size_t argz_count__ PARAMS ((const char *argz, size_t len));
72 static size_t
73 argz_count__ (argz, len)
74 const char *argz;
75 size_t len;
77 size_t count = 0;
78 while (len > 0)
80 size_t part_len = strlen (argz);
81 argz += part_len + 1;
82 len -= part_len + 1;
83 count++;
85 return count;
87 # undef __argz_count
88 # define __argz_count(argz, len) argz_count__ (argz, len)
89 #else
90 # ifdef _LIBC
91 # define __argz_count(argz, len) INTUSE(__argz_count) (argz, len)
92 # endif
93 #endif /* !_LIBC && !HAVE___ARGZ_COUNT */
95 #if !defined _LIBC && !defined HAVE___ARGZ_STRINGIFY
96 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
97 except the last into the character SEP. */
98 static void argz_stringify__ PARAMS ((char *argz, size_t len, int sep));
100 static void
101 argz_stringify__ (argz, len, sep)
102 char *argz;
103 size_t len;
104 int sep;
106 while (len > 0)
108 size_t part_len = strlen (argz);
109 argz += part_len;
110 len -= part_len + 1;
111 if (len > 0)
112 *argz++ = sep;
115 # undef __argz_stringify
116 # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
117 #else
118 # ifdef _LIBC
119 # define __argz_stringify(argz, len, sep) \
120 INTUSE(__argz_stringify) (argz, len, sep)
121 # endif
122 #endif /* !_LIBC && !HAVE___ARGZ_STRINGIFY */
124 #if !defined _LIBC && !defined HAVE___ARGZ_NEXT
125 static char *argz_next__ PARAMS ((char *argz, size_t argz_len,
126 const char *entry));
128 static char *
129 argz_next__ (argz, argz_len, entry)
130 char *argz;
131 size_t argz_len;
132 const char *entry;
134 if (entry)
136 if (entry < argz + argz_len)
137 entry = strchr (entry, '\0') + 1;
139 return entry >= argz + argz_len ? NULL : (char *) entry;
141 else
142 if (argz_len > 0)
143 return argz;
144 else
145 return 0;
147 # undef __argz_next
148 # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
149 #endif /* !_LIBC && !HAVE___ARGZ_NEXT */
152 /* Return number of bits set in X. */
153 #ifndef ARCH_POP
154 static int pop PARAMS ((int x));
156 static inline int
157 pop (x)
158 int x;
160 /* We assume that no more than 16 bits are used. */
161 x = ((x & ~0x5555) >> 1) + (x & 0x5555);
162 x = ((x & ~0x3333) >> 2) + (x & 0x3333);
163 x = ((x >> 4) + x) & 0x0f0f;
164 x = ((x >> 8) + x) & 0xff;
166 return x;
168 #endif
171 struct loaded_l10nfile *
172 _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
173 territory, codeset, normalized_codeset, modifier,
174 filename, do_allocate)
175 struct loaded_l10nfile **l10nfile_list;
176 const char *dirlist;
177 size_t dirlist_len;
178 int mask;
179 const char *language;
180 const char *territory;
181 const char *codeset;
182 const char *normalized_codeset;
183 const char *modifier;
184 const char *filename;
185 int do_allocate;
187 char *abs_filename;
188 struct loaded_l10nfile *last = NULL;
189 struct loaded_l10nfile *retval;
190 char *cp;
191 size_t entries;
192 int cnt;
194 /* Allocate room for the full file name. */
195 abs_filename = (char *) malloc (dirlist_len
196 + strlen (language)
197 + ((mask & XPG_TERRITORY) != 0
198 ? strlen (territory) + 1 : 0)
199 + ((mask & XPG_CODESET) != 0
200 ? strlen (codeset) + 1 : 0)
201 + ((mask & XPG_NORM_CODESET) != 0
202 ? strlen (normalized_codeset) + 1 : 0)
203 + ((mask & XPG_MODIFIER) != 0
204 ? strlen (modifier) + 1 : 0)
205 + 1 + strlen (filename) + 1);
207 if (abs_filename == NULL)
208 return NULL;
210 retval = NULL;
211 last = NULL;
213 /* Construct file name. */
214 memcpy (abs_filename, dirlist, dirlist_len);
215 __argz_stringify (abs_filename, dirlist_len, ':');
216 cp = abs_filename + (dirlist_len - 1);
217 *cp++ = '/';
218 cp = stpcpy (cp, language);
220 if ((mask & XPG_TERRITORY) != 0)
222 *cp++ = '_';
223 cp = stpcpy (cp, territory);
225 if ((mask & XPG_CODESET) != 0)
227 *cp++ = '.';
228 cp = stpcpy (cp, codeset);
230 if ((mask & XPG_NORM_CODESET) != 0)
232 *cp++ = '.';
233 cp = stpcpy (cp, normalized_codeset);
235 if ((mask & XPG_MODIFIER) != 0)
237 *cp++ = '@';
238 cp = stpcpy (cp, modifier);
241 *cp++ = '/';
242 stpcpy (cp, filename);
244 /* Look in list of already loaded domains whether it is already
245 available. */
246 last = NULL;
247 for (retval = *l10nfile_list; retval != NULL; retval = retval->next)
248 if (retval->filename != NULL)
250 int compare = strcmp (retval->filename, abs_filename);
251 if (compare == 0)
252 /* We found it! */
253 break;
254 if (compare < 0)
256 /* It's not in the list. */
257 retval = NULL;
258 break;
261 last = retval;
264 if (retval != NULL || do_allocate == 0)
266 free (abs_filename);
267 return retval;
270 retval = (struct loaded_l10nfile *)
271 malloc (sizeof (*retval) + (__argz_count (dirlist, dirlist_len)
272 * (1 << pop (mask))
273 * sizeof (struct loaded_l10nfile *)));
274 if (retval == NULL)
276 free (abs_filename);
277 return NULL;
280 retval->filename = abs_filename;
281 /* If more than one directory is in the list this is a pseudo-entry
282 which just references others. We do not try to load data for it,
283 ever. */
284 retval->decided = (__argz_count (dirlist, dirlist_len) != 1
285 || ((mask & XPG_CODESET) != 0
286 && (mask & XPG_NORM_CODESET) != 0));
287 retval->data = NULL;
289 if (last == NULL)
291 retval->next = *l10nfile_list;
292 *l10nfile_list = retval;
294 else
296 retval->next = last->next;
297 last->next = retval;
300 entries = 0;
301 /* If the DIRLIST is a real list the RETVAL entry corresponds not to
302 a real file. So we have to use the DIRLIST separation mechanism
303 of the inner loop. */
304 cnt = __argz_count (dirlist, dirlist_len) == 1 ? mask - 1 : mask;
305 for (; cnt >= 0; --cnt)
306 if ((cnt & ~mask) == 0)
308 /* Iterate over all elements of the DIRLIST. */
309 char *dir = NULL;
311 while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
312 != NULL)
313 retval->successor[entries++]
314 = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1, cnt,
315 language, territory, codeset,
316 normalized_codeset, modifier, filename, 1);
318 retval->successor[entries] = NULL;
320 return retval;
323 /* Normalize codeset name. There is no standard for the codeset
324 names. Normalization allows the user to use any of the common
325 names. The return value is dynamically allocated and has to be
326 freed by the caller. */
327 const char *
328 _nl_normalize_codeset (codeset, name_len)
329 const char *codeset;
330 size_t name_len;
332 int len = 0;
333 int only_digit = 1;
334 char *retval;
335 char *wp;
336 size_t cnt;
337 #ifdef NOT_IN_libc
338 locale_t locale = newlocale (0, "C", NULL);
339 #else
340 # define locale _nl_C_locobj_ptr
341 #endif
343 for (cnt = 0; cnt < name_len; ++cnt)
344 if (__isalnum_l ((unsigned char) codeset[cnt], locale))
346 ++len;
348 if (! __isdigit_l ((unsigned char) codeset[cnt], locale))
349 only_digit = 0;
352 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
354 if (retval != NULL)
356 wp = retval;
357 if (only_digit)
358 wp = stpcpy (wp, "iso");
360 for (cnt = 0; cnt < name_len; ++cnt)
361 if (__isalpha_l ((unsigned char) codeset[cnt], locale))
362 *wp++ = __tolower_l ((unsigned char) codeset[cnt], locale);
363 else if (__isdigit_l ((unsigned char) codeset[cnt], locale))
364 *wp++ = codeset[cnt];
366 *wp = '\0';
369 return (const char *) retval;
373 /* @@ begin of epilog @@ */
375 /* We don't want libintl.a to depend on any other library. So we
376 avoid the non-standard function stpcpy. In GNU C Library this
377 function is available, though. Also allow the symbol HAVE_STPCPY
378 to be defined. */
379 #if !_LIBC && !HAVE_STPCPY
380 static char *
381 stpcpy (dest, src)
382 char *dest;
383 const char *src;
385 while ((*dest++ = *src++) != '\0')
386 /* Do nothing. */ ;
387 return dest - 1;
389 #endif