This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
S_do_trans_complex(): re-indent
[perl5.git] / doop.c
CommitLineData
a0d0e21e 1/* doop.c
79072805 2 *
1129b882 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
f4b6e4b3 4 * 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall and others
79072805
LW
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 *
a0d0e21e
LW
9 */
10
11/*
4ac71550
TC
12 * 'So that was the job I felt I had to do when I started,' thought Sam.
13 *
14 * [p.934 of _The Lord of the Rings_, VI/iii: "Mount Doom"]
79072805
LW
15 */
16
166f8a29 17/* This file contains some common functions needed to carry out certain
347f3823 18 * ops. For example, both pp_sprintf() and pp_prtf() call the function
fc7d284e 19 * do_sprintf() found in this file.
166f8a29
DM
20 */
21
79072805 22#include "EXTERN.h"
864dbfa3 23#define PERL_IN_DOOP_C
79072805
LW
24#include "perl.h"
25
64ca3a65 26#ifndef PERL_MICRO
79072805
LW
27#include <signal.h>
28#endif
29
334c6444
DM
30
31/* Helper function for do_trans().
32 * Handles non-utf8 cases(*) not involving the /c, /d, /s flags,
33 * and where search and replacement charlists aren't identical.
34 * (*) i.e. where the search and replacement charlists are non-utf8. sv may
35 * or may not be utf8.
36 */
37
942e002e 38STATIC I32
c395bd6c 39S_do_trans_simple(pTHX_ SV * const sv)
79072805 40{
4757a243 41 I32 matches = 0;
463ee0b2 42 STRLEN len;
9138d6ca 43 U8 *s = (U8*)SvPV_nomg(sv,len);
c395bd6c 44 U8 * const send = s+len;
c4420975 45 const short * const tbl = (short*)cPVOP->op_pv;
7918f24d
NC
46
47 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE;
48
4757a243 49 if (!tbl)
7d85a32c 50 Perl_croak(aTHX_ "panic: do_trans_simple line %d",__LINE__);
a0ed51b3 51
1e54db1a 52 /* First, take care of non-UTF-8 input strings, because they're easy */
1aa99e6b 53 if (!SvUTF8(sv)) {
01ec43d0 54 while (s < send) {
f54cb97a
AL
55 const I32 ch = tbl[*s];
56 if (ch >= 0) {
01ec43d0 57 matches++;
c4420975 58 *s = (U8)ch;
01ec43d0 59 }
c4420975 60 s++;
01ec43d0
GS
61 }
62 SvSETMAGIC(sv);
9b877dbb
IH
63 }
64 else {
c395bd6c
AL
65 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
66 U8 *d;
67 U8 *dstart;
68
69 /* Allow for expansion: $_="a".chr(400); tr/a/\xFE/, FE needs encoding */
70 if (grows)
71 Newx(d, len*2+1, U8);
72 else
73 d = s;
74 dstart = d;
75 while (s < send) {
76 STRLEN ulen;
77 I32 ch;
78
79 /* Need to check this, otherwise 128..255 won't match */
80 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
81 if (c < 0x100 && (ch = tbl[c]) >= 0) {
82 matches++;
83 d = uvchr_to_utf8(d, ch);
84 s += ulen;
85 }
86 else { /* No match -> copy */
87 Move(s, d, ulen, U8);
88 d += ulen;
89 s += ulen;
90 }
91 }
92 if (grows) {
93 sv_setpvn(sv, (char*)dstart, d - dstart);
94 Safefree(dstart);
95 }
96 else {
97 *d = '\0';
98 SvCUR_set(sv, d - dstart);
99 }
100 SvUTF8_on(sv);
101 SvSETMAGIC(sv);
9b877dbb 102 }
4757a243
LW
103 return matches;
104}
105
334c6444
DM
106
107/* Helper function for do_trans().
108 * Handles non-utf8 cases(*) where search and replacement charlists are
109 * identical: so the string isn't modified, and only a count of modifiable
110 * chars is needed.
111 * Note that it doesn't handle /d or /s, since these modify the string
112 * even if the replacement list is empty.
113 * (*) i.e. where the search and replacement charlists are non-utf8. sv may
114 * or may not be utf8.
115 */
116
942e002e 117STATIC I32
c395bd6c 118S_do_trans_count(pTHX_ SV * const sv)
4757a243 119{
4757a243 120 STRLEN len;
9138d6ca 121 const U8 *s = (const U8*)SvPV_nomg_const(sv, len);
c395bd6c
AL
122 const U8 * const send = s + len;
123 I32 matches = 0;
f54cb97a 124 const short * const tbl = (short*)cPVOP->op_pv;
7918f24d
NC
125
126 PERL_ARGS_ASSERT_DO_TRANS_COUNT;
127
4757a243 128 if (!tbl)
7d85a32c 129 Perl_croak(aTHX_ "panic: do_trans_count line %d",__LINE__);
4757a243 130
c395bd6c 131 if (!SvUTF8(sv)) {
1aa99e6b
IH
132 while (s < send) {
133 if (tbl[*s++] >= 0)
036b4402 134 matches++;
1aa99e6b 135 }
c395bd6c 136 }
fabdb6c0
AL
137 else {
138 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
1aa99e6b 139 while (s < send) {
1aa99e6b 140 STRLEN ulen;
9f7f3913 141 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
8973db79
JH
142 if (c < 0x100) {
143 if (tbl[c] >= 0)
144 matches++;
145 } else if (complement)
1aa99e6b
IH
146 matches++;
147 s += ulen;
148 }
fabdb6c0 149 }
4757a243
LW
150
151 return matches;
152}
153
334c6444
DM
154
155/* Helper function for do_trans().
156 * Handles non-utf8 cases(*) involving the /c, /d, /s flags,
157 * and where search and replacement charlists aren't identical.
158 * (*) i.e. where the search and replacement charlists are non-utf8. sv may
159 * or may not be utf8.
160 */
161
942e002e 162STATIC I32
c395bd6c 163S_do_trans_complex(pTHX_ SV * const sv)
4757a243 164{
c395bd6c 165 STRLEN len;
9138d6ca 166 U8 *s = (U8*)SvPV_nomg(sv, len);
c395bd6c 167 U8 * const send = s+len;
4757a243 168 I32 matches = 0;
f54cb97a 169 const short * const tbl = (short*)cPVOP->op_pv;
7918f24d
NC
170
171 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX;
172
4757a243 173 if (!tbl)
7d85a32c 174 Perl_croak(aTHX_ "panic: do_trans_complex line %d",__LINE__);
4757a243 175
c395bd6c
AL
176 if (!SvUTF8(sv)) {
177 U8 *d = s;
178 U8 * const dstart = d;
4757a243 179
1aa99e6b 180 if (PL_op->op_private & OPpTRANS_SQUASH) {
f54cb97a 181 const U8* p = send;
1aa99e6b 182 while (s < send) {
f54cb97a
AL
183 const I32 ch = tbl[*s];
184 if (ch >= 0) {
eb160463 185 *d = (U8)ch;
036b4402 186 matches++;
1aa99e6b
IH
187 if (p != d - 1 || *p != *d)
188 p = d++;
189 }
190 else if (ch == -1) /* -1 is unmapped character */
191 *d++ = *s;
192 else if (ch == -2) /* -2 is delete character */
193 matches++;
194 s++;
195 }
a0ed51b3 196 }
1aa99e6b
IH
197 else {
198 while (s < send) {
f54cb97a
AL
199 const I32 ch = tbl[*s];
200 if (ch >= 0) {
1aa99e6b 201 matches++;
eb160463 202 *d++ = (U8)ch;
1aa99e6b
IH
203 }
204 else if (ch == -1) /* -1 is unmapped character */
205 *d++ = *s;
206 else if (ch == -2) /* -2 is delete character */
207 matches++;
208 s++;
209 }
210 }
76ef7183 211 *d = '\0';
1aa99e6b 212 SvCUR_set(sv, d - dstart);
4757a243 213 }
c395bd6c 214 else { /* is utf8 */
fabdb6c0
AL
215 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
216 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
217 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
c395bd6c
AL
218 U8 *d;
219 U8 *dstart;
ea088e55 220 SSize_t excess = 0;
fabdb6c0 221
9b877dbb 222 if (grows)
a02a5408 223 Newx(d, len*2+1, U8);
9b877dbb
IH
224 else
225 d = s;
1aa99e6b 226 dstart = d;
f146bab6 227 if (complement)
ea088e55
DM
228 /* number of replacement chars in excess of any 0x00..0xff
229 * search characters */
230 excess = (SSize_t)tbl[0x100];
1aa99e6b
IH
231
232 if (PL_op->op_private & OPpTRANS_SQUASH) {
1aa99e6b
IH
233 UV pch = 0xfeedface;
234 while (s < send) {
235 STRLEN len;
9f7f3913
TS
236 const UV comp = utf8n_to_uvchr(s, send - s, &len,
237 UTF8_ALLOW_DEFAULT);
f146bab6
DM
238 UV ch;
239 short sch;
1aa99e6b 240
8973db79
JH
241 if (comp > 0xff) {
242 if (!complement) {
640af7f1 243 Move(s, d, len, U8);
8973db79
JH
244 d += len;
245 }
246 else {
334c6444 247 /* use the implicit 0x100..0x7fffffff search range */
8973db79 248 matches++;
f146bab6
DM
249 ch = del
250 /* setting ch to pch forces char to be deleted */
251 ? ((excess >= (IV)comp - 0xff) ? (UV)tbl[comp+2]
252 : pch )
253
254 : ( (excess == -1) ? comp :
255 (UV)(( excess == 0
256 || excess < (IV)comp - 0xff) ? tbl[0x101]
257 : tbl[comp+2]
258 )
259 );
5f636448
DM
260 if (ch != pch) {
261 d = uvchr_to_utf8(d, ch);
262 pch = ch;
263 }
264 s += len;
265 continue;
8973db79 266 }
9b877dbb 267 }
f146bab6
DM
268 else if ((sch = tbl[comp]) >= 0) {
269 ch = (UV)sch;
1aa99e6b 270 matches++;
f146bab6 271 if (ch != pch) {
9041c2e3 272 d = uvchr_to_utf8(d, ch);
f146bab6 273 pch = ch;
1aa99e6b
IH
274 }
275 s += len;
276 continue;
277 }
f146bab6 278 else if (sch == -1) { /* -1 is unmapped character */
640af7f1 279 Move(s, d, len, U8);
9b877dbb
IH
280 d += len;
281 }
f146bab6 282 else if (sch == -2) /* -2 is delete character */
1aa99e6b
IH
283 matches++;
284 s += len;
285 pch = 0xfeedface;
286 }
5d06d08e 287 }
1aa99e6b
IH
288 else {
289 while (s < send) {
290 STRLEN len;
9f7f3913
TS
291 const UV comp = utf8n_to_uvchr(s, send - s, &len,
292 UTF8_ALLOW_DEFAULT);
f146bab6
DM
293 UV ch;
294 short sch;
8973db79
JH
295 if (comp > 0xff) {
296 if (!complement) {
0376ff32 297 Move(s, d, len, U8);
8973db79
JH
298 d += len;
299 }
300 else {
334c6444 301 /* use the implicit 0x100..0x7fffffff search range */
8973db79 302 matches++;
f146bab6
DM
303 if (del) {
304 if (excess >= (IV)comp - 0xff) {
305 ch = (UV)tbl[comp+2];
306 d = uvchr_to_utf8(d, ch);
307 }
308 }
309 else {
ea088e55
DM
310 /* tr/...//c should call S_do_trans_count
311 * instead */
312 assert(excess != -1);
f146bab6
DM
313 ch = (UV)( excess == 0
314 || excess < (IV)comp-0xff) ? tbl[0x101]
315 : tbl[comp+2];
ea088e55 316 d = uvchr_to_utf8(d, ch);
f146bab6 317 }
8973db79 318 }
9b877dbb 319 }
f146bab6
DM
320 else if ((sch = tbl[comp]) >= 0) {
321 d = uvchr_to_utf8(d, (UV)sch);
1aa99e6b
IH
322 matches++;
323 }
f146bab6 324 else if (sch == -1) { /* -1 is unmapped character */
640af7f1 325 Move(s, d, len, U8);
9b877dbb 326 d += len;
1aa99e6b 327 }
f146bab6 328 else if (sch == -2) /* -2 is delete character */
1aa99e6b
IH
329 matches++;
330 s += len;
331 }
332 }
9b877dbb
IH
333 if (grows) {
334 sv_setpvn(sv, (char*)dstart, d - dstart);
335 Safefree(dstart);
336 }
337 else {
338 *d = '\0';
339 SvCUR_set(sv, d - dstart);
340 }
1aa99e6b 341 SvUTF8_on(sv);
4757a243 342 }
5e44153e 343 SvSETMAGIC(sv);
4757a243
LW
344 return matches;
345}
346
334c6444
DM
347
348/* Helper function for do_trans().
349 * Handles utf8 cases(*) not involving the /c, /d, /s flags,
350 * and where search and replacement charlists aren't identical.
351 * (*) i.e. where the search or replacement charlists are utf8. sv may
352 * or may not be utf8.
353 */
354
942e002e 355STATIC I32
c395bd6c 356S_do_trans_simple_utf8(pTHX_ SV * const sv)
4757a243 357{
4757a243
LW
358 U8 *s;
359 U8 *send;
360 U8 *d;
036b4402 361 U8 *start;
1aa99e6b 362 U8 *dstart, *dend;
4757a243 363 I32 matches = 0;
f54cb97a 364 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
4757a243 365 STRLEN len;
043e41b8
DM
366 SV* const rv =
367#ifdef USE_ITHREADS
368 PAD_SVl(cPADOP->op_padix);
369#else
ad64d0ec 370 MUTABLE_SV(cSVOP->op_sv);
043e41b8 371#endif
85fbaab2 372 HV* const hv = MUTABLE_HV(SvRV(rv));
a4fc7abc 373 SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
f54cb97a
AL
374 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
375 const UV extra = none + 1;
b7953727 376 UV final = 0;
1aa99e6b 377 U8 hibit = 0;
4757a243 378
7918f24d
NC
379 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE_UTF8;
380
9138d6ca 381 s = (U8*)SvPV_nomg(sv, len);
c395bd6c 382 if (!SvUTF8(sv)) {
40b85b80
KW
383 hibit = ! is_utf8_invariant_string(s, len);
384 if (hibit) {
385 s = bytes_to_utf8(s, &len);
c4d5f83a 386 }
1aa99e6b 387 }
4757a243 388 send = s + len;
036b4402 389 start = s;
4757a243 390
a4fc7abc 391 svp = hv_fetchs(hv, "FINAL", FALSE);
4757a243
LW
392 if (svp)
393 final = SvUV(*svp);
394
9b877dbb
IH
395 if (grows) {
396 /* d needs to be bigger than s, in case e.g. upgrading is required */
a02a5408 397 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
9b877dbb
IH
398 dend = d + len * 3;
399 dstart = d;
400 }
401 else {
402 dstart = d = s;
403 dend = d + len;
404 }
1aa99e6b 405
4757a243 406 while (s < send) {
0bcc34c2
AL
407 const UV uv = swash_fetch(rv, s, TRUE);
408 if (uv < none) {
4757a243
LW
409 s += UTF8SKIP(s);
410 matches++;
c80e42f3 411 d = uvchr_to_utf8(d, uv);
4757a243
LW
412 }
413 else if (uv == none) {
4373e329 414 const int i = UTF8SKIP(s);
0376ff32 415 Move(s, d, i, U8);
9b877dbb
IH
416 d += i;
417 s += i;
4757a243
LW
418 }
419 else if (uv == extra) {
4373e329 420 s += UTF8SKIP(s);
4757a243 421 matches++;
c80e42f3 422 d = uvchr_to_utf8(d, final);
4757a243
LW
423 }
424 else
425 s += UTF8SKIP(s);
1aa99e6b 426
9b877dbb 427 if (d > dend) {
4373e329
AL
428 const STRLEN clen = d - dstart;
429 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
9b877dbb 430 if (!grows)
7d85a32c 431 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
89ebb4a3 432 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
1aa99e6b
IH
433 d = dstart + clen;
434 dend = dstart + nlen;
435 }
4757a243 436 }
16ec844d 437 if (grows || hibit) {
9b877dbb
IH
438 sv_setpvn(sv, (char*)dstart, d - dstart);
439 Safefree(dstart);
16ec844d
IH
440 if (grows && hibit)
441 Safefree(start);
9b877dbb
IH
442 }
443 else {
444 *d = '\0';
445 SvCUR_set(sv, d - dstart);
446 }
4757a243 447 SvSETMAGIC(sv);
1aa99e6b 448 SvUTF8_on(sv);
4757a243
LW
449
450 return matches;
451}
452
334c6444
DM
453
454/* Helper function for do_trans().
455 * Handles utf8 cases(*) where search and replacement charlists are
456 * identical: so the string isn't modified, and only a count of modifiable
457 * chars is needed.
458 * Note that it doesn't handle /d or /s, since these modify the string
459 * even if the replacement charlist is empty.
460 * (*) i.e. where the search or replacement charlists are utf8. sv may
461 * or may not be utf8.
462 */
463
942e002e 464STATIC I32
c395bd6c 465S_do_trans_count_utf8(pTHX_ SV * const sv)
4757a243 466{
93524f2b 467 const U8 *s;
cbbf8932
AL
468 const U8 *start = NULL;
469 const U8 *send;
4757a243
LW
470 I32 matches = 0;
471 STRLEN len;
043e41b8
DM
472 SV* const rv =
473#ifdef USE_ITHREADS
474 PAD_SVl(cPADOP->op_padix);
475#else
ad64d0ec 476 MUTABLE_SV(cSVOP->op_sv);
043e41b8 477#endif
85fbaab2 478 HV* const hv = MUTABLE_HV(SvRV(rv));
a4fc7abc 479 SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
f54cb97a
AL
480 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
481 const UV extra = none + 1;
1aa99e6b 482 U8 hibit = 0;
4757a243 483
7918f24d
NC
484 PERL_ARGS_ASSERT_DO_TRANS_COUNT_UTF8;
485
9138d6ca 486 s = (const U8*)SvPV_nomg_const(sv, len);
1aa99e6b 487 if (!SvUTF8(sv)) {
40b85b80
KW
488 hibit = ! is_utf8_invariant_string(s, len);
489 if (hibit) {
490 start = s = bytes_to_utf8(s, &len);
c4d5f83a 491 }
1aa99e6b 492 }
4757a243
LW
493 send = s + len;
494
495 while (s < send) {
c4420975
AL
496 const UV uv = swash_fetch(rv, s, TRUE);
497 if (uv < none || uv == extra)
4757a243 498 matches++;
834a4ddd 499 s += UTF8SKIP(s);
4757a243 500 }
1aa99e6b
IH
501 if (hibit)
502 Safefree(start);
4757a243
LW
503
504 return matches;
505}
506
334c6444
DM
507
508/* Helper function for do_trans().
509 * Handles utf8 cases(*) involving the /c, /d, /s flags,
510 * and where search and replacement charlists aren't identical.
511 * (*) i.e. where the search or replacement charlists are utf8. sv may
512 * or may not be utf8.
513 */
514
942e002e 515STATIC I32
c395bd6c 516S_do_trans_complex_utf8(pTHX_ SV * const sv)
4757a243 517{
1aa99e6b 518 U8 *start, *send;
4757a243
LW
519 U8 *d;
520 I32 matches = 0;
4373e329
AL
521 const I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
522 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
523 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
043e41b8
DM
524 SV* const rv =
525#ifdef USE_ITHREADS
526 PAD_SVl(cPADOP->op_padix);
527#else
ad64d0ec 528 MUTABLE_SV(cSVOP->op_sv);
043e41b8 529#endif
85fbaab2 530 HV * const hv = MUTABLE_HV(SvRV(rv));
a4fc7abc 531 SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
f54cb97a
AL
532 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
533 const UV extra = none + 1;
b7953727 534 UV final = 0;
45005bfb 535 bool havefinal = FALSE;
4757a243 536 STRLEN len;
1aa99e6b 537 U8 *dstart, *dend;
1aa99e6b 538 U8 hibit = 0;
9138d6ca 539 U8 *s = (U8*)SvPV_nomg(sv, len);
7918f24d
NC
540
541 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX_UTF8;
542
c395bd6c 543 if (!SvUTF8(sv)) {
40b85b80
KW
544 hibit = ! is_utf8_invariant_string(s, len);
545 if (hibit) {
546 s = bytes_to_utf8(s, &len);
c4d5f83a 547 }
1aa99e6b 548 }
4757a243 549 send = s + len;
1aa99e6b 550 start = s;
4757a243 551
a4fc7abc 552 svp = hv_fetchs(hv, "FINAL", FALSE);
45005bfb 553 if (svp) {
4757a243 554 final = SvUV(*svp);
45005bfb
JH
555 havefinal = TRUE;
556 }
4757a243 557
9b877dbb
IH
558 if (grows) {
559 /* d needs to be bigger than s, in case e.g. upgrading is required */
a02a5408 560 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
9b877dbb
IH
561 dend = d + len * 3;
562 dstart = d;
563 }
564 else {
565 dstart = d = s;
566 dend = d + len;
567 }
4757a243
LW
568
569 if (squash) {
570 UV puv = 0xfeedface;
571 while (s < send) {
f54cb97a 572 UV uv = swash_fetch(rv, s, TRUE);
9041c2e3 573
9b877dbb 574 if (d > dend) {
4373e329
AL
575 const STRLEN clen = d - dstart;
576 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
9b877dbb 577 if (!grows)
7d85a32c 578 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
89ebb4a3 579 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
1aa99e6b
IH
580 d = dstart + clen;
581 dend = dstart + nlen;
4757a243
LW
582 }
583 if (uv < none) {
584 matches++;
94472101 585 s += UTF8SKIP(s);
4757a243 586 if (uv != puv) {
c80e42f3 587 d = uvchr_to_utf8(d, uv);
4757a243
LW
588 puv = uv;
589 }
4757a243
LW
590 continue;
591 }
592 else if (uv == none) { /* "none" is unmapped character */
4373e329 593 const int i = UTF8SKIP(s);
0376ff32 594 Move(s, d, i, U8);
9b877dbb
IH
595 d += i;
596 s += i;
4757a243
LW
597 puv = 0xfeedface;
598 continue;
a0ed51b3 599 }
4757a243
LW
600 else if (uv == extra && !del) {
601 matches++;
45005bfb
JH
602 if (havefinal) {
603 s += UTF8SKIP(s);
604 if (puv != final) {
c80e42f3 605 d = uvchr_to_utf8(d, final);
45005bfb
JH
606 puv = final;
607 }
608 }
609 else {
610 STRLEN len;
c80e42f3 611 uv = utf8n_to_uvchr(s, send - s, &len, UTF8_ALLOW_DEFAULT);
45005bfb 612 if (uv != puv) {
0376ff32 613 Move(s, d, len, U8);
45005bfb
JH
614 d += len;
615 puv = uv;
616 }
617 s += len;
4757a243 618 }
4757a243
LW
619 continue;
620 }
01ec43d0
GS
621 matches++; /* "none+1" is delete character */
622 s += UTF8SKIP(s);
a0ed51b3 623 }
79072805
LW
624 }
625 else {
4757a243 626 while (s < send) {
f54cb97a 627 const UV uv = swash_fetch(rv, s, TRUE);
9b877dbb 628 if (d > dend) {
f54cb97a
AL
629 const STRLEN clen = d - dstart;
630 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
9b877dbb 631 if (!grows)
7d85a32c 632 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
89ebb4a3 633 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
1aa99e6b
IH
634 d = dstart + clen;
635 dend = dstart + nlen;
a0ed51b3 636 }
4757a243
LW
637 if (uv < none) {
638 matches++;
01ec43d0 639 s += UTF8SKIP(s);
c80e42f3 640 d = uvchr_to_utf8(d, uv);
4757a243 641 continue;
a0ed51b3 642 }
4757a243 643 else if (uv == none) { /* "none" is unmapped character */
f54cb97a 644 const int i = UTF8SKIP(s);
0376ff32 645 Move(s, d, i, U8);
9b877dbb
IH
646 d += i;
647 s += i;
4757a243 648 continue;
79072805 649 }
4757a243
LW
650 else if (uv == extra && !del) {
651 matches++;
01ec43d0 652 s += UTF8SKIP(s);
c80e42f3 653 d = uvchr_to_utf8(d, final);
4757a243
LW
654 continue;
655 }
01ec43d0
GS
656 matches++; /* "none+1" is delete character */
657 s += UTF8SKIP(s);
79072805 658 }
4757a243 659 }
16ec844d 660 if (grows || hibit) {
9b877dbb
IH
661 sv_setpvn(sv, (char*)dstart, d - dstart);
662 Safefree(dstart);
16ec844d
IH
663 if (grows && hibit)
664 Safefree(start);
9b877dbb
IH
665 }
666 else {
667 *d = '\0';
668 SvCUR_set(sv, d - dstart);
669 }
1aa99e6b 670 SvUTF8_on(sv);
4757a243
LW
671 SvSETMAGIC(sv);
672
673 return matches;
674}
675
334c6444
DM
676
677/* Execute a tr//. sv is the value to be translated, while PL_op
678 * should be an OP_TRANS or OP_TRANSR op, whose op_pv field contains a
679 * translation table or whose op_sv field contains a swash.
680 * Returns a count of number of characters translated
681 */
682
4757a243 683I32
864dbfa3 684Perl_do_trans(pTHX_ SV *sv)
4757a243
LW
685{
686 STRLEN len;
a53bfdae
HS
687 const I32 flags = PL_op->op_private;
688 const I32 hasutf = flags & (OPpTRANS_FROM_UTF | OPpTRANS_TO_UTF);
4757a243 689
7918f24d
NC
690 PERL_ARGS_ASSERT_DO_TRANS;
691
a53bfdae
HS
692 if (SvREADONLY(sv) && !(flags & OPpTRANS_IDENTICAL)) {
693 Perl_croak_no_modify();
2233f375 694 }
10516c54 695 (void)SvPV_const(sv, len);
4757a243
LW
696 if (!len)
697 return 0;
a53bfdae 698 if (!(flags & OPpTRANS_IDENTICAL)) {
4499db73 699 if (!SvPOKp(sv) || SvTHINKFIRST(sv))
b4cc4d79 700 (void)SvPV_force_nomg(sv, len);
2de7b02f 701 (void)SvPOK_only_UTF8(sv);
d59e14db 702 }
4757a243 703
a53bfdae
HS
704 /* If we use only OPpTRANS_IDENTICAL to bypass the READONLY check,
705 * we must also rely on it to choose the readonly strategy.
706 */
707 if (flags & OPpTRANS_IDENTICAL) {
708 return hasutf ? do_trans_count_utf8(sv) : do_trans_count(sv);
709 } else if (flags & (OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
710 return hasutf ? do_trans_complex_utf8(sv) : do_trans_complex(sv);
711 } else {
712 return hasutf ? do_trans_simple_utf8(sv) : do_trans_simple(sv);
79072805 713 }
79072805
LW
714}
715
716void
5aaab254 717Perl_do_join(pTHX_ SV *sv, SV *delim, SV **mark, SV **sp)
79072805 718{
53c1dcc0 719 SV ** const oldmark = mark;
eb578fdb
KW
720 I32 items = sp - mark;
721 STRLEN len;
463ee0b2 722 STRLEN delimlen;
810a07df 723 const char * const delims = SvPV_const(delim, delimlen);
79072805 724
7918f24d
NC
725 PERL_ARGS_ASSERT_DO_JOIN;
726
79072805
LW
727 mark++;
728 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
862a34c6 729 SvUPGRADE(sv, SVt_PV);
79072805
LW
730 if (SvLEN(sv) < len + items) { /* current length is way too short */
731 while (items-- > 0) {
1426bbf4 732 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
f54cb97a 733 STRLEN tmplen;
5c144d81 734 SvPV_const(*mark, tmplen);
463ee0b2 735 len += tmplen;
79072805
LW
736 }
737 mark++;
738 }
739 SvGROW(sv, len + 1); /* so try to pre-extend */
740
741 mark = oldmark;
db7c17d7 742 items = sp - mark;
79072805
LW
743 ++mark;
744 }
745
f607343f 746 SvPVCLEAR(sv);
e4803c42 747 /* sv_setpv retains old UTF8ness [perl #24846] */
fb622db0 748 SvUTF8_off(sv);
e4803c42 749
284167a5 750 if (TAINTING_get && SvMAGICAL(sv))
8d6d96c1
HS
751 SvTAINTED_off(sv);
752
463ee0b2 753 if (items-- > 0) {
92d29cee
JH
754 if (*mark)
755 sv_catsv(sv, *mark);
463ee0b2
LW
756 mark++;
757 }
8d6d96c1 758
c512ce4f 759 if (delimlen) {
810a07df 760 const U32 delimflag = DO_UTF8(delim) ? SV_CATUTF8 : SV_CATBYTES;
79072805 761 for (; items > 0; items--,mark++) {
f0ee3863
FC
762 STRLEN len;
763 const char *s;
810a07df 764 sv_catpvn_flags(sv,delims,delimlen,delimflag);
f0ee3863
FC
765 s = SvPV_const(*mark,len);
766 sv_catpvn_flags(sv,s,len,
767 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
79072805
LW
768 }
769 }
770 else {
771 for (; items > 0; items--,mark++)
f0ee3863
FC
772 {
773 STRLEN len;
774 const char *s = SvPV_const(*mark,len);
775 sv_catpvn_flags(sv,s,len,
776 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
777 }
79072805
LW
778 }
779 SvSETMAGIC(sv);
780}
781
782void
03a22d83 783Perl_do_sprintf(pTHX_ SV *sv, SSize_t len, SV **sarg)
79072805 784{
46fc3d4c 785 STRLEN patlen;
53c1dcc0 786 const char * const pat = SvPV_const(*sarg, patlen);
46fc3d4c 787 bool do_taint = FALSE;
788
7918f24d 789 PERL_ARGS_ASSERT_DO_SPRINTF;
03a22d83 790 assert(len >= 1);
7918f24d 791
e06d98fb
DM
792 if (SvTAINTED(*sarg))
793 TAINT_PROPER(
794 (PL_op && PL_op->op_type < OP_max)
795 ? (PL_op->op_type == OP_PRTF)
796 ? "printf"
797 : PL_op_name[PL_op->op_type]
798 : "(unknown)"
799 );
5b781b5b 800 SvUTF8_off(sv);
2cf2cfc6
A
801 if (DO_UTF8(*sarg))
802 SvUTF8_on(sv);
03a22d83 803 sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, (Size_t)(len - 1), &do_taint);
79072805 804 SvSETMAGIC(sv);
46fc3d4c 805 if (do_taint)
806 SvTAINTED_on(sv);
79072805
LW
807}
808
33b45480 809/* currently converts input to bytes if possible, but doesn't sweat failure */
81e118e0 810UV
d69c4304 811Perl_do_vecget(pTHX_ SV *sv, STRLEN offset, int size)
81e118e0 812{
67dd6f35 813 STRLEN srclen, len, avail, uoffset, bitoffs = 0;
fc9668ae
DM
814 const I32 svpv_flags = ((PL_op->op_flags & OPf_MOD || LVRET)
815 ? SV_UNDEF_RETURNS_NULL : 0);
816 unsigned char *s = (unsigned char *)
817 SvPV_flags(sv, srclen, (svpv_flags|SV_GMAGIC));
81e118e0
JH
818 UV retnum = 0;
819
032061d2 820 if (!s) {
fc9668ae 821 s = (unsigned char *)"";
032061d2
BF
822 }
823
7918f24d
NC
824 PERL_ARGS_ASSERT_DO_VECGET;
825
8e84507e 826 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
a50d7633 827 Perl_croak(aTHX_ "Illegal number of bits in vec");
246fae53 828
fc9668ae 829 if (SvUTF8(sv)) {
315f3fc1
KW
830 if (Perl_sv_utf8_downgrade(aTHX_ sv, TRUE)) {
831 /* PVX may have changed */
832 s = (unsigned char *) SvPV_flags(sv, srclen, svpv_flags);
833 }
834 else {
835 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
836 "Use of strings with code points over 0xFF as"
837 " arguments to vec is deprecated. This will"
838 " be a fatal error in Perl 5.32");
839 }
fc9668ae 840 }
246fae53 841
bbb8a7e0
MHM
842 if (size < 8) {
843 bitoffs = ((offset%8)*size)%8;
844 uoffset = offset/(8/size);
845 }
67dd6f35
DM
846 else if (size > 8) {
847 int n = size/8;
848 if (offset > Size_t_MAX / n - 1) /* would overflow */
849 return 0;
850 uoffset = offset*n;
851 }
bbb8a7e0
MHM
852 else
853 uoffset = offset;
854
67dd6f35
DM
855 if (uoffset >= srclen)
856 return 0;
857
858 len = (bitoffs + size + 7)/8; /* required number of bytes */
859 avail = srclen - uoffset; /* available number of bytes */
860
861 /* Does the byte range overlap the end of the string? If so,
862 * handle specially. */
863 if (avail < len) {
81e118e0
JH
864 if (size <= 8)
865 retnum = 0;
866 else {
81e118e0 867 if (size == 16) {
67dd6f35
DM
868 assert(avail == 1);
869 retnum = (UV) s[uoffset] << 8;
81e118e0
JH
870 }
871 else if (size == 32) {
67dd6f35
DM
872 assert(avail >= 1 && avail <= 3);
873 if (avail == 1)
81e118e0 874 retnum =
bb7a0f54 875 ((UV) s[uoffset ] << 24);
67dd6f35 876 else if (avail == 2)
81e118e0 877 retnum =
bb7a0f54
MHM
878 ((UV) s[uoffset ] << 24) +
879 ((UV) s[uoffset + 1] << 16);
81e118e0
JH
880 else
881 retnum =
bb7a0f54
MHM
882 ((UV) s[uoffset ] << 24) +
883 ((UV) s[uoffset + 1] << 16) +
884 ( s[uoffset + 2] << 8);
81e118e0 885 }
d7d93a81 886#ifdef UV_IS_QUAD
c5a0f51a 887 else if (size == 64) {
a2a5de95
NC
888 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
889 "Bit vector size > 32 non-portable");
67dd6f35
DM
890 assert(avail >= 1 && avail <= 7);
891 if (avail == 1)
c5a0f51a 892 retnum =
bb7a0f54 893 (UV) s[uoffset ] << 56;
67dd6f35 894 else if (avail == 2)
c5a0f51a 895 retnum =
bb7a0f54
MHM
896 ((UV) s[uoffset ] << 56) +
897 ((UV) s[uoffset + 1] << 48);
67dd6f35 898 else if (avail == 3)
c5a0f51a 899 retnum =
bb7a0f54
MHM
900 ((UV) s[uoffset ] << 56) +
901 ((UV) s[uoffset + 1] << 48) +
902 ((UV) s[uoffset + 2] << 40);
67dd6f35 903 else if (avail == 4)
c5a0f51a 904 retnum =
bb7a0f54
MHM
905 ((UV) s[uoffset ] << 56) +
906 ((UV) s[uoffset + 1] << 48) +
907 ((UV) s[uoffset + 2] << 40) +
908 ((UV) s[uoffset + 3] << 32);
67dd6f35 909 else if (avail == 5)
c5a0f51a 910 retnum =
bb7a0f54
MHM
911 ((UV) s[uoffset ] << 56) +
912 ((UV) s[uoffset + 1] << 48) +
913 ((UV) s[uoffset + 2] << 40) +
914 ((UV) s[uoffset + 3] << 32) +
e7aca353 915 ((UV) s[uoffset + 4] << 24);
67dd6f35 916 else if (avail == 6)
c5a0f51a 917 retnum =
bb7a0f54
MHM
918 ((UV) s[uoffset ] << 56) +
919 ((UV) s[uoffset + 1] << 48) +
920 ((UV) s[uoffset + 2] << 40) +
921 ((UV) s[uoffset + 3] << 32) +
922 ((UV) s[uoffset + 4] << 24) +
923 ((UV) s[uoffset + 5] << 16);
c5a0f51a 924 else
8e84507e 925 retnum =
bb7a0f54
MHM
926 ((UV) s[uoffset ] << 56) +
927 ((UV) s[uoffset + 1] << 48) +
928 ((UV) s[uoffset + 2] << 40) +
929 ((UV) s[uoffset + 3] << 32) +
930 ((UV) s[uoffset + 4] << 24) +
931 ((UV) s[uoffset + 5] << 16) +
e7aca353 932 ((UV) s[uoffset + 6] << 8);
c5a0f51a
JH
933 }
934#endif
81e118e0
JH
935 }
936 }
937 else if (size < 8)
bbb8a7e0 938 retnum = (s[uoffset] >> bitoffs) & ((1 << size) - 1);
81e118e0 939 else {
81e118e0 940 if (size == 8)
bb7a0f54 941 retnum = s[uoffset];
81e118e0
JH
942 else if (size == 16)
943 retnum =
bb7a0f54
MHM
944 ((UV) s[uoffset] << 8) +
945 s[uoffset + 1];
81e118e0
JH
946 else if (size == 32)
947 retnum =
bb7a0f54
MHM
948 ((UV) s[uoffset ] << 24) +
949 ((UV) s[uoffset + 1] << 16) +
950 ( s[uoffset + 2] << 8) +
951 s[uoffset + 3];
d7d93a81 952#ifdef UV_IS_QUAD
c5a0f51a 953 else if (size == 64) {
a2a5de95
NC
954 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
955 "Bit vector size > 32 non-portable");
c5a0f51a 956 retnum =
bb7a0f54
MHM
957 ((UV) s[uoffset ] << 56) +
958 ((UV) s[uoffset + 1] << 48) +
959 ((UV) s[uoffset + 2] << 40) +
960 ((UV) s[uoffset + 3] << 32) +
961 ((UV) s[uoffset + 4] << 24) +
962 ((UV) s[uoffset + 5] << 16) +
963 ( s[uoffset + 6] << 8) +
964 s[uoffset + 7];
c5a0f51a
JH
965 }
966#endif
81e118e0
JH
967 }
968
969 return retnum;
970}
971
33b45480
SB
972/* currently converts input to bytes if possible but doesn't sweat failures,
973 * although it does ensure that the string it clobbers is not marked as
974 * utf8-valid any more
975 */
79072805 976void
864dbfa3 977Perl_do_vecset(pTHX_ SV *sv)
79072805 978{
67dd6f35 979 STRLEN offset, bitoffs = 0;
eb578fdb
KW
980 int size;
981 unsigned char *s;
982 UV lval;
79072805 983 I32 mask;
a0d0e21e
LW
984 STRLEN targlen;
985 STRLEN len;
c4420975 986 SV * const targ = LvTARG(sv);
1b92e694 987 char errflags = LvFLAGS(sv);
79072805 988
7918f24d
NC
989 PERL_ARGS_ASSERT_DO_VECSET;
990
1b92e694
DM
991 /* some out-of-range errors have been deferred if/until the LV is
992 * actually written to: f(vec($s,-1,8)) is not always fatal */
993 if (errflags) {
b063b0a8
DIM
994 assert(!(errflags & ~(LVf_NEG_OFF|LVf_OUT_OF_RANGE)));
995 if (errflags & LVf_NEG_OFF)
1b92e694
DM
996 Perl_croak_nocontext("Negative offset to vec in lvalue context");
997 Perl_croak_nocontext("Out of memory!");
998 }
999
8990e307
LW
1000 if (!targ)
1001 return;
032061d2
BF
1002 s = (unsigned char*)SvPV_force_flags(targ, targlen,
1003 SV_GMAGIC | SV_UNDEF_RETURNS_NULL);
246fae53 1004 if (SvUTF8(targ)) {
33b45480
SB
1005 /* This is handled by the SvPOK_only below...
1006 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
1007 SvUTF8_off(targ);
1008 */
1009 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
246fae53
MG
1010 }
1011
4ebbc975 1012 (void)SvPOK_only(targ);
81e118e0 1013 lval = SvUV(sv);
79072805
LW
1014 offset = LvTARGOFF(sv);
1015 size = LvTARGLEN(sv);
67dd6f35 1016
8e84507e 1017 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
a50d7633 1018 Perl_croak(aTHX_ "Illegal number of bits in vec");
8e84507e 1019
bbb8a7e0
MHM
1020 if (size < 8) {
1021 bitoffs = ((offset%8)*size)%8;
1022 offset /= 8/size;
1023 }
67dd6f35
DM
1024 else if (size > 8) {
1025 int n = size/8;
1026 if (offset > Size_t_MAX / n - 1) /* would overflow */
1027 Perl_croak_nocontext("Out of memory!");
1028 offset *= n;
1029 }
bbb8a7e0 1030
67dd6f35
DM
1031 len = (bitoffs + size + 7)/8; /* required number of bytes */
1032 if (targlen < offset || targlen - offset < len) {
1033 STRLEN newlen = offset > Size_t_MAX - len - 1 ? /* avoid overflow */
1034 Size_t_MAX : offset + len + 1;
1035 s = (unsigned char*)SvGROW(targ, newlen);
1036 (void)memzero((char *)(s + targlen), newlen - targlen);
1037 SvCUR_set(targ, newlen - 1);
a0d0e21e 1038 }
8e84507e 1039
79072805
LW
1040 if (size < 8) {
1041 mask = (1 << size) - 1;
79072805 1042 lval &= mask;
bbb8a7e0
MHM
1043 s[offset] &= ~(mask << bitoffs);
1044 s[offset] |= lval << bitoffs;
79072805
LW
1045 }
1046 else {
1047 if (size == 8)
eb160463 1048 s[offset ] = (U8)( lval & 0xff);
79072805 1049 else if (size == 16) {
eb160463
GS
1050 s[offset ] = (U8)((lval >> 8) & 0xff);
1051 s[offset+1] = (U8)( lval & 0xff);
79072805
LW
1052 }
1053 else if (size == 32) {
eb160463
GS
1054 s[offset ] = (U8)((lval >> 24) & 0xff);
1055 s[offset+1] = (U8)((lval >> 16) & 0xff);
1056 s[offset+2] = (U8)((lval >> 8) & 0xff);
1057 s[offset+3] = (U8)( lval & 0xff);
c5a0f51a 1058 }
d7d93a81 1059#ifdef UV_IS_QUAD
c5a0f51a 1060 else if (size == 64) {
a2a5de95
NC
1061 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
1062 "Bit vector size > 32 non-portable");
eb160463
GS
1063 s[offset ] = (U8)((lval >> 56) & 0xff);
1064 s[offset+1] = (U8)((lval >> 48) & 0xff);
1065 s[offset+2] = (U8)((lval >> 40) & 0xff);
1066 s[offset+3] = (U8)((lval >> 32) & 0xff);
1067 s[offset+4] = (U8)((lval >> 24) & 0xff);
1068 s[offset+5] = (U8)((lval >> 16) & 0xff);
1069 s[offset+6] = (U8)((lval >> 8) & 0xff);
1070 s[offset+7] = (U8)( lval & 0xff);
79072805 1071 }
dc1e3f56 1072#endif
79072805 1073 }
7bb043c3 1074 SvSETMAGIC(targ);
79072805
LW
1075}
1076
1077void
864dbfa3 1078Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
79072805
LW
1079{
1080#ifdef LIBERAL
eb578fdb
KW
1081 long *dl;
1082 long *ll;
1083 long *rl;
79072805 1084#endif
eb578fdb 1085 char *dc;
463ee0b2
LW
1086 STRLEN leftlen;
1087 STRLEN rightlen;
eb578fdb
KW
1088 const char *lc;
1089 const char *rc;
1090 STRLEN len;
bb7a0f54 1091 STRLEN lensave;
e62f0680
NC
1092 const char *lsave;
1093 const char *rsave;
bb7a0f54 1094 STRLEN needlen = 0;
08b6664b 1095 bool result_needs_to_be_utf8 = FALSE;
b50535da
KW
1096 bool left_utf8 = FALSE;
1097 bool right_utf8 = FALSE;
1098 U8 * left_non_downgraded = NULL;
1099 U8 * right_non_downgraded = NULL;
1100 Size_t left_non_downgraded_len = 0;
1101 Size_t right_non_downgraded_len = 0;
1102 char * non_downgraded = NULL;
1103 Size_t non_downgraded_len = 0;
0c57e439 1104
7918f24d 1105 PERL_ARGS_ASSERT_DO_VOP;
79072805 1106
6b349a5c 1107 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv)))
f607343f 1108 SvPVCLEAR(sv); /* avoid undef warning on |= and ^= */
8c8eee82 1109 if (sv == left) {
08b6664b 1110 lc = SvPV_force_nomg(left, leftlen);
8c8eee82
BM
1111 }
1112 else {
08b6664b 1113 lc = SvPV_nomg_const(left, leftlen);
8c8eee82
BM
1114 SvPV_force_nomg_nolen(sv);
1115 }
08b6664b 1116 rc = SvPV_nomg_const(right, rightlen);
12abf4f0 1117
0e6e7171 1118 /* This needs to come after SvPV to ensure that string overloading has
12abf4f0
NC
1119 fired off. */
1120
08b6664b
KW
1121 /* Create downgraded temporaries of any UTF-8 encoded operands */
1122 if (DO_UTF8(left)) {
b50535da 1123 const U8 * save_lc = (U8 *) lc;
08b6664b 1124
b50535da 1125 left_utf8 = TRUE;
08b6664b
KW
1126 result_needs_to_be_utf8 = TRUE;
1127
b50535da
KW
1128 left_non_downgraded_len = leftlen;
1129 lc = (char *) bytes_from_utf8_loc((const U8 *) lc, &leftlen,
1130 &left_utf8,
1131 (const U8 **) &left_non_downgraded);
1132 /* Calculate the number of trailing unconvertible bytes. This quantity
1133 * is the original length minus the length of the converted portion. */
1134 left_non_downgraded_len -= left_non_downgraded - save_lc;
1135 SAVEFREEPV(lc);
12abf4f0 1136 }
08b6664b 1137 if (DO_UTF8(right)) {
b50535da 1138 const U8 * save_rc = (U8 *) rc;
08b6664b 1139
b50535da 1140 right_utf8 = TRUE;
08b6664b
KW
1141 result_needs_to_be_utf8 = TRUE;
1142
b50535da
KW
1143 right_non_downgraded_len = rightlen;
1144 rc = (char *) bytes_from_utf8_loc((const U8 *) rc, &rightlen,
1145 &right_utf8,
1146 (const U8 **) &right_non_downgraded);
1147 right_non_downgraded_len -= right_non_downgraded - save_rc;
1148 SAVEFREEPV(rc);
1149 }
1150
1151 /* We set 'len' to the length that the operation actually operates on. The
1152 * dangling part of the longer operand doesn't actually participate in the
1153 * operation. What happens is that we pretend that the shorter operand has
1154 * been extended to the right by enough imaginary zeros to match the length
1155 * of the longer one. But we know in advance the result of the operation
1156 * on zeros without having to do it. In the case of '&', the result is
1157 * zero, and the dangling portion is simply discarded. For '|' and '^', the
1158 * result is the same as the other operand, so the dangling part is just
1159 * appended to the final result, unchanged. We currently accept above-FF
1160 * code points in the dangling portion, as that's how it has long worked,
1161 * and code depends on it staying that way. But it is now fatal for
1162 * above-FF to appear in the portion that does get operated on. Hence, any
1163 * above-FF must come only in the longer operand, and only in its dangling
1164 * portion. That means that at least one of the operands has to be
1165 * entirely non-UTF-8, and the length of that operand has to be before the
1166 * first above-FF in the other */
1167 if (left_utf8) {
1168 if (right_utf8 || rightlen > leftlen) {
08b6664b
KW
1169 Perl_croak(aTHX_ fatal_above_ff_msg, PL_op_desc[optype]);
1170 }
b50535da
KW
1171 len = rightlen;
1172 }
1173 else if (right_utf8) {
1174 if (leftlen > rightlen) {
1175 Perl_croak(aTHX_ fatal_above_ff_msg, PL_op_desc[optype]);
1176 }
1177 len = leftlen;
1178 }
1179 else { /* Neither is UTF-8 */
1180 len = leftlen < rightlen ? leftlen : rightlen;
12abf4f0
NC
1181 }
1182
b50535da 1183 lensave = len;
08b6664b
KW
1184 lsave = lc;
1185 rsave = rc;
b50535da 1186
9fdd7463
JH
1187 SvCUR_set(sv, len);
1188 (void)SvPOK_only(sv);
08b6664b 1189 if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
2596d9fe 1190 dc = SvPV_force_nomg_nolen(sv);
bb7a0f54
MHM
1191 if (SvLEN(sv) < len + 1) {
1192 dc = SvGROW(sv, len + 1);
ff68c719 1193 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1194 }
1195 }
1196 else {
bb7a0f54
MHM
1197 needlen = optype == OP_BIT_AND
1198 ? len : (leftlen > rightlen ? leftlen : rightlen);
a02a5408 1199 Newxz(dc, needlen + 1, char);
aa0a69cb 1200 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
ff68c719 1201 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
79072805 1202 }
0c57e439 1203
79072805
LW
1204#ifdef LIBERAL
1205 if (len >= sizeof(long)*4 &&
bb7a0f54
MHM
1206 !((unsigned long)dc % sizeof(long)) &&
1207 !((unsigned long)lc % sizeof(long)) &&
1208 !((unsigned long)rc % sizeof(long))) /* It's almost always aligned... */
79072805 1209 {
bb7a0f54 1210 const STRLEN remainder = len % (sizeof(long)*4);
79072805
LW
1211 len /= (sizeof(long)*4);
1212
1213 dl = (long*)dc;
1214 ll = (long*)lc;
1215 rl = (long*)rc;
1216
1217 switch (optype) {
1218 case OP_BIT_AND:
1219 while (len--) {
1220 *dl++ = *ll++ & *rl++;
1221 *dl++ = *ll++ & *rl++;
1222 *dl++ = *ll++ & *rl++;
1223 *dl++ = *ll++ & *rl++;
1224 }
1225 break;
a0d0e21e 1226 case OP_BIT_XOR:
79072805
LW
1227 while (len--) {
1228 *dl++ = *ll++ ^ *rl++;
1229 *dl++ = *ll++ ^ *rl++;
1230 *dl++ = *ll++ ^ *rl++;
1231 *dl++ = *ll++ ^ *rl++;
1232 }
1233 break;
1234 case OP_BIT_OR:
1235 while (len--) {
1236 *dl++ = *ll++ | *rl++;
1237 *dl++ = *ll++ | *rl++;
1238 *dl++ = *ll++ | *rl++;
1239 *dl++ = *ll++ | *rl++;
1240 }
1241 }
1242
1243 dc = (char*)dl;
1244 lc = (char*)ll;
1245 rc = (char*)rl;
1246
1247 len = remainder;
1248 }
1249#endif
27a9d47d
KW
1250 switch (optype) {
1251 case OP_BIT_AND:
1252 while (len--)
1253 *dc++ = *lc++ & *rc++;
1254 *dc = '\0';
1255 break;
1256 case OP_BIT_XOR:
1257 while (len--)
1258 *dc++ = *lc++ ^ *rc++;
1259 goto mop_up;
1260 case OP_BIT_OR:
1261 while (len--)
1262 *dc++ = *lc++ | *rc++;
1263 mop_up:
1264 len = lensave;
1265 if (rightlen > len) {
1266 if (dc == rc)
1267 SvCUR(sv) = rightlen;
1268 else
1269 sv_catpvn_nomg(sv, rsave + len, rightlen - len);
1270 }
1271 else if (leftlen > len) {
1272 if (dc == lc)
1273 SvCUR(sv) = leftlen;
1274 else
1275 sv_catpvn_nomg(sv, lsave + len, leftlen - len);
1276 }
1277 *SvEND(sv) = '\0';
392582f8 1278
b50535da
KW
1279 /* If there is trailing stuff that couldn't be converted from UTF-8, it
1280 * is appended as-is for the ^ and | operators. This preserves
1281 * backwards compatibility */
1282 if (right_non_downgraded) {
1283 non_downgraded = (char *) right_non_downgraded;
1284 non_downgraded_len = right_non_downgraded_len;
1285 }
1286 else if (left_non_downgraded) {
1287 non_downgraded = (char *) left_non_downgraded;
1288 non_downgraded_len = left_non_downgraded_len;
1289 }
1290
27a9d47d
KW
1291 break;
1292 }
08b6664b
KW
1293
1294 if (result_needs_to_be_utf8) {
b50535da
KW
1295 sv_utf8_upgrade_nomg(sv);
1296
1297 /* Append any trailing UTF-8 as-is. */
1298 if (non_downgraded) {
1299 sv_catpvn_nomg(sv, non_downgraded, non_downgraded_len);
1300 }
79072805 1301 }
08b6664b 1302
fb73857a 1303 SvTAINT(sv);
79072805 1304}
463ee0b2 1305
b1c05ba5 1306
a2232057
DM
1307/* Perl_do_kv() may be:
1308 * * called directly as the pp function for pp_keys() and pp_values();
a2232057
DM
1309 * * It may also be called directly when the op is OP_AVHVSWITCH, to
1310 * implement CORE::keys(), CORE::values().
1311 *
1312 * In all cases it expects an HV on the stack and returns a list of keys,
1313 * values, or key-value pairs, depending on PL_op.
1314 */
b1c05ba5 1315
463ee0b2 1316OP *
cea2e8a9 1317Perl_do_kv(pTHX)
463ee0b2 1318{
39644a26 1319 dSP;
73ff03e8 1320 HV * const keys = MUTABLE_HV(POPs);
1c23e2bd 1321 const U8 gimme = GIMME_V;
a2232057 1322
af3b1cba 1323 const I32 dokeys = (PL_op->op_type == OP_KEYS)
a2232057 1324 || ( PL_op->op_type == OP_AVHVSWITCH
94184451
DM
1325 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1326 + OP_EACH == OP_KEYS);
a2232057 1327
af3b1cba 1328 const I32 dovalues = (PL_op->op_type == OP_VALUES)
a2232057 1329 || ( PL_op->op_type == OP_AVHVSWITCH
94184451
DM
1330 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1331 + OP_EACH == OP_VALUES);
a2232057 1332
af3b1cba 1333 assert( PL_op->op_type == OP_KEYS
a2232057
DM
1334 || PL_op->op_type == OP_VALUES
1335 || PL_op->op_type == OP_AVHVSWITCH);
463ee0b2 1336
4fa080db
DM
1337 assert(!( PL_op->op_type == OP_VALUES
1338 && (PL_op->op_private & OPpMAYBE_LVSUB)));
1339
800e9ae0 1340 (void)hv_iterinit(keys); /* always reset iterator regardless */
748a9306 1341
54310121 1342 if (gimme == G_VOID)
aa689395 1343 RETURN;
1344
54310121 1345 if (gimme == G_SCALAR) {
78f9721b 1346 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
2154eca7
EB
1347 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
1348 sv_magic(ret, NULL, PERL_MAGIC_nkeys, NULL, 0);
1349 LvTYPE(ret) = 'k';
1350 LvTARG(ret) = SvREFCNT_inc_simple(keys);
1351 PUSHs(ret);
81714fb9 1352 }
463ee0b2 1353 else {
2154eca7
EB
1354 IV i;
1355 dTARGET;
1356
af3b1cba
DM
1357 /* note that in 'scalar(keys %h)' the OP_KEYS is usually
1358 * optimised away and the action is performed directly by the
1359 * padhv or rv2hv op. We now only get here via OP_AVHVSWITCH
1360 * and \&CORE::keys
1361 */
2154eca7 1362 if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) ) {
1b95d04f 1363 i = HvUSEDKEYS(keys);
2154eca7
EB
1364 }
1365 else {
1366 i = 0;
1367 while (hv_iternext(keys)) i++;
1368 }
1369 PUSHi( i );
463ee0b2 1370 }
463ee0b2
LW
1371 RETURN;
1372 }
1373
a061ab0b
FC
1374 if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
1375 const I32 flags = is_lvalue_sub();
1376 if (flags && !(flags & OPpENTERSUB_INARGS))
1377 /* diag_listed_as: Can't modify %s in %s */
1378 Perl_croak(aTHX_ "Can't modify keys in list assignment");
1379 }
1380
8dc9003f
DM
1381 PUTBACK;
1382 hv_pushkv(keys, (dokeys | (dovalues << 1)));
1383 return NORMAL;
463ee0b2 1384}
4e35701f 1385
af3babe4 1386/*
14d04a33 1387 * ex: set ts=8 sts=4 sw=4 et:
37442d52 1388 */