Commit | Line | Data |
98994639 |
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 |
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 |
21 | /* utility functions for handling locale-specific stuff like what |
22 | * character represents the decimal point. |
23 | */ |
24 | |
98994639 |
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 |
33 | #ifdef I_LANGINFO |
34 | # include <langinfo.h> |
35 | #endif |
36 | |
a4af207c |
37 | #include "reentr.h" |
38 | |
27da23d5 |
39 | #if defined(USE_LOCALE_NUMERIC) || defined(USE_LOCALE_COLLATE) |
98994639 |
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 |
57 | bool okay = TRUE; |
58 | |
8772537c |
59 | if (s) { |
60 | const char * const t = strchr(s, '.'); |
98994639 |
61 | okay = FALSE; |
8772537c |
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 |
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 |
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 |
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 |
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 |
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 |
111 | { |
112 | #ifdef USE_LOCALE_NUMERIC |
97aff369 |
113 | dVAR; |
98994639 |
114 | |
115 | if (! newnum) { |
43c5f42d |
116 | Safefree(PL_numeric_name); |
117 | PL_numeric_name = NULL; |
98994639 |
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 |
126 | PL_numeric_standard = ((*newnum == 'C' && newnum[1] == '\0') |
127 | || strEQ(newnum, "POSIX")); |
98994639 |
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 |
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 |
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 |
172 | { |
173 | #ifdef USE_LOCALE_CTYPE |
27da23d5 |
174 | dVAR; |
98994639 |
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); |
96a5add6 |
188 | PERL_UNUSED_CONTEXT; |
98994639 |
189 | } |
190 | |
191 | /* |
192 | * Set up for a new collation locale. |
193 | */ |
194 | void |
8772537c |
195 | Perl_new_collate(pTHX_ const char *newcoll) |
98994639 |
196 | { |
197 | #ifdef USE_LOCALE_COLLATE |
97aff369 |
198 | dVAR; |
98994639 |
199 | |
200 | if (! newcoll) { |
201 | if (PL_collation_name) { |
202 | ++PL_collation_ix; |
203 | Safefree(PL_collation_name); |
204 | PL_collation_name = NULL; |
205 | } |
206 | PL_collation_standard = TRUE; |
207 | PL_collxfrm_base = 0; |
208 | PL_collxfrm_mult = 2; |
209 | return; |
210 | } |
211 | |
212 | if (! PL_collation_name || strNE(PL_collation_name, newcoll)) { |
213 | ++PL_collation_ix; |
214 | Safefree(PL_collation_name); |
215 | PL_collation_name = stdize_locale(savepv(newcoll)); |
770526c1 |
216 | PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0') |
217 | || strEQ(newcoll, "POSIX")); |
98994639 |
218 | |
219 | { |
220 | /* 2: at most so many chars ('a', 'b'). */ |
221 | /* 50: surely no system expands a char more. */ |
222 | #define XFRMBUFSIZE (2 * 50) |
223 | char xbuf[XFRMBUFSIZE]; |
8772537c |
224 | const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE); |
225 | const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE); |
226 | const SSize_t mult = fb - fa; |
98994639 |
227 | if (mult < 1) |
228 | Perl_croak(aTHX_ "strxfrm() gets absurd"); |
eb160463 |
229 | PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0; |
98994639 |
230 | PL_collxfrm_mult = mult; |
231 | } |
232 | } |
233 | |
234 | #endif /* USE_LOCALE_COLLATE */ |
235 | } |
236 | |
237 | /* |
238 | * Initialize locale awareness. |
239 | */ |
240 | int |
241 | Perl_init_i18nl10n(pTHX_ int printwarn) |
242 | { |
243 | int ok = 1; |
244 | /* returns |
245 | * 1 = set ok or not applicable, |
246 | * 0 = fallback to C locale, |
247 | * -1 = fallback to C locale failed |
248 | */ |
249 | |
250 | #if defined(USE_LOCALE) |
97aff369 |
251 | dVAR; |
98994639 |
252 | |
253 | #ifdef USE_LOCALE_CTYPE |
254 | char *curctype = NULL; |
255 | #endif /* USE_LOCALE_CTYPE */ |
256 | #ifdef USE_LOCALE_COLLATE |
257 | char *curcoll = NULL; |
258 | #endif /* USE_LOCALE_COLLATE */ |
259 | #ifdef USE_LOCALE_NUMERIC |
260 | char *curnum = NULL; |
261 | #endif /* USE_LOCALE_NUMERIC */ |
262 | #ifdef __GLIBC__ |
7452cf6a |
263 | char * const language = PerlEnv_getenv("LANGUAGE"); |
98994639 |
264 | #endif |
7452cf6a |
265 | char * const lc_all = PerlEnv_getenv("LC_ALL"); |
266 | char * const lang = PerlEnv_getenv("LANG"); |
98994639 |
267 | bool setlocale_failure = FALSE; |
268 | |
269 | #ifdef LOCALE_ENVIRON_REQUIRED |
270 | |
271 | /* |
272 | * Ultrix setlocale(..., "") fails if there are no environment |
273 | * variables from which to get a locale name. |
274 | */ |
275 | |
276 | bool done = FALSE; |
277 | |
278 | #ifdef LC_ALL |
279 | if (lang) { |
280 | if (setlocale(LC_ALL, "")) |
281 | done = TRUE; |
282 | else |
283 | setlocale_failure = TRUE; |
284 | } |
285 | if (!setlocale_failure) { |
286 | #ifdef USE_LOCALE_CTYPE |
287 | if (! (curctype = |
288 | setlocale(LC_CTYPE, |
289 | (!done && (lang || PerlEnv_getenv("LC_CTYPE"))) |
bd61b366 |
290 | ? "" : NULL))) |
98994639 |
291 | setlocale_failure = TRUE; |
292 | else |
293 | curctype = savepv(curctype); |
294 | #endif /* USE_LOCALE_CTYPE */ |
295 | #ifdef USE_LOCALE_COLLATE |
296 | if (! (curcoll = |
297 | setlocale(LC_COLLATE, |
298 | (!done && (lang || PerlEnv_getenv("LC_COLLATE"))) |
bd61b366 |
299 | ? "" : NULL))) |
98994639 |
300 | setlocale_failure = TRUE; |
301 | else |
302 | curcoll = savepv(curcoll); |
303 | #endif /* USE_LOCALE_COLLATE */ |
304 | #ifdef USE_LOCALE_NUMERIC |
305 | if (! (curnum = |
306 | setlocale(LC_NUMERIC, |
307 | (!done && (lang || PerlEnv_getenv("LC_NUMERIC"))) |
bd61b366 |
308 | ? "" : NULL))) |
98994639 |
309 | setlocale_failure = TRUE; |
310 | else |
311 | curnum = savepv(curnum); |
312 | #endif /* USE_LOCALE_NUMERIC */ |
313 | } |
314 | |
315 | #endif /* LC_ALL */ |
316 | |
317 | #endif /* !LOCALE_ENVIRON_REQUIRED */ |
318 | |
319 | #ifdef LC_ALL |
320 | if (! setlocale(LC_ALL, "")) |
321 | setlocale_failure = TRUE; |
322 | #endif /* LC_ALL */ |
323 | |
324 | if (!setlocale_failure) { |
325 | #ifdef USE_LOCALE_CTYPE |
326 | if (! (curctype = setlocale(LC_CTYPE, ""))) |
327 | setlocale_failure = TRUE; |
328 | else |
329 | curctype = savepv(curctype); |
330 | #endif /* USE_LOCALE_CTYPE */ |
331 | #ifdef USE_LOCALE_COLLATE |
332 | if (! (curcoll = setlocale(LC_COLLATE, ""))) |
333 | setlocale_failure = TRUE; |
334 | else |
335 | curcoll = savepv(curcoll); |
336 | #endif /* USE_LOCALE_COLLATE */ |
337 | #ifdef USE_LOCALE_NUMERIC |
338 | if (! (curnum = setlocale(LC_NUMERIC, ""))) |
339 | setlocale_failure = TRUE; |
340 | else |
341 | curnum = savepv(curnum); |
342 | #endif /* USE_LOCALE_NUMERIC */ |
343 | } |
344 | |
345 | if (setlocale_failure) { |
346 | char *p; |
0bd48802 |
347 | const bool locwarn = (printwarn > 1 || |
98994639 |
348 | (printwarn && |
349 | (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p)))); |
350 | |
351 | if (locwarn) { |
352 | #ifdef LC_ALL |
353 | |
354 | PerlIO_printf(Perl_error_log, |
355 | "perl: warning: Setting locale failed.\n"); |
356 | |
357 | #else /* !LC_ALL */ |
358 | |
359 | PerlIO_printf(Perl_error_log, |
360 | "perl: warning: Setting locale failed for the categories:\n\t"); |
361 | #ifdef USE_LOCALE_CTYPE |
362 | if (! curctype) |
363 | PerlIO_printf(Perl_error_log, "LC_CTYPE "); |
364 | #endif /* USE_LOCALE_CTYPE */ |
365 | #ifdef USE_LOCALE_COLLATE |
366 | if (! curcoll) |
367 | PerlIO_printf(Perl_error_log, "LC_COLLATE "); |
368 | #endif /* USE_LOCALE_COLLATE */ |
369 | #ifdef USE_LOCALE_NUMERIC |
370 | if (! curnum) |
371 | PerlIO_printf(Perl_error_log, "LC_NUMERIC "); |
372 | #endif /* USE_LOCALE_NUMERIC */ |
373 | PerlIO_printf(Perl_error_log, "\n"); |
374 | |
375 | #endif /* LC_ALL */ |
376 | |
377 | PerlIO_printf(Perl_error_log, |
378 | "perl: warning: Please check that your locale settings:\n"); |
379 | |
380 | #ifdef __GLIBC__ |
381 | PerlIO_printf(Perl_error_log, |
382 | "\tLANGUAGE = %c%s%c,\n", |
383 | language ? '"' : '(', |
384 | language ? language : "unset", |
385 | language ? '"' : ')'); |
386 | #endif |
387 | |
388 | PerlIO_printf(Perl_error_log, |
389 | "\tLC_ALL = %c%s%c,\n", |
390 | lc_all ? '"' : '(', |
391 | lc_all ? lc_all : "unset", |
392 | lc_all ? '"' : ')'); |
393 | |
394 | #if defined(USE_ENVIRON_ARRAY) |
395 | { |
396 | char **e; |
397 | for (e = environ; *e; e++) { |
398 | if (strnEQ(*e, "LC_", 3) |
399 | && strnNE(*e, "LC_ALL=", 7) |
400 | && (p = strchr(*e, '='))) |
401 | PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n", |
402 | (int)(p - *e), *e, p + 1); |
403 | } |
404 | } |
405 | #else |
406 | PerlIO_printf(Perl_error_log, |
407 | "\t(possibly more locale environment variables)\n"); |
408 | #endif |
409 | |
410 | PerlIO_printf(Perl_error_log, |
411 | "\tLANG = %c%s%c\n", |
412 | lang ? '"' : '(', |
413 | lang ? lang : "unset", |
414 | lang ? '"' : ')'); |
415 | |
416 | PerlIO_printf(Perl_error_log, |
417 | " are supported and installed on your system.\n"); |
418 | } |
419 | |
420 | #ifdef LC_ALL |
421 | |
422 | if (setlocale(LC_ALL, "C")) { |
423 | if (locwarn) |
424 | PerlIO_printf(Perl_error_log, |
425 | "perl: warning: Falling back to the standard locale (\"C\").\n"); |
426 | ok = 0; |
427 | } |
428 | else { |
429 | if (locwarn) |
430 | PerlIO_printf(Perl_error_log, |
431 | "perl: warning: Failed to fall back to the standard locale (\"C\").\n"); |
432 | ok = -1; |
433 | } |
434 | |
435 | #else /* ! LC_ALL */ |
436 | |
437 | if (0 |
438 | #ifdef USE_LOCALE_CTYPE |
439 | || !(curctype || setlocale(LC_CTYPE, "C")) |
440 | #endif /* USE_LOCALE_CTYPE */ |
441 | #ifdef USE_LOCALE_COLLATE |
442 | || !(curcoll || setlocale(LC_COLLATE, "C")) |
443 | #endif /* USE_LOCALE_COLLATE */ |
444 | #ifdef USE_LOCALE_NUMERIC |
445 | || !(curnum || setlocale(LC_NUMERIC, "C")) |
446 | #endif /* USE_LOCALE_NUMERIC */ |
447 | ) |
448 | { |
449 | if (locwarn) |
450 | PerlIO_printf(Perl_error_log, |
451 | "perl: warning: Cannot fall back to the standard locale (\"C\").\n"); |
452 | ok = -1; |
453 | } |
454 | |
455 | #endif /* ! LC_ALL */ |
456 | |
457 | #ifdef USE_LOCALE_CTYPE |
bd61b366 |
458 | curctype = savepv(setlocale(LC_CTYPE, NULL)); |
98994639 |
459 | #endif /* USE_LOCALE_CTYPE */ |
460 | #ifdef USE_LOCALE_COLLATE |
bd61b366 |
461 | curcoll = savepv(setlocale(LC_COLLATE, NULL)); |
98994639 |
462 | #endif /* USE_LOCALE_COLLATE */ |
463 | #ifdef USE_LOCALE_NUMERIC |
bd61b366 |
464 | curnum = savepv(setlocale(LC_NUMERIC, NULL)); |
98994639 |
465 | #endif /* USE_LOCALE_NUMERIC */ |
466 | } |
467 | else { |
468 | |
469 | #ifdef USE_LOCALE_CTYPE |
470 | new_ctype(curctype); |
471 | #endif /* USE_LOCALE_CTYPE */ |
472 | |
473 | #ifdef USE_LOCALE_COLLATE |
474 | new_collate(curcoll); |
475 | #endif /* USE_LOCALE_COLLATE */ |
476 | |
477 | #ifdef USE_LOCALE_NUMERIC |
478 | new_numeric(curnum); |
479 | #endif /* USE_LOCALE_NUMERIC */ |
b310b053 |
480 | |
98994639 |
481 | } |
482 | |
483 | #endif /* USE_LOCALE */ |
484 | |
ec71e770 |
485 | #ifdef USE_PERLIO |
b310b053 |
486 | { |
fde18df1 |
487 | /* Set PL_utf8locale to TRUE if using PerlIO _and_ |
ec71e770 |
488 | any of the following are true: |
085a54d9 |
489 | - nl_langinfo(CODESET) contains /^utf-?8/i |
61de9fb5 |
490 | - $ENV{LC_ALL} contains /^utf-?8/i |
085a54d9 |
491 | - $ENV{LC_CTYPE} contains /^utf-?8/i |
61de9fb5 |
492 | - $ENV{LANG} contains /^utf-?8/i |
493 | The LC_ALL, LC_CTYPE, LANG obey the usual override |
494 | hierarchy of locale environment variables. (LANGUAGE |
495 | affects only LC_MESSAGES only under glibc.) (If present, |
496 | it overrides LC_MESSAGES for GNU gettext, and it also |
497 | can have more than one locale, separated by spaces, |
498 | in case you need to know.) |
8aa8f774 |
499 | If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE}) |
a05d7ebb |
500 | are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer |
fde18df1 |
501 | on STDIN, STDOUT, STDERR, _and_ the default open discipline. |
085a54d9 |
502 | */ |
fde18df1 |
503 | bool utf8locale = FALSE; |
b310b053 |
504 | char *codeset = NULL; |
505 | #if defined(HAS_NL_LANGINFO) && defined(CODESET) |
506 | codeset = nl_langinfo(CODESET); |
507 | #endif |
61de9fb5 |
508 | if (codeset) |
89529cee |
509 | utf8locale = (Perl_ibcmp(aTHX_ codeset, STR_WITH_LEN("UTF-8")) == 0 || |
510 | Perl_ibcmp(aTHX_ codeset, STR_WITH_LEN("UTF8") ) == 0); |
077a72a9 |
511 | #if defined(USE_LOCALE) |
61de9fb5 |
512 | else { /* nl_langinfo(CODESET) is supposed to correctly |
513 | * interpret the locale environment variables, |
514 | * but just in case it fails, let's do this manually. */ |
515 | if (lang) |
89529cee |
516 | utf8locale = (Perl_ibcmp(aTHX_ lang, STR_WITH_LEN("UTF-8")) == 0 || |
517 | Perl_ibcmp(aTHX_ lang, STR_WITH_LEN("UTF8") ) == 0); |
b310b053 |
518 | #ifdef USE_LOCALE_CTYPE |
61de9fb5 |
519 | if (curctype) |
89529cee |
520 | utf8locale = (Perl_ibcmp(aTHX_ curctype, STR_WITH_LEN("UTF-8")) == 0 || |
521 | Perl_ibcmp(aTHX_ curctype, STR_WITH_LEN("UTF8") ) == 0); |
b310b053 |
522 | #endif |
61de9fb5 |
523 | if (lc_all) |
89529cee |
524 | utf8locale = (Perl_ibcmp(aTHX_ lc_all, STR_WITH_LEN("UTF-8")) == 0 || |
525 | Perl_ibcmp(aTHX_ lc_all, STR_WITH_LEN("UTF8") ) == 0); |
61de9fb5 |
526 | } |
fde18df1 |
527 | #endif /* USE_LOCALE */ |
528 | if (utf8locale) |
529 | PL_utf8locale = TRUE; |
530 | } |
a05d7ebb |
531 | /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO. |
fde18df1 |
532 | This is an alternative to using the -C command line switch |
533 | (the -C if present will override this). */ |
534 | { |
dd374669 |
535 | const char *p = PerlEnv_getenv("PERL_UNICODE"); |
a05d7ebb |
536 | PL_unicode = p ? parse_unicode_opts(&p) : 0; |
b310b053 |
537 | } |
ec71e770 |
538 | #endif |
b310b053 |
539 | |
98994639 |
540 | #ifdef USE_LOCALE_CTYPE |
43c5f42d |
541 | Safefree(curctype); |
98994639 |
542 | #endif /* USE_LOCALE_CTYPE */ |
543 | #ifdef USE_LOCALE_COLLATE |
43c5f42d |
544 | Safefree(curcoll); |
98994639 |
545 | #endif /* USE_LOCALE_COLLATE */ |
546 | #ifdef USE_LOCALE_NUMERIC |
43c5f42d |
547 | Safefree(curnum); |
98994639 |
548 | #endif /* USE_LOCALE_NUMERIC */ |
549 | return ok; |
550 | } |
551 | |
98994639 |
552 | #ifdef USE_LOCALE_COLLATE |
553 | |
554 | /* |
555 | * mem_collxfrm() is a bit like strxfrm() but with two important |
556 | * differences. First, it handles embedded NULs. Second, it allocates |
557 | * a bit more memory than needed for the transformed data itself. |
558 | * The real transformed data begins at offset sizeof(collationix). |
559 | * Please see sv_collxfrm() to see how this is used. |
560 | */ |
166f8a29 |
561 | |
98994639 |
562 | char * |
563 | Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen) |
564 | { |
97aff369 |
565 | dVAR; |
98994639 |
566 | char *xbuf; |
567 | STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */ |
568 | |
569 | /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */ |
570 | /* the +1 is for the terminating NUL. */ |
571 | |
572 | xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1; |
a02a5408 |
573 | Newx(xbuf, xAlloc, char); |
98994639 |
574 | if (! xbuf) |
575 | goto bad; |
576 | |
577 | *(U32*)xbuf = PL_collation_ix; |
578 | xout = sizeof(PL_collation_ix); |
579 | for (xin = 0; xin < len; ) { |
580 | SSize_t xused; |
581 | |
582 | for (;;) { |
583 | xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout); |
584 | if (xused == -1) |
585 | goto bad; |
eb160463 |
586 | if ((STRLEN)xused < xAlloc - xout) |
98994639 |
587 | break; |
588 | xAlloc = (2 * xAlloc) + 1; |
589 | Renew(xbuf, xAlloc, char); |
590 | if (! xbuf) |
591 | goto bad; |
592 | } |
593 | |
594 | xin += strlen(s + xin) + 1; |
595 | xout += xused; |
596 | |
597 | /* Embedded NULs are understood but silently skipped |
598 | * because they make no sense in locale collation. */ |
599 | } |
600 | |
601 | xbuf[xout] = '\0'; |
602 | *xlen = xout - sizeof(PL_collation_ix); |
603 | return xbuf; |
604 | |
605 | bad: |
606 | Safefree(xbuf); |
607 | *xlen = 0; |
608 | return NULL; |
609 | } |
610 | |
611 | #endif /* USE_LOCALE_COLLATE */ |
612 | |
66610fdd |
613 | /* |
614 | * Local variables: |
615 | * c-indentation-style: bsd |
616 | * c-basic-offset: 4 |
617 | * indent-tabs-mode: t |
618 | * End: |
619 | * |
37442d52 |
620 | * ex: set ts=8 sts=4 sw=4 noet: |
621 | */ |