Commit | Line | Data |
---|---|---|
98994639 HS |
1 | /* locale.c |
2 | * | |
1129b882 NC |
3 | * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
4 | * 2002, 2003, 2005, 2006, 2007, 2008 by Larry Wall and others | |
98994639 HS |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public | |
7 | * License or the Artistic License, as specified in the README file. | |
8 | * | |
9 | */ | |
10 | ||
11 | /* | |
4ac71550 | 12 | * A Elbereth Gilthoniel, |
cdad3b53 | 13 | * silivren penna míriel |
4ac71550 | 14 | * o menel aglar elenath! |
cdad3b53 | 15 | * Na-chaered palan-díriel |
4ac71550 TC |
16 | * o galadhremmin ennorath, |
17 | * Fanuilos, le linnathon | |
18 | * nef aear, si nef aearon! | |
19 | * | |
20 | * [p.238 of _The Lord of the Rings_, II/i: "Many Meetings"] | |
98994639 HS |
21 | */ |
22 | ||
166f8a29 DM |
23 | /* utility functions for handling locale-specific stuff like what |
24 | * character represents the decimal point. | |
0d071d52 | 25 | * |
7d4bcc4a KW |
26 | * All C programs have an underlying locale. Perl code generally doesn't pay |
27 | * any attention to it except within the scope of a 'use locale'. For most | |
0d071d52 KW |
28 | * categories, it accomplishes this by just using different operations if it is |
29 | * in such scope than if not. However, various libc functions called by Perl | |
30 | * are affected by the LC_NUMERIC category, so there are macros in perl.h that | |
31 | * are used to toggle between the current locale and the C locale depending on | |
a9ad02a8 KW |
32 | * the desired behavior of those functions at the moment. And, LC_MESSAGES is |
33 | * switched to the C locale for outputting the message unless within the scope | |
34 | * of 'use locale'. | |
166f8a29 DM |
35 | */ |
36 | ||
98994639 HS |
37 | #include "EXTERN.h" |
38 | #define PERL_IN_LOCALE_C | |
f7416781 | 39 | #include "perl_langinfo.h" |
98994639 HS |
40 | #include "perl.h" |
41 | ||
a4af207c JH |
42 | #include "reentr.h" |
43 | ||
0dec74cd KW |
44 | #ifdef I_WCHAR |
45 | # include <wchar.h> | |
46 | #endif | |
47 | ||
2fcc0ca9 KW |
48 | /* If the environment says to, we can output debugging information during |
49 | * initialization. This is done before option parsing, and before any thread | |
50 | * creation, so can be a file-level static */ | |
5a4b0634 KW |
51 | #if ! defined(DEBUGGING) || defined(PERL_GLOBAL_STRUCT) |
52 | # define debug_initialization 0 | |
53 | # define DEBUG_INITIALIZATION_set(v) | |
54 | #else | |
2fcc0ca9 | 55 | static bool debug_initialization = FALSE; |
5a4b0634 | 56 | # define DEBUG_INITIALIZATION_set(v) (debug_initialization = v) |
2fcc0ca9 KW |
57 | #endif |
58 | ||
291a84fb KW |
59 | /* strlen() of a literal string constant. We might want this more general, |
60 | * but using it in just this file for now. A problem with more generality is | |
61 | * the compiler warnings about comparing unlike signs */ | |
ff1b739b KW |
62 | #define STRLENs(s) (sizeof("" s "") - 1) |
63 | ||
63e5b0d7 KW |
64 | /* Is the C string input 'name' "C" or "POSIX"? If so, and 'name' is the |
65 | * return of setlocale(), then this is extremely likely to be the C or POSIX | |
66 | * locale. However, the output of setlocale() is documented to be opaque, but | |
67 | * the odds are extremely small that it would return these two strings for some | |
68 | * other locale. Note that VMS in these two locales includes many non-ASCII | |
69 | * characters as controls and punctuation (below are hex bytes): | |
70 | * cntrl: 84-97 9B-9F | |
71 | * punct: A1-A3 A5 A7-AB B0-B3 B5-B7 B9-BD BF-CF D1-DD DF-EF F1-FD | |
72 | * Oddly, none there are listed as alphas, though some represent alphabetics | |
73 | * http://www.nntp.perl.org/group/perl.perl5.porters/2013/02/msg198753.html */ | |
74 | #define isNAME_C_OR_POSIX(name) \ | |
75 | ( (name) != NULL \ | |
76 | && (( *(name) == 'C' && (*(name + 1)) == '\0') \ | |
77 | || strEQ((name), "POSIX"))) | |
78 | ||
8ef6e574 KW |
79 | #ifdef USE_LOCALE |
80 | ||
47280b20 KW |
81 | /* This code keeps a LRU cache of the UTF-8ness of the locales it has so-far |
82 | * looked up. This is in the form of a C string: */ | |
83 | ||
84 | #define UTF8NESS_SEP "\v" | |
85 | #define UTF8NESS_PREFIX "\f" | |
86 | ||
87 | /* So, the string looks like: | |
98994639 | 88 | * |
47280b20 | 89 | * \vC\a0\vPOSIX\a0\vam_ET\a0\vaf_ZA.utf8\a1\ven_US.UTF-8\a1\0 |
98994639 | 90 | * |
47280b20 KW |
91 | * where the digit 0 after the \a indicates that the locale starting just |
92 | * after the preceding \v is not UTF-8, and the digit 1 mean it is. */ | |
93 | ||
94 | STATIC_ASSERT_DECL(STRLENs(UTF8NESS_SEP) == 1); | |
95 | STATIC_ASSERT_DECL(STRLENs(UTF8NESS_PREFIX) == 1); | |
96 | ||
97 | #define C_and_POSIX_utf8ness UTF8NESS_SEP "C" UTF8NESS_PREFIX "0" \ | |
98 | UTF8NESS_SEP "POSIX" UTF8NESS_PREFIX "0" | |
99 | ||
100 | /* The cache is initialized to C_and_POSIX_utf8ness at start up. These are | |
101 | * kept there always. The remining portion of the cache is LRU, with the | |
102 | * oldest looked-up locale at the tail end */ | |
103 | ||
98994639 HS |
104 | STATIC char * |
105 | S_stdize_locale(pTHX_ char *locs) | |
106 | { | |
47280b20 KW |
107 | /* Standardize the locale name from a string returned by 'setlocale', |
108 | * possibly modifying that string. | |
109 | * | |
110 | * The typical return value of setlocale() is either | |
111 | * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL | |
112 | * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL | |
113 | * (the space-separated values represent the various sublocales, | |
114 | * in some unspecified order). This is not handled by this function. | |
115 | * | |
116 | * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n", | |
117 | * which is harmful for further use of the string in setlocale(). This | |
118 | * function removes the trailing new line and everything up through the '=' | |
119 | * */ | |
120 | ||
7452cf6a | 121 | const char * const s = strchr(locs, '='); |
98994639 HS |
122 | bool okay = TRUE; |
123 | ||
7918f24d NC |
124 | PERL_ARGS_ASSERT_STDIZE_LOCALE; |
125 | ||
8772537c AL |
126 | if (s) { |
127 | const char * const t = strchr(s, '.'); | |
98994639 | 128 | okay = FALSE; |
8772537c AL |
129 | if (t) { |
130 | const char * const u = strchr(t, '\n'); | |
131 | if (u && (u[1] == 0)) { | |
132 | const STRLEN len = u - s; | |
133 | Move(s + 1, locs, len, char); | |
134 | locs[len] = 0; | |
135 | okay = TRUE; | |
98994639 HS |
136 | } |
137 | } | |
138 | } | |
139 | ||
140 | if (!okay) | |
141 | Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs); | |
142 | ||
143 | return locs; | |
144 | } | |
145 | ||
e5f10d49 KW |
146 | /* Two parallel arrays; first the locale categories Perl uses on this system; |
147 | * the second array is their names. These arrays are in mostly arbitrary | |
148 | * order. */ | |
149 | ||
150 | const int categories[] = { | |
151 | ||
152 | # ifdef USE_LOCALE_NUMERIC | |
153 | LC_NUMERIC, | |
154 | # endif | |
155 | # ifdef USE_LOCALE_CTYPE | |
156 | LC_CTYPE, | |
157 | # endif | |
158 | # ifdef USE_LOCALE_COLLATE | |
159 | LC_COLLATE, | |
160 | # endif | |
161 | # ifdef USE_LOCALE_TIME | |
162 | LC_TIME, | |
163 | # endif | |
164 | # ifdef USE_LOCALE_MESSAGES | |
165 | LC_MESSAGES, | |
166 | # endif | |
167 | # ifdef USE_LOCALE_MONETARY | |
168 | LC_MONETARY, | |
169 | # endif | |
9821811f KW |
170 | # ifdef USE_LOCALE_ADDRESS |
171 | LC_ADDRESS, | |
172 | # endif | |
173 | # ifdef USE_LOCALE_IDENTIFICATION | |
174 | LC_IDENTIFICATION, | |
175 | # endif | |
176 | # ifdef USE_LOCALE_MEASUREMENT | |
177 | LC_MEASUREMENT, | |
178 | # endif | |
179 | # ifdef USE_LOCALE_PAPER | |
180 | LC_PAPER, | |
181 | # endif | |
182 | # ifdef USE_LOCALE_TELEPHONE | |
183 | LC_TELEPHONE, | |
184 | # endif | |
e5f10d49 KW |
185 | # ifdef LC_ALL |
186 | LC_ALL, | |
187 | # endif | |
188 | -1 /* Placeholder because C doesn't allow a | |
189 | trailing comma, and it would get complicated | |
190 | with all the #ifdef's */ | |
191 | }; | |
192 | ||
193 | /* The top-most real element is LC_ALL */ | |
194 | ||
195 | const char * category_names[] = { | |
196 | ||
197 | # ifdef USE_LOCALE_NUMERIC | |
198 | "LC_NUMERIC", | |
199 | # endif | |
200 | # ifdef USE_LOCALE_CTYPE | |
201 | "LC_CTYPE", | |
202 | # endif | |
203 | # ifdef USE_LOCALE_COLLATE | |
204 | "LC_COLLATE", | |
205 | # endif | |
206 | # ifdef USE_LOCALE_TIME | |
207 | "LC_TIME", | |
208 | # endif | |
209 | # ifdef USE_LOCALE_MESSAGES | |
210 | "LC_MESSAGES", | |
211 | # endif | |
212 | # ifdef USE_LOCALE_MONETARY | |
213 | "LC_MONETARY", | |
214 | # endif | |
9821811f KW |
215 | # ifdef USE_LOCALE_ADDRESS |
216 | "LC_ADDRESS", | |
217 | # endif | |
218 | # ifdef USE_LOCALE_IDENTIFICATION | |
219 | "LC_IDENTIFICATION", | |
220 | # endif | |
221 | # ifdef USE_LOCALE_MEASUREMENT | |
222 | "LC_MEASUREMENT", | |
223 | # endif | |
224 | # ifdef USE_LOCALE_PAPER | |
225 | "LC_PAPER", | |
226 | # endif | |
227 | # ifdef USE_LOCALE_TELEPHONE | |
228 | "LC_TELEPHONE", | |
229 | # endif | |
e5f10d49 KW |
230 | # ifdef LC_ALL |
231 | "LC_ALL", | |
232 | # endif | |
233 | NULL /* Placeholder */ | |
234 | }; | |
235 | ||
236 | # ifdef LC_ALL | |
237 | ||
238 | /* On systems with LC_ALL, it is kept in the highest index position. (-2 | |
239 | * to account for the final unused placeholder element.) */ | |
240 | # define NOMINAL_LC_ALL_INDEX (C_ARRAY_LENGTH(categories) - 2) | |
241 | ||
242 | # else | |
243 | ||
244 | /* On systems without LC_ALL, we pretend it is there, one beyond the real | |
245 | * top element, hence in the unused placeholder element. */ | |
246 | # define NOMINAL_LC_ALL_INDEX (C_ARRAY_LENGTH(categories) - 1) | |
247 | ||
248 | # endif | |
249 | ||
250 | /* Pretending there is an LC_ALL element just above allows us to avoid most | |
251 | * special cases. Most loops through these arrays in the code below are | |
252 | * written like 'for (i = 0; i < NOMINAL_LC_ALL_INDEX; i++)'. They will work | |
253 | * on either type of system. But the code must be written to not access the | |
948523db KW |
254 | * element at 'LC_ALL_INDEX' except on platforms that have it. This can be |
255 | * checked for at compile time by using the #define LC_ALL_INDEX which is only | |
256 | * defined if we do have LC_ALL. */ | |
e5f10d49 | 257 | |
b09aaf40 KW |
258 | STATIC const char * |
259 | S_category_name(const int category) | |
260 | { | |
261 | unsigned int i; | |
262 | ||
263 | #ifdef LC_ALL | |
264 | ||
265 | if (category == LC_ALL) { | |
266 | return "LC_ALL"; | |
267 | } | |
268 | ||
269 | #endif | |
270 | ||
271 | for (i = 0; i < NOMINAL_LC_ALL_INDEX; i++) { | |
272 | if (category == categories[i]) { | |
273 | return category_names[i]; | |
274 | } | |
275 | } | |
276 | ||
277 | { | |
278 | const char suffix[] = " (unknown)"; | |
279 | int temp = category; | |
280 | Size_t length = sizeof(suffix) + 1; | |
281 | char * unknown; | |
282 | dTHX; | |
283 | ||
284 | if (temp < 0) { | |
285 | length++; | |
286 | temp = - temp; | |
287 | } | |
288 | ||
289 | /* Calculate the number of digits */ | |
290 | while (temp >= 10) { | |
291 | temp /= 10; | |
292 | length++; | |
293 | } | |
294 | ||
295 | Newx(unknown, length, char); | |
296 | my_snprintf(unknown, length, "%d%s", category, suffix); | |
297 | SAVEFREEPV(unknown); | |
298 | return unknown; | |
299 | } | |
300 | } | |
301 | ||
948523db KW |
302 | /* Now create LC_foo_INDEX #defines for just those categories on this system */ |
303 | # ifdef USE_LOCALE_NUMERIC | |
304 | # define LC_NUMERIC_INDEX 0 | |
305 | # define _DUMMY_NUMERIC LC_NUMERIC_INDEX | |
306 | # else | |
307 | # define _DUMMY_NUMERIC -1 | |
308 | # endif | |
309 | # ifdef USE_LOCALE_CTYPE | |
310 | # define LC_CTYPE_INDEX _DUMMY_NUMERIC + 1 | |
311 | # define _DUMMY_CTYPE LC_CTYPE_INDEX | |
312 | # else | |
313 | # define _DUMMY_CTYPE _DUMMY_NUMERIC | |
314 | # endif | |
315 | # ifdef USE_LOCALE_COLLATE | |
316 | # define LC_COLLATE_INDEX _DUMMY_CTYPE + 1 | |
317 | # define _DUMMY_COLLATE LC_COLLATE_INDEX | |
318 | # else | |
319 | # define _DUMMY_COLLATE _DUMMY_COLLATE | |
320 | # endif | |
321 | # ifdef USE_LOCALE_TIME | |
322 | # define LC_TIME_INDEX _DUMMY_COLLATE + 1 | |
323 | # define _DUMMY_TIME LC_TIME_INDEX | |
324 | # else | |
325 | # define _DUMMY_TIME _DUMMY_COLLATE | |
326 | # endif | |
327 | # ifdef USE_LOCALE_MESSAGES | |
328 | # define LC_MESSAGES_INDEX _DUMMY_TIME + 1 | |
329 | # define _DUMMY_MESSAGES LC_MESSAGES_INDEX | |
330 | # else | |
331 | # define _DUMMY_MESSAGES _DUMMY_TIME | |
332 | # endif | |
333 | # ifdef USE_LOCALE_MONETARY | |
334 | # define LC_MONETARY_INDEX _DUMMY_MESSAGES + 1 | |
335 | # define _DUMMY_MONETARY LC_MONETARY_INDEX | |
336 | # else | |
337 | # define _DUMMY_MONETARY _DUMMY_MESSAGES | |
338 | # endif | |
9821811f KW |
339 | # ifdef USE_LOCALE_ADDRESS |
340 | # define LC_ADDRESS_INDEX _DUMMY_MONETARY + 1 | |
341 | # define _DUMMY_ADDRESS LC_ADDRESS_INDEX | |
342 | # else | |
343 | # define _DUMMY_ADDRESS _DUMMY_MONETARY | |
344 | # endif | |
345 | # ifdef USE_LOCALE_IDENTIFICATION | |
346 | # define LC_IDENTIFICATION_INDEX _DUMMY_ADDRESS + 1 | |
347 | # define _DUMMY_IDENTIFICATION LC_IDENTIFICATION_INDEX | |
348 | # else | |
349 | # define _DUMMY_IDENTIFICATION _DUMMY_ADDRESS | |
350 | # endif | |
351 | # ifdef USE_LOCALE_MEASUREMENT | |
352 | # define LC_MEASUREMENT_INDEX _DUMMY_IDENTIFICATION + 1 | |
353 | # define _DUMMY_MEASUREMENT LC_MEASUREMENT_INDEX | |
354 | # else | |
355 | # define _DUMMY_MEASUREMENT _DUMMY_IDENTIFICATION | |
356 | # endif | |
357 | # ifdef USE_LOCALE_PAPER | |
358 | # define LC_PAPER_INDEX _DUMMY_MEASUREMENT + 1 | |
359 | # define _DUMMY_PAPER LC_PAPER_INDEX | |
360 | # else | |
361 | # define _DUMMY_PAPER _DUMMY_MEASUREMENT | |
362 | # endif | |
363 | # ifdef USE_LOCALE_TELEPHONE | |
364 | # define LC_TELEPHONE_INDEX _DUMMY_PAPER + 1 | |
365 | # define _DUMMY_TELEPHONE LC_TELEPHONE_INDEX | |
366 | # else | |
367 | # define _DUMMY_TELEPHONE _DUMMY_PAPER | |
368 | # endif | |
948523db | 369 | # ifdef LC_ALL |
9821811f | 370 | # define LC_ALL_INDEX _DUMMY_TELEPHONE + 1 |
948523db KW |
371 | # endif |
372 | #endif /* ifdef USE_LOCALE */ | |
8ef6e574 | 373 | |
d2b24094 KW |
374 | /* Windows requres a customized base-level setlocale() */ |
375 | # ifdef WIN32 | |
376 | # define my_setlocale(cat, locale) win32_setlocale(cat, locale) | |
377 | # else | |
378 | # define my_setlocale(cat, locale) setlocale(cat, locale) | |
379 | # endif | |
380 | ||
837ce802 KW |
381 | /* Just placeholders for now. "_c" is intended to be called when the category |
382 | * is a constant known at compile time; "_r", not known until run time */ | |
d2b24094 KW |
383 | # define do_setlocale_c(category, locale) my_setlocale(category, locale) |
384 | # define do_setlocale_r(category, locale) my_setlocale(category, locale) | |
837ce802 | 385 | |
a4f00dcc | 386 | STATIC void |
86799d2d | 387 | S_set_numeric_radix(pTHX_ const bool use_locale) |
98994639 | 388 | { |
86799d2d KW |
389 | /* If 'use_locale' is FALSE, set to use a dot for the radix character. If |
390 | * TRUE, use the radix character derived from the current locale */ | |
7d4bcc4a | 391 | |
86799d2d KW |
392 | #if defined(USE_LOCALE_NUMERIC) && ( defined(HAS_LOCALECONV) \ |
393 | || defined(HAS_NL_LANGINFO)) | |
98994639 | 394 | |
87f8e8e7 KW |
395 | const char * radix = (use_locale) |
396 | ? my_nl_langinfo(PERL_RADIXCHAR, FALSE) | |
397 | /* FALSE => already in dest locale */ | |
398 | : "."; | |
2213a3be | 399 | |
87f8e8e7 | 400 | sv_setpv(PL_numeric_radix_sv, radix); |
86799d2d | 401 | |
87f8e8e7 KW |
402 | /* If this is valid UTF-8 that isn't totally ASCII, and we are in |
403 | * a UTF-8 locale, then mark the radix as being in UTF-8 */ | |
404 | if (is_utf8_non_invariant_string((U8 *) SvPVX(PL_numeric_radix_sv), | |
7a393424 | 405 | SvCUR(PL_numeric_radix_sv)) |
87f8e8e7 KW |
406 | && _is_cur_LC_category_utf8(LC_NUMERIC)) |
407 | { | |
408 | SvUTF8_on(PL_numeric_radix_sv); | |
409 | } | |
86799d2d KW |
410 | |
411 | # ifdef DEBUGGING | |
7d4bcc4a | 412 | |
2fcc0ca9 KW |
413 | if (DEBUG_L_TEST || debug_initialization) { |
414 | PerlIO_printf(Perl_debug_log, "Locale radix is '%s', ?UTF-8=%d\n", | |
3ca88433 KW |
415 | SvPVX(PL_numeric_radix_sv), |
416 | cBOOL(SvUTF8(PL_numeric_radix_sv))); | |
2fcc0ca9 | 417 | } |
69014004 | 418 | |
86799d2d KW |
419 | # endif |
420 | #endif /* USE_LOCALE_NUMERIC and can find the radix char */ | |
7d4bcc4a | 421 | |
98994639 HS |
422 | } |
423 | ||
a39edc4c | 424 | |
98994639 | 425 | void |
8772537c | 426 | Perl_new_numeric(pTHX_ const char *newnum) |
98994639 | 427 | { |
7d4bcc4a KW |
428 | |
429 | #ifndef USE_LOCALE_NUMERIC | |
430 | ||
431 | PERL_UNUSED_ARG(newnum); | |
432 | ||
433 | #else | |
0d071d52 | 434 | |
291a84fb | 435 | /* Called after each libc setlocale() call affecting LC_NUMERIC, to tell |
0d071d52 KW |
436 | * core Perl this and that 'newnum' is the name of the new locale. |
437 | * It installs this locale as the current underlying default. | |
438 | * | |
439 | * The default locale and the C locale can be toggled between by use of the | |
5792c642 KW |
440 | * set_numeric_underlying() and set_numeric_standard() functions, which |
441 | * should probably not be called directly, but only via macros like | |
0d071d52 KW |
442 | * SET_NUMERIC_STANDARD() in perl.h. |
443 | * | |
444 | * The toggling is necessary mainly so that a non-dot radix decimal point | |
445 | * character can be output, while allowing internal calculations to use a | |
446 | * dot. | |
447 | * | |
448 | * This sets several interpreter-level variables: | |
bb304765 | 449 | * PL_numeric_name The underlying locale's name: a copy of 'newnum' |
892e6465 | 450 | * PL_numeric_underlying A boolean indicating if the toggled state is such |
7738054c KW |
451 | * that the current locale is the program's underlying |
452 | * locale | |
453 | * PL_numeric_standard An int indicating if the toggled state is such | |
4c68b815 KW |
454 | * that the current locale is the C locale or |
455 | * indistinguishable from the C locale. If non-zero, it | |
456 | * is in C; if > 1, it means it may not be toggled away | |
7738054c | 457 | * from C. |
4c68b815 KW |
458 | * PL_numeric_underlying_is_standard A bool kept by this function |
459 | * indicating that the underlying locale and the standard | |
460 | * C locale are indistinguishable for the purposes of | |
461 | * LC_NUMERIC. This happens when both of the above two | |
462 | * variables are true at the same time. (Toggling is a | |
463 | * no-op under these circumstances.) This variable is | |
464 | * used to avoid having to recalculate. | |
0d071d52 KW |
465 | * Any code changing the locale (outside this file) should use |
466 | * POSIX::setlocale, which calls this function. Therefore this function | |
467 | * should be called directly only from this file and from | |
468 | * POSIX::setlocale() */ | |
469 | ||
b03f34cf | 470 | char *save_newnum; |
98994639 HS |
471 | |
472 | if (! newnum) { | |
43c5f42d NC |
473 | Safefree(PL_numeric_name); |
474 | PL_numeric_name = NULL; | |
98994639 | 475 | PL_numeric_standard = TRUE; |
892e6465 | 476 | PL_numeric_underlying = TRUE; |
4c68b815 | 477 | PL_numeric_underlying_is_standard = TRUE; |
98994639 HS |
478 | return; |
479 | } | |
480 | ||
b03f34cf | 481 | save_newnum = stdize_locale(savepv(newnum)); |
892e6465 | 482 | PL_numeric_underlying = TRUE; |
4c68b815 KW |
483 | PL_numeric_standard = isNAME_C_OR_POSIX(save_newnum); |
484 | ||
485 | /* If its name isn't C nor POSIX, it could still be indistinguishable from | |
486 | * them */ | |
487 | if (! PL_numeric_standard) { | |
488 | PL_numeric_standard = cBOOL(strEQ(".", my_nl_langinfo(PERL_RADIXCHAR, | |
489 | FALSE /* Don't toggle locale */ )) | |
490 | && strEQ("", my_nl_langinfo(PERL_THOUSEP, | |
491 | FALSE))); | |
492 | } | |
abe1abcf | 493 | |
4c68b815 | 494 | /* Save the new name if it isn't the same as the previous one, if any */ |
b03f34cf | 495 | if (! PL_numeric_name || strNE(PL_numeric_name, save_newnum)) { |
98994639 | 496 | Safefree(PL_numeric_name); |
b03f34cf | 497 | PL_numeric_name = save_newnum; |
b03f34cf | 498 | } |
abe1abcf KW |
499 | else { |
500 | Safefree(save_newnum); | |
501 | } | |
4c28b29c | 502 | |
4c68b815 KW |
503 | PL_numeric_underlying_is_standard = PL_numeric_standard; |
504 | ||
7e5377f7 | 505 | # ifdef HAS_POSIX_2008_LOCALE |
e1aa2579 KW |
506 | |
507 | PL_underlying_numeric_obj = newlocale(LC_NUMERIC_MASK, | |
508 | PL_numeric_name, | |
509 | PL_underlying_numeric_obj); | |
510 | ||
511 | #endif | |
512 | ||
4c68b815 KW |
513 | if (DEBUG_L_TEST || debug_initialization) { |
514 | PerlIO_printf(Perl_debug_log, "Called new_numeric with %s, PL_numeric_name=%s\n", newnum, PL_numeric_name); | |
515 | } | |
516 | ||
4c28b29c KW |
517 | /* Keep LC_NUMERIC in the C locale. This is for XS modules, so they don't |
518 | * have to worry about the radix being a non-dot. (Core operations that | |
519 | * need the underlying locale change to it temporarily). */ | |
520 | set_numeric_standard(); | |
521 | ||
98994639 | 522 | #endif /* USE_LOCALE_NUMERIC */ |
7d4bcc4a | 523 | |
98994639 HS |
524 | } |
525 | ||
526 | void | |
527 | Perl_set_numeric_standard(pTHX) | |
528 | { | |
7d4bcc4a | 529 | |
98994639 | 530 | #ifdef USE_LOCALE_NUMERIC |
7d4bcc4a | 531 | |
28c1bf33 KW |
532 | /* Toggle the LC_NUMERIC locale to C. Most code should use the macros like |
533 | * SET_NUMERIC_STANDARD() in perl.h instead of calling this directly. The | |
534 | * macro avoids calling this routine if toggling isn't necessary according | |
535 | * to our records (which could be wrong if some XS code has changed the | |
536 | * locale behind our back) */ | |
0d071d52 | 537 | |
837ce802 | 538 | do_setlocale_c(LC_NUMERIC, "C"); |
a9b8c0d8 | 539 | PL_numeric_standard = TRUE; |
4c68b815 | 540 | PL_numeric_underlying = PL_numeric_underlying_is_standard; |
86799d2d | 541 | set_numeric_radix(0); |
7d4bcc4a KW |
542 | |
543 | # ifdef DEBUGGING | |
544 | ||
2fcc0ca9 KW |
545 | if (DEBUG_L_TEST || debug_initialization) { |
546 | PerlIO_printf(Perl_debug_log, | |
58e4a467 | 547 | "LC_NUMERIC locale now is standard C\n"); |
2fcc0ca9 | 548 | } |
98994639 | 549 | |
7d4bcc4a | 550 | # endif |
98994639 | 551 | #endif /* USE_LOCALE_NUMERIC */ |
7d4bcc4a | 552 | |
98994639 HS |
553 | } |
554 | ||
555 | void | |
5792c642 | 556 | Perl_set_numeric_underlying(pTHX) |
98994639 | 557 | { |
7d4bcc4a | 558 | |
98994639 | 559 | #ifdef USE_LOCALE_NUMERIC |
7d4bcc4a | 560 | |
28c1bf33 | 561 | /* Toggle the LC_NUMERIC locale to the current underlying default. Most |
7d4bcc4a KW |
562 | * code should use the macros like SET_NUMERIC_UNDERLYING() in perl.h |
563 | * instead of calling this directly. The macro avoids calling this routine | |
564 | * if toggling isn't necessary according to our records (which could be | |
565 | * wrong if some XS code has changed the locale behind our back) */ | |
a9b8c0d8 | 566 | |
837ce802 | 567 | do_setlocale_c(LC_NUMERIC, PL_numeric_name); |
4c68b815 | 568 | PL_numeric_standard = PL_numeric_underlying_is_standard; |
892e6465 | 569 | PL_numeric_underlying = TRUE; |
3c62fd46 | 570 | set_numeric_radix(! PL_numeric_standard); |
7d4bcc4a KW |
571 | |
572 | # ifdef DEBUGGING | |
573 | ||
2fcc0ca9 KW |
574 | if (DEBUG_L_TEST || debug_initialization) { |
575 | PerlIO_printf(Perl_debug_log, | |
58e4a467 | 576 | "LC_NUMERIC locale now is %s\n", |
2fcc0ca9 KW |
577 | PL_numeric_name); |
578 | } | |
98994639 | 579 | |
7d4bcc4a | 580 | # endif |
98994639 | 581 | #endif /* USE_LOCALE_NUMERIC */ |
7d4bcc4a | 582 | |
98994639 HS |
583 | } |
584 | ||
585 | /* | |
586 | * Set up for a new ctype locale. | |
587 | */ | |
a4f00dcc KW |
588 | STATIC void |
589 | S_new_ctype(pTHX_ const char *newctype) | |
98994639 | 590 | { |
7d4bcc4a KW |
591 | |
592 | #ifndef USE_LOCALE_CTYPE | |
593 | ||
594 | PERL_ARGS_ASSERT_NEW_CTYPE; | |
595 | PERL_UNUSED_ARG(newctype); | |
596 | PERL_UNUSED_CONTEXT; | |
597 | ||
598 | #else | |
0d071d52 | 599 | |
291a84fb | 600 | /* Called after each libc setlocale() call affecting LC_CTYPE, to tell |
0d071d52 KW |
601 | * core Perl this and that 'newctype' is the name of the new locale. |
602 | * | |
603 | * This function sets up the folding arrays for all 256 bytes, assuming | |
604 | * that tofold() is tolc() since fold case is not a concept in POSIX, | |
605 | * | |
606 | * Any code changing the locale (outside this file) should use | |
607 | * POSIX::setlocale, which calls this function. Therefore this function | |
608 | * should be called directly only from this file and from | |
609 | * POSIX::setlocale() */ | |
610 | ||
27da23d5 | 611 | dVAR; |
68067e4e | 612 | UV i; |
98994639 | 613 | |
8b7358b9 KW |
614 | /* Don't check for problems if we are suppressing the warnings */ |
615 | bool check_for_problems = ckWARN_d(WARN_LOCALE) || UNLIKELY(DEBUG_L_TEST); | |
616 | ||
7918f24d NC |
617 | PERL_ARGS_ASSERT_NEW_CTYPE; |
618 | ||
215c5139 KW |
619 | /* We will replace any bad locale warning with 1) nothing if the new one is |
620 | * ok; or 2) a new warning for the bad new locale */ | |
621 | if (PL_warn_locale) { | |
622 | SvREFCNT_dec_NN(PL_warn_locale); | |
623 | PL_warn_locale = NULL; | |
624 | } | |
625 | ||
c1284011 | 626 | PL_in_utf8_CTYPE_locale = _is_cur_LC_category_utf8(LC_CTYPE); |
31f05a37 KW |
627 | |
628 | /* A UTF-8 locale gets standard rules. But note that code still has to | |
629 | * handle this specially because of the three problematic code points */ | |
630 | if (PL_in_utf8_CTYPE_locale) { | |
631 | Copy(PL_fold_latin1, PL_fold_locale, 256, U8); | |
632 | } | |
8b7358b9 KW |
633 | |
634 | /* We don't populate the other lists if a UTF-8 locale, but do check that | |
635 | * everything works as expected, unless checking turned off */ | |
636 | if (check_for_problems || ! PL_in_utf8_CTYPE_locale) { | |
8c6180a9 KW |
637 | /* Assume enough space for every character being bad. 4 spaces each |
638 | * for the 94 printable characters that are output like "'x' "; and 5 | |
639 | * spaces each for "'\\' ", "'\t' ", and "'\n' "; plus a terminating | |
640 | * NUL */ | |
8b7358b9 | 641 | char bad_chars_list[ (94 * 4) + (3 * 5) + 1 ] = { '\0' }; |
8c6180a9 KW |
642 | bool multi_byte_locale = FALSE; /* Assume is a single-byte locale |
643 | to start */ | |
644 | unsigned int bad_count = 0; /* Count of bad characters */ | |
645 | ||
baa60164 | 646 | for (i = 0; i < 256; i++) { |
8b7358b9 | 647 | if (! PL_in_utf8_CTYPE_locale) { |
5a8a8f21 KW |
648 | if (isupper(i)) |
649 | PL_fold_locale[i] = (U8) tolower(i); | |
650 | else if (islower(i)) | |
651 | PL_fold_locale[i] = (U8) toupper(i); | |
baa60164 KW |
652 | else |
653 | PL_fold_locale[i] = (U8) i; | |
8b7358b9 | 654 | } |
8c6180a9 KW |
655 | |
656 | /* If checking for locale problems, see if the native ASCII-range | |
657 | * printables plus \n and \t are in their expected categories in | |
658 | * the new locale. If not, this could mean big trouble, upending | |
659 | * Perl's and most programs' assumptions, like having a | |
660 | * metacharacter with special meaning become a \w. Fortunately, | |
661 | * it's very rare to find locales that aren't supersets of ASCII | |
662 | * nowadays. It isn't a problem for most controls to be changed | |
663 | * into something else; we check only \n and \t, though perhaps \r | |
664 | * could be an issue as well. */ | |
7d4bcc4a | 665 | if ( check_for_problems |
8c6180a9 KW |
666 | && (isGRAPH_A(i) || isBLANK_A(i) || i == '\n')) |
667 | { | |
8b7358b9 KW |
668 | bool is_bad = FALSE; |
669 | char name[3] = { '\0' }; | |
670 | ||
671 | /* Convert the name into a string */ | |
672 | if (isPRINT_A(i)) { | |
673 | name[0] = i; | |
674 | name[1] = '\0'; | |
675 | } | |
676 | else if (i == '\n') { | |
677 | my_strlcpy(name, "\n", sizeof(name)); | |
678 | } | |
679 | else { | |
680 | my_strlcpy(name, "\t", sizeof(name)); | |
681 | } | |
682 | ||
683 | /* Check each possibe class */ | |
684 | if (UNLIKELY(cBOOL(isalnum(i)) != cBOOL(isALPHANUMERIC_A(i)))) { | |
685 | is_bad = TRUE; | |
686 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
687 | "isalnum('%s') unexpectedly is %d\n", | |
688 | name, cBOOL(isalnum(i)))); | |
689 | } | |
690 | if (UNLIKELY(cBOOL(isalpha(i)) != cBOOL(isALPHA_A(i)))) { | |
691 | is_bad = TRUE; | |
692 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
693 | "isalpha('%s') unexpectedly is %d\n", | |
694 | name, cBOOL(isalpha(i)))); | |
695 | } | |
696 | if (UNLIKELY(cBOOL(isdigit(i)) != cBOOL(isDIGIT_A(i)))) { | |
697 | is_bad = TRUE; | |
698 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
699 | "isdigit('%s') unexpectedly is %d\n", | |
700 | name, cBOOL(isdigit(i)))); | |
701 | } | |
702 | if (UNLIKELY(cBOOL(isgraph(i)) != cBOOL(isGRAPH_A(i)))) { | |
703 | is_bad = TRUE; | |
704 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
705 | "isgraph('%s') unexpectedly is %d\n", | |
706 | name, cBOOL(isgraph(i)))); | |
707 | } | |
708 | if (UNLIKELY(cBOOL(islower(i)) != cBOOL(isLOWER_A(i)))) { | |
709 | is_bad = TRUE; | |
710 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
711 | "islower('%s') unexpectedly is %d\n", | |
712 | name, cBOOL(islower(i)))); | |
713 | } | |
714 | if (UNLIKELY(cBOOL(isprint(i)) != cBOOL(isPRINT_A(i)))) { | |
715 | is_bad = TRUE; | |
716 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
717 | "isprint('%s') unexpectedly is %d\n", | |
718 | name, cBOOL(isprint(i)))); | |
719 | } | |
720 | if (UNLIKELY(cBOOL(ispunct(i)) != cBOOL(isPUNCT_A(i)))) { | |
721 | is_bad = TRUE; | |
722 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
723 | "ispunct('%s') unexpectedly is %d\n", | |
724 | name, cBOOL(ispunct(i)))); | |
725 | } | |
726 | if (UNLIKELY(cBOOL(isspace(i)) != cBOOL(isSPACE_A(i)))) { | |
727 | is_bad = TRUE; | |
728 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
729 | "isspace('%s') unexpectedly is %d\n", | |
730 | name, cBOOL(isspace(i)))); | |
731 | } | |
732 | if (UNLIKELY(cBOOL(isupper(i)) != cBOOL(isUPPER_A(i)))) { | |
733 | is_bad = TRUE; | |
734 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
735 | "isupper('%s') unexpectedly is %d\n", | |
736 | name, cBOOL(isupper(i)))); | |
737 | } | |
738 | if (UNLIKELY(cBOOL(isxdigit(i))!= cBOOL(isXDIGIT_A(i)))) { | |
739 | is_bad = TRUE; | |
740 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
741 | "isxdigit('%s') unexpectedly is %d\n", | |
742 | name, cBOOL(isxdigit(i)))); | |
743 | } | |
744 | if (UNLIKELY(tolower(i) != (int) toLOWER_A(i))) { | |
745 | is_bad = TRUE; | |
746 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
747 | "tolower('%s')=0x%x instead of the expected 0x%x\n", | |
748 | name, tolower(i), (int) toLOWER_A(i))); | |
749 | } | |
750 | if (UNLIKELY(toupper(i) != (int) toUPPER_A(i))) { | |
751 | is_bad = TRUE; | |
752 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
753 | "toupper('%s')=0x%x instead of the expected 0x%x\n", | |
754 | name, toupper(i), (int) toUPPER_A(i))); | |
755 | } | |
756 | if (UNLIKELY((i == '\n' && ! isCNTRL_LC(i)))) { | |
757 | is_bad = TRUE; | |
758 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
759 | "'\\n' (=%02X) is not a control\n", (int) i)); | |
760 | } | |
761 | ||
762 | /* Add to the list; Separate multiple entries with a blank */ | |
763 | if (is_bad) { | |
764 | if (bad_count) { | |
765 | my_strlcat(bad_chars_list, " ", sizeof(bad_chars_list)); | |
8c6180a9 | 766 | } |
8b7358b9 KW |
767 | my_strlcat(bad_chars_list, name, sizeof(bad_chars_list)); |
768 | bad_count++; | |
8c6180a9 KW |
769 | } |
770 | } | |
771 | } | |
772 | ||
7d4bcc4a KW |
773 | # ifdef MB_CUR_MAX |
774 | ||
8c6180a9 | 775 | /* We only handle single-byte locales (outside of UTF-8 ones; so if |
d35fca5f | 776 | * this locale requires more than one byte, there are going to be |
8c6180a9 | 777 | * problems. */ |
9c8a6dc2 KW |
778 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
779 | "%s:%d: check_for_problems=%d, MB_CUR_MAX=%d\n", | |
780 | __FILE__, __LINE__, check_for_problems, (int) MB_CUR_MAX)); | |
781 | ||
8b7358b9 KW |
782 | if ( check_for_problems && MB_CUR_MAX > 1 |
783 | && ! PL_in_utf8_CTYPE_locale | |
ba1a4362 KW |
784 | |
785 | /* Some platforms return MB_CUR_MAX > 1 for even the "C" | |
786 | * locale. Just assume that the implementation for them (plus | |
787 | * for POSIX) is correct and the > 1 value is spurious. (Since | |
788 | * these are specially handled to never be considered UTF-8 | |
789 | * locales, as long as this is the only problem, everything | |
790 | * should work fine */ | |
791 | && strNE(newctype, "C") && strNE(newctype, "POSIX")) | |
792 | { | |
8c6180a9 KW |
793 | multi_byte_locale = TRUE; |
794 | } | |
7d4bcc4a KW |
795 | |
796 | # endif | |
8c6180a9 | 797 | |
8b7358b9 KW |
798 | if (UNLIKELY(bad_count) || UNLIKELY(multi_byte_locale)) { |
799 | if (UNLIKELY(bad_count) && PL_in_utf8_CTYPE_locale) { | |
800 | PL_warn_locale = Perl_newSVpvf(aTHX_ | |
801 | "Locale '%s' contains (at least) the following characters" | |
802 | " which have\nnon-standard meanings: %s\nThe Perl program" | |
803 | " will use the standard meanings", | |
804 | newctype, bad_chars_list); | |
805 | ||
806 | } | |
807 | else { | |
808 | PL_warn_locale = Perl_newSVpvf(aTHX_ | |
8c6180a9 | 809 | "Locale '%s' may not work well.%s%s%s\n", |
780fcc9f | 810 | newctype, |
8c6180a9 KW |
811 | (multi_byte_locale) |
812 | ? " Some characters in it are not recognized by" | |
813 | " Perl." | |
814 | : "", | |
815 | (bad_count) | |
816 | ? "\nThe following characters (and maybe others)" | |
817 | " may not have the same meaning as the Perl" | |
818 | " program expects:\n" | |
819 | : "", | |
820 | (bad_count) | |
821 | ? bad_chars_list | |
822 | : "" | |
823 | ); | |
8b7358b9 KW |
824 | } |
825 | ||
826 | Perl_sv_catpvf(aTHX_ PL_warn_locale, "\n"); | |
827 | ||
cc9eaeb0 | 828 | /* If we are actually in the scope of the locale or are debugging, |
bddebb56 KW |
829 | * output the message now. If not in that scope, we save the |
830 | * message to be output at the first operation using this locale, | |
831 | * if that actually happens. Most programs don't use locales, so | |
832 | * they are immune to bad ones. */ | |
cc9eaeb0 | 833 | if (IN_LC(LC_CTYPE) || UNLIKELY(DEBUG_L_TEST)) { |
780fcc9f | 834 | |
780fcc9f KW |
835 | /* The '0' below suppresses a bogus gcc compiler warning */ |
836 | Perl_warner(aTHX_ packWARN(WARN_LOCALE), SvPVX(PL_warn_locale), 0); | |
bddebb56 | 837 | |
bddebb56 KW |
838 | if (IN_LC(LC_CTYPE)) { |
839 | SvREFCNT_dec_NN(PL_warn_locale); | |
840 | PL_warn_locale = NULL; | |
841 | } | |
780fcc9f | 842 | } |
baa60164 | 843 | } |
31f05a37 | 844 | } |
98994639 HS |
845 | |
846 | #endif /* USE_LOCALE_CTYPE */ | |
7d4bcc4a | 847 | |
98994639 HS |
848 | } |
849 | ||
98994639 | 850 | void |
2726666d KW |
851 | Perl__warn_problematic_locale() |
852 | { | |
2726666d KW |
853 | |
854 | #ifdef USE_LOCALE_CTYPE | |
855 | ||
5f04a188 KW |
856 | dTHX; |
857 | ||
858 | /* Internal-to-core function that outputs the message in PL_warn_locale, | |
859 | * and then NULLS it. Should be called only through the macro | |
860 | * _CHECK_AND_WARN_PROBLEMATIC_LOCALE */ | |
861 | ||
2726666d | 862 | if (PL_warn_locale) { |
2726666d KW |
863 | Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), |
864 | SvPVX(PL_warn_locale), | |
865 | 0 /* dummy to avoid compiler warning */ ); | |
2726666d KW |
866 | SvREFCNT_dec_NN(PL_warn_locale); |
867 | PL_warn_locale = NULL; | |
868 | } | |
869 | ||
870 | #endif | |
871 | ||
872 | } | |
873 | ||
a4f00dcc KW |
874 | STATIC void |
875 | S_new_collate(pTHX_ const char *newcoll) | |
98994639 | 876 | { |
7d4bcc4a KW |
877 | |
878 | #ifndef USE_LOCALE_COLLATE | |
879 | ||
880 | PERL_UNUSED_ARG(newcoll); | |
881 | PERL_UNUSED_CONTEXT; | |
882 | ||
883 | #else | |
0d071d52 | 884 | |
291a84fb | 885 | /* Called after each libc setlocale() call affecting LC_COLLATE, to tell |
0d071d52 KW |
886 | * core Perl this and that 'newcoll' is the name of the new locale. |
887 | * | |
d35fca5f KW |
888 | * The design of locale collation is that every locale change is given an |
889 | * index 'PL_collation_ix'. The first time a string particpates in an | |
890 | * operation that requires collation while locale collation is active, it | |
891 | * is given PERL_MAGIC_collxfrm magic (via sv_collxfrm_flags()). That | |
892 | * magic includes the collation index, and the transformation of the string | |
893 | * by strxfrm(), q.v. That transformation is used when doing comparisons, | |
894 | * instead of the string itself. If a string changes, the magic is | |
895 | * cleared. The next time the locale changes, the index is incremented, | |
896 | * and so we know during a comparison that the transformation is not | |
897 | * necessarily still valid, and so is recomputed. Note that if the locale | |
898 | * changes enough times, the index could wrap (a U32), and it is possible | |
899 | * that a transformation would improperly be considered valid, leading to | |
900 | * an unlikely bug */ | |
0d071d52 | 901 | |
98994639 HS |
902 | if (! newcoll) { |
903 | if (PL_collation_name) { | |
904 | ++PL_collation_ix; | |
905 | Safefree(PL_collation_name); | |
906 | PL_collation_name = NULL; | |
907 | } | |
908 | PL_collation_standard = TRUE; | |
00bf60ca | 909 | is_standard_collation: |
98994639 HS |
910 | PL_collxfrm_base = 0; |
911 | PL_collxfrm_mult = 2; | |
165a1c52 | 912 | PL_in_utf8_COLLATE_locale = FALSE; |
f28f4d2a | 913 | PL_strxfrm_NUL_replacement = '\0'; |
a4a439fb | 914 | PL_strxfrm_max_cp = 0; |
98994639 HS |
915 | return; |
916 | } | |
917 | ||
d35fca5f | 918 | /* If this is not the same locale as currently, set the new one up */ |
98994639 HS |
919 | if (! PL_collation_name || strNE(PL_collation_name, newcoll)) { |
920 | ++PL_collation_ix; | |
921 | Safefree(PL_collation_name); | |
922 | PL_collation_name = stdize_locale(savepv(newcoll)); | |
a39edc4c | 923 | PL_collation_standard = isNAME_C_OR_POSIX(newcoll); |
00bf60ca KW |
924 | if (PL_collation_standard) { |
925 | goto is_standard_collation; | |
926 | } | |
98994639 | 927 | |
165a1c52 | 928 | PL_in_utf8_COLLATE_locale = _is_cur_LC_category_utf8(LC_COLLATE); |
f28f4d2a | 929 | PL_strxfrm_NUL_replacement = '\0'; |
a4a439fb | 930 | PL_strxfrm_max_cp = 0; |
165a1c52 | 931 | |
59c018b9 KW |
932 | /* A locale collation definition includes primary, secondary, tertiary, |
933 | * etc. weights for each character. To sort, the primary weights are | |
934 | * used, and only if they compare equal, then the secondary weights are | |
935 | * used, and only if they compare equal, then the tertiary, etc. | |
936 | * | |
937 | * strxfrm() works by taking the input string, say ABC, and creating an | |
938 | * output transformed string consisting of first the primary weights, | |
939 | * A¹B¹C¹ followed by the secondary ones, A²B²C²; and then the | |
940 | * tertiary, etc, yielding A¹B¹C¹ A²B²C² A³B³C³ .... Some characters | |
941 | * may not have weights at every level. In our example, let's say B | |
942 | * doesn't have a tertiary weight, and A doesn't have a secondary | |
943 | * weight. The constructed string is then going to be | |
944 | * A¹B¹C¹ B²C² A³C³ .... | |
945 | * This has the desired effect that strcmp() will look at the secondary | |
946 | * or tertiary weights only if the strings compare equal at all higher | |
947 | * priority weights. The spaces shown here, like in | |
c342d20e | 948 | * "A¹B¹C¹ A²B²C² " |
59c018b9 KW |
949 | * are not just for readability. In the general case, these must |
950 | * actually be bytes, which we will call here 'separator weights'; and | |
951 | * they must be smaller than any other weight value, but since these | |
952 | * are C strings, only the terminating one can be a NUL (some | |
953 | * implementations may include a non-NUL separator weight just before | |
954 | * the NUL). Implementations tend to reserve 01 for the separator | |
955 | * weights. They are needed so that a shorter string's secondary | |
956 | * weights won't be misconstrued as primary weights of a longer string, | |
957 | * etc. By making them smaller than any other weight, the shorter | |
958 | * string will sort first. (Actually, if all secondary weights are | |
959 | * smaller than all primary ones, there is no need for a separator | |
960 | * weight between those two levels, etc.) | |
961 | * | |
962 | * The length of the transformed string is roughly a linear function of | |
963 | * the input string. It's not exactly linear because some characters | |
964 | * don't have weights at all levels. When we call strxfrm() we have to | |
965 | * allocate some memory to hold the transformed string. The | |
966 | * calculations below try to find coefficients 'm' and 'b' for this | |
967 | * locale so that m*x + b equals how much space we need, given the size | |
968 | * of the input string in 'x'. If we calculate too small, we increase | |
969 | * the size as needed, and call strxfrm() again, but it is better to | |
970 | * get it right the first time to avoid wasted expensive string | |
971 | * transformations. */ | |
972 | ||
98994639 | 973 | { |
79f120c8 KW |
974 | /* We use the string below to find how long the tranformation of it |
975 | * is. Almost all locales are supersets of ASCII, or at least the | |
976 | * ASCII letters. We use all of them, half upper half lower, | |
977 | * because if we used fewer, we might hit just the ones that are | |
978 | * outliers in a particular locale. Most of the strings being | |
979 | * collated will contain a preponderance of letters, and even if | |
980 | * they are above-ASCII, they are likely to have the same number of | |
981 | * weight levels as the ASCII ones. It turns out that digits tend | |
982 | * to have fewer levels, and some punctuation has more, but those | |
983 | * are relatively sparse in text, and khw believes this gives a | |
984 | * reasonable result, but it could be changed if experience so | |
985 | * dictates. */ | |
986 | const char longer[] = "ABCDEFGHIJKLMnopqrstuvwxyz"; | |
987 | char * x_longer; /* Transformed 'longer' */ | |
988 | Size_t x_len_longer; /* Length of 'x_longer' */ | |
989 | ||
990 | char * x_shorter; /* We also transform a substring of 'longer' */ | |
991 | Size_t x_len_shorter; | |
992 | ||
a4a439fb | 993 | /* _mem_collxfrm() is used get the transformation (though here we |
79f120c8 KW |
994 | * are interested only in its length). It is used because it has |
995 | * the intelligence to handle all cases, but to work, it needs some | |
996 | * values of 'm' and 'b' to get it started. For the purposes of | |
997 | * this calculation we use a very conservative estimate of 'm' and | |
998 | * 'b'. This assumes a weight can be multiple bytes, enough to | |
999 | * hold any UV on the platform, and there are 5 levels, 4 weight | |
1000 | * bytes, and a trailing NUL. */ | |
1001 | PL_collxfrm_base = 5; | |
1002 | PL_collxfrm_mult = 5 * sizeof(UV); | |
1003 | ||
1004 | /* Find out how long the transformation really is */ | |
a4a439fb KW |
1005 | x_longer = _mem_collxfrm(longer, |
1006 | sizeof(longer) - 1, | |
1007 | &x_len_longer, | |
1008 | ||
1009 | /* We avoid converting to UTF-8 in the | |
1010 | * called function by telling it the | |
1011 | * string is in UTF-8 if the locale is a | |
1012 | * UTF-8 one. Since the string passed | |
1013 | * here is invariant under UTF-8, we can | |
1014 | * claim it's UTF-8 even though it isn't. | |
1015 | * */ | |
1016 | PL_in_utf8_COLLATE_locale); | |
79f120c8 KW |
1017 | Safefree(x_longer); |
1018 | ||
1019 | /* Find out how long the transformation of a substring of 'longer' | |
1020 | * is. Together the lengths of these transformations are | |
1021 | * sufficient to calculate 'm' and 'b'. The substring is all of | |
1022 | * 'longer' except the first character. This minimizes the chances | |
1023 | * of being swayed by outliers */ | |
a4a439fb | 1024 | x_shorter = _mem_collxfrm(longer + 1, |
79f120c8 | 1025 | sizeof(longer) - 2, |
a4a439fb KW |
1026 | &x_len_shorter, |
1027 | PL_in_utf8_COLLATE_locale); | |
79f120c8 KW |
1028 | Safefree(x_shorter); |
1029 | ||
1030 | /* If the results are nonsensical for this simple test, the whole | |
1031 | * locale definition is suspect. Mark it so that locale collation | |
1032 | * is not active at all for it. XXX Should we warn? */ | |
1033 | if ( x_len_shorter == 0 | |
1034 | || x_len_longer == 0 | |
1035 | || x_len_shorter >= x_len_longer) | |
1036 | { | |
1037 | PL_collxfrm_mult = 0; | |
1038 | PL_collxfrm_base = 0; | |
1039 | } | |
1040 | else { | |
1041 | SSize_t base; /* Temporary */ | |
1042 | ||
1043 | /* We have both: m * strlen(longer) + b = x_len_longer | |
1044 | * m * strlen(shorter) + b = x_len_shorter; | |
1045 | * subtracting yields: | |
1046 | * m * (strlen(longer) - strlen(shorter)) | |
1047 | * = x_len_longer - x_len_shorter | |
1048 | * But we have set things up so that 'shorter' is 1 byte smaller | |
1049 | * than 'longer'. Hence: | |
1050 | * m = x_len_longer - x_len_shorter | |
1051 | * | |
1052 | * But if something went wrong, make sure the multiplier is at | |
1053 | * least 1. | |
1054 | */ | |
1055 | if (x_len_longer > x_len_shorter) { | |
1056 | PL_collxfrm_mult = (STRLEN) x_len_longer - x_len_shorter; | |
1057 | } | |
1058 | else { | |
1059 | PL_collxfrm_mult = 1; | |
1060 | } | |
1061 | ||
1062 | /* mx + b = len | |
1063 | * so: b = len - mx | |
1064 | * but in case something has gone wrong, make sure it is | |
1065 | * non-negative */ | |
1066 | base = x_len_longer - PL_collxfrm_mult * (sizeof(longer) - 1); | |
1067 | if (base < 0) { | |
1068 | base = 0; | |
1069 | } | |
1070 | ||
1071 | /* Add 1 for the trailing NUL */ | |
1072 | PL_collxfrm_base = base + 1; | |
1073 | } | |
58eebef2 | 1074 | |
7d4bcc4a KW |
1075 | # ifdef DEBUGGING |
1076 | ||
58eebef2 KW |
1077 | if (DEBUG_L_TEST || debug_initialization) { |
1078 | PerlIO_printf(Perl_debug_log, | |
b07929e4 KW |
1079 | "%s:%d: ?UTF-8 locale=%d; x_len_shorter=%zu, " |
1080 | "x_len_longer=%zu," | |
1081 | " collate multipler=%zu, collate base=%zu\n", | |
58eebef2 KW |
1082 | __FILE__, __LINE__, |
1083 | PL_in_utf8_COLLATE_locale, | |
1084 | x_len_shorter, x_len_longer, | |
1085 | PL_collxfrm_mult, PL_collxfrm_base); | |
1086 | } | |
7d4bcc4a KW |
1087 | # endif |
1088 | ||
98994639 HS |
1089 | } |
1090 | } | |
1091 | ||
1092 | #endif /* USE_LOCALE_COLLATE */ | |
7d4bcc4a | 1093 | |
98994639 HS |
1094 | } |
1095 | ||
d2b24094 | 1096 | #ifdef WIN32 |
b8cc575c | 1097 | |
a4f00dcc | 1098 | STATIC char * |
b8cc575c | 1099 | S_win32_setlocale(pTHX_ int category, const char* locale) |
b385bb4d KW |
1100 | { |
1101 | /* This, for Windows, emulates POSIX setlocale() behavior. There is no | |
7d4bcc4a KW |
1102 | * difference between the two unless the input locale is "", which normally |
1103 | * means on Windows to get the machine default, which is set via the | |
1104 | * computer's "Regional and Language Options" (or its current equivalent). | |
1105 | * In POSIX, it instead means to find the locale from the user's | |
1106 | * environment. This routine changes the Windows behavior to first look in | |
1107 | * the environment, and, if anything is found, use that instead of going to | |
1108 | * the machine default. If there is no environment override, the machine | |
1109 | * default is used, by calling the real setlocale() with "". | |
1110 | * | |
1111 | * The POSIX behavior is to use the LC_ALL variable if set; otherwise to | |
1112 | * use the particular category's variable if set; otherwise to use the LANG | |
1113 | * variable. */ | |
b385bb4d | 1114 | |
175c4cf9 | 1115 | bool override_LC_ALL = FALSE; |
89f7b9aa | 1116 | char * result; |
e5f10d49 | 1117 | unsigned int i; |
89f7b9aa | 1118 | |
b385bb4d | 1119 | if (locale && strEQ(locale, "")) { |
7d4bcc4a KW |
1120 | |
1121 | # ifdef LC_ALL | |
1122 | ||
b385bb4d KW |
1123 | locale = PerlEnv_getenv("LC_ALL"); |
1124 | if (! locale) { | |
e5f10d49 KW |
1125 | if (category == LC_ALL) { |
1126 | override_LC_ALL = TRUE; | |
1127 | } | |
1128 | else { | |
7d4bcc4a KW |
1129 | |
1130 | # endif | |
7d4bcc4a | 1131 | |
e5f10d49 KW |
1132 | for (i = 0; i < NOMINAL_LC_ALL_INDEX; i++) { |
1133 | if (category == categories[i]) { | |
1134 | locale = PerlEnv_getenv(category_names[i]); | |
1135 | goto found_locale; | |
1136 | } | |
1137 | } | |
7d4bcc4a | 1138 | |
b385bb4d | 1139 | locale = PerlEnv_getenv("LANG"); |
481465ea | 1140 | if (! locale) { |
b385bb4d KW |
1141 | locale = ""; |
1142 | } | |
e5f10d49 KW |
1143 | |
1144 | found_locale: ; | |
7d4bcc4a KW |
1145 | |
1146 | # ifdef LC_ALL | |
1147 | ||
e5f10d49 | 1148 | } |
b385bb4d | 1149 | } |
7d4bcc4a KW |
1150 | |
1151 | # endif | |
1152 | ||
b385bb4d KW |
1153 | } |
1154 | ||
89f7b9aa | 1155 | result = setlocale(category, locale); |
bbc98134 | 1156 | DEBUG_L(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", __FILE__, __LINE__, |
a4f00dcc | 1157 | setlocale_debug_string(category, locale, result))); |
89f7b9aa | 1158 | |
481465ea | 1159 | if (! override_LC_ALL) { |
89f7b9aa KW |
1160 | return result; |
1161 | } | |
1162 | ||
dfd77d7a | 1163 | /* Here the input category was LC_ALL, and we have set it to what is in the |
481465ea KW |
1164 | * LANG variable or the system default if there is no LANG. But these have |
1165 | * lower priority than the other LC_foo variables, so override it for each | |
1166 | * one that is set. (If they are set to "", it means to use the same thing | |
1167 | * we just set LC_ALL to, so can skip) */ | |
7d4bcc4a | 1168 | |
948523db | 1169 | for (i = 0; i < LC_ALL_INDEX; i++) { |
e5f10d49 KW |
1170 | result = PerlEnv_getenv(category_names[i]); |
1171 | if (result && strNE(result, "")) { | |
1172 | setlocale(categories[i], result); | |
1173 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", | |
1174 | __FILE__, __LINE__, | |
1175 | setlocale_debug_string(categories[i], result, "not captured"))); | |
1176 | } | |
89f7b9aa | 1177 | } |
7d4bcc4a | 1178 | |
bbc98134 KW |
1179 | result = setlocale(LC_ALL, NULL); |
1180 | DEBUG_L(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", | |
1181 | __FILE__, __LINE__, | |
a4f00dcc | 1182 | setlocale_debug_string(LC_ALL, NULL, result))); |
89f7b9aa | 1183 | |
bbc98134 | 1184 | return result; |
b385bb4d KW |
1185 | } |
1186 | ||
1187 | #endif | |
1188 | ||
a4f00dcc KW |
1189 | char * |
1190 | Perl_setlocale(int category, const char * locale) | |
1191 | { | |
1192 | /* This wraps POSIX::setlocale() */ | |
1193 | ||
1194 | char * retval; | |
1159483a | 1195 | char * newlocale; |
a4f00dcc KW |
1196 | dTHX; |
1197 | ||
a4f00dcc KW |
1198 | #ifdef USE_LOCALE_NUMERIC |
1199 | ||
291a84fb KW |
1200 | /* A NULL locale means only query what the current one is. We have the |
1201 | * LC_NUMERIC name saved, because we are normally switched into the C | |
1202 | * locale for it. For an LC_ALL query, switch back to get the correct | |
1203 | * results. All other categories don't require special handling */ | |
a4f00dcc KW |
1204 | if (locale == NULL) { |
1205 | if (category == LC_NUMERIC) { | |
1206 | return savepv(PL_numeric_name); | |
1207 | } | |
1208 | ||
7d4bcc4a | 1209 | # ifdef LC_ALL |
a4f00dcc | 1210 | |
c0d737a8 KW |
1211 | else if (category == LC_ALL && ! PL_numeric_underlying) { |
1212 | ||
a4f00dcc KW |
1213 | SET_NUMERIC_UNDERLYING(); |
1214 | } | |
1215 | ||
7d4bcc4a | 1216 | # endif |
a4f00dcc KW |
1217 | |
1218 | } | |
1219 | ||
1220 | #endif | |
1221 | ||
e5f10d49 KW |
1222 | /* Save retval since subsequent setlocale() calls may overwrite it. */ |
1223 | retval = savepv(do_setlocale_r(category, locale)); | |
a4f00dcc KW |
1224 | |
1225 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1226 | "%s:%d: %s\n", __FILE__, __LINE__, | |
1227 | setlocale_debug_string(category, locale, retval))); | |
1228 | if (! retval) { | |
1229 | /* Should never happen that a query would return an error, but be | |
1230 | * sure and reset to C locale */ | |
1231 | if (locale == 0) { | |
1232 | SET_NUMERIC_STANDARD(); | |
1233 | } | |
7d4bcc4a | 1234 | |
a4f00dcc KW |
1235 | return NULL; |
1236 | } | |
1237 | ||
a4f00dcc KW |
1238 | /* If locale == NULL, we are just querying the state, but may have switched |
1239 | * to NUMERIC_UNDERLYING. Switch back before returning. */ | |
1240 | if (locale == NULL) { | |
1241 | SET_NUMERIC_STANDARD(); | |
1242 | return retval; | |
1243 | } | |
a4f00dcc | 1244 | |
1159483a KW |
1245 | /* Now that have switched locales, we have to update our records to |
1246 | * correspond. */ | |
a4f00dcc | 1247 | |
1159483a | 1248 | switch (category) { |
a4f00dcc | 1249 | |
1159483a | 1250 | #ifdef USE_LOCALE_CTYPE |
a4f00dcc | 1251 | |
1159483a KW |
1252 | case LC_CTYPE: |
1253 | new_ctype(retval); | |
1254 | break; | |
a4f00dcc | 1255 | |
1159483a | 1256 | #endif |
a4f00dcc KW |
1257 | #ifdef USE_LOCALE_COLLATE |
1258 | ||
1159483a KW |
1259 | case LC_COLLATE: |
1260 | new_collate(retval); | |
1261 | break; | |
a4f00dcc | 1262 | |
1159483a | 1263 | #endif |
a4f00dcc KW |
1264 | #ifdef USE_LOCALE_NUMERIC |
1265 | ||
1159483a KW |
1266 | case LC_NUMERIC: |
1267 | new_numeric(retval); | |
1268 | break; | |
a4f00dcc | 1269 | |
1159483a KW |
1270 | #endif |
1271 | #ifdef LC_ALL | |
a4f00dcc | 1272 | |
1159483a | 1273 | case LC_ALL: |
a4f00dcc | 1274 | |
1159483a KW |
1275 | /* LC_ALL updates all the things we care about. The values may not |
1276 | * be the same as 'retval', as the locale "" may have set things | |
1277 | * individually */ | |
a4f00dcc | 1278 | |
1159483a | 1279 | # ifdef USE_LOCALE_CTYPE |
a4f00dcc | 1280 | |
1159483a KW |
1281 | newlocale = do_setlocale_c(LC_CTYPE, NULL); |
1282 | new_ctype(newlocale); | |
a4f00dcc | 1283 | |
1159483a KW |
1284 | # endif /* USE_LOCALE_CTYPE */ |
1285 | # ifdef USE_LOCALE_COLLATE | |
1286 | ||
1287 | newlocale = do_setlocale_c(LC_COLLATE, NULL); | |
1288 | new_collate(newlocale); | |
a4f00dcc | 1289 | |
7d4bcc4a | 1290 | # endif |
1159483a | 1291 | # ifdef USE_LOCALE_NUMERIC |
a4f00dcc | 1292 | |
1159483a KW |
1293 | newlocale = do_setlocale_c(LC_NUMERIC, NULL); |
1294 | new_numeric(newlocale); | |
a4f00dcc | 1295 | |
1159483a KW |
1296 | # endif /* USE_LOCALE_NUMERIC */ |
1297 | #endif /* LC_ALL */ | |
a4f00dcc | 1298 | |
1159483a KW |
1299 | default: |
1300 | break; | |
a4f00dcc KW |
1301 | } |
1302 | ||
1303 | return retval; | |
1304 | ||
f7416781 KW |
1305 | |
1306 | } | |
1307 | ||
1308 | PERL_STATIC_INLINE const char * | |
1309 | S_save_to_buffer(const char * string, char **buf, Size_t *buf_size, const Size_t offset) | |
1310 | { | |
1311 | /* Copy the NUL-terminated 'string' to 'buf' + 'offset'. 'buf' has size 'buf_size', | |
1312 | * growing it if necessary */ | |
1313 | ||
1314 | const Size_t string_size = strlen(string) + offset + 1; | |
1315 | ||
1316 | PERL_ARGS_ASSERT_SAVE_TO_BUFFER; | |
1317 | ||
1318 | if (*buf_size == 0) { | |
1319 | Newx(*buf, string_size, char); | |
1320 | *buf_size = string_size; | |
1321 | } | |
1322 | else if (string_size > *buf_size) { | |
1323 | Renew(*buf, string_size, char); | |
1324 | *buf_size = string_size; | |
1325 | } | |
1326 | ||
1327 | Copy(string, *buf + offset, string_size - offset, char); | |
1328 | return *buf; | |
1329 | } | |
1330 | ||
1331 | /* | |
1332 | ||
1333 | =head1 Locale-related functions and macros | |
1334 | ||
1335 | =for apidoc Perl_langinfo | |
1336 | ||
7d4bcc4a | 1337 | This is an (almost ª) drop-in replacement for the system C<L<nl_langinfo(3)>>, |
f7416781 KW |
1338 | taking the same C<item> parameter values, and returning the same information. |
1339 | But it is more thread-safe than regular C<nl_langinfo()>, and hides the quirks | |
1340 | of Perl's locale handling from your code, and can be used on systems that lack | |
1341 | a native C<nl_langinfo>. | |
1342 | ||
1343 | Expanding on these: | |
1344 | ||
1345 | =over | |
1346 | ||
1347 | =item * | |
1348 | ||
1349 | It delivers the correct results for the C<RADIXCHAR> and C<THOUSESEP> items, | |
1350 | without you having to write extra code. The reason for the extra code would be | |
1351 | because these are from the C<LC_NUMERIC> locale category, which is normally | |
1352 | kept set to the C locale by Perl, no matter what the underlying locale is | |
1353 | supposed to be, and so to get the expected results, you have to temporarily | |
1354 | toggle into the underlying locale, and later toggle back. (You could use | |
1355 | plain C<nl_langinfo> and C<L</STORE_LC_NUMERIC_FORCE_TO_UNDERLYING>> for this | |
1356 | but then you wouldn't get the other advantages of C<Perl_langinfo()>; not | |
1357 | keeping C<LC_NUMERIC> in the C locale would break a lot of CPAN, which is | |
1358 | expecting the radix (decimal point) character to be a dot.) | |
1359 | ||
1360 | =item * | |
1361 | ||
1362 | Depending on C<item>, it works on systems that don't have C<nl_langinfo>, hence | |
1363 | makes your code more portable. Of the fifty-some possible items specified by | |
1364 | the POSIX 2008 standard, | |
1365 | L<http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/langinfo.h.html>, | |
1366 | only two are completely unimplemented. It uses various techniques to recover | |
1367 | the other items, including calling C<L<localeconv(3)>>, and C<L<strftime(3)>>, | |
1368 | both of which are specified in C89, so should be always be available. Later | |
1369 | C<strftime()> versions have additional capabilities; C<""> is returned for | |
1370 | those not available on your system. | |
1371 | ||
1372 | The details for those items which may differ from what this emulation returns | |
1373 | and what a native C<nl_langinfo()> would return are: | |
1374 | ||
1375 | =over | |
1376 | ||
1377 | =item C<CODESET> | |
1378 | ||
1379 | =item C<ERA> | |
1380 | ||
1381 | Unimplemented, so returns C<"">. | |
1382 | ||
1383 | =item C<YESEXPR> | |
1384 | ||
c1566110 KW |
1385 | =item C<YESSTR> |
1386 | ||
f7416781 KW |
1387 | =item C<NOEXPR> |
1388 | ||
c1566110 KW |
1389 | =item C<NOSTR> |
1390 | ||
1391 | Only the values for English are returned. C<YESSTR> and C<NOSTR> have been | |
1392 | removed from POSIX 2008, and are retained for backwards compatibility. Your | |
1393 | platform's C<nl_langinfo> may not support them. | |
f7416781 KW |
1394 | |
1395 | =item C<D_FMT> | |
1396 | ||
1397 | Always evaluates to C<%x>, the locale's appropriate date representation. | |
1398 | ||
1399 | =item C<T_FMT> | |
1400 | ||
1401 | Always evaluates to C<%X>, the locale's appropriate time representation. | |
1402 | ||
1403 | =item C<D_T_FMT> | |
1404 | ||
1405 | Always evaluates to C<%c>, the locale's appropriate date and time | |
1406 | representation. | |
1407 | ||
1408 | =item C<CRNCYSTR> | |
1409 | ||
1410 | The return may be incorrect for those rare locales where the currency symbol | |
1411 | replaces the radix character. | |
1412 | Send email to L<mailto:perlbug@perl.org> if you have examples of it needing | |
1413 | to work differently. | |
1414 | ||
1415 | =item C<ALT_DIGITS> | |
1416 | ||
1417 | Currently this gives the same results as Linux does. | |
1418 | Send email to L<mailto:perlbug@perl.org> if you have examples of it needing | |
1419 | to work differently. | |
1420 | ||
1421 | =item C<ERA_D_FMT> | |
1422 | ||
1423 | =item C<ERA_T_FMT> | |
1424 | ||
1425 | =item C<ERA_D_T_FMT> | |
1426 | ||
1427 | =item C<T_FMT_AMPM> | |
1428 | ||
1429 | These are derived by using C<strftime()>, and not all versions of that function | |
1430 | know about them. C<""> is returned for these on such systems. | |
1431 | ||
1432 | =back | |
1433 | ||
1434 | When using C<Perl_langinfo> on systems that don't have a native | |
1435 | C<nl_langinfo()>, you must | |
1436 | ||
1437 | #include "perl_langinfo.h" | |
1438 | ||
1439 | before the C<perl.h> C<#include>. You can replace your C<langinfo.h> | |
1440 | C<#include> with this one. (Doing it this way keeps out the symbols that plain | |
1441 | C<langinfo.h> imports into the namespace for code that doesn't need it.) | |
1442 | ||
1443 | You also should not use the bare C<langinfo.h> item names, but should preface | |
1444 | them with C<PERL_>, so use C<PERL_RADIXCHAR> instead of plain C<RADIXCHAR>. | |
1445 | The C<PERL_I<foo>> versions will also work for this function on systems that do | |
1446 | have a native C<nl_langinfo>. | |
1447 | ||
1448 | =item * | |
1449 | ||
1450 | It is thread-friendly, returning its result in a buffer that won't be | |
1451 | overwritten by another thread, so you don't have to code for that possibility. | |
1452 | The buffer can be overwritten by the next call to C<nl_langinfo> or | |
1453 | C<Perl_langinfo> in the same thread. | |
1454 | ||
1455 | =item * | |
1456 | ||
7d4bcc4a | 1457 | ª It returns S<C<const char *>>, whereas plain C<nl_langinfo()> returns S<C<char |
f7416781 KW |
1458 | *>>, but you are (only by documentation) forbidden to write into the buffer. |
1459 | By declaring this C<const>, the compiler enforces this restriction. The extra | |
1460 | C<const> is why this isn't an unequivocal drop-in replacement for | |
1461 | C<nl_langinfo>. | |
1462 | ||
1463 | =back | |
1464 | ||
1465 | The original impetus for C<Perl_langinfo()> was so that code that needs to | |
1466 | find out the current currency symbol, floating point radix character, or digit | |
1467 | grouping separator can use, on all systems, the simpler and more | |
1468 | thread-friendly C<nl_langinfo> API instead of C<L<localeconv(3)>> which is a | |
1469 | pain to make thread-friendly. For other fields returned by C<localeconv>, it | |
1470 | is better to use the methods given in L<perlcall> to call | |
1471 | L<C<POSIX::localeconv()>|POSIX/localeconv>, which is thread-friendly. | |
1472 | ||
1473 | =cut | |
1474 | ||
1475 | */ | |
1476 | ||
1477 | const char * | |
1478 | #ifdef HAS_NL_LANGINFO | |
1479 | Perl_langinfo(const nl_item item) | |
1480 | #else | |
1481 | Perl_langinfo(const int item) | |
1482 | #endif | |
1483 | { | |
f61748ac KW |
1484 | return my_nl_langinfo(item, TRUE); |
1485 | } | |
1486 | ||
1487 | const char * | |
1488 | #ifdef HAS_NL_LANGINFO | |
1489 | S_my_nl_langinfo(const nl_item item, bool toggle) | |
1490 | #else | |
1491 | S_my_nl_langinfo(const int item, bool toggle) | |
1492 | #endif | |
1493 | { | |
ae74815b | 1494 | dTHX; |
f7416781 | 1495 | |
5a854ab3 KW |
1496 | /* We only need to toggle into the underlying LC_NUMERIC locale for these |
1497 | * two items, and only if not already there */ | |
1498 | if (toggle && (( item != PERL_RADIXCHAR && item != PERL_THOUSEP) | |
1499 | || PL_numeric_underlying)) | |
1500 | { | |
1501 | toggle = FALSE; | |
1502 | } | |
1503 | ||
ab340fff KW |
1504 | #if defined(HAS_NL_LANGINFO) /* nl_langinfo() is available. */ |
1505 | #if ! defined(HAS_POSIX_2008_LOCALE) | |
f7416781 | 1506 | |
ab340fff | 1507 | /* Here, use plain nl_langinfo(), switching to the underlying LC_NUMERIC |
ae74815b KW |
1508 | * for those items dependent on it. This must be copied to a buffer before |
1509 | * switching back, as some systems destroy the buffer when setlocale() is | |
1510 | * called */ | |
f7416781 | 1511 | |
038d3702 KW |
1512 | { |
1513 | DECLARATION_FOR_LC_NUMERIC_MANIPULATION; | |
1514 | ||
b2fee59c | 1515 | if (toggle) { |
038d3702 | 1516 | STORE_LC_NUMERIC_FORCE_TO_UNDERLYING(); |
b2fee59c | 1517 | } |
f7416781 | 1518 | |
5acc4454 KW |
1519 | LOCALE_LOCK; /* Prevent interference from another thread executing |
1520 | this code section (the only call to nl_langinfo in | |
1521 | the core) */ | |
1522 | ||
b2fee59c KW |
1523 | save_to_buffer(nl_langinfo(item), &PL_langinfo_buf, |
1524 | &PL_langinfo_bufsize, 0); | |
f7416781 | 1525 | |
5acc4454 KW |
1526 | LOCALE_UNLOCK; |
1527 | ||
b2fee59c | 1528 | if (toggle) { |
038d3702 | 1529 | RESTORE_LC_NUMERIC(); |
b2fee59c | 1530 | } |
f7416781 KW |
1531 | } |
1532 | ||
ab340fff KW |
1533 | # else /* Use nl_langinfo_l(), avoiding both a mutex and changing the locale */ |
1534 | ||
5a854ab3 | 1535 | { |
b2fee59c KW |
1536 | bool do_free = FALSE; |
1537 | locale_t cur = uselocale((locale_t) 0); | |
ab340fff | 1538 | |
b2fee59c KW |
1539 | if (cur == LC_GLOBAL_LOCALE) { |
1540 | cur = duplocale(LC_GLOBAL_LOCALE); | |
1541 | do_free = TRUE; | |
1542 | } | |
ab340fff | 1543 | |
b2fee59c | 1544 | if (toggle) { |
e1aa2579 KW |
1545 | if (PL_underlying_numeric_obj) { |
1546 | cur = PL_underlying_numeric_obj; | |
1547 | } | |
1548 | else { | |
1549 | cur = newlocale(LC_NUMERIC_MASK, PL_numeric_name, cur); | |
1550 | do_free = TRUE; | |
1551 | } | |
b2fee59c | 1552 | } |
ab340fff | 1553 | |
b2fee59c KW |
1554 | save_to_buffer(nl_langinfo_l(item, cur), |
1555 | &PL_langinfo_buf, &PL_langinfo_bufsize, 0); | |
1556 | if (do_free) { | |
1557 | freelocale(cur); | |
1558 | } | |
5a854ab3 | 1559 | } |
ab340fff | 1560 | |
c1566110 KW |
1561 | # endif |
1562 | ||
1563 | if (strEQ(PL_langinfo_buf, "")) { | |
1564 | if (item == PERL_YESSTR) { | |
1565 | return "yes"; | |
1566 | } | |
1567 | if (item == PERL_NOSTR) { | |
1568 | return "no"; | |
1569 | } | |
1570 | } | |
1571 | ||
ab340fff KW |
1572 | return PL_langinfo_buf; |
1573 | ||
f7416781 | 1574 | #else /* Below, emulate nl_langinfo as best we can */ |
43dd6b15 KW |
1575 | |
1576 | { | |
1577 | ||
f7416781 KW |
1578 | # ifdef HAS_LOCALECONV |
1579 | ||
43dd6b15 | 1580 | const struct lconv* lc; |
038d3702 | 1581 | DECLARATION_FOR_LC_NUMERIC_MANIPULATION; |
f7416781 KW |
1582 | |
1583 | # endif | |
1584 | # ifdef HAS_STRFTIME | |
1585 | ||
43dd6b15 KW |
1586 | struct tm tm; |
1587 | bool return_format = FALSE; /* Return the %format, not the value */ | |
1588 | const char * format; | |
f7416781 KW |
1589 | |
1590 | # endif | |
1591 | ||
43dd6b15 KW |
1592 | /* We copy the results to a per-thread buffer, even if not |
1593 | * multi-threaded. This is in part to simplify this code, and partly | |
1594 | * because we need a buffer anyway for strftime(), and partly because a | |
1595 | * call of localeconv() could otherwise wipe out the buffer, and the | |
1596 | * programmer would not be expecting this, as this is a nl_langinfo() | |
1597 | * substitute after all, so s/he might be thinking their localeconv() | |
1598 | * is safe until another localeconv() call. */ | |
f7416781 | 1599 | |
43dd6b15 KW |
1600 | switch (item) { |
1601 | Size_t len; | |
1602 | const char * retval; | |
f7416781 | 1603 | |
43dd6b15 KW |
1604 | /* These 2 are unimplemented */ |
1605 | case PERL_CODESET: | |
1606 | case PERL_ERA: /* For use with strftime() %E modifier */ | |
f7416781 | 1607 | |
43dd6b15 KW |
1608 | default: |
1609 | return ""; | |
f7416781 | 1610 | |
43dd6b15 KW |
1611 | /* We use only an English set, since we don't know any more */ |
1612 | case PERL_YESEXPR: return "^[+1yY]"; | |
1613 | case PERL_YESSTR: return "yes"; | |
1614 | case PERL_NOEXPR: return "^[-0nN]"; | |
1615 | case PERL_NOSTR: return "no"; | |
f7416781 KW |
1616 | |
1617 | # ifdef HAS_LOCALECONV | |
1618 | ||
43dd6b15 | 1619 | case PERL_CRNCYSTR: |
f7416781 | 1620 | |
43dd6b15 KW |
1621 | /* We don't bother with localeconv_l() because any system that |
1622 | * has it is likely to also have nl_langinfo() */ | |
291a84fb | 1623 | |
5acc4454 KW |
1624 | LOCALE_LOCK; /* Prevent interference with other threads |
1625 | using localeconv() */ | |
1626 | ||
43dd6b15 KW |
1627 | lc = localeconv(); |
1628 | if ( ! lc | |
1629 | || ! lc->currency_symbol | |
1630 | || strEQ("", lc->currency_symbol)) | |
1631 | { | |
1632 | LOCALE_UNLOCK; | |
1633 | return ""; | |
1634 | } | |
f7416781 | 1635 | |
43dd6b15 KW |
1636 | /* Leave the first spot empty to be filled in below */ |
1637 | save_to_buffer(lc->currency_symbol, &PL_langinfo_buf, | |
1638 | &PL_langinfo_bufsize, 1); | |
1639 | if (lc->mon_decimal_point && strEQ(lc->mon_decimal_point, "")) | |
1640 | { /* khw couldn't figure out how the localedef specifications | |
1641 | would show that the $ should replace the radix; this is | |
1642 | just a guess as to how it might work.*/ | |
1643 | *PL_langinfo_buf = '.'; | |
1644 | } | |
1645 | else if (lc->p_cs_precedes) { | |
1646 | *PL_langinfo_buf = '-'; | |
1647 | } | |
1648 | else { | |
1649 | *PL_langinfo_buf = '+'; | |
1650 | } | |
f7416781 | 1651 | |
43dd6b15 KW |
1652 | LOCALE_UNLOCK; |
1653 | break; | |
f7416781 | 1654 | |
43dd6b15 KW |
1655 | case PERL_RADIXCHAR: |
1656 | case PERL_THOUSEP: | |
f7416781 | 1657 | |
43dd6b15 | 1658 | if (toggle) { |
038d3702 | 1659 | STORE_LC_NUMERIC_FORCE_TO_UNDERLYING(); |
c0d737a8 | 1660 | } |
f7416781 | 1661 | |
5acc4454 KW |
1662 | LOCALE_LOCK; /* Prevent interference with other threads |
1663 | using localeconv() */ | |
1664 | ||
43dd6b15 KW |
1665 | lc = localeconv(); |
1666 | if (! lc) { | |
33394adc KW |
1667 | retval = ""; |
1668 | } | |
43dd6b15 KW |
1669 | else { |
1670 | retval = (item == PERL_RADIXCHAR) | |
1671 | ? lc->decimal_point | |
1672 | : lc->thousands_sep; | |
1673 | if (! retval) { | |
1674 | retval = ""; | |
1675 | } | |
1676 | } | |
f7416781 | 1677 | |
43dd6b15 KW |
1678 | save_to_buffer(retval, &PL_langinfo_buf, |
1679 | &PL_langinfo_bufsize, 0); | |
f7416781 | 1680 | |
5acc4454 KW |
1681 | LOCALE_UNLOCK; |
1682 | ||
43dd6b15 | 1683 | if (toggle) { |
038d3702 | 1684 | RESTORE_LC_NUMERIC(); |
43dd6b15 | 1685 | } |
f7416781 | 1686 | |
43dd6b15 | 1687 | break; |
f7416781 KW |
1688 | |
1689 | # endif | |
1690 | # ifdef HAS_STRFTIME | |
1691 | ||
43dd6b15 KW |
1692 | /* These are defined by C89, so we assume that strftime supports |
1693 | * them, and so are returned unconditionally; they may not be what | |
1694 | * the locale actually says, but should give good enough results | |
1695 | * for someone using them as formats (as opposed to trying to parse | |
1696 | * them to figure out what the locale says). The other format | |
1697 | * items are actually tested to verify they work on the platform */ | |
1698 | case PERL_D_FMT: return "%x"; | |
1699 | case PERL_T_FMT: return "%X"; | |
1700 | case PERL_D_T_FMT: return "%c"; | |
1701 | ||
1702 | /* These formats are only available in later strfmtime's */ | |
1703 | case PERL_ERA_D_FMT: case PERL_ERA_T_FMT: case PERL_ERA_D_T_FMT: | |
1704 | case PERL_T_FMT_AMPM: | |
1705 | ||
1706 | /* The rest can be gotten from most versions of strftime(). */ | |
1707 | case PERL_ABDAY_1: case PERL_ABDAY_2: case PERL_ABDAY_3: | |
1708 | case PERL_ABDAY_4: case PERL_ABDAY_5: case PERL_ABDAY_6: | |
1709 | case PERL_ABDAY_7: | |
1710 | case PERL_ALT_DIGITS: | |
1711 | case PERL_AM_STR: case PERL_PM_STR: | |
1712 | case PERL_ABMON_1: case PERL_ABMON_2: case PERL_ABMON_3: | |
1713 | case PERL_ABMON_4: case PERL_ABMON_5: case PERL_ABMON_6: | |
1714 | case PERL_ABMON_7: case PERL_ABMON_8: case PERL_ABMON_9: | |
1715 | case PERL_ABMON_10: case PERL_ABMON_11: case PERL_ABMON_12: | |
1716 | case PERL_DAY_1: case PERL_DAY_2: case PERL_DAY_3: case PERL_DAY_4: | |
1717 | case PERL_DAY_5: case PERL_DAY_6: case PERL_DAY_7: | |
1718 | case PERL_MON_1: case PERL_MON_2: case PERL_MON_3: case PERL_MON_4: | |
1719 | case PERL_MON_5: case PERL_MON_6: case PERL_MON_7: case PERL_MON_8: | |
1720 | case PERL_MON_9: case PERL_MON_10: case PERL_MON_11: | |
1721 | case PERL_MON_12: | |
1722 | ||
1723 | LOCALE_LOCK; | |
1724 | ||
1725 | init_tm(&tm); /* Precaution against core dumps */ | |
1726 | tm.tm_sec = 30; | |
1727 | tm.tm_min = 30; | |
1728 | tm.tm_hour = 6; | |
1729 | tm.tm_year = 2017 - 1900; | |
1730 | tm.tm_wday = 0; | |
1731 | tm.tm_mon = 0; | |
1732 | switch (item) { | |
1733 | default: | |
1734 | LOCALE_UNLOCK; | |
1735 | Perl_croak(aTHX_ | |
1736 | "panic: %s: %d: switch case: %d problem", | |
1737 | __FILE__, __LINE__, item); | |
1738 | NOT_REACHED; /* NOTREACHED */ | |
1739 | ||
1740 | case PERL_PM_STR: tm.tm_hour = 18; | |
1741 | case PERL_AM_STR: | |
1742 | format = "%p"; | |
1743 | break; | |
1744 | ||
1745 | case PERL_ABDAY_7: tm.tm_wday++; | |
1746 | case PERL_ABDAY_6: tm.tm_wday++; | |
1747 | case PERL_ABDAY_5: tm.tm_wday++; | |
1748 | case PERL_ABDAY_4: tm.tm_wday++; | |
1749 | case PERL_ABDAY_3: tm.tm_wday++; | |
1750 | case PERL_ABDAY_2: tm.tm_wday++; | |
1751 | case PERL_ABDAY_1: | |
1752 | format = "%a"; | |
1753 | break; | |
1754 | ||
1755 | case PERL_DAY_7: tm.tm_wday++; | |
1756 | case PERL_DAY_6: tm.tm_wday++; | |
1757 | case PERL_DAY_5: tm.tm_wday++; | |
1758 | case PERL_DAY_4: tm.tm_wday++; | |
1759 | case PERL_DAY_3: tm.tm_wday++; | |
1760 | case PERL_DAY_2: tm.tm_wday++; | |
1761 | case PERL_DAY_1: | |
1762 | format = "%A"; | |
1763 | break; | |
1764 | ||
1765 | case PERL_ABMON_12: tm.tm_mon++; | |
1766 | case PERL_ABMON_11: tm.tm_mon++; | |
1767 | case PERL_ABMON_10: tm.tm_mon++; | |
1768 | case PERL_ABMON_9: tm.tm_mon++; | |
1769 | case PERL_ABMON_8: tm.tm_mon++; | |
1770 | case PERL_ABMON_7: tm.tm_mon++; | |
1771 | case PERL_ABMON_6: tm.tm_mon++; | |
1772 | case PERL_ABMON_5: tm.tm_mon++; | |
1773 | case PERL_ABMON_4: tm.tm_mon++; | |
1774 | case PERL_ABMON_3: tm.tm_mon++; | |
1775 | case PERL_ABMON_2: tm.tm_mon++; | |
1776 | case PERL_ABMON_1: | |
1777 | format = "%b"; | |
1778 | break; | |
1779 | ||
1780 | case PERL_MON_12: tm.tm_mon++; | |
1781 | case PERL_MON_11: tm.tm_mon++; | |
1782 | case PERL_MON_10: tm.tm_mon++; | |
1783 | case PERL_MON_9: tm.tm_mon++; | |
1784 | case PERL_MON_8: tm.tm_mon++; | |
1785 | case PERL_MON_7: tm.tm_mon++; | |
1786 | case PERL_MON_6: tm.tm_mon++; | |
1787 | case PERL_MON_5: tm.tm_mon++; | |
1788 | case PERL_MON_4: tm.tm_mon++; | |
1789 | case PERL_MON_3: tm.tm_mon++; | |
1790 | case PERL_MON_2: tm.tm_mon++; | |
1791 | case PERL_MON_1: | |
1792 | format = "%B"; | |
1793 | break; | |
1794 | ||
1795 | case PERL_T_FMT_AMPM: | |
1796 | format = "%r"; | |
1797 | return_format = TRUE; | |
1798 | break; | |
1799 | ||
1800 | case PERL_ERA_D_FMT: | |
1801 | format = "%Ex"; | |
1802 | return_format = TRUE; | |
1803 | break; | |
1804 | ||
1805 | case PERL_ERA_T_FMT: | |
1806 | format = "%EX"; | |
1807 | return_format = TRUE; | |
1808 | break; | |
1809 | ||
1810 | case PERL_ERA_D_T_FMT: | |
1811 | format = "%Ec"; | |
1812 | return_format = TRUE; | |
1813 | break; | |
1814 | ||
1815 | case PERL_ALT_DIGITS: | |
1816 | tm.tm_wday = 0; | |
1817 | format = "%Ow"; /* Find the alternate digit for 0 */ | |
1818 | break; | |
1819 | } | |
f7416781 | 1820 | |
43dd6b15 KW |
1821 | /* We can't use my_strftime() because it doesn't look at |
1822 | * tm_wday */ | |
1823 | while (0 == strftime(PL_langinfo_buf, PL_langinfo_bufsize, | |
1824 | format, &tm)) | |
1825 | { | |
1826 | /* A zero return means one of: | |
1827 | * a) there wasn't enough space in PL_langinfo_buf | |
1828 | * b) the format, like a plain %p, returns empty | |
1829 | * c) it was an illegal format, though some | |
1830 | * implementations of strftime will just return the | |
1831 | * illegal format as a plain character sequence. | |
1832 | * | |
1833 | * To quickly test for case 'b)', try again but precede | |
1834 | * the format with a plain character. If that result is | |
1835 | * still empty, the problem is either 'a)' or 'c)' */ | |
1836 | ||
1837 | Size_t format_size = strlen(format) + 1; | |
1838 | Size_t mod_size = format_size + 1; | |
1839 | char * mod_format; | |
1840 | char * temp_result; | |
1841 | ||
1842 | Newx(mod_format, mod_size, char); | |
1843 | Newx(temp_result, PL_langinfo_bufsize, char); | |
6873aa47 | 1844 | *mod_format = ' '; |
43dd6b15 KW |
1845 | my_strlcpy(mod_format + 1, format, mod_size); |
1846 | len = strftime(temp_result, | |
1847 | PL_langinfo_bufsize, | |
1848 | mod_format, &tm); | |
1849 | Safefree(mod_format); | |
1850 | Safefree(temp_result); | |
1851 | ||
1852 | /* If 'len' is non-zero, it means that we had a case like | |
1853 | * %p which means the current locale doesn't use a.m. or | |
1854 | * p.m., and that is valid */ | |
1855 | if (len == 0) { | |
1856 | ||
1857 | /* Here, still didn't work. If we get well beyond a | |
1858 | * reasonable size, bail out to prevent an infinite | |
1859 | * loop. */ | |
1860 | ||
1861 | if (PL_langinfo_bufsize > 100 * format_size) { | |
1862 | *PL_langinfo_buf = '\0'; | |
1863 | } | |
1864 | else { | |
1865 | /* Double the buffer size to retry; Add 1 in case | |
1866 | * original was 0, so we aren't stuck at 0. */ | |
1867 | PL_langinfo_bufsize *= 2; | |
1868 | PL_langinfo_bufsize++; | |
1869 | Renew(PL_langinfo_buf, PL_langinfo_bufsize, char); | |
1870 | continue; | |
1871 | } | |
1872 | } | |
f7416781 | 1873 | |
f7416781 | 1874 | break; |
43dd6b15 | 1875 | } |
f7416781 | 1876 | |
43dd6b15 KW |
1877 | /* Here, we got a result. |
1878 | * | |
1879 | * If the item is 'ALT_DIGITS', PL_langinfo_buf contains the | |
1880 | * alternate format for wday 0. If the value is the same as | |
1881 | * the normal 0, there isn't an alternate, so clear the buffer. | |
1882 | * */ | |
1883 | if ( item == PERL_ALT_DIGITS | |
1884 | && strEQ(PL_langinfo_buf, "0")) | |
1885 | { | |
1886 | *PL_langinfo_buf = '\0'; | |
1887 | } | |
f7416781 | 1888 | |
43dd6b15 KW |
1889 | /* ALT_DIGITS is problematic. Experiments on it showed that |
1890 | * strftime() did not always work properly when going from | |
1891 | * alt-9 to alt-10. Only a few locales have this item defined, | |
1892 | * and in all of them on Linux that khw was able to find, | |
1893 | * nl_langinfo() merely returned the alt-0 character, possibly | |
1894 | * doubled. Most Unicode digits are in blocks of 10 | |
1895 | * consecutive code points, so that is sufficient information | |
1896 | * for those scripts, as we can infer alt-1, alt-2, .... But | |
1897 | * for a Japanese locale, a CJK ideographic 0 is returned, and | |
1898 | * the CJK digits are not in code point order, so you can't | |
1899 | * really infer anything. The localedef for this locale did | |
1900 | * specify the succeeding digits, so that strftime() works | |
1901 | * properly on them, without needing to infer anything. But | |
1902 | * the nl_langinfo() return did not give sufficient information | |
1903 | * for the caller to understand what's going on. So until | |
1904 | * there is evidence that it should work differently, this | |
1905 | * returns the alt-0 string for ALT_DIGITS. | |
1906 | * | |
1907 | * wday was chosen because its range is all a single digit. | |
1908 | * Things like tm_sec have two digits as the minimum: '00' */ | |
f7416781 | 1909 | |
43dd6b15 | 1910 | LOCALE_UNLOCK; |
f7416781 | 1911 | |
43dd6b15 KW |
1912 | /* If to return the format, not the value, overwrite the buffer |
1913 | * with it. But some strftime()s will keep the original format | |
1914 | * if illegal, so change those to "" */ | |
1915 | if (return_format) { | |
1916 | if (strEQ(PL_langinfo_buf, format)) { | |
f7416781 KW |
1917 | *PL_langinfo_buf = '\0'; |
1918 | } | |
43dd6b15 KW |
1919 | else { |
1920 | save_to_buffer(format, &PL_langinfo_buf, | |
1921 | &PL_langinfo_bufsize, 0); | |
f7416781 KW |
1922 | } |
1923 | } | |
1924 | ||
1925 | break; | |
f7416781 KW |
1926 | |
1927 | # endif | |
1928 | ||
43dd6b15 | 1929 | } |
f7416781 KW |
1930 | } |
1931 | ||
1932 | return PL_langinfo_buf; | |
1933 | ||
1934 | #endif | |
1935 | ||
a4f00dcc | 1936 | } |
b385bb4d | 1937 | |
98994639 HS |
1938 | /* |
1939 | * Initialize locale awareness. | |
1940 | */ | |
1941 | int | |
1942 | Perl_init_i18nl10n(pTHX_ int printwarn) | |
1943 | { | |
0e92a118 KW |
1944 | /* printwarn is |
1945 | * | |
1946 | * 0 if not to output warning when setup locale is bad | |
1947 | * 1 if to output warning based on value of PERL_BADLANG | |
1948 | * >1 if to output regardless of PERL_BADLANG | |
1949 | * | |
1950 | * returns | |
98994639 | 1951 | * 1 = set ok or not applicable, |
0e92a118 KW |
1952 | * 0 = fallback to a locale of lower priority |
1953 | * -1 = fallback to all locales failed, not even to the C locale | |
6b058d42 KW |
1954 | * |
1955 | * Under -DDEBUGGING, if the environment variable PERL_DEBUG_LOCALE_INIT is | |
1956 | * set, debugging information is output. | |
1957 | * | |
1958 | * This looks more complicated than it is, mainly due to the #ifdefs. | |
1959 | * | |
1960 | * We try to set LC_ALL to the value determined by the environment. If | |
1961 | * there is no LC_ALL on this platform, we try the individual categories we | |
1962 | * know about. If this works, we are done. | |
1963 | * | |
1964 | * But if it doesn't work, we have to do something else. We search the | |
1965 | * environment variables ourselves instead of relying on the system to do | |
1966 | * it. We look at, in order, LC_ALL, LANG, a system default locale (if we | |
1967 | * think there is one), and the ultimate fallback "C". This is all done in | |
1968 | * the same loop as above to avoid duplicating code, but it makes things | |
7d4bcc4a KW |
1969 | * more complex. The 'trial_locales' array is initialized with just one |
1970 | * element; it causes the behavior described in the paragraph above this to | |
1971 | * happen. If that fails, we add elements to 'trial_locales', and do extra | |
1972 | * loop iterations to cause the behavior described in this paragraph. | |
6b058d42 KW |
1973 | * |
1974 | * On Ultrix, the locale MUST come from the environment, so there is | |
1975 | * preliminary code to set it. I (khw) am not sure that it is necessary, | |
1976 | * and that this couldn't be folded into the loop, but barring any real | |
1977 | * platforms to test on, it's staying as-is | |
1978 | * | |
1979 | * A slight complication is that in embedded Perls, the locale may already | |
1980 | * be set-up, and we don't want to get it from the normal environment | |
1981 | * variables. This is handled by having a special environment variable | |
1982 | * indicate we're in this situation. We simply set setlocale's 2nd | |
1983 | * parameter to be a NULL instead of "". That indicates to setlocale that | |
1984 | * it is not to change anything, but to return the current value, | |
1985 | * effectively initializing perl's db to what the locale already is. | |
1986 | * | |
1987 | * We play the same trick with NULL if a LC_ALL succeeds. We call | |
1988 | * setlocale() on the individual categores with NULL to get their existing | |
1989 | * values for our db, instead of trying to change them. | |
1990 | * */ | |
98994639 | 1991 | |
0e92a118 KW |
1992 | int ok = 1; |
1993 | ||
7d4bcc4a KW |
1994 | #ifndef USE_LOCALE |
1995 | ||
1996 | PERL_UNUSED_ARG(printwarn); | |
1997 | ||
1998 | #else /* USE_LOCALE */ | |
7d4bcc4a KW |
1999 | # ifdef __GLIBC__ |
2000 | ||
175c4cf9 | 2001 | const char * const language = savepv(PerlEnv_getenv("LANGUAGE")); |
7d4bcc4a KW |
2002 | |
2003 | # endif | |
65ebb059 | 2004 | |
ccd65d51 KW |
2005 | /* NULL uses the existing already set up locale */ |
2006 | const char * const setlocale_init = (PerlEnv_getenv("PERL_SKIP_LOCALE_INIT")) | |
2007 | ? NULL | |
2008 | : ""; | |
c3fcd832 KW |
2009 | const char* trial_locales[5]; /* 5 = 1 each for "", LC_ALL, LANG, "", C */ |
2010 | unsigned int trial_locales_count; | |
175c4cf9 KW |
2011 | const char * const lc_all = savepv(PerlEnv_getenv("LC_ALL")); |
2012 | const char * const lang = savepv(PerlEnv_getenv("LANG")); | |
98994639 | 2013 | bool setlocale_failure = FALSE; |
65ebb059 | 2014 | unsigned int i; |
175c4cf9 KW |
2015 | |
2016 | /* A later getenv() could zap this, so only use here */ | |
2017 | const char * const bad_lang_use_once = PerlEnv_getenv("PERL_BADLANG"); | |
2018 | ||
2019 | const bool locwarn = (printwarn > 1 | |
e5f10d49 KW |
2020 | || ( printwarn |
2021 | && ( ! bad_lang_use_once | |
22ff3130 | 2022 | || ( |
e5f10d49 KW |
2023 | /* disallow with "" or "0" */ |
2024 | *bad_lang_use_once | |
2025 | && strNE("0", bad_lang_use_once))))); | |
ea92aad8 | 2026 | |
291a84fb | 2027 | /* setlocale() return vals; not copied so must be looked at immediately */ |
8de4332b | 2028 | const char * sl_result[NOMINAL_LC_ALL_INDEX + 1]; |
291a84fb KW |
2029 | |
2030 | /* current locale for given category; should have been copied so aren't | |
2031 | * volatile */ | |
8de4332b | 2032 | const char * curlocales[NOMINAL_LC_ALL_INDEX + 1]; |
291a84fb | 2033 | |
7d4bcc4a KW |
2034 | # ifdef WIN32 |
2035 | ||
6bce99ee JH |
2036 | /* In some systems you can find out the system default locale |
2037 | * and use that as the fallback locale. */ | |
7d4bcc4a KW |
2038 | # define SYSTEM_DEFAULT_LOCALE |
2039 | # endif | |
2040 | # ifdef SYSTEM_DEFAULT_LOCALE | |
2041 | ||
65ebb059 | 2042 | const char *system_default_locale = NULL; |
98994639 | 2043 | |
7d4bcc4a | 2044 | # endif |
948523db KW |
2045 | |
2046 | # ifndef DEBUGGING | |
2047 | # define DEBUG_LOCALE_INIT(a,b,c) | |
2048 | # else | |
7d4bcc4a | 2049 | |
8298454c | 2050 | DEBUG_INITIALIZATION_set(cBOOL(PerlEnv_getenv("PERL_DEBUG_LOCALE_INIT"))); |
7d4bcc4a KW |
2051 | |
2052 | # define DEBUG_LOCALE_INIT(category, locale, result) \ | |
2fcc0ca9 KW |
2053 | STMT_START { \ |
2054 | if (debug_initialization) { \ | |
2055 | PerlIO_printf(Perl_debug_log, \ | |
2056 | "%s:%d: %s\n", \ | |
2057 | __FILE__, __LINE__, \ | |
a4f00dcc | 2058 | setlocale_debug_string(category, \ |
2fcc0ca9 KW |
2059 | locale, \ |
2060 | result)); \ | |
2061 | } \ | |
2062 | } STMT_END | |
2fcc0ca9 | 2063 | |
948523db KW |
2064 | /* Make sure the parallel arrays are properly set up */ |
2065 | # ifdef USE_LOCALE_NUMERIC | |
2066 | assert(categories[LC_NUMERIC_INDEX] == LC_NUMERIC); | |
2067 | assert(strEQ(category_names[LC_NUMERIC_INDEX], "LC_NUMERIC")); | |
2068 | # endif | |
2069 | # ifdef USE_LOCALE_CTYPE | |
2070 | assert(categories[LC_CTYPE_INDEX] == LC_CTYPE); | |
2071 | assert(strEQ(category_names[LC_CTYPE_INDEX], "LC_CTYPE")); | |
2072 | # endif | |
2073 | # ifdef USE_LOCALE_COLLATE | |
2074 | assert(categories[LC_COLLATE_INDEX] == LC_COLLATE); | |
2075 | assert(strEQ(category_names[LC_COLLATE_INDEX], "LC_COLLATE")); | |
2076 | # endif | |
2077 | # ifdef USE_LOCALE_TIME | |
2078 | assert(categories[LC_TIME_INDEX] == LC_TIME); | |
2079 | assert(strEQ(category_names[LC_TIME_INDEX], "LC_TIME")); | |
2080 | # endif | |
2081 | # ifdef USE_LOCALE_MESSAGES | |
2082 | assert(categories[LC_MESSAGES_INDEX] == LC_MESSAGES); | |
2083 | assert(strEQ(category_names[LC_MESSAGES_INDEX], "LC_MESSAGES")); | |
2084 | # endif | |
2085 | # ifdef USE_LOCALE_MONETARY | |
2086 | assert(categories[LC_MONETARY_INDEX] == LC_MONETARY); | |
2087 | assert(strEQ(category_names[LC_MONETARY_INDEX], "LC_MONETARY")); | |
2088 | # endif | |
9821811f KW |
2089 | # ifdef USE_LOCALE_ADDRESS |
2090 | assert(categories[LC_ADDRESS_INDEX] == LC_ADDRESS); | |
2091 | assert(strEQ(category_names[LC_ADDRESS_INDEX], "LC_ADDRESS")); | |
2092 | # endif | |
2093 | # ifdef USE_LOCALE_IDENTIFICATION | |
2094 | assert(categories[LC_IDENTIFICATION_INDEX] == LC_IDENTIFICATION); | |
2095 | assert(strEQ(category_names[LC_IDENTIFICATION_INDEX], "LC_IDENTIFICATION")); | |
2096 | # endif | |
2097 | # ifdef USE_LOCALE_MEASUREMENT | |
2098 | assert(categories[LC_MEASUREMENT_INDEX] == LC_MEASUREMENT); | |
2099 | assert(strEQ(category_names[LC_MEASUREMENT_INDEX], "LC_MEASUREMENT")); | |
2100 | # endif | |
2101 | # ifdef USE_LOCALE_PAPER | |
2102 | assert(categories[LC_PAPER_INDEX] == LC_PAPER); | |
2103 | assert(strEQ(category_names[LC_PAPER_INDEX], "LC_PAPER")); | |
2104 | # endif | |
2105 | # ifdef USE_LOCALE_TELEPHONE | |
2106 | assert(categories[LC_TELEPHONE_INDEX] == LC_TELEPHONE); | |
2107 | assert(strEQ(category_names[LC_TELEPHONE_INDEX], "LC_TELEPHONE")); | |
2108 | # endif | |
948523db KW |
2109 | # ifdef LC_ALL |
2110 | assert(categories[LC_ALL_INDEX] == LC_ALL); | |
2111 | assert(strEQ(category_names[LC_ALL_INDEX], "LC_ALL")); | |
2112 | assert(NOMINAL_LC_ALL_INDEX == LC_ALL_INDEX); | |
2113 | # endif | |
2114 | # endif /* DEBUGGING */ | |
47280b20 KW |
2115 | |
2116 | /* Initialize the cache of the program's UTF-8ness for the always known | |
2117 | * locales C and POSIX */ | |
2118 | my_strlcpy(PL_locale_utf8ness, C_and_POSIX_utf8ness, | |
2119 | sizeof(PL_locale_utf8ness)); | |
2120 | ||
3ca88433 KW |
2121 | PL_numeric_radix_sv = newSVpvs("."); |
2122 | ||
b56c4436 | 2123 | # ifdef LOCALE_ENVIRON_REQUIRED |
98994639 HS |
2124 | |
2125 | /* | |
2126 | * Ultrix setlocale(..., "") fails if there are no environment | |
2127 | * variables from which to get a locale name. | |
2128 | */ | |
2129 | ||
b56c4436 KW |
2130 | # ifndef LC_ALL |
2131 | # error Ultrix without LC_ALL not implemented | |
2132 | # else | |
7d4bcc4a | 2133 | |
b56c4436 KW |
2134 | { |
2135 | bool done = FALSE; | |
ea92aad8 KW |
2136 | if (lang) { |
2137 | sl_result[LC_ALL_INDEX] = do_setlocale_c(LC_ALL, setlocale_init); | |
2138 | DEBUG_LOCALE_INIT(LC_ALL, setlocale_init, sl_result[LC_ALL_INDEX]); | |
2139 | if (sl_result[LC_ALL_INDEX]) | |
2140 | done = TRUE; | |
2141 | else | |
e5f10d49 | 2142 | setlocale_failure = TRUE; |
ea92aad8 KW |
2143 | } |
2144 | if (! setlocale_failure) { | |
2145 | const char * locale_param; | |
2146 | for (i = 0; i < LC_ALL_INDEX; i++) { | |
2147 | locale_param = (! done && (lang || PerlEnv_getenv(category_names[i]))) | |
2148 | ? setlocale_init | |
2149 | : NULL; | |
2150 | sl_result[i] = do_setlocale_r(categories[i], locale_param); | |
2151 | if (! sl_result[i]) { | |
2152 | setlocale_failure = TRUE; | |
2153 | } | |
2154 | DEBUG_LOCALE_INIT(categories[i], locale_param, sl_result[i]); | |
e5f10d49 | 2155 | } |
c835d6be | 2156 | } |
7d4bcc4a KW |
2157 | } |
2158 | ||
2159 | # endif /* LC_ALL */ | |
e5f10d49 | 2160 | # endif /* LOCALE_ENVIRON_REQUIRED */ |
98994639 | 2161 | |
65ebb059 | 2162 | /* We try each locale in the list until we get one that works, or exhaust |
20a240df KW |
2163 | * the list. Normally the loop is executed just once. But if setting the |
2164 | * locale fails, inside the loop we add fallback trials to the array and so | |
2165 | * will execute the loop multiple times */ | |
c3fcd832 KW |
2166 | trial_locales[0] = setlocale_init; |
2167 | trial_locales_count = 1; | |
7d4bcc4a | 2168 | |
65ebb059 KW |
2169 | for (i= 0; i < trial_locales_count; i++) { |
2170 | const char * trial_locale = trial_locales[i]; | |
2171 | ||
2172 | if (i > 0) { | |
2173 | ||
2174 | /* XXX This is to preserve old behavior for LOCALE_ENVIRON_REQUIRED | |
2175 | * when i==0, but I (khw) don't think that behavior makes much | |
2176 | * sense */ | |
2177 | setlocale_failure = FALSE; | |
2178 | ||
7d4bcc4a | 2179 | # ifdef SYSTEM_DEFAULT_LOCALE |
291a84fb | 2180 | # ifdef WIN32 /* Note that assumes Win32 has LC_ALL */ |
7d4bcc4a | 2181 | |
65ebb059 KW |
2182 | /* On Windows machines, an entry of "" after the 0th means to use |
2183 | * the system default locale, which we now proceed to get. */ | |
2184 | if (strEQ(trial_locale, "")) { | |
2185 | unsigned int j; | |
2186 | ||
2187 | /* Note that this may change the locale, but we are going to do | |
2188 | * that anyway just below */ | |
837ce802 | 2189 | system_default_locale = do_setlocale_c(LC_ALL, ""); |
5d1187d1 | 2190 | DEBUG_LOCALE_INIT(LC_ALL, "", system_default_locale); |
65ebb059 | 2191 | |
7d4bcc4a | 2192 | /* Skip if invalid or if it's already on the list of locales to |
65ebb059 KW |
2193 | * try */ |
2194 | if (! system_default_locale) { | |
2195 | goto next_iteration; | |
2196 | } | |
2197 | for (j = 0; j < trial_locales_count; j++) { | |
2198 | if (strEQ(system_default_locale, trial_locales[j])) { | |
2199 | goto next_iteration; | |
2200 | } | |
2201 | } | |
2202 | ||
2203 | trial_locale = system_default_locale; | |
2204 | } | |
ec0202b5 KW |
2205 | # else |
2206 | # error SYSTEM_DEFAULT_LOCALE only implemented for Win32 | |
2207 | # endif | |
7d4bcc4a | 2208 | # endif /* SYSTEM_DEFAULT_LOCALE */ |
291a84fb KW |
2209 | |
2210 | } /* For i > 0 */ | |
65ebb059 | 2211 | |
7d4bcc4a KW |
2212 | # ifdef LC_ALL |
2213 | ||
948523db KW |
2214 | sl_result[LC_ALL_INDEX] = do_setlocale_c(LC_ALL, trial_locale); |
2215 | DEBUG_LOCALE_INIT(LC_ALL, trial_locale, sl_result[LC_ALL_INDEX]); | |
2216 | if (! sl_result[LC_ALL_INDEX]) { | |
49c85077 | 2217 | setlocale_failure = TRUE; |
7cd8b568 KW |
2218 | } |
2219 | else { | |
2220 | /* Since LC_ALL succeeded, it should have changed all the other | |
2221 | * categories it can to its value; so we massage things so that the | |
2222 | * setlocales below just return their category's current values. | |
2223 | * This adequately handles the case in NetBSD where LC_COLLATE may | |
2224 | * not be defined for a locale, and setting it individually will | |
7d4bcc4a | 2225 | * fail, whereas setting LC_ALL succeeds, leaving LC_COLLATE set to |
7cd8b568 KW |
2226 | * the POSIX locale. */ |
2227 | trial_locale = NULL; | |
2228 | } | |
7d4bcc4a KW |
2229 | |
2230 | # endif /* LC_ALL */ | |
98994639 | 2231 | |
e5f10d49 KW |
2232 | if (! setlocale_failure) { |
2233 | unsigned int j; | |
2234 | for (j = 0; j < NOMINAL_LC_ALL_INDEX; j++) { | |
2235 | curlocales[j] | |
2236 | = savepv(do_setlocale_r(categories[j], trial_locale)); | |
2237 | if (! curlocales[j]) { | |
2238 | setlocale_failure = TRUE; | |
2239 | } | |
2240 | DEBUG_LOCALE_INIT(categories[j], trial_locale, curlocales[j]); | |
2241 | } | |
c835d6be | 2242 | |
e5f10d49 KW |
2243 | if (! setlocale_failure) { /* All succeeded */ |
2244 | break; /* Exit trial_locales loop */ | |
49c85077 | 2245 | } |
65ebb059 | 2246 | } |
98994639 | 2247 | |
49c85077 KW |
2248 | /* Here, something failed; will need to try a fallback. */ |
2249 | ok = 0; | |
65ebb059 | 2250 | |
49c85077 KW |
2251 | if (i == 0) { |
2252 | unsigned int j; | |
98994639 | 2253 | |
65ebb059 | 2254 | if (locwarn) { /* Output failure info only on the first one */ |
7d4bcc4a KW |
2255 | |
2256 | # ifdef LC_ALL | |
98994639 | 2257 | |
49c85077 KW |
2258 | PerlIO_printf(Perl_error_log, |
2259 | "perl: warning: Setting locale failed.\n"); | |
98994639 | 2260 | |
7d4bcc4a | 2261 | # else /* !LC_ALL */ |
98994639 | 2262 | |
49c85077 KW |
2263 | PerlIO_printf(Perl_error_log, |
2264 | "perl: warning: Setting locale failed for the categories:\n\t"); | |
7d4bcc4a | 2265 | |
e5f10d49 KW |
2266 | for (j = 0; j < NOMINAL_LC_ALL_INDEX; j++) { |
2267 | if (! curlocales[j]) { | |
2268 | PerlIO_printf(Perl_error_log, category_names[j]); | |
2269 | } | |
2270 | else { | |
2271 | Safefree(curlocales[j]); | |
2272 | } | |
2273 | } | |
7d4bcc4a | 2274 | |
7d4bcc4a | 2275 | # endif /* LC_ALL */ |
98994639 | 2276 | |
49c85077 KW |
2277 | PerlIO_printf(Perl_error_log, |
2278 | "perl: warning: Please check that your locale settings:\n"); | |
98994639 | 2279 | |
7d4bcc4a KW |
2280 | # ifdef __GLIBC__ |
2281 | ||
49c85077 KW |
2282 | PerlIO_printf(Perl_error_log, |
2283 | "\tLANGUAGE = %c%s%c,\n", | |
2284 | language ? '"' : '(', | |
2285 | language ? language : "unset", | |
2286 | language ? '"' : ')'); | |
7d4bcc4a | 2287 | # endif |
98994639 | 2288 | |
49c85077 KW |
2289 | PerlIO_printf(Perl_error_log, |
2290 | "\tLC_ALL = %c%s%c,\n", | |
2291 | lc_all ? '"' : '(', | |
2292 | lc_all ? lc_all : "unset", | |
2293 | lc_all ? '"' : ')'); | |
98994639 | 2294 | |
7d4bcc4a KW |
2295 | # if defined(USE_ENVIRON_ARRAY) |
2296 | ||
49c85077 | 2297 | { |
cd999af9 | 2298 | char **e; |
d5e32b93 KW |
2299 | |
2300 | /* Look through the environment for any variables of the | |
2301 | * form qr/ ^ LC_ [A-Z]+ = /x, except LC_ALL which was | |
2302 | * already handled above. These are assumed to be locale | |
2303 | * settings. Output them and their values. */ | |
cd999af9 | 2304 | for (e = environ; *e; e++) { |
d5e32b93 KW |
2305 | const STRLEN prefix_len = sizeof("LC_") - 1; |
2306 | STRLEN uppers_len; | |
2307 | ||
cd999af9 | 2308 | if ( strBEGINs(*e, "LC_") |
c8b388b0 | 2309 | && ! strBEGINs(*e, "LC_ALL=") |
d5e32b93 KW |
2310 | && (uppers_len = strspn(*e + prefix_len, |
2311 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ")) | |
2312 | && ((*e)[prefix_len + uppers_len] == '=')) | |
cd999af9 KW |
2313 | { |
2314 | PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n", | |
d5e32b93 KW |
2315 | (int) (prefix_len + uppers_len), *e, |
2316 | *e + prefix_len + uppers_len + 1); | |
cd999af9 KW |
2317 | } |
2318 | } | |
49c85077 | 2319 | } |
7d4bcc4a KW |
2320 | |
2321 | # else | |
2322 | ||
49c85077 KW |
2323 | PerlIO_printf(Perl_error_log, |
2324 | "\t(possibly more locale environment variables)\n"); | |
7d4bcc4a KW |
2325 | |
2326 | # endif | |
98994639 | 2327 | |
49c85077 KW |
2328 | PerlIO_printf(Perl_error_log, |
2329 | "\tLANG = %c%s%c\n", | |
2330 | lang ? '"' : '(', | |
2331 | lang ? lang : "unset", | |
2332 | lang ? '"' : ')'); | |
98994639 | 2333 | |
49c85077 KW |
2334 | PerlIO_printf(Perl_error_log, |
2335 | " are supported and installed on your system.\n"); | |
2336 | } | |
98994639 | 2337 | |
65ebb059 | 2338 | /* Calculate what fallback locales to try. We have avoided this |
f6bab5f6 | 2339 | * until we have to, because failure is quite unlikely. This will |
65ebb059 KW |
2340 | * usually change the upper bound of the loop we are in. |
2341 | * | |
2342 | * Since the system's default way of setting the locale has not | |
2343 | * found one that works, We use Perl's defined ordering: LC_ALL, | |
2344 | * LANG, and the C locale. We don't try the same locale twice, so | |
2345 | * don't add to the list if already there. (On POSIX systems, the | |
2346 | * LC_ALL element will likely be a repeat of the 0th element "", | |
6b058d42 KW |
2347 | * but there's no harm done by doing it explicitly. |
2348 | * | |
2349 | * Note that this tries the LC_ALL environment variable even on | |
2350 | * systems which have no LC_ALL locale setting. This may or may | |
2351 | * not have been originally intentional, but there's no real need | |
2352 | * to change the behavior. */ | |
65ebb059 KW |
2353 | if (lc_all) { |
2354 | for (j = 0; j < trial_locales_count; j++) { | |
2355 | if (strEQ(lc_all, trial_locales[j])) { | |
2356 | goto done_lc_all; | |
2357 | } | |
2358 | } | |
2359 | trial_locales[trial_locales_count++] = lc_all; | |
2360 | } | |
2361 | done_lc_all: | |
98994639 | 2362 | |
65ebb059 KW |
2363 | if (lang) { |
2364 | for (j = 0; j < trial_locales_count; j++) { | |
2365 | if (strEQ(lang, trial_locales[j])) { | |
2366 | goto done_lang; | |
2367 | } | |
2368 | } | |
2369 | trial_locales[trial_locales_count++] = lang; | |
2370 | } | |
2371 | done_lang: | |
2372 | ||
7d4bcc4a KW |
2373 | # if defined(WIN32) && defined(LC_ALL) |
2374 | ||
65ebb059 KW |
2375 | /* For Windows, we also try the system default locale before "C". |
2376 | * (If there exists a Windows without LC_ALL we skip this because | |
2377 | * it gets too complicated. For those, the "C" is the next | |
2378 | * fallback possibility). The "" is the same as the 0th element of | |
2379 | * the array, but the code at the loop above knows to treat it | |
2380 | * differently when not the 0th */ | |
2381 | trial_locales[trial_locales_count++] = ""; | |
7d4bcc4a KW |
2382 | |
2383 | # endif | |
65ebb059 KW |
2384 | |
2385 | for (j = 0; j < trial_locales_count; j++) { | |
2386 | if (strEQ("C", trial_locales[j])) { | |
2387 | goto done_C; | |
2388 | } | |
2389 | } | |
2390 | trial_locales[trial_locales_count++] = "C"; | |
98994639 | 2391 | |
65ebb059 KW |
2392 | done_C: ; |
2393 | } /* end of first time through the loop */ | |
98994639 | 2394 | |
7d4bcc4a KW |
2395 | # ifdef WIN32 |
2396 | ||
65ebb059 | 2397 | next_iteration: ; |
7d4bcc4a KW |
2398 | |
2399 | # endif | |
65ebb059 KW |
2400 | |
2401 | } /* end of looping through the trial locales */ | |
2402 | ||
2403 | if (ok < 1) { /* If we tried to fallback */ | |
2404 | const char* msg; | |
2405 | if (! setlocale_failure) { /* fallback succeeded */ | |
2406 | msg = "Falling back to"; | |
2407 | } | |
2408 | else { /* fallback failed */ | |
e5f10d49 | 2409 | unsigned int j; |
98994639 | 2410 | |
65ebb059 KW |
2411 | /* We dropped off the end of the loop, so have to decrement i to |
2412 | * get back to the value the last time through */ | |
2413 | i--; | |
98994639 | 2414 | |
65ebb059 KW |
2415 | ok = -1; |
2416 | msg = "Failed to fall back to"; | |
2417 | ||
2418 | /* To continue, we should use whatever values we've got */ | |
7d4bcc4a | 2419 | |
e5f10d49 KW |
2420 | for (j = 0; j < NOMINAL_LC_ALL_INDEX; j++) { |
2421 | Safefree(curlocales[j]); | |
2422 | curlocales[j] = savepv(do_setlocale_r(categories[j], NULL)); | |
2423 | DEBUG_LOCALE_INIT(categories[j], NULL, curlocales[j]); | |
2424 | } | |
65ebb059 KW |
2425 | } |
2426 | ||
2427 | if (locwarn) { | |
2428 | const char * description; | |
2429 | const char * name = ""; | |
2430 | if (strEQ(trial_locales[i], "C")) { | |
2431 | description = "the standard locale"; | |
2432 | name = "C"; | |
2433 | } | |
7d4bcc4a KW |
2434 | |
2435 | # ifdef SYSTEM_DEFAULT_LOCALE | |
2436 | ||
65ebb059 KW |
2437 | else if (strEQ(trial_locales[i], "")) { |
2438 | description = "the system default locale"; | |
2439 | if (system_default_locale) { | |
2440 | name = system_default_locale; | |
2441 | } | |
2442 | } | |
7d4bcc4a KW |
2443 | |
2444 | # endif /* SYSTEM_DEFAULT_LOCALE */ | |
2445 | ||
65ebb059 KW |
2446 | else { |
2447 | description = "a fallback locale"; | |
2448 | name = trial_locales[i]; | |
2449 | } | |
2450 | if (name && strNE(name, "")) { | |
2451 | PerlIO_printf(Perl_error_log, | |
2452 | "perl: warning: %s %s (\"%s\").\n", msg, description, name); | |
2453 | } | |
2454 | else { | |
2455 | PerlIO_printf(Perl_error_log, | |
2456 | "perl: warning: %s %s.\n", msg, description); | |
2457 | } | |
2458 | } | |
2459 | } /* End of tried to fallback */ | |
98994639 | 2460 | |
e5f10d49 KW |
2461 | /* Done with finding the locales; update our records */ |
2462 | ||
7d4bcc4a KW |
2463 | # ifdef USE_LOCALE_CTYPE |
2464 | ||
948523db | 2465 | new_ctype(curlocales[LC_CTYPE_INDEX]); |
98994639 | 2466 | |
e5f10d49 | 2467 | # endif |
7d4bcc4a KW |
2468 | # ifdef USE_LOCALE_COLLATE |
2469 | ||
948523db | 2470 | new_collate(curlocales[LC_COLLATE_INDEX]); |
98994639 | 2471 | |
e5f10d49 | 2472 | # endif |
7d4bcc4a KW |
2473 | # ifdef USE_LOCALE_NUMERIC |
2474 | ||
948523db | 2475 | new_numeric(curlocales[LC_NUMERIC_INDEX]); |
e5f10d49 KW |
2476 | |
2477 | # endif | |
2478 | ||
948523db | 2479 | for (i = 0; i < NOMINAL_LC_ALL_INDEX; i++) { |
47280b20 KW |
2480 | |
2481 | # if defined(USE_ITHREADS) | |
2482 | ||
2483 | /* This caches whether each category's locale is UTF-8 or not. This | |
2484 | * may involve changing the locale. It is ok to do this at | |
2485 | * initialization time before any threads have started, but not later. | |
2486 | * Caching means that if the program heeds our dictate not to change | |
2487 | * locales in threaded applications, this data will remain valid, and | |
2488 | * it may get queried without changing locales. If the environment is | |
2489 | * such that all categories have the same locale, this isn't needed, as | |
2490 | * the code will not change the locale; but this handles the uncommon | |
2491 | * case where the environment has disparate locales for the categories | |
2492 | * */ | |
2493 | (void) _is_cur_LC_category_utf8(categories[i]); | |
2494 | ||
2495 | # endif | |
2496 | ||
e5f10d49 KW |
2497 | Safefree(curlocales[i]); |
2498 | } | |
b310b053 | 2499 | |
7d4bcc4a KW |
2500 | # if defined(USE_PERLIO) && defined(USE_LOCALE_CTYPE) |
2501 | ||
49c85077 | 2502 | /* Set PL_utf8locale to TRUE if using PerlIO _and_ the current LC_CTYPE |
50bf02bd KW |
2503 | * locale is UTF-8. The call to new_ctype() just above has already |
2504 | * calculated the latter value and saved it in PL_in_utf8_CTYPE_locale. If | |
2505 | * both PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE}) | |
2506 | * are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer on | |
2507 | * STDIN, STDOUT, STDERR, _and_ the default open discipline. */ | |
2508 | PL_utf8locale = PL_in_utf8_CTYPE_locale; | |
49c85077 | 2509 | |
a05d7ebb | 2510 | /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO. |
fde18df1 JH |
2511 | This is an alternative to using the -C command line switch |
2512 | (the -C if present will override this). */ | |
2513 | { | |
dd374669 | 2514 | const char *p = PerlEnv_getenv("PERL_UNICODE"); |
a05d7ebb | 2515 | PL_unicode = p ? parse_unicode_opts(&p) : 0; |
5a22a2bb NC |
2516 | if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG) |
2517 | PL_utf8cache = -1; | |
b310b053 JH |
2518 | } |
2519 | ||
7d4bcc4a | 2520 | # endif |
7d4bcc4a KW |
2521 | # ifdef __GLIBC__ |
2522 | ||
175c4cf9 | 2523 | Safefree(language); |
7d4bcc4a KW |
2524 | |
2525 | # endif | |
175c4cf9 KW |
2526 | |
2527 | Safefree(lc_all); | |
2528 | Safefree(lang); | |
2529 | ||
e3305790 | 2530 | #endif /* USE_LOCALE */ |
2fcc0ca9 | 2531 | #ifdef DEBUGGING |
7d4bcc4a | 2532 | |
2fcc0ca9 | 2533 | /* So won't continue to output stuff */ |
27cdc72e | 2534 | DEBUG_INITIALIZATION_set(FALSE); |
7d4bcc4a | 2535 | |
2fcc0ca9 KW |
2536 | #endif |
2537 | ||
98994639 HS |
2538 | return ok; |
2539 | } | |
2540 | ||
98994639 HS |
2541 | #ifdef USE_LOCALE_COLLATE |
2542 | ||
a4a439fb | 2543 | char * |
a4a439fb KW |
2544 | Perl__mem_collxfrm(pTHX_ const char *input_string, |
2545 | STRLEN len, /* Length of 'input_string' */ | |
2546 | STRLEN *xlen, /* Set to length of returned string | |
2547 | (not including the collation index | |
2548 | prefix) */ | |
2549 | bool utf8 /* Is the input in UTF-8? */ | |
6696cfa7 | 2550 | ) |
98994639 | 2551 | { |
a4a439fb KW |
2552 | |
2553 | /* _mem_collxfrm() is a bit like strxfrm() but with two important | |
2554 | * differences. First, it handles embedded NULs. Second, it allocates a bit | |
2555 | * more memory than needed for the transformed data itself. The real | |
55e5378d | 2556 | * transformed data begins at offset COLLXFRM_HDR_LEN. *xlen is set to |
a4a439fb KW |
2557 | * the length of that, and doesn't include the collation index size. |
2558 | * Please see sv_collxfrm() to see how this is used. */ | |
2559 | ||
55e5378d KW |
2560 | #define COLLXFRM_HDR_LEN sizeof(PL_collation_ix) |
2561 | ||
6696cfa7 KW |
2562 | char * s = (char *) input_string; |
2563 | STRLEN s_strlen = strlen(input_string); | |
79f120c8 | 2564 | char *xbuf = NULL; |
55e5378d | 2565 | STRLEN xAlloc; /* xalloc is a reserved word in VC */ |
17f41037 | 2566 | STRLEN length_in_chars; |
c664130f | 2567 | bool first_time = TRUE; /* Cleared after first loop iteration */ |
98994639 | 2568 | |
a4a439fb KW |
2569 | PERL_ARGS_ASSERT__MEM_COLLXFRM; |
2570 | ||
2571 | /* Must be NUL-terminated */ | |
2572 | assert(*(input_string + len) == '\0'); | |
7918f24d | 2573 | |
79f120c8 KW |
2574 | /* If this locale has defective collation, skip */ |
2575 | if (PL_collxfrm_base == 0 && PL_collxfrm_mult == 0) { | |
c7202dee KW |
2576 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
2577 | "_mem_collxfrm: locale's collation is defective\n")); | |
79f120c8 KW |
2578 | goto bad; |
2579 | } | |
2580 | ||
6696cfa7 KW |
2581 | /* Replace any embedded NULs with the control that sorts before any others. |
2582 | * This will give as good as possible results on strings that don't | |
2583 | * otherwise contain that character, but otherwise there may be | |
2584 | * less-than-perfect results with that character and NUL. This is | |
fdc080f3 | 2585 | * unavoidable unless we replace strxfrm with our own implementation. */ |
fd43f63c KW |
2586 | if (UNLIKELY(s_strlen < len)) { /* Only execute if there is an embedded |
2587 | NUL */ | |
6696cfa7 KW |
2588 | char * e = s + len; |
2589 | char * sans_nuls; | |
fdc080f3 | 2590 | STRLEN sans_nuls_len; |
94762aa0 | 2591 | int try_non_controls; |
afc4976f KW |
2592 | char this_replacement_char[] = "?\0"; /* Room for a two-byte string, |
2593 | making sure 2nd byte is NUL. | |
2594 | */ | |
2595 | STRLEN this_replacement_len; | |
2596 | ||
1e4c9676 KW |
2597 | /* If we don't know what non-NUL control character sorts lowest for |
2598 | * this locale, find it */ | |
f28f4d2a | 2599 | if (PL_strxfrm_NUL_replacement == '\0') { |
6696cfa7 | 2600 | int j; |
afc4976f | 2601 | char * cur_min_x = NULL; /* The min_char's xfrm, (except it also |
6696cfa7 KW |
2602 | includes the collation index |
2603 | prefixed. */ | |
2604 | ||
91c0e2e0 | 2605 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "Looking to replace NUL\n")); |
94762aa0 KW |
2606 | |
2607 | /* Unlikely, but it may be that no control will work to replace | |
1e4c9676 KW |
2608 | * NUL, in which case we instead look for any character. Controls |
2609 | * are preferred because collation order is, in general, context | |
2610 | * sensitive, with adjoining characters affecting the order, and | |
2611 | * controls are less likely to have such interactions, allowing the | |
2612 | * NUL-replacement to stand on its own. (Another way to look at it | |
2613 | * is to imagine what would happen if the NUL were replaced by a | |
2614 | * combining character; it wouldn't work out all that well.) */ | |
94762aa0 KW |
2615 | for (try_non_controls = 0; |
2616 | try_non_controls < 2; | |
2617 | try_non_controls++) | |
2618 | { | |
d4ff9586 KW |
2619 | /* Look through all legal code points (NUL isn't) */ |
2620 | for (j = 1; j < 256; j++) { | |
2621 | char * x; /* j's xfrm plus collation index */ | |
2622 | STRLEN x_len; /* length of 'x' */ | |
2623 | STRLEN trial_len = 1; | |
736a4fed | 2624 | char cur_source[] = { '\0', '\0' }; |
d4ff9586 | 2625 | |
736a4fed KW |
2626 | /* Skip non-controls the first time through the loop. The |
2627 | * controls in a UTF-8 locale are the L1 ones */ | |
afc4976f KW |
2628 | if (! try_non_controls && (PL_in_utf8_COLLATE_locale) |
2629 | ? ! isCNTRL_L1(j) | |
2630 | : ! isCNTRL_LC(j)) | |
2631 | { | |
d4ff9586 | 2632 | continue; |
6696cfa7 | 2633 | } |
6696cfa7 | 2634 | |
736a4fed KW |
2635 | /* Create a 1-char string of the current code point */ |
2636 | cur_source[0] = (char) j; | |
2637 | ||
d4ff9586 KW |
2638 | /* Then transform it */ |
2639 | x = _mem_collxfrm(cur_source, trial_len, &x_len, | |
afc4976f | 2640 | 0 /* The string is not in UTF-8 */); |
6696cfa7 | 2641 | |
1e4c9676 | 2642 | /* Ignore any character that didn't successfully transform. |
d4ff9586 KW |
2643 | * */ |
2644 | if (! x) { | |
2645 | continue; | |
2646 | } | |
6696cfa7 | 2647 | |
d4ff9586 KW |
2648 | /* If this character's transformation is lower than |
2649 | * the current lowest, this one becomes the lowest */ | |
2650 | if ( cur_min_x == NULL | |
2651 | || strLT(x + COLLXFRM_HDR_LEN, | |
2652 | cur_min_x + COLLXFRM_HDR_LEN)) | |
2653 | { | |
f28f4d2a | 2654 | PL_strxfrm_NUL_replacement = j; |
d4ff9586 | 2655 | cur_min_x = x; |
d4ff9586 KW |
2656 | } |
2657 | else { | |
2658 | Safefree(x); | |
2659 | } | |
1e4c9676 | 2660 | } /* end of loop through all 255 characters */ |
6696cfa7 | 2661 | |
1e4c9676 | 2662 | /* Stop looking if found */ |
94762aa0 KW |
2663 | if (cur_min_x) { |
2664 | break; | |
2665 | } | |
2666 | ||
2667 | /* Unlikely, but possible, if there aren't any controls that | |
2668 | * work in the locale, repeat the loop, looking for any | |
2669 | * character that works */ | |
2670 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
2671 | "_mem_collxfrm: No control worked. Trying non-controls\n")); | |
1e4c9676 | 2672 | } /* End of loop to try first the controls, then any char */ |
6696cfa7 | 2673 | |
94762aa0 KW |
2674 | if (! cur_min_x) { |
2675 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
2676 | "_mem_collxfrm: Couldn't find any character to replace" | |
2677 | " embedded NULs in locale %s with", PL_collation_name)); | |
2678 | goto bad; | |
58eebef2 KW |
2679 | } |
2680 | ||
94762aa0 KW |
2681 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
2682 | "_mem_collxfrm: Replacing embedded NULs in locale %s with " | |
f28f4d2a | 2683 | "0x%02X\n", PL_collation_name, PL_strxfrm_NUL_replacement)); |
94762aa0 | 2684 | |
6696cfa7 | 2685 | Safefree(cur_min_x); |
1e4c9676 | 2686 | } /* End of determining the character that is to replace NULs */ |
afc4976f KW |
2687 | |
2688 | /* If the replacement is variant under UTF-8, it must match the | |
291a84fb | 2689 | * UTF8-ness of the original */ |
f28f4d2a KW |
2690 | if ( ! UVCHR_IS_INVARIANT(PL_strxfrm_NUL_replacement) && utf8) { |
2691 | this_replacement_char[0] = | |
2692 | UTF8_EIGHT_BIT_HI(PL_strxfrm_NUL_replacement); | |
2693 | this_replacement_char[1] = | |
2694 | UTF8_EIGHT_BIT_LO(PL_strxfrm_NUL_replacement); | |
afc4976f KW |
2695 | this_replacement_len = 2; |
2696 | } | |
2697 | else { | |
f28f4d2a | 2698 | this_replacement_char[0] = PL_strxfrm_NUL_replacement; |
afc4976f KW |
2699 | /* this_replacement_char[1] = '\0' was done at initialization */ |
2700 | this_replacement_len = 1; | |
6696cfa7 KW |
2701 | } |
2702 | ||
2703 | /* The worst case length for the replaced string would be if every | |
2704 | * character in it is NUL. Multiply that by the length of each | |
2705 | * replacement, and allow for a trailing NUL */ | |
afc4976f | 2706 | sans_nuls_len = (len * this_replacement_len) + 1; |
fdc080f3 | 2707 | Newx(sans_nuls, sans_nuls_len, char); |
6696cfa7 KW |
2708 | *sans_nuls = '\0'; |
2709 | ||
6696cfa7 KW |
2710 | /* Replace each NUL with the lowest collating control. Loop until have |
2711 | * exhausted all the NULs */ | |
2712 | while (s + s_strlen < e) { | |
6069d6c5 | 2713 | my_strlcat(sans_nuls, s, sans_nuls_len); |
6696cfa7 KW |
2714 | |
2715 | /* Do the actual replacement */ | |
6069d6c5 | 2716 | my_strlcat(sans_nuls, this_replacement_char, sans_nuls_len); |
6696cfa7 KW |
2717 | |
2718 | /* Move past the input NUL */ | |
2719 | s += s_strlen + 1; | |
2720 | s_strlen = strlen(s); | |
2721 | } | |
2722 | ||
2723 | /* And add anything that trails the final NUL */ | |
6069d6c5 | 2724 | my_strlcat(sans_nuls, s, sans_nuls_len); |
6696cfa7 KW |
2725 | |
2726 | /* Switch so below we transform this modified string */ | |
2727 | s = sans_nuls; | |
2728 | len = strlen(s); | |
1e4c9676 | 2729 | } /* End of replacing NULs */ |
6696cfa7 | 2730 | |
a4a439fb KW |
2731 | /* Make sure the UTF8ness of the string and locale match */ |
2732 | if (utf8 != PL_in_utf8_COLLATE_locale) { | |
2733 | const char * const t = s; /* Temporary so we can later find where the | |
2734 | input was */ | |
2735 | ||
2736 | /* Here they don't match. Change the string's to be what the locale is | |
2737 | * expecting */ | |
2738 | ||
2739 | if (! utf8) { /* locale is UTF-8, but input isn't; upgrade the input */ | |
2740 | s = (char *) bytes_to_utf8((const U8 *) s, &len); | |
2741 | utf8 = TRUE; | |
2742 | } | |
2743 | else { /* locale is not UTF-8; but input is; downgrade the input */ | |
2744 | ||
2745 | s = (char *) bytes_from_utf8((const U8 *) s, &len, &utf8); | |
2746 | ||
2747 | /* If the downgrade was successful we are done, but if the input | |
2748 | * contains things that require UTF-8 to represent, have to do | |
2749 | * damage control ... */ | |
2750 | if (UNLIKELY(utf8)) { | |
2751 | ||
2752 | /* What we do is construct a non-UTF-8 string with | |
2753 | * 1) the characters representable by a single byte converted | |
2754 | * to be so (if necessary); | |
2755 | * 2) and the rest converted to collate the same as the | |
2756 | * highest collating representable character. That makes | |
2757 | * them collate at the end. This is similar to how we | |
2758 | * handle embedded NULs, but we use the highest collating | |
2759 | * code point instead of the smallest. Like the NUL case, | |
2760 | * this isn't perfect, but is the best we can reasonably | |
2761 | * do. Every above-255 code point will sort the same as | |
2762 | * the highest-sorting 0-255 code point. If that code | |
2763 | * point can combine in a sequence with some other code | |
2764 | * points for weight calculations, us changing something to | |
2765 | * be it can adversely affect the results. But in most | |
2766 | * cases, it should work reasonably. And note that this is | |
2767 | * really an illegal situation: using code points above 255 | |
2768 | * on a locale where only 0-255 are valid. If two strings | |
2769 | * sort entirely equal, then the sort order for the | |
2770 | * above-255 code points will be in code point order. */ | |
2771 | ||
2772 | utf8 = FALSE; | |
2773 | ||
2774 | /* If we haven't calculated the code point with the maximum | |
2775 | * collating order for this locale, do so now */ | |
2776 | if (! PL_strxfrm_max_cp) { | |
2777 | int j; | |
2778 | ||
2779 | /* The current transformed string that collates the | |
2780 | * highest (except it also includes the prefixed collation | |
2781 | * index. */ | |
2782 | char * cur_max_x = NULL; | |
2783 | ||
2784 | /* Look through all legal code points (NUL isn't) */ | |
2785 | for (j = 1; j < 256; j++) { | |
2786 | char * x; | |
2787 | STRLEN x_len; | |
736a4fed | 2788 | char cur_source[] = { '\0', '\0' }; |
a4a439fb | 2789 | |
736a4fed KW |
2790 | /* Create a 1-char string of the current code point */ |
2791 | cur_source[0] = (char) j; | |
a4a439fb KW |
2792 | |
2793 | /* Then transform it */ | |
2794 | x = _mem_collxfrm(cur_source, 1, &x_len, FALSE); | |
2795 | ||
2796 | /* If something went wrong (which it shouldn't), just | |
2797 | * ignore this code point */ | |
94762aa0 | 2798 | if (! x) { |
a4a439fb KW |
2799 | continue; |
2800 | } | |
2801 | ||
2802 | /* If this character's transformation is higher than | |
2803 | * the current highest, this one becomes the highest */ | |
2804 | if ( cur_max_x == NULL | |
55e5378d KW |
2805 | || strGT(x + COLLXFRM_HDR_LEN, |
2806 | cur_max_x + COLLXFRM_HDR_LEN)) | |
a4a439fb KW |
2807 | { |
2808 | PL_strxfrm_max_cp = j; | |
2809 | cur_max_x = x; | |
2810 | } | |
2811 | else { | |
2812 | Safefree(x); | |
2813 | } | |
2814 | } | |
2815 | ||
94762aa0 KW |
2816 | if (! cur_max_x) { |
2817 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
2818 | "_mem_collxfrm: Couldn't find any character to" | |
2819 | " replace above-Latin1 chars in locale %s with", | |
2820 | PL_collation_name)); | |
2821 | goto bad; | |
2822 | } | |
2823 | ||
58eebef2 KW |
2824 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
2825 | "_mem_collxfrm: highest 1-byte collating character" | |
2826 | " in locale %s is 0x%02X\n", | |
2827 | PL_collation_name, | |
2828 | PL_strxfrm_max_cp)); | |
58eebef2 | 2829 | |
a4a439fb KW |
2830 | Safefree(cur_max_x); |
2831 | } | |
2832 | ||
2833 | /* Here we know which legal code point collates the highest. | |
2834 | * We are ready to construct the non-UTF-8 string. The length | |
2835 | * will be at least 1 byte smaller than the input string | |
2836 | * (because we changed at least one 2-byte character into a | |
2837 | * single byte), but that is eaten up by the trailing NUL */ | |
2838 | Newx(s, len, char); | |
2839 | ||
2840 | { | |
2841 | STRLEN i; | |
2842 | STRLEN d= 0; | |
042d9e50 | 2843 | char * e = (char *) t + len; |
a4a439fb KW |
2844 | |
2845 | for (i = 0; i < len; i+= UTF8SKIP(t + i)) { | |
2846 | U8 cur_char = t[i]; | |
2847 | if (UTF8_IS_INVARIANT(cur_char)) { | |
2848 | s[d++] = cur_char; | |
2849 | } | |
042d9e50 | 2850 | else if (UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(t + i, e)) { |
a4a439fb KW |
2851 | s[d++] = EIGHT_BIT_UTF8_TO_NATIVE(cur_char, t[i+1]); |
2852 | } | |
2853 | else { /* Replace illegal cp with highest collating | |
2854 | one */ | |
2855 | s[d++] = PL_strxfrm_max_cp; | |
2856 | } | |
2857 | } | |
2858 | s[d++] = '\0'; | |
2859 | Renew(s, d, char); /* Free up unused space */ | |
2860 | } | |
2861 | } | |
2862 | } | |
2863 | ||
2864 | /* Here, we have constructed a modified version of the input. It could | |
2865 | * be that we already had a modified copy before we did this version. | |
2866 | * If so, that copy is no longer needed */ | |
2867 | if (t != input_string) { | |
2868 | Safefree(t); | |
2869 | } | |
2870 | } | |
2871 | ||
17f41037 KW |
2872 | length_in_chars = (utf8) |
2873 | ? utf8_length((U8 *) s, (U8 *) s + len) | |
2874 | : len; | |
2875 | ||
59c018b9 KW |
2876 | /* The first element in the output is the collation id, used by |
2877 | * sv_collxfrm(); then comes the space for the transformed string. The | |
2878 | * equation should give us a good estimate as to how much is needed */ | |
55e5378d | 2879 | xAlloc = COLLXFRM_HDR_LEN |
a4a439fb | 2880 | + PL_collxfrm_base |
17f41037 | 2881 | + (PL_collxfrm_mult * length_in_chars); |
a02a5408 | 2882 | Newx(xbuf, xAlloc, char); |
c7202dee KW |
2883 | if (UNLIKELY(! xbuf)) { |
2884 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
2885 | "_mem_collxfrm: Couldn't malloc %zu bytes\n", xAlloc)); | |
98994639 | 2886 | goto bad; |
c7202dee | 2887 | } |
98994639 | 2888 | |
d35fca5f | 2889 | /* Store the collation id */ |
98994639 | 2890 | *(U32*)xbuf = PL_collation_ix; |
d35fca5f KW |
2891 | |
2892 | /* Then the transformation of the input. We loop until successful, or we | |
2893 | * give up */ | |
4ebeff16 | 2894 | for (;;) { |
1adab0a7 | 2895 | |
55e5378d | 2896 | *xlen = strxfrm(xbuf + COLLXFRM_HDR_LEN, s, xAlloc - COLLXFRM_HDR_LEN); |
4ebeff16 KW |
2897 | |
2898 | /* If the transformed string occupies less space than we told strxfrm() | |
2899 | * was available, it means it successfully transformed the whole | |
2900 | * string. */ | |
55e5378d | 2901 | if (*xlen < xAlloc - COLLXFRM_HDR_LEN) { |
17f41037 | 2902 | |
1adab0a7 KW |
2903 | /* Some systems include a trailing NUL in the returned length. |
2904 | * Ignore it, using a loop in case multiple trailing NULs are | |
2905 | * returned. */ | |
2906 | while ( (*xlen) > 0 | |
2907 | && *(xbuf + COLLXFRM_HDR_LEN + (*xlen) - 1) == '\0') | |
2908 | { | |
2909 | (*xlen)--; | |
2910 | } | |
2911 | ||
17f41037 KW |
2912 | /* If the first try didn't get it, it means our prediction was low. |
2913 | * Modify the coefficients so that we predict a larger value in any | |
2914 | * future transformations */ | |
2915 | if (! first_time) { | |
2916 | STRLEN needed = *xlen + 1; /* +1 For trailing NUL */ | |
2917 | STRLEN computed_guess = PL_collxfrm_base | |
2918 | + (PL_collxfrm_mult * length_in_chars); | |
e1c30f0c KW |
2919 | |
2920 | /* On zero-length input, just keep current slope instead of | |
2921 | * dividing by 0 */ | |
2922 | const STRLEN new_m = (length_in_chars != 0) | |
2923 | ? needed / length_in_chars | |
2924 | : PL_collxfrm_mult; | |
17f41037 KW |
2925 | |
2926 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, | |
b07929e4 KW |
2927 | "%s: %d: initial size of %zu bytes for a length " |
2928 | "%zu string was insufficient, %zu needed\n", | |
17f41037 | 2929 | __FILE__, __LINE__, |
b07929e4 | 2930 | computed_guess, length_in_chars, needed)); |
17f41037 KW |
2931 | |
2932 | /* If slope increased, use it, but discard this result for | |
2933 | * length 1 strings, as we can't be sure that it's a real slope | |
2934 | * change */ | |
2935 | if (length_in_chars > 1 && new_m > PL_collxfrm_mult) { | |
7d4bcc4a KW |
2936 | |
2937 | # ifdef DEBUGGING | |
2938 | ||
17f41037 KW |
2939 | STRLEN old_m = PL_collxfrm_mult; |
2940 | STRLEN old_b = PL_collxfrm_base; | |
7d4bcc4a KW |
2941 | |
2942 | # endif | |
2943 | ||
17f41037 KW |
2944 | PL_collxfrm_mult = new_m; |
2945 | PL_collxfrm_base = 1; /* +1 For trailing NUL */ | |
2946 | computed_guess = PL_collxfrm_base | |
2947 | + (PL_collxfrm_mult * length_in_chars); | |
2948 | if (computed_guess < needed) { | |
2949 | PL_collxfrm_base += needed - computed_guess; | |
2950 | } | |
2951 | ||
2952 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, | |
b07929e4 KW |
2953 | "%s: %d: slope is now %zu; was %zu, base " |
2954 | "is now %zu; was %zu\n", | |
17f41037 | 2955 | __FILE__, __LINE__, |
b07929e4 KW |
2956 | PL_collxfrm_mult, old_m, |
2957 | PL_collxfrm_base, old_b)); | |
17f41037 KW |
2958 | } |
2959 | else { /* Slope didn't change, but 'b' did */ | |
2960 | const STRLEN new_b = needed | |
2961 | - computed_guess | |
2962 | + PL_collxfrm_base; | |
2963 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, | |
b07929e4 | 2964 | "%s: %d: base is now %zu; was %zu\n", |
17f41037 | 2965 | __FILE__, __LINE__, |
b07929e4 | 2966 | new_b, PL_collxfrm_base)); |
17f41037 KW |
2967 | PL_collxfrm_base = new_b; |
2968 | } | |
2969 | } | |
2970 | ||
4ebeff16 KW |
2971 | break; |
2972 | } | |
bb0f664e | 2973 | |
c7202dee KW |
2974 | if (UNLIKELY(*xlen >= PERL_INT_MAX)) { |
2975 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
2976 | "_mem_collxfrm: Needed %zu bytes, max permissible is %u\n", | |
2977 | *xlen, PERL_INT_MAX)); | |
4ebeff16 | 2978 | goto bad; |
c7202dee | 2979 | } |
d35fca5f | 2980 | |
c664130f | 2981 | /* A well-behaved strxfrm() returns exactly how much space it needs |
1adab0a7 KW |
2982 | * (usually not including the trailing NUL) when it fails due to not |
2983 | * enough space being provided. Assume that this is the case unless | |
2984 | * it's been proven otherwise */ | |
c664130f | 2985 | if (LIKELY(PL_strxfrm_is_behaved) && first_time) { |
55e5378d | 2986 | xAlloc = *xlen + COLLXFRM_HDR_LEN + 1; |
c664130f KW |
2987 | } |
2988 | else { /* Here, either: | |
2989 | * 1) The strxfrm() has previously shown bad behavior; or | |
2990 | * 2) It isn't the first time through the loop, which means | |
2991 | * that the strxfrm() is now showing bad behavior, because | |
2992 | * we gave it what it said was needed in the previous | |
2993 | * iteration, and it came back saying it needed still more. | |
2994 | * (Many versions of cygwin fit this. When the buffer size | |
2995 | * isn't sufficient, they return the input size instead of | |
2996 | * how much is needed.) | |
d4ff9586 KW |
2997 | * Increase the buffer size by a fixed percentage and try again. |
2998 | * */ | |
6ddd902c | 2999 | xAlloc += (xAlloc / 4) + 1; |
c664130f | 3000 | PL_strxfrm_is_behaved = FALSE; |
c664130f | 3001 | |
7d4bcc4a KW |
3002 | # ifdef DEBUGGING |
3003 | ||
58eebef2 KW |
3004 | if (DEBUG_Lv_TEST || debug_initialization) { |
3005 | PerlIO_printf(Perl_debug_log, | |
3006 | "_mem_collxfrm required more space than previously calculated" | |
b07929e4 | 3007 | " for locale %s, trying again with new guess=%d+%zu\n", |
58eebef2 | 3008 | PL_collation_name, (int) COLLXFRM_HDR_LEN, |
b07929e4 | 3009 | xAlloc - COLLXFRM_HDR_LEN); |
58eebef2 | 3010 | } |
7d4bcc4a KW |
3011 | |
3012 | # endif | |
3013 | ||
58eebef2 | 3014 | } |
c664130f | 3015 | |
4ebeff16 | 3016 | Renew(xbuf, xAlloc, char); |
c7202dee KW |
3017 | if (UNLIKELY(! xbuf)) { |
3018 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
3019 | "_mem_collxfrm: Couldn't realloc %zu bytes\n", xAlloc)); | |
4ebeff16 | 3020 | goto bad; |
c7202dee | 3021 | } |
c664130f KW |
3022 | |
3023 | first_time = FALSE; | |
4ebeff16 | 3024 | } |
98994639 | 3025 | |
6696cfa7 | 3026 | |
7d4bcc4a KW |
3027 | # ifdef DEBUGGING |
3028 | ||
58eebef2 | 3029 | if (DEBUG_Lv_TEST || debug_initialization) { |
c7202dee KW |
3030 | |
3031 | print_collxfrm_input_and_return(s, s + len, xlen, utf8); | |
3032 | PerlIO_printf(Perl_debug_log, "Its xfrm is:"); | |
7e2f38b2 KW |
3033 | PerlIO_printf(Perl_debug_log, "%s\n", |
3034 | _byte_dump_string((U8 *) xbuf + COLLXFRM_HDR_LEN, | |
3035 | *xlen, 1)); | |
58eebef2 | 3036 | } |
7d4bcc4a KW |
3037 | |
3038 | # endif | |
58eebef2 | 3039 | |
3c5f993e | 3040 | /* Free up unneeded space; retain ehough for trailing NUL */ |
55e5378d | 3041 | Renew(xbuf, COLLXFRM_HDR_LEN + *xlen + 1, char); |
98994639 | 3042 | |
6696cfa7 KW |
3043 | if (s != input_string) { |
3044 | Safefree(s); | |
98994639 HS |
3045 | } |
3046 | ||
98994639 HS |
3047 | return xbuf; |
3048 | ||
3049 | bad: | |
3050 | Safefree(xbuf); | |
6696cfa7 KW |
3051 | if (s != input_string) { |
3052 | Safefree(s); | |
3053 | } | |
98994639 | 3054 | *xlen = 0; |
7d4bcc4a KW |
3055 | |
3056 | # ifdef DEBUGGING | |
3057 | ||
58eebef2 | 3058 | if (DEBUG_Lv_TEST || debug_initialization) { |
c7202dee | 3059 | print_collxfrm_input_and_return(s, s + len, NULL, utf8); |
58eebef2 | 3060 | } |
7d4bcc4a KW |
3061 | |
3062 | # endif | |
3063 | ||
98994639 HS |
3064 | return NULL; |
3065 | } | |
3066 | ||
7d4bcc4a | 3067 | # ifdef DEBUGGING |
c7202dee | 3068 | |
4cbaac56 | 3069 | STATIC void |
c7202dee KW |
3070 | S_print_collxfrm_input_and_return(pTHX_ |
3071 | const char * const s, | |
3072 | const char * const e, | |
3073 | const STRLEN * const xlen, | |
3074 | const bool is_utf8) | |
3075 | { | |
c7202dee KW |
3076 | |
3077 | PERL_ARGS_ASSERT_PRINT_COLLXFRM_INPUT_AND_RETURN; | |
3078 | ||
511e4ff7 DM |
3079 | PerlIO_printf(Perl_debug_log, "_mem_collxfrm[%" UVuf "]: returning ", |
3080 | (UV)PL_collation_ix); | |
c7202dee | 3081 | if (xlen) { |
08b6dc1d | 3082 | PerlIO_printf(Perl_debug_log, "%zu", *xlen); |
c7202dee KW |
3083 | } |
3084 | else { | |
3085 | PerlIO_printf(Perl_debug_log, "NULL"); | |
3086 | } | |
3087 | PerlIO_printf(Perl_debug_log, " for locale '%s', string='", | |
3088 | PL_collation_name); | |
9c8a6dc2 KW |
3089 | print_bytes_for_locale(s, e, is_utf8); |
3090 | ||
3091 | PerlIO_printf(Perl_debug_log, "'\n"); | |
3092 | } | |
3093 | ||
3094 | STATIC void | |
3095 | S_print_bytes_for_locale(pTHX_ | |
3096 | const char * const s, | |
3097 | const char * const e, | |
3098 | const bool is_utf8) | |
3099 | { | |
3100 | const char * t = s; | |
3101 | bool prev_was_printable = TRUE; | |
3102 | bool first_time = TRUE; | |
3103 | ||
3104 | PERL_ARGS_ASSERT_PRINT_BYTES_FOR_LOCALE; | |
c7202dee KW |
3105 | |
3106 | while (t < e) { | |
3107 | UV cp = (is_utf8) | |
3108 | ? utf8_to_uvchr_buf((U8 *) t, e, NULL) | |
3109 | : * (U8 *) t; | |
3110 | if (isPRINT(cp)) { | |
3111 | if (! prev_was_printable) { | |
3112 | PerlIO_printf(Perl_debug_log, " "); | |
3113 | } | |
3114 | PerlIO_printf(Perl_debug_log, "%c", (U8) cp); | |
3115 | prev_was_printable = TRUE; | |
3116 | } | |
3117 | else { | |
3118 | if (! first_time) { | |
3119 | PerlIO_printf(Perl_debug_log, " "); | |
3120 | } | |
147e3846 | 3121 | PerlIO_printf(Perl_debug_log, "%02" UVXf, cp); |
c7202dee KW |
3122 | prev_was_printable = FALSE; |
3123 | } | |
3124 | t += (is_utf8) ? UTF8SKIP(t) : 1; | |
3125 | first_time = FALSE; | |
3126 | } | |
c7202dee KW |
3127 | } |
3128 | ||
7d4bcc4a | 3129 | # endif /* #ifdef DEBUGGING */ |
98994639 | 3130 | #endif /* USE_LOCALE_COLLATE */ |
58eebef2 | 3131 | |
8ef6e574 KW |
3132 | #ifdef USE_LOCALE |
3133 | ||
962aa53f KW |
3134 | STATIC const char * |
3135 | S_switch_category_locale_to_template(pTHX_ const int switch_category, const int template_category, const char * template_locale) | |
3136 | { | |
3137 | /* Changes the locale for LC_'switch_category" to that of | |
3138 | * LC_'template_category', if they aren't already the same. If not NULL, | |
3139 | * 'template_locale' is the locale that 'template_category' is in. | |
3140 | * | |
3141 | * Returns the original locale for 'switch_category' so can be switched | |
3142 | * back to with the companion function restore_switched_locale(), (NULL if | |
3143 | * no restoral is necessary.) */ | |
3144 | ||
3145 | char * restore_to_locale = NULL; | |
3146 | ||
3147 | if (switch_category == template_category) { /* No changes needed */ | |
3148 | return NULL; | |
3149 | } | |
3150 | ||
3151 | /* Find the original locale of the category we may need to change, so that | |
3152 | * it can be restored to later */ | |
d6189047 KW |
3153 | restore_to_locale = stdize_locale(savepv(do_setlocale_r(switch_category, |
3154 | NULL))); | |
962aa53f KW |
3155 | if (! restore_to_locale) { |
3156 | Perl_croak(aTHX_ | |
3157 | "panic: %s: %d: Could not find current %s locale, errno=%d\n", | |
3158 | __FILE__, __LINE__, category_name(switch_category), errno); | |
3159 | } | |
962aa53f KW |
3160 | |
3161 | /* If the locale of the template category wasn't passed in, find it now */ | |
3162 | if (template_locale == NULL) { | |
3163 | template_locale = do_setlocale_r(template_category, NULL); | |
3164 | if (! template_locale) { | |
3165 | Perl_croak(aTHX_ | |
3166 | "panic: %s: %d: Could not find current %s locale, errno=%d\n", | |
3167 | __FILE__, __LINE__, category_name(template_category), errno); | |
3168 | } | |
3169 | } | |
3170 | ||
3171 | /* It the locales are the same, there's nothing to do */ | |
3172 | if (strEQ(restore_to_locale, template_locale)) { | |
3173 | Safefree(restore_to_locale); | |
3174 | ||
3175 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s locale unchanged as %s\n", | |
3176 | category_name(switch_category), restore_to_locale)); | |
3177 | ||
3178 | return NULL; | |
3179 | } | |
3180 | ||
3181 | /* Finally, change the locale to the template one */ | |
3182 | if (! do_setlocale_r(switch_category, template_locale)) { | |
3183 | Perl_croak(aTHX_ | |
3184 | "panic: %s: %d: Could not change %s locale to %s, errno=%d\n", | |
3185 | __FILE__, __LINE__, category_name(switch_category), | |
3186 | template_locale, errno); | |
3187 | } | |
3188 | ||
3189 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s locale switched to %s\n", | |
3190 | category_name(switch_category), template_locale)); | |
3191 | ||
3192 | return restore_to_locale; | |
3193 | } | |
3194 | ||
3195 | STATIC void | |
3196 | S_restore_switched_locale(pTHX_ const int category, const char * const original_locale) | |
3197 | { | |
3198 | /* Restores the locale for LC_'category' to 'original_locale', or do | |
3199 | * nothing if the latter parameter is NULL */ | |
3200 | ||
3201 | if (original_locale == NULL) { | |
3202 | return; | |
3203 | } | |
3204 | ||
3205 | if (! do_setlocale_r(category, original_locale)) { | |
3206 | Perl_croak(aTHX_ | |
3207 | "panic: %s: %d: setlocale %s restore to %s failed, errno=%d\n", | |
3208 | __FILE__, __LINE__, | |
3209 | category_name(category), original_locale, errno); | |
3210 | } | |
3211 | ||
3212 | Safefree(original_locale); | |
3213 | } | |
3214 | ||
c1284011 KW |
3215 | bool |
3216 | Perl__is_cur_LC_category_utf8(pTHX_ int category) | |
7d74bb61 KW |
3217 | { |
3218 | /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE | |
3219 | * otherwise. 'category' may not be LC_ALL. If the platform doesn't have | |
119ee68b | 3220 | * nl_langinfo(), nor MB_CUR_MAX, this employs a heuristic, which hence |
609548d2 KW |
3221 | * could give the wrong result. The result will very likely be correct for |
3222 | * languages that have commonly used non-ASCII characters, but for notably | |
3223 | * English, it comes down to if the locale's name ends in something like | |
3224 | * "UTF-8". It errs on the side of not being a UTF-8 locale. */ | |
7d74bb61 | 3225 | |
47280b20 | 3226 | /* Name of current locale corresponding to the input category */ |
8de4332b | 3227 | const char *save_input_locale = NULL; |
47280b20 KW |
3228 | |
3229 | bool is_utf8 = FALSE; /* The return value */ | |
7d74bb61 | 3230 | |
47280b20 KW |
3231 | /* The variables below are for the cache of previous lookups using this |
3232 | * function. The cache is a C string, described at the definition for | |
3233 | * 'C_and_POSIX_utf8ness'. | |
3234 | * | |
3235 | * The first part of the cache is fixed, for the C and POSIX locales. The | |
3236 | * varying part starts just after them. */ | |
3237 | char * utf8ness_cache = PL_locale_utf8ness + STRLENs(C_and_POSIX_utf8ness); | |
3238 | ||
3239 | Size_t utf8ness_cache_size; /* Size of the varying portion */ | |
3240 | Size_t input_name_len; /* Length in bytes of save_input_locale */ | |
3241 | Size_t input_name_len_with_overhead; /* plus extra chars used to store | |
3242 | the name in the cache */ | |
3243 | char * delimited; /* The name plus the delimiters used to store | |
3244 | it in the cache */ | |
3245 | char * name_pos; /* position of 'delimited' in the cache, or 0 | |
3246 | if not there */ | |
3247 | ||
3248 | ||
7d4bcc4a KW |
3249 | # ifdef LC_ALL |
3250 | ||
7d74bb61 | 3251 | assert(category != LC_ALL); |
7d4bcc4a KW |
3252 | |
3253 | # endif | |
7d74bb61 | 3254 | |
47280b20 | 3255 | /* Get the desired category's locale */ |
d6189047 | 3256 | save_input_locale = stdize_locale(savepv(do_setlocale_r(category, NULL))); |
7d74bb61 | 3257 | if (! save_input_locale) { |
47280b20 | 3258 | Perl_croak(aTHX_ |
d707d779 KW |
3259 | "panic: %s: %d: Could not find current %s locale, errno=%d\n", |
3260 | __FILE__, __LINE__, category_name(category), errno); | |
7d74bb61 | 3261 | } |
47280b20 | 3262 | |
47280b20 KW |
3263 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
3264 | "Current locale for %s is %s\n", | |
3265 | category_name(category), save_input_locale)); | |
3266 | ||
3267 | input_name_len = strlen(save_input_locale); | |
3268 | ||
3269 | /* In our cache, each name is accompanied by two delimiters and a single | |
3270 | * utf8ness digit */ | |
3271 | input_name_len_with_overhead = input_name_len + 3; | |
3272 | ||
3273 | /* Allocate and populate space for a copy of the name surrounded by the | |
3274 | * delimiters */ | |
3275 | Newx(delimited, input_name_len_with_overhead, char); | |
3276 | delimited[0] = UTF8NESS_SEP[0]; | |
3277 | Copy(save_input_locale, delimited + 1, input_name_len, char); | |
3278 | delimited[input_name_len+1] = UTF8NESS_PREFIX[0]; | |
3279 | delimited[input_name_len+2] = '\0'; | |
3280 | ||
3281 | /* And see if that is in the cache */ | |
3282 | name_pos = instr(PL_locale_utf8ness, delimited); | |
3283 | if (name_pos) { | |
3284 | is_utf8 = *(name_pos + input_name_len_with_overhead - 1) - '0'; | |
3285 | ||
3286 | # ifdef DEBUGGING | |
3287 | ||
3288 | if (DEBUG_Lv_TEST || debug_initialization) { | |
3289 | PerlIO_printf(Perl_debug_log, "UTF8ness for locale %s=%d, \n", | |
3290 | save_input_locale, is_utf8); | |
3291 | } | |
3292 | ||
3293 | # endif | |
3294 | ||
3295 | /* And, if not already in that position, move it to the beginning of | |
3296 | * the non-constant portion of the list, since it is the most recently | |
3297 | * used. (We don't have to worry about overflow, since just moving | |
3298 | * existing names around) */ | |
3299 | if (name_pos > utf8ness_cache) { | |
3300 | Move(utf8ness_cache, | |
3301 | utf8ness_cache + input_name_len_with_overhead, | |
3302 | name_pos - utf8ness_cache, char); | |
3303 | Copy(delimited, | |
3304 | utf8ness_cache, | |
3305 | input_name_len_with_overhead - 1, char); | |
3306 | utf8ness_cache[input_name_len_with_overhead - 1] = is_utf8 + '0'; | |
3307 | } | |
3308 | ||
3309 | Safefree(delimited); | |
b07fffd1 | 3310 | Safefree(save_input_locale); |
47280b20 | 3311 | return is_utf8; |
7d74bb61 KW |
3312 | } |
3313 | ||
47280b20 KW |
3314 | /* Here we don't have stored the utf8ness for the input locale. We have to |
3315 | * calculate it */ | |
3316 | ||
94646a69 KW |
3317 | # if defined(USE_LOCALE_CTYPE) \ |
3318 | && ( (defined(HAS_NL_LANGINFO) && defined(CODESET)) \ | |
3319 | || (defined(HAS_MBTOWC) || defined(HAS_MBRTOWC))) | |
7d74bb61 | 3320 | |
0dec74cd | 3321 | { |
962aa53f KW |
3322 | const char *original_ctype_locale |
3323 | = switch_category_locale_to_template(LC_CTYPE, | |
3324 | category, | |
3325 | save_input_locale); | |
69014004 | 3326 | |
7d74bb61 | 3327 | /* Here the current LC_CTYPE is set to the locale of the category whose |
0dec74cd | 3328 | * information is desired. This means that nl_langinfo() and mbtowc() |
1d958db2 | 3329 | * should give the correct results */ |
119ee68b | 3330 | |
0dec74cd KW |
3331 | # ifdef MB_CUR_MAX /* But we can potentially rule out UTF-8ness, avoiding |
3332 | calling the functions if we have this */ | |
3333 | ||
3334 | /* Standard UTF-8 needs at least 4 bytes to represent the maximum | |
3335 | * Unicode code point. */ | |
3336 | ||
3337 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\tMB_CUR_MAX=%d\n", | |
3338 | (int) MB_CUR_MAX)); | |
3339 | if ((unsigned) MB_CUR_MAX < STRLENs(MAX_UNICODE_UTF8)) { | |
3340 | is_utf8 = FALSE; | |
b5b2847c KW |
3341 | restore_switched_locale(LC_CTYPE, original_ctype_locale); |
3342 | goto finish_and_return; | |
0dec74cd KW |
3343 | } |
3344 | ||
3345 | # endif | |
7d4bcc4a KW |
3346 | # if defined(HAS_NL_LANGINFO) && defined(CODESET) |
3347 | ||
0dec74cd KW |
3348 | { /* The task is easiest if the platform has this POSIX 2001 function. |
3349 | Except on some platforms it can wrongly return "", so have to have | |
3350 | a fallback. And it can return that it's UTF-8, even if there are | |
3351 | variances from that. For example, Turkish locales may use the | |
3352 | alternate dotted I rules, and sometimes it appears to be a | |
3353 | defective locale definition. XXX We should probably check for | |
3354 | these in the Latin1 range and warn */ | |
c70a3e68 KW |
3355 | const char *codeset = my_nl_langinfo(PERL_CODESET, FALSE); |
3356 | /* FALSE => already in dest locale */ | |
119ee68b | 3357 | |
c70a3e68 KW |
3358 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
3359 | "\tnllanginfo returned CODESET '%s'\n", codeset)); | |
3360 | ||
3361 | if (codeset && strNE(codeset, "")) { | |
1d958db2 | 3362 | |
1d075173 KW |
3363 | /* If the implementation of foldEQ() somehow were |
3364 | * to change to not go byte-by-byte, this could | |
3365 | * read past end of string, as only one length is | |
3366 | * checked. But currently, a premature NUL will | |
3367 | * compare false, and it will stop there */ | |
3368 | is_utf8 = cBOOL( foldEQ(codeset, STR_WITH_LEN("UTF-8")) | |
3369 | || foldEQ(codeset, STR_WITH_LEN("UTF8"))); | |
1d958db2 | 3370 | |
69014004 KW |
3371 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
3372 | "\tnllanginfo returned CODESET '%s'; ?UTF8 locale=%d\n", | |
3373 | codeset, is_utf8)); | |
b5b2847c KW |
3374 | restore_switched_locale(LC_CTYPE, original_ctype_locale); |
3375 | goto finish_and_return; | |
1d958db2 | 3376 | } |
119ee68b KW |
3377 | } |
3378 | ||
7d4bcc4a | 3379 | # endif |
94646a69 | 3380 | # if defined(HAS_MBTOWC) || defined(HAS_MBRTOWC) |
0dec74cd KW |
3381 | /* We can see if this is a UTF-8-like locale if have mbtowc(). It was a |
3382 | * late adder to C89, so very likely to have it. However, testing has | |
3383 | * shown that, like nl_langinfo() above, there are locales that are not | |
3384 | * strictly UTF-8 that this will return that they are */ | |
69014004 | 3385 | |
0dec74cd | 3386 | { |
119ee68b | 3387 | wchar_t wc; |
51fc4b19 KW |
3388 | int len; |
3389 | ||
94646a69 KW |
3390 | # if defined(HAS_MBRTOWC) && defined(USE_ITHREADS) |
3391 | ||
3392 | mbstate_t ps; | |
3393 | ||
3394 | # endif | |
3395 | ||
3396 | /* mbrtowc() and mbtowc() convert a byte string to a wide | |
3397 | * character. Feed a byte string to one of them and check that the | |
3398 | * result is the expected Unicode code point */ | |
3399 | ||
3400 | # if defined(HAS_MBRTOWC) && defined(USE_ITHREADS) | |
3401 | /* Prefer this function if available, as it's reentrant */ | |
3402 | ||
3403 | memset(&ps, 0, sizeof(ps));; | |
3404 | PERL_UNUSED_RESULT(mbrtowc(&wc, NULL, 0, &ps)); /* Reset any shift | |
3405 | state */ | |
3406 | errno = 0; | |
3407 | len = mbrtowc(&wc, STR_WITH_LEN(REPLACEMENT_CHARACTER_UTF8), &ps); | |
3408 | ||
3409 | # else | |
0dec74cd | 3410 | |
856b881c | 3411 | PERL_UNUSED_RESULT(mbtowc(&wc, NULL, 0));/* Reset any shift state */ |
69014004 | 3412 | errno = 0; |
b1d4925c | 3413 | len = mbtowc(&wc, STR_WITH_LEN(REPLACEMENT_CHARACTER_UTF8)); |
51fc4b19 | 3414 | |
94646a69 KW |
3415 | # endif |
3416 | ||
0dec74cd KW |
3417 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
3418 | "\treturn from mbtowc; len=%d; code_point=%x; errno=%d\n", | |
3419 | len, (unsigned int) wc, errno)); | |
b1d4925c | 3420 | |
0dec74cd KW |
3421 | is_utf8 = cBOOL( len == STRLENs(REPLACEMENT_CHARACTER_UTF8) |
3422 | && wc == (wchar_t) UNICODE_REPLACEMENT); | |
119ee68b | 3423 | } |
7d4bcc4a | 3424 | |
962aa53f | 3425 | restore_switched_locale(LC_CTYPE, original_ctype_locale); |
47280b20 | 3426 | goto finish_and_return; |
7d74bb61 | 3427 | } |
119ee68b | 3428 | |
0dec74cd KW |
3429 | # endif |
3430 | # else | |
7d74bb61 | 3431 | |
0dec74cd KW |
3432 | /* Here, we must have a C89 compiler that doesn't have mbtowc(). Next |
3433 | * try looking at the currency symbol to see if it disambiguates | |
3434 | * things. Often that will be in the native script, and if the symbol | |
3435 | * isn't in UTF-8, we know that the locale isn't. If it is non-ASCII | |
3436 | * UTF-8, we infer that the locale is too, as the odds of a non-UTF8 | |
3437 | * string being valid UTF-8 are quite small */ | |
fa9b773e | 3438 | |
660ee27b KW |
3439 | # ifdef USE_LOCALE_MONETARY |
3440 | ||
3441 | /* If have LC_MONETARY, we can look at the currency symbol. Often that | |
3442 | * will be in the native script. We do this one first because there is | |
3443 | * just one string to examine, so potentially avoids work */ | |
7d4bcc4a | 3444 | |
9db16864 KW |
3445 | { |
3446 | const char *original_monetary_locale | |
660ee27b KW |
3447 | = switch_category_locale_to_template(LC_MONETARY, |
3448 | category, | |
3449 | save_input_locale); | |
9db16864 | 3450 | bool only_ascii = FALSE; |
660ee27b KW |
3451 | const U8 * currency_string |
3452 | = (const U8 *) my_nl_langinfo(PERL_CRNCYSTR, FALSE); | |
3453 | /* 2nd param not relevant for this item */ | |
3454 | const U8 * first_variant; | |
fa9b773e | 3455 | |
660ee27b KW |
3456 | assert( *currency_string == '-' |
3457 | || *currency_string == '+' | |
3458 | || *currency_string == '.'); | |
97f4de96 | 3459 | |
660ee27b | 3460 | currency_string++; |
fa9b773e | 3461 | |
660ee27b | 3462 | if (is_utf8_invariant_string_loc(currency_string, 0, &first_variant)) |
9db16864 KW |
3463 | { |
3464 | DEBUG_L(PerlIO_printf(Perl_debug_log, "Couldn't get currency symbol for %s, or contains only ASCII; can't use for determining if UTF-8 locale\n", save_input_locale)); | |
3465 | only_ascii = TRUE; | |
3466 | } | |
3467 | else { | |
660ee27b | 3468 | is_utf8 = is_strict_utf8_string(first_variant, 0); |
9db16864 | 3469 | } |
fa9b773e | 3470 | |
9db16864 | 3471 | restore_switched_locale(LC_MONETARY, original_monetary_locale); |
fa9b773e | 3472 | |
9db16864 | 3473 | if (! only_ascii) { |
fa9b773e | 3474 | |
9db16864 KW |
3475 | /* It isn't a UTF-8 locale if the symbol is not legal UTF-8; |
3476 | * otherwise assume the locale is UTF-8 if and only if the symbol | |
3477 | * is non-ascii UTF-8. */ | |
3478 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?Currency symbol for %s is UTF-8=%d\n", | |
3479 | save_input_locale, is_utf8)); | |
3480 | goto finish_and_return; | |
3481 | } | |
13542a67 | 3482 | } |
fa9b773e | 3483 | |
660ee27b | 3484 | # endif /* USE_LOCALE_MONETARY */ |
7d4bcc4a | 3485 | # if defined(HAS_STRFTIME) && defined(USE_LOCALE_TIME) |
15f7e74e | 3486 | |
9db16864 KW |
3487 | /* Still haven't found a non-ASCII string to disambiguate UTF-8 or not. Try |
3488 | * the names of the months and weekdays, timezone, and am/pm indicator */ | |
3489 | { | |
3490 | const char *original_time_locale | |
3491 | = switch_category_locale_to_template(LC_TIME, | |
3492 | category, | |
3493 | save_input_locale); | |
3494 | int hour = 10; | |
3495 | bool is_dst = FALSE; | |
3496 | int dom = 1; | |
3497 | int month = 0; | |
3498 | int i; | |
3499 | char * formatted_time; | |
3500 | ||
3501 | /* Here the current LC_TIME is set to the locale of the category | |
3502 | * whose information is desired. Look at all the days of the week and | |
3503 | * month names, and the timezone and am/pm indicator for UTF-8 variant | |
3504 | * characters. The first such a one found will tell us if the locale | |
3505 | * is UTF-8 or not */ | |
3506 | ||
3507 | for (i = 0; i < 7 + 12; i++) { /* 7 days; 12 months */ | |
3508 | formatted_time = my_strftime("%A %B %Z %p", | |
3509 | 0, 0, hour, dom, month, 2012 - 1900, 0, 0, is_dst); | |
3510 | if ( ! formatted_time | |
3511 | || is_utf8_invariant_string((U8 *) formatted_time, 0)) | |
3512 | { | |
15f7e74e | 3513 | |
9db16864 KW |
3514 | /* Here, we didn't find a non-ASCII. Try the next time through |
3515 | * with the complemented dst and am/pm, and try with the next | |
3516 | * weekday. After we have gotten all weekdays, try the next | |
3517 | * month */ | |
3518 | is_dst = ! is_dst; | |
3519 | hour = (hour + 12) % 24; | |
3520 | dom++; | |
3521 | if (i > 6) { | |
3522 | month++; | |
3523 | } | |
3524 | continue; | |
15f7e74e | 3525 | } |
9db16864 KW |
3526 | |
3527 | /* Here, we have a non-ASCII. Return TRUE is it is valid UTF8; | |
3528 | * false otherwise. But first, restore LC_TIME to its original | |
3529 | * locale if we changed it */ | |
3530 | restore_switched_locale(LC_TIME, original_time_locale); | |
3531 | ||
3532 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?time-related strings for %s are UTF-8=%d\n", | |
3533 | save_input_locale, | |
3534 | is_utf8_string((U8 *) formatted_time, 0))); | |
3535 | is_utf8 = is_utf8_string((U8 *) formatted_time, 0); | |
3536 | goto finish_and_return; | |
15f7e74e KW |
3537 | } |
3538 | ||
9db16864 KW |
3539 | /* Falling off the end of the loop indicates all the names were just |
3540 | * ASCII. Go on to the next test. If we changed it, restore LC_TIME | |
3541 | * to its original locale */ | |
962aa53f | 3542 | restore_switched_locale(LC_TIME, original_time_locale); |
9db16864 | 3543 | DEBUG_L(PerlIO_printf(Perl_debug_log, "All time-related words for %s contain only ASCII; can't use for determining if UTF-8 locale\n", save_input_locale)); |
15f7e74e KW |
3544 | } |
3545 | ||
7d4bcc4a | 3546 | # endif |
15f7e74e | 3547 | |
7d4bcc4a | 3548 | # if 0 && defined(USE_LOCALE_MESSAGES) && defined(HAS_SYS_ERRLIST) |
855aeb93 | 3549 | |
9db16864 KW |
3550 | /* This code is ifdefd out because it was found to not be necessary in testing |
3551 | * on our dromedary test machine, which has over 700 locales. There, this | |
3552 | * added no value to looking at the currency symbol and the time strings. I | |
3553 | * left it in so as to avoid rewriting it if real-world experience indicates | |
3554 | * that dromedary is an outlier. Essentially, instead of returning abpve if we | |
3555 | * haven't found illegal utf8, we continue on and examine all the strerror() | |
3556 | * messages on the platform for utf8ness. If all are ASCII, we still don't | |
3557 | * know the answer; but otherwise we have a pretty good indication of the | |
3558 | * utf8ness. The reason this doesn't help much is that the messages may not | |
3559 | * have been translated into the locale. The currency symbol and time strings | |
3560 | * are much more likely to have been translated. */ | |
3561 | { | |
3562 | int e; | |
3563 | bool non_ascii = FALSE; | |
3564 | const char *original_messages_locale | |
3565 | = switch_category_locale_to_template(LC_MESSAGES, | |
3566 | category, | |
3567 | save_input_locale); | |
3568 | const char * errmsg = NULL; | |
3569 | ||
3570 | /* Here the current LC_MESSAGES is set to the locale of the category | |
3571 | * whose information is desired. Look through all the messages. We | |
3572 | * can't use Strerror() here because it may expand to code that | |
3573 | * segfaults in miniperl */ | |
3574 | ||
3575 | for (e = 0; e <= sys_nerr; e++) { | |
3576 | errno = 0; | |
3577 | errmsg = sys_errlist[e]; | |
3578 | if (errno || !errmsg) { | |
3579 | break; | |
3580 | } | |
3581 | errmsg = savepv(errmsg); | |
3582 | if (! is_utf8_invariant_string((U8 *) errmsg, 0)) { | |
3583 | non_ascii = TRUE; | |
3584 | is_utf8 = is_utf8_string((U8 *) errmsg, 0); | |
3585 | break; | |
3586 | } | |
855aeb93 | 3587 | } |
9db16864 | 3588 | Safefree(errmsg); |
855aeb93 | 3589 | |
9db16864 | 3590 | restore_switched_locale(LC_MESSAGES, original_messages_locale); |
855aeb93 | 3591 | |
9db16864 | 3592 | if (non_ascii) { |
5857e934 | 3593 | |
9db16864 KW |
3594 | /* Any non-UTF-8 message means not a UTF-8 locale; if all are valid, |
3595 | * any non-ascii means it is one; otherwise we assume it isn't */ | |
3596 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?error messages for %s are UTF-8=%d\n", | |
3597 | save_input_locale, | |
3598 | is_utf8)); | |
3599 | goto finish_and_return; | |
3600 | } | |
855aeb93 | 3601 | |
9db16864 KW |
3602 | DEBUG_L(PerlIO_printf(Perl_debug_log, "All error messages for %s contain only ASCII; can't use for determining if UTF-8 locale\n", save_input_locale)); |
3603 | } | |
855aeb93 | 3604 | |
7d4bcc4a | 3605 | # endif |
0dec74cd | 3606 | # ifndef EBCDIC /* On os390, even if the name ends with "UTF-8', it isn't a |
92c0a900 | 3607 | UTF-8 locale */ |
7d4bcc4a | 3608 | |
97f4de96 KW |
3609 | /* As a last resort, look at the locale name to see if it matches |
3610 | * qr/UTF -? * 8 /ix, or some other common locale names. This "name", the | |
3611 | * return of setlocale(), is actually defined to be opaque, so we can't | |
3612 | * really rely on the absence of various substrings in the name to indicate | |
3613 | * its UTF-8ness, but if it has UTF8 in the name, it is extremely likely to | |
3614 | * be a UTF-8 locale. Similarly for the other common names */ | |
3615 | ||
0dec74cd | 3616 | { |
c36d8df8 | 3617 | const Size_t final_pos = strlen(save_input_locale) - 1; |
0dec74cd | 3618 | |
c36d8df8 KW |
3619 | if (final_pos >= 3) { |
3620 | const char *name = save_input_locale; | |
97f4de96 | 3621 | |
c36d8df8 KW |
3622 | /* Find next 'U' or 'u' and look from there */ |
3623 | while ((name += strcspn(name, "Uu") + 1) | |
3624 | <= save_input_locale + final_pos - 2) | |
97f4de96 | 3625 | { |
c36d8df8 KW |
3626 | if ( isALPHA_FOLD_NE(*name, 't') |
3627 | || isALPHA_FOLD_NE(*(name + 1), 'f')) | |
3628 | { | |
3629 | continue; | |
3630 | } | |
3631 | name += 2; | |
3632 | if (*(name) == '-') { | |
3633 | if ((name > save_input_locale + final_pos - 1)) { | |
3634 | break; | |
3635 | } | |
3636 | name++; | |
3637 | } | |
3638 | if (*(name) == '8') { | |
3639 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
3640 | "Locale %s ends with UTF-8 in name\n", | |
3641 | save_input_locale)); | |
3642 | is_utf8 = TRUE; | |
3643 | goto finish_and_return; | |
97f4de96 | 3644 | } |
97f4de96 | 3645 | } |
c36d8df8 KW |
3646 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
3647 | "Locale %s doesn't end with UTF-8 in name\n", | |
3648 | save_input_locale)); | |
97f4de96 | 3649 | } |
97f4de96 | 3650 | |
4d8d465a | 3651 | # ifdef WIN32 |
7d4bcc4a | 3652 | |
c36d8df8 KW |
3653 | /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */ |
3654 | if (memENDs(save_input_locale, final_pos, "65001")) { | |
3655 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
8a0832a1 | 3656 | "Locale %s ends with 65001 in name, is UTF-8 locale\n", |
97f4de96 | 3657 | save_input_locale)); |
c36d8df8 KW |
3658 | is_utf8 = TRUE; |
3659 | goto finish_and_return; | |
3660 | } | |
0dec74cd | 3661 | } |
7d4bcc4a | 3662 | |
4d8d465a KW |
3663 | # endif |
3664 | # endif | |
97f4de96 KW |
3665 | |
3666 | /* Other common encodings are the ISO 8859 series, which aren't UTF-8. But | |
3667 | * since we are about to return FALSE anyway, there is no point in doing | |
3668 | * this extra work */ | |
7d4bcc4a | 3669 | |
0dec74cd | 3670 | # if 0 |
97f4de96 KW |
3671 | if (instr(save_input_locale, "8859")) { |
3672 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
3673 | "Locale %s has 8859 in name, not UTF-8 locale\n", | |
3674 | save_input_locale)); | |
47280b20 KW |
3675 | is_utf8 = FALSE; |
3676 | goto finish_and_return; | |
97f4de96 | 3677 | } |
0dec74cd | 3678 | # endif |
97f4de96 | 3679 | |
69014004 KW |
3680 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
3681 | "Assuming locale %s is not a UTF-8 locale\n", | |
3682 | save_input_locale)); | |
47280b20 KW |
3683 | is_utf8 = FALSE; |
3684 | ||
0dec74cd KW |
3685 | # endif /* the code that is compiled when no modern LC_CTYPE */ |
3686 | ||
47280b20 KW |
3687 | finish_and_return: |
3688 | ||
3689 | /* Cache this result so we don't have to go through all this next time. */ | |
3690 | utf8ness_cache_size = sizeof(PL_locale_utf8ness) | |
3691 | - (utf8ness_cache - PL_locale_utf8ness); | |
3692 | ||
3693 | /* But we can't save it if it is too large for the total space available */ | |
3694 | if (LIKELY(input_name_len_with_overhead < utf8ness_cache_size)) { | |
3695 | Size_t utf8ness_cache_len = strlen(utf8ness_cache); | |
3696 | ||
3697 | /* Here it can fit, but we may need to clear out the oldest cached | |
3698 | * result(s) to do so. Check */ | |
3699 | if (utf8ness_cache_len + input_name_len_with_overhead | |
3700 | >= utf8ness_cache_size) | |
3701 | { | |
3702 | /* Here we have to clear something out to make room for this. | |
3703 | * Start looking at the rightmost place where it could fit and find | |
3704 | * the beginning of the entry that extends past that. */ | |
3705 | char * cutoff = (char *) my_memrchr(utf8ness_cache, | |
3706 | UTF8NESS_SEP[0], | |
3707 | utf8ness_cache_size | |
3708 | - input_name_len_with_overhead); | |
3709 | ||
3710 | assert(cutoff); | |
3711 | assert(cutoff >= utf8ness_cache); | |
3712 | ||
3713 | /* This and all subsequent entries must be removed */ | |
3714 | *cutoff = '\0'; | |
3715 | utf8ness_cache_len = strlen(utf8ness_cache); | |
3716 | } | |
3717 | ||
3718 | /* Make space for the new entry */ | |
3719 | Move(utf8ness_cache, | |
3720 | utf8ness_cache + input_name_len_with_overhead, | |
3721 | utf8ness_cache_len + 1 /* Incl. trailing NUL */, char); | |
3722 | ||
3723 | /* And insert it */ | |
3724 | Copy(delimited, utf8ness_cache, input_name_len_with_overhead - 1, char); | |
3725 | utf8ness_cache[input_name_len_with_overhead - 1] = is_utf8 + '0'; | |
3726 | ||
3727 | if ((PL_locale_utf8ness[strlen(PL_locale_utf8ness)-1] | |
3728 | & (PERL_UINTMAX_T) ~1) != '0') | |
3729 | { | |
3730 | Perl_croak(aTHX_ | |
7fcf9272 KW |
3731 | "panic: %s: %d: Corrupt utf8ness_cache=%s\nlen=%zu," |
3732 | " inserted_name=%s, its_len=%zu\n", | |
47280b20 KW |
3733 | __FILE__, __LINE__, |
3734 | PL_locale_utf8ness, strlen(PL_locale_utf8ness), | |
3735 | delimited, input_name_len_with_overhead); | |
3736 | } | |
3737 | } | |
3738 | ||
3739 | # ifdef DEBUGGING | |
3740 | ||
3741 | if (DEBUG_Lv_TEST || debug_initialization) { | |
3742 | PerlIO_printf(Perl_debug_log, | |
3743 | "PL_locale_utf8ness is now %s; returning %d\n", | |
3744 | PL_locale_utf8ness, is_utf8); | |
3745 | } | |
3746 | ||
3747 | # endif | |
3748 | ||
3749 | Safefree(delimited); | |
fa9b773e | 3750 | Safefree(save_input_locale); |
47280b20 | 3751 | return is_utf8; |
7d74bb61 KW |
3752 | } |
3753 | ||
8ef6e574 | 3754 | #endif |
7d74bb61 | 3755 | |
d6ded950 KW |
3756 | bool |
3757 | Perl__is_in_locale_category(pTHX_ const bool compiling, const int category) | |
3758 | { | |
1a4f13e1 | 3759 | dVAR; |