Commit | Line | Data |
---|---|---|
98994639 HS |
1 | /* locale.c |
2 | * | |
4bb101f2 | 3 | * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
acde74e1 | 4 | * 2000, 2001, 2002, 2003, 2005, 2006, 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 | /* | |
12 | * A Elbereth Gilthoniel, | |
13 | * silivren penna míriel | |
14 | * o menel aglar elenath! | |
15 | * Na-chaered palan-díriel | |
16 | * o galadhremmin ennorath, | |
17 | * Fanuilos, le linnathon | |
18 | * nef aear, si nef aearon! | |
19 | */ | |
20 | ||
166f8a29 DM |
21 | /* utility functions for handling locale-specific stuff like what |
22 | * character represents the decimal point. | |
23 | */ | |
24 | ||
98994639 HS |
25 | #include "EXTERN.h" |
26 | #define PERL_IN_LOCALE_C | |
27 | #include "perl.h" | |
28 | ||
29 | #ifdef I_LOCALE | |
30 | # include <locale.h> | |
31 | #endif | |
32 | ||
b310b053 JH |
33 | #ifdef I_LANGINFO |
34 | # include <langinfo.h> | |
35 | #endif | |
36 | ||
a4af207c JH |
37 | #include "reentr.h" |
38 | ||
27da23d5 | 39 | #if defined(USE_LOCALE_NUMERIC) || defined(USE_LOCALE_COLLATE) |
98994639 HS |
40 | /* |
41 | * Standardize the locale name from a string returned by 'setlocale'. | |
42 | * | |
43 | * The standard return value of setlocale() is either | |
44 | * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL | |
45 | * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL | |
46 | * (the space-separated values represent the various sublocales, | |
47 | * in some unspecificed order) | |
48 | * | |
49 | * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n", | |
50 | * which is harmful for further use of the string in setlocale(). | |
51 | * | |
52 | */ | |
53 | STATIC char * | |
54 | S_stdize_locale(pTHX_ char *locs) | |
55 | { | |
7452cf6a | 56 | const char * const s = strchr(locs, '='); |
98994639 HS |
57 | bool okay = TRUE; |
58 | ||
8772537c AL |
59 | if (s) { |
60 | const char * const t = strchr(s, '.'); | |
98994639 | 61 | okay = FALSE; |
8772537c AL |
62 | if (t) { |
63 | const char * const u = strchr(t, '\n'); | |
64 | if (u && (u[1] == 0)) { | |
65 | const STRLEN len = u - s; | |
66 | Move(s + 1, locs, len, char); | |
67 | locs[len] = 0; | |
68 | okay = TRUE; | |
98994639 HS |
69 | } |
70 | } | |
71 | } | |
72 | ||
73 | if (!okay) | |
74 | Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs); | |
75 | ||
76 | return locs; | |
77 | } | |
27da23d5 | 78 | #endif |
98994639 HS |
79 | |
80 | void | |
81 | Perl_set_numeric_radix(pTHX) | |
82 | { | |
83 | #ifdef USE_LOCALE_NUMERIC | |
97aff369 | 84 | dVAR; |
98994639 | 85 | # ifdef HAS_LOCALECONV |
7452cf6a | 86 | const struct lconv* const lc = localeconv(); |
98994639 | 87 | |
98994639 HS |
88 | if (lc && lc->decimal_point) { |
89 | if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) { | |
90 | SvREFCNT_dec(PL_numeric_radix_sv); | |
a0714e2c | 91 | PL_numeric_radix_sv = NULL; |
98994639 HS |
92 | } |
93 | else { | |
94 | if (PL_numeric_radix_sv) | |
95 | sv_setpv(PL_numeric_radix_sv, lc->decimal_point); | |
96 | else | |
97 | PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0); | |
98 | } | |
99 | } | |
100 | else | |
a0714e2c | 101 | PL_numeric_radix_sv = NULL; |
98994639 HS |
102 | # endif /* HAS_LOCALECONV */ |
103 | #endif /* USE_LOCALE_NUMERIC */ | |
104 | } | |
105 | ||
106 | /* | |
107 | * Set up for a new numeric locale. | |
108 | */ | |
109 | void | |
8772537c | 110 | Perl_new_numeric(pTHX_ const char *newnum) |
98994639 HS |
111 | { |
112 | #ifdef USE_LOCALE_NUMERIC | |
97aff369 | 113 | dVAR; |
98994639 HS |
114 | |
115 | if (! newnum) { | |
43c5f42d NC |
116 | Safefree(PL_numeric_name); |
117 | PL_numeric_name = NULL; | |
98994639 HS |
118 | PL_numeric_standard = TRUE; |
119 | PL_numeric_local = TRUE; | |
120 | return; | |
121 | } | |
122 | ||
123 | if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) { | |
124 | Safefree(PL_numeric_name); | |
125 | PL_numeric_name = stdize_locale(savepv(newnum)); | |
770526c1 NC |
126 | PL_numeric_standard = ((*newnum == 'C' && newnum[1] == '\0') |
127 | || strEQ(newnum, "POSIX")); | |
98994639 HS |
128 | PL_numeric_local = TRUE; |
129 | set_numeric_radix(); | |
130 | } | |
131 | ||
132 | #endif /* USE_LOCALE_NUMERIC */ | |
133 | } | |
134 | ||
135 | void | |
136 | Perl_set_numeric_standard(pTHX) | |
137 | { | |
138 | #ifdef USE_LOCALE_NUMERIC | |
97aff369 | 139 | dVAR; |
98994639 HS |
140 | |
141 | if (! PL_numeric_standard) { | |
142 | setlocale(LC_NUMERIC, "C"); | |
143 | PL_numeric_standard = TRUE; | |
144 | PL_numeric_local = FALSE; | |
145 | set_numeric_radix(); | |
146 | } | |
147 | ||
148 | #endif /* USE_LOCALE_NUMERIC */ | |
149 | } | |
150 | ||
151 | void | |
152 | Perl_set_numeric_local(pTHX) | |
153 | { | |
154 | #ifdef USE_LOCALE_NUMERIC | |
97aff369 | 155 | dVAR; |
98994639 HS |
156 | |
157 | if (! PL_numeric_local) { | |
158 | setlocale(LC_NUMERIC, PL_numeric_name); | |
159 | PL_numeric_standard = FALSE; | |
160 | PL_numeric_local = TRUE; | |
161 | set_numeric_radix(); | |
162 | } | |
163 | ||
164 | #endif /* USE_LOCALE_NUMERIC */ | |
165 | } | |
166 | ||
167 | /* | |
168 | * Set up for a new ctype locale. | |
169 | */ | |
170 | void | |
8772537c | 171 | Perl_new_ctype(pTHX_ const char *newctype) |
98994639 HS |
172 | { |
173 | #ifdef USE_LOCALE_CTYPE | |
27da23d5 | 174 | dVAR; |
98994639 HS |
175 | int i; |
176 | ||
177 | for (i = 0; i < 256; i++) { | |
178 | if (isUPPER_LC(i)) | |
179 | PL_fold_locale[i] = toLOWER_LC(i); | |
180 | else if (isLOWER_LC(i)) | |
181 | PL_fold_locale[i] = toUPPER_LC(i); | |
182 | else | |
183 | PL_fold_locale[i] = i; | |
184 | } | |
185 | ||
186 | #endif /* USE_LOCALE_CTYPE */ | |
8772537c | 187 | PERL_UNUSED_ARG(newctype); |
98994639 HS |
188 | } |
189 | ||
190 | /* | |
191 | * Set up for a new collation locale. | |
192 | */ | |
193 | void | |
8772537c | 194 | Perl_new_collate(pTHX_ const char *newcoll) |
98994639 HS |
195 | { |
196 | #ifdef USE_LOCALE_COLLATE | |
97aff369 | 197 | dVAR; |
98994639 HS |
198 | |
199 | if (! newcoll) { | |
200 | if (PL_collation_name) { | |
201 | ++PL_collation_ix; | |
202 | Safefree(PL_collation_name); | |
203 | PL_collation_name = NULL; | |
204 | } | |
205 | PL_collation_standard = TRUE; | |
206 | PL_collxfrm_base = 0; | |
207 | PL_collxfrm_mult = 2; | |
208 | return; | |
209 | } | |
210 | ||
211 | if (! PL_collation_name || strNE(PL_collation_name, newcoll)) { | |
212 | ++PL_collation_ix; | |
213 | Safefree(PL_collation_name); | |
214 | PL_collation_name = stdize_locale(savepv(newcoll)); | |
770526c1 NC |
215 | PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0') |
216 | || strEQ(newcoll, "POSIX")); | |
98994639 HS |
217 | |
218 | { | |
219 | /* 2: at most so many chars ('a', 'b'). */ | |
220 | /* 50: surely no system expands a char more. */ | |
221 | #define XFRMBUFSIZE (2 * 50) | |
222 | char xbuf[XFRMBUFSIZE]; | |
8772537c AL |
223 | const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE); |
224 | const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE); | |
225 | const SSize_t mult = fb - fa; | |
98994639 HS |
226 | if (mult < 1) |
227 | Perl_croak(aTHX_ "strxfrm() gets absurd"); | |
eb160463 | 228 | PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0; |
98994639 HS |
229 | PL_collxfrm_mult = mult; |
230 | } | |
231 | } | |
232 | ||
233 | #endif /* USE_LOCALE_COLLATE */ | |
234 | } | |
235 | ||
236 | /* | |
237 | * Initialize locale awareness. | |
238 | */ | |
239 | int | |
240 | Perl_init_i18nl10n(pTHX_ int printwarn) | |
241 | { | |
242 | int ok = 1; | |
243 | /* returns | |
244 | * 1 = set ok or not applicable, | |
245 | * 0 = fallback to C locale, | |
246 | * -1 = fallback to C locale failed | |
247 | */ | |
248 | ||
249 | #if defined(USE_LOCALE) | |
97aff369 | 250 | dVAR; |
98994639 HS |
251 | |
252 | #ifdef USE_LOCALE_CTYPE | |
253 | char *curctype = NULL; | |
254 | #endif /* USE_LOCALE_CTYPE */ | |
255 | #ifdef USE_LOCALE_COLLATE | |
256 | char *curcoll = NULL; | |
257 | #endif /* USE_LOCALE_COLLATE */ | |
258 | #ifdef USE_LOCALE_NUMERIC | |
259 | char *curnum = NULL; | |
260 | #endif /* USE_LOCALE_NUMERIC */ | |
261 | #ifdef __GLIBC__ | |
7452cf6a | 262 | char * const language = PerlEnv_getenv("LANGUAGE"); |
98994639 | 263 | #endif |
7452cf6a AL |
264 | char * const lc_all = PerlEnv_getenv("LC_ALL"); |
265 | char * const lang = PerlEnv_getenv("LANG"); | |
98994639 HS |
266 | bool setlocale_failure = FALSE; |
267 | ||
268 | #ifdef LOCALE_ENVIRON_REQUIRED | |
269 | ||
270 | /* | |
271 | * Ultrix setlocale(..., "") fails if there are no environment | |
272 | * variables from which to get a locale name. | |
273 | */ | |
274 | ||
275 | bool done = FALSE; | |
276 | ||
277 | #ifdef LC_ALL | |
278 | if (lang) { | |
279 | if (setlocale(LC_ALL, "")) | |
280 | done = TRUE; | |
281 | else | |
282 | setlocale_failure = TRUE; | |
283 | } | |
284 | if (!setlocale_failure) { | |
285 | #ifdef USE_LOCALE_CTYPE | |
286 | if (! (curctype = | |
287 | setlocale(LC_CTYPE, | |
288 | (!done && (lang || PerlEnv_getenv("LC_CTYPE"))) | |
bd61b366 | 289 | ? "" : NULL))) |
98994639 HS |
290 | setlocale_failure = TRUE; |
291 | else | |
292 | curctype = savepv(curctype); | |
293 | #endif /* USE_LOCALE_CTYPE */ | |
294 | #ifdef USE_LOCALE_COLLATE | |
295 | if (! (curcoll = | |
296 | setlocale(LC_COLLATE, | |
297 | (!done && (lang || PerlEnv_getenv("LC_COLLATE"))) | |
bd61b366 | 298 | ? "" : NULL))) |
98994639 HS |
299 | setlocale_failure = TRUE; |
300 | else | |
301 | curcoll = savepv(curcoll); | |
302 | #endif /* USE_LOCALE_COLLATE */ | |
303 | #ifdef USE_LOCALE_NUMERIC | |
304 | if (! (curnum = | |
305 | setlocale(LC_NUMERIC, | |
306 | (!done && (lang || PerlEnv_getenv("LC_NUMERIC"))) | |
bd61b366 | 307 | ? "" : NULL))) |
98994639 HS |
308 | setlocale_failure = TRUE; |
309 | else | |
310 | curnum = savepv(curnum); | |
311 | #endif /* USE_LOCALE_NUMERIC */ | |
312 | } | |
313 | ||
314 | #endif /* LC_ALL */ | |
315 | ||
316 | #endif /* !LOCALE_ENVIRON_REQUIRED */ | |
317 | ||
318 | #ifdef LC_ALL | |
319 | if (! setlocale(LC_ALL, "")) | |
320 | setlocale_failure = TRUE; | |
321 | #endif /* LC_ALL */ | |
322 | ||
323 | if (!setlocale_failure) { | |
324 | #ifdef USE_LOCALE_CTYPE | |
325 | if (! (curctype = setlocale(LC_CTYPE, ""))) | |
326 | setlocale_failure = TRUE; | |
327 | else | |
328 | curctype = savepv(curctype); | |
329 | #endif /* USE_LOCALE_CTYPE */ | |
330 | #ifdef USE_LOCALE_COLLATE | |
331 | if (! (curcoll = setlocale(LC_COLLATE, ""))) | |
332 | setlocale_failure = TRUE; | |
333 | else | |
334 | curcoll = savepv(curcoll); | |
335 | #endif /* USE_LOCALE_COLLATE */ | |
336 | #ifdef USE_LOCALE_NUMERIC | |
337 | if (! (curnum = setlocale(LC_NUMERIC, ""))) | |
338 | setlocale_failure = TRUE; | |
339 | else | |
340 | curnum = savepv(curnum); | |
341 | #endif /* USE_LOCALE_NUMERIC */ | |
342 | } | |
343 | ||
344 | if (setlocale_failure) { | |
345 | char *p; | |
0bd48802 | 346 | const bool locwarn = (printwarn > 1 || |
98994639 HS |
347 | (printwarn && |
348 | (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p)))); | |
349 | ||
350 | if (locwarn) { | |
351 | #ifdef LC_ALL | |
352 | ||
353 | PerlIO_printf(Perl_error_log, | |
354 | "perl: warning: Setting locale failed.\n"); | |
355 | ||
356 | #else /* !LC_ALL */ | |
357 | ||
358 | PerlIO_printf(Perl_error_log, | |
359 | "perl: warning: Setting locale failed for the categories:\n\t"); | |
360 | #ifdef USE_LOCALE_CTYPE | |
361 | if (! curctype) | |
362 | PerlIO_printf(Perl_error_log, "LC_CTYPE "); | |
363 | #endif /* USE_LOCALE_CTYPE */ | |
364 | #ifdef USE_LOCALE_COLLATE | |
365 | if (! curcoll) | |
366 | PerlIO_printf(Perl_error_log, "LC_COLLATE "); | |
367 | #endif /* USE_LOCALE_COLLATE */ | |
368 | #ifdef USE_LOCALE_NUMERIC | |
369 | if (! curnum) | |
370 | PerlIO_printf(Perl_error_log, "LC_NUMERIC "); | |
371 | #endif /* USE_LOCALE_NUMERIC */ | |
372 | PerlIO_printf(Perl_error_log, "\n"); | |
373 | ||
374 | #endif /* LC_ALL */ | |
375 | ||
376 | PerlIO_printf(Perl_error_log, | |
377 | "perl: warning: Please check that your locale settings:\n"); | |
378 | ||
379 | #ifdef __GLIBC__ | |
380 | PerlIO_printf(Perl_error_log, | |
381 | "\tLANGUAGE = %c%s%c,\n", | |
382 | language ? '"' : '(', | |
383 | language ? language : "unset", | |
384 | language ? '"' : ')'); | |
385 | #endif | |
386 | ||
387 | PerlIO_printf(Perl_error_log, | |
388 | "\tLC_ALL = %c%s%c,\n", | |
389 | lc_all ? '"' : '(', | |
390 | lc_all ? lc_all : "unset", | |
391 | lc_all ? '"' : ')'); | |
392 | ||
393 | #if defined(USE_ENVIRON_ARRAY) | |
394 | { | |
395 | char **e; | |
396 | for (e = environ; *e; e++) { | |
397 | if (strnEQ(*e, "LC_", 3) | |
398 | && strnNE(*e, "LC_ALL=", 7) | |
399 | && (p = strchr(*e, '='))) | |
400 | PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n", | |
401 | (int)(p - *e), *e, p + 1); | |
402 | } | |
403 | } | |
404 | #else | |
405 | PerlIO_printf(Perl_error_log, | |
406 | "\t(possibly more locale environment variables)\n"); | |
407 | #endif | |
408 | ||
409 | PerlIO_printf(Perl_error_log, | |
410 | "\tLANG = %c%s%c\n", | |
411 | lang ? '"' : '(', | |
412 | lang ? lang : "unset", | |
413 | lang ? '"' : ')'); | |
414 | ||
415 | PerlIO_printf(Perl_error_log, | |
416 | " are supported and installed on your system.\n"); | |
417 | } | |
418 | ||
419 | #ifdef LC_ALL | |
420 | ||
421 | if (setlocale(LC_ALL, "C")) { | |
422 | if (locwarn) | |
423 | PerlIO_printf(Perl_error_log, | |
424 | "perl: warning: Falling back to the standard locale (\"C\").\n"); | |
425 | ok = 0; | |
426 | } | |
427 | else { | |
428 | if (locwarn) | |
429 | PerlIO_printf(Perl_error_log, | |
430 | "perl: warning: Failed to fall back to the standard locale (\"C\").\n"); | |
431 | ok = -1; | |
432 | } | |
433 | ||
434 | #else /* ! LC_ALL */ | |
435 | ||
436 | if (0 | |
437 | #ifdef USE_LOCALE_CTYPE | |
438 | || !(curctype || setlocale(LC_CTYPE, "C")) | |
439 | #endif /* USE_LOCALE_CTYPE */ | |
440 | #ifdef USE_LOCALE_COLLATE | |
441 | || !(curcoll || setlocale(LC_COLLATE, "C")) | |
442 | #endif /* USE_LOCALE_COLLATE */ | |
443 | #ifdef USE_LOCALE_NUMERIC | |
444 | || !(curnum || setlocale(LC_NUMERIC, "C")) | |
445 | #endif /* USE_LOCALE_NUMERIC */ | |
446 | ) | |
447 | { | |
448 | if (locwarn) | |
449 | PerlIO_printf(Perl_error_log, | |
450 | "perl: warning: Cannot fall back to the standard locale (\"C\").\n"); | |
451 | ok = -1; | |
452 | } | |
453 | ||
454 | #endif /* ! LC_ALL */ | |
455 | ||
456 | #ifdef USE_LOCALE_CTYPE | |
bd61b366 | 457 | curctype = savepv(setlocale(LC_CTYPE, NULL)); |
98994639 HS |
458 | #endif /* USE_LOCALE_CTYPE */ |
459 | #ifdef USE_LOCALE_COLLATE | |
bd61b366 | 460 | curcoll = savepv(setlocale(LC_COLLATE, NULL)); |
98994639 HS |
461 | #endif /* USE_LOCALE_COLLATE */ |
462 | #ifdef USE_LOCALE_NUMERIC | |
bd61b366 | 463 | curnum = savepv(setlocale(LC_NUMERIC, NULL)); |
98994639 HS |
464 | #endif /* USE_LOCALE_NUMERIC */ |
465 | } | |
466 | else { | |
467 | ||
468 | #ifdef USE_LOCALE_CTYPE | |
469 | new_ctype(curctype); | |
470 | #endif /* USE_LOCALE_CTYPE */ | |
471 | ||
472 | #ifdef USE_LOCALE_COLLATE | |
473 | new_collate(curcoll); | |
474 | #endif /* USE_LOCALE_COLLATE */ | |
475 | ||
476 | #ifdef USE_LOCALE_NUMERIC | |
477 | new_numeric(curnum); | |
478 | #endif /* USE_LOCALE_NUMERIC */ | |
b310b053 | 479 | |
98994639 HS |
480 | } |
481 | ||
482 | #endif /* USE_LOCALE */ | |
483 | ||
ec71e770 | 484 | #ifdef USE_PERLIO |
b310b053 | 485 | { |
fde18df1 | 486 | /* Set PL_utf8locale to TRUE if using PerlIO _and_ |
ec71e770 | 487 | any of the following are true: |
085a54d9 | 488 | - nl_langinfo(CODESET) contains /^utf-?8/i |
61de9fb5 | 489 | - $ENV{LC_ALL} contains /^utf-?8/i |
085a54d9 | 490 | - $ENV{LC_CTYPE} contains /^utf-?8/i |
61de9fb5 JH |
491 | - $ENV{LANG} contains /^utf-?8/i |
492 | The LC_ALL, LC_CTYPE, LANG obey the usual override | |
493 | hierarchy of locale environment variables. (LANGUAGE | |
494 | affects only LC_MESSAGES only under glibc.) (If present, | |
495 | it overrides LC_MESSAGES for GNU gettext, and it also | |
496 | can have more than one locale, separated by spaces, | |
497 | in case you need to know.) | |
8aa8f774 | 498 | If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE}) |
a05d7ebb | 499 | are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer |
fde18df1 | 500 | on STDIN, STDOUT, STDERR, _and_ the default open discipline. |
085a54d9 | 501 | */ |
fde18df1 | 502 | bool utf8locale = FALSE; |
b310b053 JH |
503 | char *codeset = NULL; |
504 | #if defined(HAS_NL_LANGINFO) && defined(CODESET) | |
505 | codeset = nl_langinfo(CODESET); | |
506 | #endif | |
61de9fb5 | 507 | if (codeset) |
89529cee GA |
508 | utf8locale = (Perl_ibcmp(aTHX_ codeset, STR_WITH_LEN("UTF-8")) == 0 || |
509 | Perl_ibcmp(aTHX_ codeset, STR_WITH_LEN("UTF8") ) == 0); | |
077a72a9 | 510 | #if defined(USE_LOCALE) |
61de9fb5 JH |
511 | else { /* nl_langinfo(CODESET) is supposed to correctly |
512 | * interpret the locale environment variables, | |
513 | * but just in case it fails, let's do this manually. */ | |
514 | if (lang) | |
89529cee GA |
515 | utf8locale = (Perl_ibcmp(aTHX_ lang, STR_WITH_LEN("UTF-8")) == 0 || |
516 | Perl_ibcmp(aTHX_ lang, STR_WITH_LEN("UTF8") ) == 0); | |
b310b053 | 517 | #ifdef USE_LOCALE_CTYPE |
61de9fb5 | 518 | if (curctype) |
89529cee GA |
519 | utf8locale = (Perl_ibcmp(aTHX_ curctype, STR_WITH_LEN("UTF-8")) == 0 || |
520 | Perl_ibcmp(aTHX_ curctype, STR_WITH_LEN("UTF8") ) == 0); | |
b310b053 | 521 | #endif |
61de9fb5 | 522 | if (lc_all) |
89529cee GA |
523 | utf8locale = (Perl_ibcmp(aTHX_ lc_all, STR_WITH_LEN("UTF-8")) == 0 || |
524 | Perl_ibcmp(aTHX_ lc_all, STR_WITH_LEN("UTF8") ) == 0); | |
61de9fb5 | 525 | } |
fde18df1 JH |
526 | #endif /* USE_LOCALE */ |
527 | if (utf8locale) | |
528 | PL_utf8locale = TRUE; | |
529 | } | |
a05d7ebb | 530 | /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO. |
fde18df1 JH |
531 | This is an alternative to using the -C command line switch |
532 | (the -C if present will override this). */ | |
533 | { | |
dd374669 | 534 | const char *p = PerlEnv_getenv("PERL_UNICODE"); |
a05d7ebb | 535 | PL_unicode = p ? parse_unicode_opts(&p) : 0; |
b310b053 | 536 | } |
ec71e770 | 537 | #endif |
b310b053 | 538 | |
98994639 | 539 | #ifdef USE_LOCALE_CTYPE |
43c5f42d | 540 | Safefree(curctype); |
98994639 HS |
541 | #endif /* USE_LOCALE_CTYPE */ |
542 | #ifdef USE_LOCALE_COLLATE | |
43c5f42d | 543 | Safefree(curcoll); |
98994639 HS |
544 | #endif /* USE_LOCALE_COLLATE */ |
545 | #ifdef USE_LOCALE_NUMERIC | |
43c5f42d | 546 | Safefree(curnum); |
98994639 HS |
547 | #endif /* USE_LOCALE_NUMERIC */ |
548 | return ok; | |
549 | } | |
550 | ||
98994639 HS |
551 | #ifdef USE_LOCALE_COLLATE |
552 | ||
553 | /* | |
554 | * mem_collxfrm() is a bit like strxfrm() but with two important | |
555 | * differences. First, it handles embedded NULs. Second, it allocates | |
556 | * a bit more memory than needed for the transformed data itself. | |
557 | * The real transformed data begins at offset sizeof(collationix). | |
558 | * Please see sv_collxfrm() to see how this is used. | |
559 | */ | |
166f8a29 | 560 | |
98994639 HS |
561 | char * |
562 | Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen) | |
563 | { | |
97aff369 | 564 | dVAR; |
98994639 HS |
565 | char *xbuf; |
566 | STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */ | |
567 | ||
568 | /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */ | |
569 | /* the +1 is for the terminating NUL. */ | |
570 | ||
571 | xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1; | |
a02a5408 | 572 | Newx(xbuf, xAlloc, char); |
98994639 HS |
573 | if (! xbuf) |
574 | goto bad; | |
575 | ||
576 | *(U32*)xbuf = PL_collation_ix; | |
577 | xout = sizeof(PL_collation_ix); | |
578 | for (xin = 0; xin < len; ) { | |
579 | SSize_t xused; | |
580 | ||
581 | for (;;) { | |
582 | xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout); | |
583 | if (xused == -1) | |
584 | goto bad; | |
eb160463 | 585 | if ((STRLEN)xused < xAlloc - xout) |
98994639 HS |
586 | break; |
587 | xAlloc = (2 * xAlloc) + 1; | |
588 | Renew(xbuf, xAlloc, char); | |
589 | if (! xbuf) | |
590 | goto bad; | |
591 | } | |
592 | ||
593 | xin += strlen(s + xin) + 1; | |
594 | xout += xused; | |
595 | ||
596 | /* Embedded NULs are understood but silently skipped | |
597 | * because they make no sense in locale collation. */ | |
598 | } | |
599 | ||
600 | xbuf[xout] = '\0'; | |
601 | *xlen = xout - sizeof(PL_collation_ix); | |
602 | return xbuf; | |
603 | ||
604 | bad: | |
605 | Safefree(xbuf); | |
606 | *xlen = 0; | |
607 | return NULL; | |
608 | } | |
609 | ||
610 | #endif /* USE_LOCALE_COLLATE */ | |
611 | ||
66610fdd RGS |
612 | /* |
613 | * Local variables: | |
614 | * c-indentation-style: bsd | |
615 | * c-basic-offset: 4 | |
616 | * indent-tabs-mode: t | |
617 | * End: | |
618 | * | |
37442d52 RGS |
619 | * ex: set ts=8 sts=4 sw=4 noet: |
620 | */ |