This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Work properly under UTF-8 LC_CTYPE locales
[perl5.git] / locale.c
CommitLineData
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
KW
25 *
26 * All C programs have an underlying locale. Perl generally doesn't pay any
27 * attention to it except within the scope of a 'use locale'. For most
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
32 * the desired behavior of those functions at the moment.
166f8a29
DM
33 */
34
98994639
HS
35#include "EXTERN.h"
36#define PERL_IN_LOCALE_C
37#include "perl.h"
38
b310b053
JH
39#ifdef I_LANGINFO
40# include <langinfo.h>
41#endif
42
a4af207c
JH
43#include "reentr.h"
44
8ef6e574
KW
45#ifdef USE_LOCALE
46
98994639 47/*
0d071d52
KW
48 * Standardize the locale name from a string returned by 'setlocale', possibly
49 * modifying that string.
98994639 50 *
0ef2a2b2 51 * The typical return value of setlocale() is either
98994639
HS
52 * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL
53 * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL
54 * (the space-separated values represent the various sublocales,
0ef2a2b2 55 * in some unspecified order). This is not handled by this function.
98994639
HS
56 *
57 * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n",
0ef2a2b2
KW
58 * which is harmful for further use of the string in setlocale(). This
59 * function removes the trailing new line and everything up through the '='
98994639
HS
60 *
61 */
62STATIC char *
63S_stdize_locale(pTHX_ char *locs)
64{
7452cf6a 65 const char * const s = strchr(locs, '=');
98994639
HS
66 bool okay = TRUE;
67
7918f24d
NC
68 PERL_ARGS_ASSERT_STDIZE_LOCALE;
69
8772537c
AL
70 if (s) {
71 const char * const t = strchr(s, '.');
98994639 72 okay = FALSE;
8772537c
AL
73 if (t) {
74 const char * const u = strchr(t, '\n');
75 if (u && (u[1] == 0)) {
76 const STRLEN len = u - s;
77 Move(s + 1, locs, len, char);
78 locs[len] = 0;
79 okay = TRUE;
98994639
HS
80 }
81 }
82 }
83
84 if (!okay)
85 Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs);
86
87 return locs;
88}
89
8ef6e574
KW
90#endif
91
98994639
HS
92void
93Perl_set_numeric_radix(pTHX)
94{
95#ifdef USE_LOCALE_NUMERIC
97aff369 96 dVAR;
98994639 97# ifdef HAS_LOCALECONV
7452cf6a 98 const struct lconv* const lc = localeconv();
98994639 99
98994639
HS
100 if (lc && lc->decimal_point) {
101 if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) {
102 SvREFCNT_dec(PL_numeric_radix_sv);
a0714e2c 103 PL_numeric_radix_sv = NULL;
98994639
HS
104 }
105 else {
106 if (PL_numeric_radix_sv)
107 sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
108 else
109 PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
28acfe03
KW
110 if (! is_ascii_string((U8 *) lc->decimal_point, 0)
111 && is_utf8_string((U8 *) lc->decimal_point, 0)
112 && is_cur_LC_category_utf8(LC_NUMERIC))
113 {
114 SvUTF8_on(PL_numeric_radix_sv);
115 }
98994639
HS
116 }
117 }
118 else
a0714e2c 119 PL_numeric_radix_sv = NULL;
98994639
HS
120# endif /* HAS_LOCALECONV */
121#endif /* USE_LOCALE_NUMERIC */
122}
123
98994639 124void
8772537c 125Perl_new_numeric(pTHX_ const char *newnum)
98994639
HS
126{
127#ifdef USE_LOCALE_NUMERIC
0d071d52
KW
128
129 /* Called after all libc setlocale() calls affecting LC_NUMERIC, to tell
130 * core Perl this and that 'newnum' is the name of the new locale.
131 * It installs this locale as the current underlying default.
132 *
133 * The default locale and the C locale can be toggled between by use of the
134 * set_numeric_local() and set_numeric_standard() functions, which should
135 * probably not be called directly, but only via macros like
136 * SET_NUMERIC_STANDARD() in perl.h.
137 *
138 * The toggling is necessary mainly so that a non-dot radix decimal point
139 * character can be output, while allowing internal calculations to use a
140 * dot.
141 *
142 * This sets several interpreter-level variables:
143 * PL_numeric_name The default locale's name: a copy of 'newnum'
144 * PL_numeric_local A boolean indicating if the toggled state is such
145 * that the current locale is the default locale
146 * PL_numeric_standard A boolean indicating if the toggled state is such
147 * that the current locale is the C locale
148 * Note that both of the last two variables can be true at the same time,
149 * if the underlying locale is C. (Toggling is a no-op under these
150 * circumstances.)
151 *
152 * Any code changing the locale (outside this file) should use
153 * POSIX::setlocale, which calls this function. Therefore this function
154 * should be called directly only from this file and from
155 * POSIX::setlocale() */
156
b03f34cf 157 char *save_newnum;
97aff369 158 dVAR;
98994639
HS
159
160 if (! newnum) {
43c5f42d
NC
161 Safefree(PL_numeric_name);
162 PL_numeric_name = NULL;
98994639
HS
163 PL_numeric_standard = TRUE;
164 PL_numeric_local = TRUE;
165 return;
166 }
167
b03f34cf
KW
168 save_newnum = stdize_locale(savepv(newnum));
169 if (! PL_numeric_name || strNE(PL_numeric_name, save_newnum)) {
98994639 170 Safefree(PL_numeric_name);
b03f34cf 171 PL_numeric_name = save_newnum;
b03f34cf 172 }
98994639 173
e19f01cb
KW
174 PL_numeric_standard = ((*save_newnum == 'C' && save_newnum[1] == '\0')
175 || strEQ(save_newnum, "POSIX"));
176 PL_numeric_local = TRUE;
177 set_numeric_radix();
6959d69d 178
98994639
HS
179#endif /* USE_LOCALE_NUMERIC */
180}
181
182void
183Perl_set_numeric_standard(pTHX)
184{
185#ifdef USE_LOCALE_NUMERIC
97aff369 186 dVAR;
98994639 187
0d071d52
KW
188 /* Toggle the LC_NUMERIC locale to C, if not already there. Probably
189 * should use the macros like SET_NUMERIC_STANDARD() in perl.h instead of
190 * calling this directly. */
191
98994639
HS
192 if (! PL_numeric_standard) {
193 setlocale(LC_NUMERIC, "C");
194 PL_numeric_standard = TRUE;
195 PL_numeric_local = FALSE;
196 set_numeric_radix();
197 }
198
199#endif /* USE_LOCALE_NUMERIC */
200}
201
202void
203Perl_set_numeric_local(pTHX)
204{
205#ifdef USE_LOCALE_NUMERIC
97aff369 206 dVAR;
98994639 207
0d071d52
KW
208 /* Toggle the LC_NUMERIC locale to the current underlying default, if not
209 * already there. Probably should use the macros like SET_NUMERIC_LOCAL()
210 * in perl.h instead of calling this directly. */
211
98994639
HS
212 if (! PL_numeric_local) {
213 setlocale(LC_NUMERIC, PL_numeric_name);
214 PL_numeric_standard = FALSE;
215 PL_numeric_local = TRUE;
216 set_numeric_radix();
217 }
218
219#endif /* USE_LOCALE_NUMERIC */
220}
221
222/*
223 * Set up for a new ctype locale.
224 */
225void
8772537c 226Perl_new_ctype(pTHX_ const char *newctype)
98994639
HS
227{
228#ifdef USE_LOCALE_CTYPE
0d071d52
KW
229
230 /* Called after all libc setlocale() calls affecting LC_CTYPE, to tell
231 * core Perl this and that 'newctype' is the name of the new locale.
232 *
233 * This function sets up the folding arrays for all 256 bytes, assuming
234 * that tofold() is tolc() since fold case is not a concept in POSIX,
235 *
236 * Any code changing the locale (outside this file) should use
237 * POSIX::setlocale, which calls this function. Therefore this function
238 * should be called directly only from this file and from
239 * POSIX::setlocale() */
240
27da23d5 241 dVAR;
68067e4e 242 UV i;
98994639 243
7918f24d
NC
244 PERL_ARGS_ASSERT_NEW_CTYPE;
245
31f05a37
KW
246 PL_in_utf8_CTYPE_locale = is_cur_LC_category_utf8(LC_CTYPE);
247
248 /* A UTF-8 locale gets standard rules. But note that code still has to
249 * handle this specially because of the three problematic code points */
250 if (PL_in_utf8_CTYPE_locale) {
251 Copy(PL_fold_latin1, PL_fold_locale, 256, U8);
252 }
253 else {
254
77806dea
KW
255 for (i = 0; i < 256; i++) {
256 if (isUPPER_LC((U8) i))
31f05a37 257 PL_fold_locale[i] = (U8) toLOWER_LC((U8) i);
77806dea 258 else if (isLOWER_LC((U8) i))
31f05a37 259 PL_fold_locale[i] = (U8) toUPPER_LC((U8) i);
98994639 260 else
e7e76d1b 261 PL_fold_locale[i] = (U8) i;
98994639 262 }
31f05a37 263 }
98994639
HS
264
265#endif /* USE_LOCALE_CTYPE */
7918f24d 266 PERL_ARGS_ASSERT_NEW_CTYPE;
8772537c 267 PERL_UNUSED_ARG(newctype);
96a5add6 268 PERL_UNUSED_CONTEXT;
98994639
HS
269}
270
98994639 271void
8772537c 272Perl_new_collate(pTHX_ const char *newcoll)
98994639
HS
273{
274#ifdef USE_LOCALE_COLLATE
0d071d52
KW
275
276 /* Called after all libc setlocale() calls affecting LC_COLLATE, to tell
277 * core Perl this and that 'newcoll' is the name of the new locale.
278 *
279 * Any code changing the locale (outside this file) should use
280 * POSIX::setlocale, which calls this function. Therefore this function
281 * should be called directly only from this file and from
282 * POSIX::setlocale() */
283
97aff369 284 dVAR;
98994639
HS
285
286 if (! newcoll) {
287 if (PL_collation_name) {
288 ++PL_collation_ix;
289 Safefree(PL_collation_name);
290 PL_collation_name = NULL;
291 }
292 PL_collation_standard = TRUE;
293 PL_collxfrm_base = 0;
294 PL_collxfrm_mult = 2;
295 return;
296 }
297
298 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
299 ++PL_collation_ix;
300 Safefree(PL_collation_name);
301 PL_collation_name = stdize_locale(savepv(newcoll));
770526c1
NC
302 PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0')
303 || strEQ(newcoll, "POSIX"));
98994639
HS
304
305 {
306 /* 2: at most so many chars ('a', 'b'). */
307 /* 50: surely no system expands a char more. */
308#define XFRMBUFSIZE (2 * 50)
309 char xbuf[XFRMBUFSIZE];
8772537c
AL
310 const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
311 const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
312 const SSize_t mult = fb - fa;
e0601d3c 313 if (mult < 1 && !(fa == 0 && fb == 0))
ad49ad39
NC
314 Perl_croak(aTHX_ "panic: strxfrm() gets absurd - a => %"UVuf", ab => %"UVuf,
315 (UV) fa, (UV) fb);
eb160463 316 PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
98994639
HS
317 PL_collxfrm_mult = mult;
318 }
319 }
320
321#endif /* USE_LOCALE_COLLATE */
322}
323
324/*
325 * Initialize locale awareness.
326 */
327int
328Perl_init_i18nl10n(pTHX_ int printwarn)
329{
330 int ok = 1;
331 /* returns
332 * 1 = set ok or not applicable,
333 * 0 = fallback to C locale,
334 * -1 = fallback to C locale failed
335 */
336
337#if defined(USE_LOCALE)
97aff369 338 dVAR;
98994639
HS
339
340#ifdef USE_LOCALE_CTYPE
341 char *curctype = NULL;
342#endif /* USE_LOCALE_CTYPE */
343#ifdef USE_LOCALE_COLLATE
344 char *curcoll = NULL;
345#endif /* USE_LOCALE_COLLATE */
346#ifdef USE_LOCALE_NUMERIC
347 char *curnum = NULL;
348#endif /* USE_LOCALE_NUMERIC */
349#ifdef __GLIBC__
7452cf6a 350 char * const language = PerlEnv_getenv("LANGUAGE");
98994639 351#endif
ccd65d51
KW
352 /* NULL uses the existing already set up locale */
353 const char * const setlocale_init = (PerlEnv_getenv("PERL_SKIP_LOCALE_INIT"))
354 ? NULL
355 : "";
7452cf6a
AL
356 char * const lc_all = PerlEnv_getenv("LC_ALL");
357 char * const lang = PerlEnv_getenv("LANG");
98994639
HS
358 bool setlocale_failure = FALSE;
359
360#ifdef LOCALE_ENVIRON_REQUIRED
361
362 /*
363 * Ultrix setlocale(..., "") fails if there are no environment
364 * variables from which to get a locale name.
365 */
366
367 bool done = FALSE;
368
b3e384bf 369# ifdef LC_ALL
98994639 370 if (lang) {
ccd65d51 371 if (setlocale(LC_ALL, setlocale_init))
98994639
HS
372 done = TRUE;
373 else
374 setlocale_failure = TRUE;
375 }
376 if (!setlocale_failure) {
b3e384bf 377# ifdef USE_LOCALE_CTYPE
56279a21 378 Safefree(curctype);
98994639
HS
379 if (! (curctype =
380 setlocale(LC_CTYPE,
381 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
ccd65d51 382 ? setlocale_init : NULL)))
98994639
HS
383 setlocale_failure = TRUE;
384 else
385 curctype = savepv(curctype);
b3e384bf
KW
386# endif /* USE_LOCALE_CTYPE */
387# ifdef USE_LOCALE_COLLATE
56279a21 388 Safefree(curcoll);
98994639
HS
389 if (! (curcoll =
390 setlocale(LC_COLLATE,
391 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
ccd65d51 392 ? setlocale_init : NULL)))
98994639
HS
393 setlocale_failure = TRUE;
394 else
395 curcoll = savepv(curcoll);
b3e384bf
KW
396# endif /* USE_LOCALE_COLLATE */
397# ifdef USE_LOCALE_NUMERIC
56279a21 398 Safefree(curnum);
98994639
HS
399 if (! (curnum =
400 setlocale(LC_NUMERIC,
401 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
ccd65d51 402 ? setlocale_init : NULL)))
98994639
HS
403 setlocale_failure = TRUE;
404 else
405 curnum = savepv(curnum);
b3e384bf 406# endif /* USE_LOCALE_NUMERIC */
98994639
HS
407 }
408
b3e384bf 409# endif /* LC_ALL */
98994639
HS
410
411#endif /* !LOCALE_ENVIRON_REQUIRED */
412
413#ifdef LC_ALL
ccd65d51 414 if (! setlocale(LC_ALL, setlocale_init))
98994639
HS
415 setlocale_failure = TRUE;
416#endif /* LC_ALL */
417
418 if (!setlocale_failure) {
419#ifdef USE_LOCALE_CTYPE
6cb43dbf 420 Safefree(curctype);
ccd65d51 421 if (! (curctype = setlocale(LC_CTYPE, setlocale_init)))
98994639
HS
422 setlocale_failure = TRUE;
423 else
424 curctype = savepv(curctype);
425#endif /* USE_LOCALE_CTYPE */
426#ifdef USE_LOCALE_COLLATE
6cb43dbf 427 Safefree(curcoll);
ccd65d51 428 if (! (curcoll = setlocale(LC_COLLATE, setlocale_init)))
98994639
HS
429 setlocale_failure = TRUE;
430 else
431 curcoll = savepv(curcoll);
432#endif /* USE_LOCALE_COLLATE */
433#ifdef USE_LOCALE_NUMERIC
6cb43dbf 434 Safefree(curnum);
ccd65d51 435 if (! (curnum = setlocale(LC_NUMERIC, setlocale_init)))
98994639
HS
436 setlocale_failure = TRUE;
437 else
438 curnum = savepv(curnum);
439#endif /* USE_LOCALE_NUMERIC */
440 }
441
442 if (setlocale_failure) {
443 char *p;
0bd48802 444 const bool locwarn = (printwarn > 1 ||
98994639
HS
445 (printwarn &&
446 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
447
448 if (locwarn) {
449#ifdef LC_ALL
450
451 PerlIO_printf(Perl_error_log,
452 "perl: warning: Setting locale failed.\n");
453
454#else /* !LC_ALL */
455
456 PerlIO_printf(Perl_error_log,
457 "perl: warning: Setting locale failed for the categories:\n\t");
458#ifdef USE_LOCALE_CTYPE
459 if (! curctype)
460 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
461#endif /* USE_LOCALE_CTYPE */
462#ifdef USE_LOCALE_COLLATE
463 if (! curcoll)
464 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
465#endif /* USE_LOCALE_COLLATE */
466#ifdef USE_LOCALE_NUMERIC
467 if (! curnum)
468 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
469#endif /* USE_LOCALE_NUMERIC */
470 PerlIO_printf(Perl_error_log, "\n");
471
472#endif /* LC_ALL */
473
474 PerlIO_printf(Perl_error_log,
475 "perl: warning: Please check that your locale settings:\n");
476
477#ifdef __GLIBC__
478 PerlIO_printf(Perl_error_log,
479 "\tLANGUAGE = %c%s%c,\n",
480 language ? '"' : '(',
481 language ? language : "unset",
482 language ? '"' : ')');
483#endif
484
485 PerlIO_printf(Perl_error_log,
486 "\tLC_ALL = %c%s%c,\n",
487 lc_all ? '"' : '(',
488 lc_all ? lc_all : "unset",
489 lc_all ? '"' : ')');
490
491#if defined(USE_ENVIRON_ARRAY)
492 {
493 char **e;
494 for (e = environ; *e; e++) {
495 if (strnEQ(*e, "LC_", 3)
496 && strnNE(*e, "LC_ALL=", 7)
497 && (p = strchr(*e, '=')))
498 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
499 (int)(p - *e), *e, p + 1);
500 }
501 }
502#else
503 PerlIO_printf(Perl_error_log,
504 "\t(possibly more locale environment variables)\n");
505#endif
506
507 PerlIO_printf(Perl_error_log,
508 "\tLANG = %c%s%c\n",
509 lang ? '"' : '(',
510 lang ? lang : "unset",
511 lang ? '"' : ')');
512
513 PerlIO_printf(Perl_error_log,
514 " are supported and installed on your system.\n");
515 }
516
517#ifdef LC_ALL
518
519 if (setlocale(LC_ALL, "C")) {
520 if (locwarn)
521 PerlIO_printf(Perl_error_log,
522 "perl: warning: Falling back to the standard locale (\"C\").\n");
523 ok = 0;
524 }
525 else {
526 if (locwarn)
527 PerlIO_printf(Perl_error_log,
528 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
529 ok = -1;
530 }
531
532#else /* ! LC_ALL */
533
534 if (0
535#ifdef USE_LOCALE_CTYPE
536 || !(curctype || setlocale(LC_CTYPE, "C"))
537#endif /* USE_LOCALE_CTYPE */
538#ifdef USE_LOCALE_COLLATE
539 || !(curcoll || setlocale(LC_COLLATE, "C"))
540#endif /* USE_LOCALE_COLLATE */
541#ifdef USE_LOCALE_NUMERIC
542 || !(curnum || setlocale(LC_NUMERIC, "C"))
543#endif /* USE_LOCALE_NUMERIC */
544 )
545 {
546 if (locwarn)
547 PerlIO_printf(Perl_error_log,
548 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
549 ok = -1;
550 }
551
552#endif /* ! LC_ALL */
553
554#ifdef USE_LOCALE_CTYPE
56279a21 555 Safefree(curctype);
bd61b366 556 curctype = savepv(setlocale(LC_CTYPE, NULL));
98994639
HS
557#endif /* USE_LOCALE_CTYPE */
558#ifdef USE_LOCALE_COLLATE
56279a21 559 Safefree(curcoll);
bd61b366 560 curcoll = savepv(setlocale(LC_COLLATE, NULL));
98994639
HS
561#endif /* USE_LOCALE_COLLATE */
562#ifdef USE_LOCALE_NUMERIC
56279a21 563 Safefree(curnum);
bd61b366 564 curnum = savepv(setlocale(LC_NUMERIC, NULL));
98994639
HS
565#endif /* USE_LOCALE_NUMERIC */
566 }
567 else {
568
569#ifdef USE_LOCALE_CTYPE
570 new_ctype(curctype);
571#endif /* USE_LOCALE_CTYPE */
572
573#ifdef USE_LOCALE_COLLATE
574 new_collate(curcoll);
575#endif /* USE_LOCALE_COLLATE */
576
577#ifdef USE_LOCALE_NUMERIC
578 new_numeric(curnum);
579#endif /* USE_LOCALE_NUMERIC */
b310b053 580
98994639
HS
581 }
582
8ef6e574 583#if defined(USE_PERLIO) && defined(USE_LOCALE_CTYPE)
b310b053 584 {
fde18df1 585 /* Set PL_utf8locale to TRUE if using PerlIO _and_
7d74bb61 586 the current LC_CTYPE locale is UTF-8.
8aa8f774 587 If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE})
a05d7ebb 588 are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer
fde18df1 589 on STDIN, STDOUT, STDERR, _and_ the default open discipline.
085a54d9 590 */
7d74bb61 591 PL_utf8locale = is_cur_LC_category_utf8(LC_CTYPE);
fde18df1 592 }
a05d7ebb 593 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
fde18df1
JH
594 This is an alternative to using the -C command line switch
595 (the -C if present will override this). */
596 {
dd374669 597 const char *p = PerlEnv_getenv("PERL_UNICODE");
a05d7ebb 598 PL_unicode = p ? parse_unicode_opts(&p) : 0;
5a22a2bb
NC
599 if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG)
600 PL_utf8cache = -1;
b310b053 601 }
ec71e770 602#endif
b310b053 603
98994639 604#ifdef USE_LOCALE_CTYPE
43c5f42d 605 Safefree(curctype);
98994639
HS
606#endif /* USE_LOCALE_CTYPE */
607#ifdef USE_LOCALE_COLLATE
43c5f42d 608 Safefree(curcoll);
98994639
HS
609#endif /* USE_LOCALE_COLLATE */
610#ifdef USE_LOCALE_NUMERIC
43c5f42d 611 Safefree(curnum);
98994639 612#endif /* USE_LOCALE_NUMERIC */
8ef6e574
KW
613
614#endif /* USE_LOCALE */
615
98994639
HS
616 return ok;
617}
618
98994639
HS
619#ifdef USE_LOCALE_COLLATE
620
621/*
622 * mem_collxfrm() is a bit like strxfrm() but with two important
623 * differences. First, it handles embedded NULs. Second, it allocates
624 * a bit more memory than needed for the transformed data itself.
625 * The real transformed data begins at offset sizeof(collationix).
626 * Please see sv_collxfrm() to see how this is used.
627 */
166f8a29 628
98994639
HS
629char *
630Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
631{
97aff369 632 dVAR;
98994639
HS
633 char *xbuf;
634 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
635
7918f24d
NC
636 PERL_ARGS_ASSERT_MEM_COLLXFRM;
637
98994639
HS
638 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
639 /* the +1 is for the terminating NUL. */
640
641 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
a02a5408 642 Newx(xbuf, xAlloc, char);
98994639
HS
643 if (! xbuf)
644 goto bad;
645
646 *(U32*)xbuf = PL_collation_ix;
647 xout = sizeof(PL_collation_ix);
648 for (xin = 0; xin < len; ) {
224e8ef5 649 Size_t xused;
98994639
HS
650
651 for (;;) {
652 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
224e8ef5 653 if (xused >= PERL_INT_MAX)
98994639 654 goto bad;
eb160463 655 if ((STRLEN)xused < xAlloc - xout)
98994639
HS
656 break;
657 xAlloc = (2 * xAlloc) + 1;
658 Renew(xbuf, xAlloc, char);
659 if (! xbuf)
660 goto bad;
661 }
662
663 xin += strlen(s + xin) + 1;
664 xout += xused;
665
666 /* Embedded NULs are understood but silently skipped
667 * because they make no sense in locale collation. */
668 }
669
670 xbuf[xout] = '\0';
671 *xlen = xout - sizeof(PL_collation_ix);
672 return xbuf;
673
674 bad:
675 Safefree(xbuf);
676 *xlen = 0;
677 return NULL;
678}
679
680#endif /* USE_LOCALE_COLLATE */
681
8ef6e574
KW
682#ifdef USE_LOCALE
683
637917bb 684STATIC bool
7d74bb61
KW
685S_is_cur_LC_category_utf8(pTHX_ int category)
686{
687 /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE
688 * otherwise. 'category' may not be LC_ALL. If the platform doesn't have
119ee68b
KW
689 * nl_langinfo(), nor MB_CUR_MAX, this employs a heuristic, which hence
690 * could give the wrong result. It errs on the side of not being a UTF-8
691 * locale. */
7d74bb61
KW
692
693 char *save_input_locale = NULL;
7d74bb61
KW
694 STRLEN final_pos;
695
8ef6e574 696#ifdef LC_ALL
7d74bb61 697 assert(category != LC_ALL);
8ef6e574 698#endif
7d74bb61
KW
699
700 /* First dispose of the trivial cases */
b07fffd1 701 save_input_locale = setlocale(category, NULL);
7d74bb61
KW
702 if (! save_input_locale) {
703 return FALSE; /* XXX maybe should croak */
704 }
b07fffd1 705 save_input_locale = stdize_locale(savepv(save_input_locale));
7d74bb61
KW
706 if ((*save_input_locale == 'C' && save_input_locale[1] == '\0')
707 || strEQ(save_input_locale, "POSIX"))
708 {
b07fffd1 709 Safefree(save_input_locale);
7d74bb61
KW
710 return FALSE;
711 }
712
119ee68b 713#if defined(USE_LOCALE_CTYPE) && (defined(MB_CUR_MAX) || defined(HAS_NL_LANGINFO) && defined(CODESET))
7d74bb61 714
119ee68b 715 { /* Next try MB_CUR_MAX or nl_langinfo if available */
7d74bb61
KW
716
717 char *save_ctype_locale = NULL;
119ee68b 718 bool is_utf8;
7d74bb61 719
119ee68b 720 if (category != LC_CTYPE) { /* These work only on LC_CTYPE */
7d74bb61
KW
721
722 /* Get the current LC_CTYPE locale */
723 save_ctype_locale = stdize_locale(savepv(setlocale(LC_CTYPE, NULL)));
724 if (! save_ctype_locale) {
725 goto cant_use_nllanginfo;
726 }
727
728 /* If LC_CTYPE and the desired category use the same locale, this
729 * means that finding the value for LC_CTYPE is the same as finding
730 * the value for the desired category. Otherwise, switch LC_CTYPE
731 * to the desired category's locale */
732 if (strEQ(save_ctype_locale, save_input_locale)) {
733 Safefree(save_ctype_locale);
734 save_ctype_locale = NULL;
735 }
736 else if (! setlocale(LC_CTYPE, save_input_locale)) {
737 Safefree(save_ctype_locale);
738 goto cant_use_nllanginfo;
739 }
740 }
741
742 /* Here the current LC_CTYPE is set to the locale of the category whose
119ee68b
KW
743 * information is desired. This means that MB_CUR_MAX and
744 * nl_langinfo() should give the correct results */
745
746# ifdef MB_CUR_MAX
747
748 /* If we switched LC_CTYPE, switch back */
749 if (save_ctype_locale) {
750 setlocale(LC_CTYPE, save_ctype_locale);
751 Safefree(save_ctype_locale);
752 }
753
754 /* Standard UTF-8 needs at least 4 bytes to represent the maximum
755 * Unicode code point. Since UTF-8 is the only non-single byte
756 * encoding we handle, we just say any such encoding is UTF-8, and if
757 * turns out to be wrong, other things will fail */
758 is_utf8 = MB_CUR_MAX >= 4;
759
760 Safefree(save_input_locale);
761
762# ifdef HAS_MBTOWC
763
764 /* ... But, most system that have MB_CUR_MAX will also have mbtowc(),
765 * since they are both in the C99 standard. We can feed a known byte
766 * string to the latter function, and check that it gives the expected
767 * result */
768 if (is_utf8) {
769 wchar_t wc;
770 if (mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8))
771 != strlen(HYPHEN_UTF8)
772 || wc != (wchar_t) 0x2010)
773 {
774 is_utf8 = FALSE;
775 }
776 }
777
778# endif
779
780 return is_utf8;
781#else
782# if defined(HAS_NL_LANGINFO) && defined(CODESET)
783 {
648f6ebd
KW
784 char *codeset = savepv(nl_langinfo(CODESET));
785 if (codeset) {
7d74bb61 786
648f6ebd
KW
787 /* If we switched LC_CTYPE, switch back */
788 if (save_ctype_locale) {
789 setlocale(LC_CTYPE, save_ctype_locale);
790 Safefree(save_ctype_locale);
791 }
7d74bb61 792
648f6ebd
KW
793 is_utf8 = foldEQ(codeset, STR_WITH_LEN("UTF-8"))
794 || foldEQ(codeset, STR_WITH_LEN("UTF8"));
7d74bb61 795
648f6ebd
KW
796 Safefree(codeset);
797 Safefree(save_input_locale);
798 return is_utf8;
799 }
119ee68b 800 }
7d74bb61 801
119ee68b
KW
802# endif
803# endif
7d74bb61 804 }
119ee68b 805
7d74bb61
KW
806 cant_use_nllanginfo:
807
808#endif /* HAS_NL_LANGINFO etc */
809
810 /* nl_langinfo not available or failed somehow. Look at the locale name to
751e9426 811 * see if it matches qr/UTF -? 8 /ix */
7d74bb61
KW
812
813 final_pos = strlen(save_input_locale) - 1;
751e9426
KW
814 if (final_pos >= 3) {
815 char *name = save_input_locale;
816
817 /* Find next 'U' or 'u' and look from there */
818 while ((name += strcspn(name, "Uu") + 1)
819 <= save_input_locale + final_pos - 2)
7d74bb61 820 {
751e9426
KW
821 if (toFOLD(*(name)) != 't'
822 || toFOLD(*(name + 1)) != 'f')
823 {
824 continue;
825 }
826 name += 2;
827 if (*(name) == '-') {
828 if ((name > save_input_locale + final_pos - 1)) {
829 break;
830 }
831 name++;
832 }
833 if (*(name) == '8') {
834 Safefree(save_input_locale);
835 return TRUE;
836 }
7d74bb61
KW
837 }
838 }
839
840#ifdef WIN32
841 /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */
842 if (final_pos >= 4
843 && *(save_input_locale + final_pos - 0) == '1'
844 && *(save_input_locale + final_pos - 1) == '0'
845 && *(save_input_locale + final_pos - 2) == '0'
846 && *(save_input_locale + final_pos - 3) == '5'
847 && *(save_input_locale + final_pos - 4) == '6')
848 {
849 Safefree(save_input_locale);
850 return TRUE;
851 }
852#endif
853
fa9b773e
KW
854 /* Other common encodings are the ISO 8859 series, which aren't UTF-8 */
855 if (instr(save_input_locale, "8859")) {
856 Safefree(save_input_locale);
857 return FALSE;
858 }
859
860#ifdef HAS_LOCALECONV
861
862# ifdef USE_LOCALE_MONETARY
863
864 /* Here, there is nothing in the locale name to indicate whether the locale
865 * is UTF-8 or not. This "name", the return of setlocale(), is actually
866 * defined to be opaque, so we can't really rely on the absence of various
867 * substrings in the name to indicate its UTF-8ness. Look at the locale's
868 * currency symbol. Often that will be in the native script, and if the
869 * symbol isn't in UTF-8, we know that the locale isn't. If it is
870 * non-ASCII UTF-8, we infer that the locale is too.
871 * To do this, like above for LC_CTYPE, we first set LC_MONETARY to the
872 * locale of the desired category, if it isn't that locale already */
873
874 {
875 char *save_monetary_locale = NULL;
876 bool illegal_utf8 = FALSE;
877 bool only_ascii = FALSE;
878 const struct lconv* const lc = localeconv();
879
880 if (category != LC_MONETARY) {
881
882 save_monetary_locale = stdize_locale(savepv(setlocale(LC_MONETARY,
883 NULL)));
884 if (! save_monetary_locale) {
885 goto cant_use_monetary;
886 }
887
888 if (strNE(save_monetary_locale, save_input_locale)) {
889 if (! setlocale(LC_MONETARY, save_input_locale)) {
890 Safefree(save_monetary_locale);
891 goto cant_use_monetary;
892 }
893 }
894 }
895
896 /* Here the current LC_MONETARY is set to the locale of the category
897 * whose information is desired. */
898
899 if (lc && lc->currency_symbol) {
900 if (! is_utf8_string((U8 *) lc->currency_symbol, 0)) {
901 illegal_utf8 = TRUE;
902 }
903 else if (is_ascii_string((U8 *) lc->currency_symbol, 0)) {
904 only_ascii = TRUE;
905 }
906 }
907
908 /* If we changed it, restore LC_MONETARY to its original locale */
909 if (save_monetary_locale) {
910 setlocale(LC_MONETARY, save_monetary_locale);
911 Safefree(save_monetary_locale);
912 }
913
914 Safefree(save_input_locale);
915
916 /* It isn't a UTF-8 locale if the symbol is not legal UTF-8; otherwise
917 * assume the locale is UTF-8 if and only if the symbol is non-ascii
918 * UTF-8. (We can't really tell if the locale is UTF-8 or not if the
919 * symbol is just a '$', so we err on the side of it not being UTF-8)
920 * */
921 return (illegal_utf8)
922 ? FALSE
923 : ! only_ascii;
924
925 }
926 cant_use_monetary:
927
928# endif /* USE_LOCALE_MONETARY */
929#endif /* HAS_LOCALECONV */
930
931#if 0 && defined(HAS_STRERROR) && defined(USE_LOCALE_MESSAGES)
932
933/* This code is ifdefd out because it was found to not be necessary in testing
934 * on our dromedary test machine, which has over 700 locales. There, looking
935 * at just the currency symbol gave essentially the same results as doing this
936 * extra work. Executing this also caused segfaults in miniperl. I left it in
937 * so as to avoid rewriting it if real-world experience indicates that
938 * dromedary is an outlier. Essentially, instead of returning abpve if we
939 * haven't found illegal utf8, we continue on and examine all the strerror()
940 * messages on the platform for utf8ness. If all are ASCII, we still don't
941 * know the answer; but otherwise we have a pretty good indication of the
942 * utf8ness. The reason this doesn't necessarily help much is that the
943 * messages may not have been translated into the locale. The currency symbol
944 * is much more likely to have been translated. The code below would need to
945 * be altered somewhat to just be a continuation of testing the currency
946 * symbol. */
947 int e;
948 unsigned int failures = 0, non_ascii = 0;
949 char *save_messages_locale = NULL;
950
951 /* Like above for LC_CTYPE, we set LC_MESSAGES to the locale of the
952 * desired category, if it isn't that locale already */
953
954 if (category != LC_MESSAGES) {
955
956 save_messages_locale = stdize_locale(savepv(setlocale(LC_MESSAGES,
957 NULL)));
958 if (! save_messages_locale) {
959 goto cant_use_messages;
960 }
961
962 if (strEQ(save_messages_locale, save_input_locale)) {
963 Safefree(save_input_locale);
964 }
965 else if (! setlocale(LC_MESSAGES, save_input_locale)) {
966 Safefree(save_messages_locale);
967 goto cant_use_messages;
968 }
969 }
970
971 /* Here the current LC_MESSAGES is set to the locale of the category
972 * whose information is desired. Look through all the messages */
973
974 for (e = 0;
975#ifdef HAS_SYS_ERRLIST
976 e <= sys_nerr
977#endif
978 ; e++)
979 {
980 const U8* const errmsg = (U8 *) Strerror(e) ;
981 if (!errmsg)
982 break;
983 if (! is_utf8_string(errmsg, 0)) {
984 failures++;
985 break;
986 }
987 else if (! is_ascii_string(errmsg, 0)) {
988 non_ascii++;
989 }
990 }
991
992 /* And, if we changed it, restore LC_MESSAGES to its original locale */
993 if (save_messages_locale) {
994 setlocale(LC_MESSAGES, save_messages_locale);
995 Safefree(save_messages_locale);
996 }
997
998 /* Any non-UTF-8 message means not a UTF-8 locale; if all are valid,
999 * any non-ascii means it is one; otherwise we assume it isn't */
1000 return (failures) ? FALSE : non_ascii;
1001
1002 }
1003 cant_use_messages:
1004
1005#endif
1006
1007 Safefree(save_input_locale);
7d74bb61
KW
1008 return FALSE;
1009}
1010
8ef6e574 1011#endif
7d74bb61 1012
66610fdd
RGS
1013/*
1014 * Local variables:
1015 * c-indentation-style: bsd
1016 * c-basic-offset: 4
14d04a33 1017 * indent-tabs-mode: nil
66610fdd
RGS
1018 * End:
1019 *
14d04a33 1020 * ex: set ts=8 sts=4 sw=4 et:
37442d52 1021 */