This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #118091] Split gv_fetchpvn_flags into smaller functions
[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
FC
18 * ops. For example, both pp_sprintf() and pp_prtf() call the function
19 * do_printf() 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
942e002e 30STATIC I32
c395bd6c 31S_do_trans_simple(pTHX_ SV * const sv)
79072805 32{
97aff369 33 dVAR;
4757a243 34 I32 matches = 0;
463ee0b2 35 STRLEN len;
9138d6ca 36 U8 *s = (U8*)SvPV_nomg(sv,len);
c395bd6c 37 U8 * const send = s+len;
c4420975 38 const short * const tbl = (short*)cPVOP->op_pv;
7918f24d
NC
39
40 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE;
41
4757a243 42 if (!tbl)
7d85a32c 43 Perl_croak(aTHX_ "panic: do_trans_simple line %d",__LINE__);
a0ed51b3 44
1e54db1a 45 /* First, take care of non-UTF-8 input strings, because they're easy */
1aa99e6b 46 if (!SvUTF8(sv)) {
01ec43d0 47 while (s < send) {
f54cb97a
AL
48 const I32 ch = tbl[*s];
49 if (ch >= 0) {
01ec43d0 50 matches++;
c4420975 51 *s = (U8)ch;
01ec43d0 52 }
c4420975 53 s++;
01ec43d0
GS
54 }
55 SvSETMAGIC(sv);
9b877dbb
IH
56 }
57 else {
c395bd6c
AL
58 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
59 U8 *d;
60 U8 *dstart;
61
62 /* Allow for expansion: $_="a".chr(400); tr/a/\xFE/, FE needs encoding */
63 if (grows)
64 Newx(d, len*2+1, U8);
65 else
66 d = s;
67 dstart = d;
68 while (s < send) {
69 STRLEN ulen;
70 I32 ch;
71
72 /* Need to check this, otherwise 128..255 won't match */
73 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
74 if (c < 0x100 && (ch = tbl[c]) >= 0) {
75 matches++;
76 d = uvchr_to_utf8(d, ch);
77 s += ulen;
78 }
79 else { /* No match -> copy */
80 Move(s, d, ulen, U8);
81 d += ulen;
82 s += ulen;
83 }
84 }
85 if (grows) {
86 sv_setpvn(sv, (char*)dstart, d - dstart);
87 Safefree(dstart);
88 }
89 else {
90 *d = '\0';
91 SvCUR_set(sv, d - dstart);
92 }
93 SvUTF8_on(sv);
94 SvSETMAGIC(sv);
9b877dbb 95 }
4757a243
LW
96 return matches;
97}
98
942e002e 99STATIC I32
c395bd6c 100S_do_trans_count(pTHX_ SV * const sv)
4757a243 101{
97aff369 102 dVAR;
4757a243 103 STRLEN len;
9138d6ca 104 const U8 *s = (const U8*)SvPV_nomg_const(sv, len);
c395bd6c
AL
105 const U8 * const send = s + len;
106 I32 matches = 0;
f54cb97a 107 const short * const tbl = (short*)cPVOP->op_pv;
7918f24d
NC
108
109 PERL_ARGS_ASSERT_DO_TRANS_COUNT;
110
4757a243 111 if (!tbl)
7d85a32c 112 Perl_croak(aTHX_ "panic: do_trans_count line %d",__LINE__);
4757a243 113
c395bd6c 114 if (!SvUTF8(sv)) {
1aa99e6b
IH
115 while (s < send) {
116 if (tbl[*s++] >= 0)
036b4402 117 matches++;
1aa99e6b 118 }
c395bd6c 119 }
fabdb6c0
AL
120 else {
121 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
1aa99e6b 122 while (s < send) {
1aa99e6b 123 STRLEN ulen;
9f7f3913 124 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
8973db79
JH
125 if (c < 0x100) {
126 if (tbl[c] >= 0)
127 matches++;
128 } else if (complement)
1aa99e6b
IH
129 matches++;
130 s += ulen;
131 }
fabdb6c0 132 }
4757a243
LW
133
134 return matches;
135}
136
942e002e 137STATIC I32
c395bd6c 138S_do_trans_complex(pTHX_ SV * const sv)
4757a243 139{
97aff369 140 dVAR;
c395bd6c 141 STRLEN len;
9138d6ca 142 U8 *s = (U8*)SvPV_nomg(sv, len);
c395bd6c 143 U8 * const send = s+len;
4757a243 144 I32 matches = 0;
f54cb97a 145 const short * const tbl = (short*)cPVOP->op_pv;
7918f24d
NC
146
147 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX;
148
4757a243 149 if (!tbl)
7d85a32c 150 Perl_croak(aTHX_ "panic: do_trans_complex line %d",__LINE__);
4757a243 151
c395bd6c
AL
152 if (!SvUTF8(sv)) {
153 U8 *d = s;
154 U8 * const dstart = d;
4757a243 155
1aa99e6b 156 if (PL_op->op_private & OPpTRANS_SQUASH) {
f54cb97a 157 const U8* p = send;
1aa99e6b 158 while (s < send) {
f54cb97a
AL
159 const I32 ch = tbl[*s];
160 if (ch >= 0) {
eb160463 161 *d = (U8)ch;
036b4402 162 matches++;
1aa99e6b
IH
163 if (p != d - 1 || *p != *d)
164 p = d++;
165 }
166 else if (ch == -1) /* -1 is unmapped character */
167 *d++ = *s;
168 else if (ch == -2) /* -2 is delete character */
169 matches++;
170 s++;
171 }
a0ed51b3 172 }
1aa99e6b
IH
173 else {
174 while (s < send) {
f54cb97a
AL
175 const I32 ch = tbl[*s];
176 if (ch >= 0) {
1aa99e6b 177 matches++;
eb160463 178 *d++ = (U8)ch;
1aa99e6b
IH
179 }
180 else if (ch == -1) /* -1 is unmapped character */
181 *d++ = *s;
182 else if (ch == -2) /* -2 is delete character */
183 matches++;
184 s++;
185 }
186 }
76ef7183 187 *d = '\0';
1aa99e6b 188 SvCUR_set(sv, d - dstart);
4757a243 189 }
c395bd6c 190 else { /* is utf8 */
fabdb6c0
AL
191 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
192 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
193 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
c395bd6c
AL
194 U8 *d;
195 U8 *dstart;
196 STRLEN rlen = 0;
fabdb6c0 197
9b877dbb 198 if (grows)
a02a5408 199 Newx(d, len*2+1, U8);
9b877dbb
IH
200 else
201 d = s;
1aa99e6b 202 dstart = d;
8973db79
JH
203 if (complement && !del)
204 rlen = tbl[0x100];
1aa99e6b
IH
205
206 if (PL_op->op_private & OPpTRANS_SQUASH) {
1aa99e6b
IH
207 UV pch = 0xfeedface;
208 while (s < send) {
209 STRLEN len;
9f7f3913
ST
210 const UV comp = utf8n_to_uvchr(s, send - s, &len,
211 UTF8_ALLOW_DEFAULT);
f54cb97a 212 I32 ch;
1aa99e6b 213
8973db79
JH
214 if (comp > 0xff) {
215 if (!complement) {
640af7f1 216 Move(s, d, len, U8);
8973db79
JH
217 d += len;
218 }
219 else {
220 matches++;
221 if (!del) {
bb7a0f54 222 ch = (rlen == 0) ? (I32)comp :
05d340b8 223 (comp - 0x100 < rlen) ?
8973db79 224 tbl[comp+1] : tbl[0x100+rlen];
eb160463 225 if ((UV)ch != pch) {
9041c2e3 226 d = uvchr_to_utf8(d, ch);
eb160463 227 pch = (UV)ch;
8973db79
JH
228 }
229 s += len;
230 continue;
231 }
232 }
9b877dbb 233 }
1aa99e6b
IH
234 else if ((ch = tbl[comp]) >= 0) {
235 matches++;
eb160463 236 if ((UV)ch != pch) {
9041c2e3 237 d = uvchr_to_utf8(d, ch);
eb160463 238 pch = (UV)ch;
1aa99e6b
IH
239 }
240 s += len;
241 continue;
242 }
9b877dbb 243 else if (ch == -1) { /* -1 is unmapped character */
640af7f1 244 Move(s, d, len, U8);
9b877dbb
IH
245 d += len;
246 }
1aa99e6b
IH
247 else if (ch == -2) /* -2 is delete character */
248 matches++;
249 s += len;
250 pch = 0xfeedface;
251 }
5d06d08e 252 }
1aa99e6b
IH
253 else {
254 while (s < send) {
255 STRLEN len;
9f7f3913
ST
256 const UV comp = utf8n_to_uvchr(s, send - s, &len,
257 UTF8_ALLOW_DEFAULT);
f54cb97a 258 I32 ch;
8973db79
JH
259 if (comp > 0xff) {
260 if (!complement) {
0376ff32 261 Move(s, d, len, U8);
8973db79
JH
262 d += len;
263 }
264 else {
265 matches++;
266 if (!del) {
9041c2e3
NIS
267 if (comp - 0x100 < rlen)
268 d = uvchr_to_utf8(d, tbl[comp+1]);
8973db79 269 else
9041c2e3 270 d = uvchr_to_utf8(d, tbl[0x100+rlen]);
8973db79
JH
271 }
272 }
9b877dbb 273 }
1aa99e6b 274 else if ((ch = tbl[comp]) >= 0) {
9041c2e3 275 d = uvchr_to_utf8(d, ch);
1aa99e6b
IH
276 matches++;
277 }
278 else if (ch == -1) { /* -1 is unmapped character */
640af7f1 279 Move(s, d, len, U8);
9b877dbb 280 d += len;
1aa99e6b
IH
281 }
282 else if (ch == -2) /* -2 is delete character */
283 matches++;
284 s += len;
285 }
286 }
9b877dbb
IH
287 if (grows) {
288 sv_setpvn(sv, (char*)dstart, d - dstart);
289 Safefree(dstart);
290 }
291 else {
292 *d = '\0';
293 SvCUR_set(sv, d - dstart);
294 }
1aa99e6b 295 SvUTF8_on(sv);
4757a243 296 }
5e44153e 297 SvSETMAGIC(sv);
4757a243
LW
298 return matches;
299}
300
942e002e 301STATIC I32
c395bd6c 302S_do_trans_simple_utf8(pTHX_ SV * const sv)
4757a243 303{
97aff369 304 dVAR;
4757a243
LW
305 U8 *s;
306 U8 *send;
307 U8 *d;
036b4402 308 U8 *start;
1aa99e6b 309 U8 *dstart, *dend;
4757a243 310 I32 matches = 0;
f54cb97a 311 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
4757a243 312 STRLEN len;
043e41b8
DM
313 SV* const rv =
314#ifdef USE_ITHREADS
315 PAD_SVl(cPADOP->op_padix);
316#else
ad64d0ec 317 MUTABLE_SV(cSVOP->op_sv);
043e41b8 318#endif
85fbaab2 319 HV* const hv = MUTABLE_HV(SvRV(rv));
a4fc7abc 320 SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
f54cb97a
AL
321 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
322 const UV extra = none + 1;
b7953727 323 UV final = 0;
1aa99e6b 324 U8 hibit = 0;
4757a243 325
7918f24d
NC
326 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE_UTF8;
327
9138d6ca 328 s = (U8*)SvPV_nomg(sv, len);
c395bd6c 329 if (!SvUTF8(sv)) {
c4420975
AL
330 const U8 *t = s;
331 const U8 * const e = s + len;
c4d5f83a 332 while (t < e) {
f54cb97a 333 const U8 ch = *t++;
fabdb6c0
AL
334 hibit = !NATIVE_IS_INVARIANT(ch);
335 if (hibit) {
336 s = bytes_to_utf8(s, &len);
1aa99e6b 337 break;
fabdb6c0 338 }
c4d5f83a 339 }
1aa99e6b 340 }
4757a243 341 send = s + len;
036b4402 342 start = s;
4757a243 343
a4fc7abc 344 svp = hv_fetchs(hv, "FINAL", FALSE);
4757a243
LW
345 if (svp)
346 final = SvUV(*svp);
347
9b877dbb
IH
348 if (grows) {
349 /* d needs to be bigger than s, in case e.g. upgrading is required */
a02a5408 350 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
9b877dbb
IH
351 dend = d + len * 3;
352 dstart = d;
353 }
354 else {
355 dstart = d = s;
356 dend = d + len;
357 }
1aa99e6b 358
4757a243 359 while (s < send) {
0bcc34c2
AL
360 const UV uv = swash_fetch(rv, s, TRUE);
361 if (uv < none) {
4757a243
LW
362 s += UTF8SKIP(s);
363 matches++;
c80e42f3 364 d = uvchr_to_utf8(d, uv);
4757a243
LW
365 }
366 else if (uv == none) {
4373e329 367 const int i = UTF8SKIP(s);
0376ff32 368 Move(s, d, i, U8);
9b877dbb
IH
369 d += i;
370 s += i;
4757a243
LW
371 }
372 else if (uv == extra) {
4373e329 373 s += UTF8SKIP(s);
4757a243 374 matches++;
c80e42f3 375 d = uvchr_to_utf8(d, final);
4757a243
LW
376 }
377 else
378 s += UTF8SKIP(s);
1aa99e6b 379
9b877dbb 380 if (d > dend) {
4373e329
AL
381 const STRLEN clen = d - dstart;
382 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
9b877dbb 383 if (!grows)
7d85a32c 384 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
89ebb4a3 385 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
1aa99e6b
IH
386 d = dstart + clen;
387 dend = dstart + nlen;
388 }
4757a243 389 }
16ec844d 390 if (grows || hibit) {
9b877dbb
IH
391 sv_setpvn(sv, (char*)dstart, d - dstart);
392 Safefree(dstart);
16ec844d
IH
393 if (grows && hibit)
394 Safefree(start);
9b877dbb
IH
395 }
396 else {
397 *d = '\0';
398 SvCUR_set(sv, d - dstart);
399 }
4757a243 400 SvSETMAGIC(sv);
1aa99e6b 401 SvUTF8_on(sv);
4757a243
LW
402
403 return matches;
404}
405
942e002e 406STATIC I32
c395bd6c 407S_do_trans_count_utf8(pTHX_ SV * const sv)
4757a243 408{
97aff369 409 dVAR;
93524f2b 410 const U8 *s;
cbbf8932
AL
411 const U8 *start = NULL;
412 const U8 *send;
4757a243
LW
413 I32 matches = 0;
414 STRLEN len;
043e41b8
DM
415 SV* const rv =
416#ifdef USE_ITHREADS
417 PAD_SVl(cPADOP->op_padix);
418#else
ad64d0ec 419 MUTABLE_SV(cSVOP->op_sv);
043e41b8 420#endif
85fbaab2 421 HV* const hv = MUTABLE_HV(SvRV(rv));
a4fc7abc 422 SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
f54cb97a
AL
423 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
424 const UV extra = none + 1;
1aa99e6b 425 U8 hibit = 0;
4757a243 426
7918f24d
NC
427 PERL_ARGS_ASSERT_DO_TRANS_COUNT_UTF8;
428
9138d6ca 429 s = (const U8*)SvPV_nomg_const(sv, len);
1aa99e6b 430 if (!SvUTF8(sv)) {
4373e329 431 const U8 *t = s;
c4420975 432 const U8 * const e = s + len;
c4d5f83a 433 while (t < e) {
4373e329 434 const U8 ch = *t++;
fabdb6c0
AL
435 hibit = !NATIVE_IS_INVARIANT(ch);
436 if (hibit) {
437 start = s = bytes_to_utf8(s, &len);
1aa99e6b 438 break;
fabdb6c0 439 }
c4d5f83a 440 }
1aa99e6b 441 }
4757a243
LW
442 send = s + len;
443
444 while (s < send) {
c4420975
AL
445 const UV uv = swash_fetch(rv, s, TRUE);
446 if (uv < none || uv == extra)
4757a243 447 matches++;
834a4ddd 448 s += UTF8SKIP(s);
4757a243 449 }
1aa99e6b
IH
450 if (hibit)
451 Safefree(start);
4757a243
LW
452
453 return matches;
454}
455
942e002e 456STATIC I32
c395bd6c 457S_do_trans_complex_utf8(pTHX_ SV * const sv)
4757a243 458{
97aff369 459 dVAR;
1aa99e6b 460 U8 *start, *send;
4757a243
LW
461 U8 *d;
462 I32 matches = 0;
4373e329
AL
463 const I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
464 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
465 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
043e41b8
DM
466 SV* const rv =
467#ifdef USE_ITHREADS
468 PAD_SVl(cPADOP->op_padix);
469#else
ad64d0ec 470 MUTABLE_SV(cSVOP->op_sv);
043e41b8 471#endif
85fbaab2 472 HV * const hv = MUTABLE_HV(SvRV(rv));
a4fc7abc 473 SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
f54cb97a
AL
474 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
475 const UV extra = none + 1;
b7953727 476 UV final = 0;
45005bfb 477 bool havefinal = FALSE;
4757a243 478 STRLEN len;
1aa99e6b 479 U8 *dstart, *dend;
1aa99e6b 480 U8 hibit = 0;
9138d6ca 481 U8 *s = (U8*)SvPV_nomg(sv, len);
7918f24d
NC
482
483 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX_UTF8;
484
c395bd6c 485 if (!SvUTF8(sv)) {
53c1dcc0
AL
486 const U8 *t = s;
487 const U8 * const e = s + len;
c4d5f83a 488 while (t < e) {
f54cb97a 489 const U8 ch = *t++;
fabdb6c0
AL
490 hibit = !NATIVE_IS_INVARIANT(ch);
491 if (hibit) {
492 s = bytes_to_utf8(s, &len);
1aa99e6b 493 break;
fabdb6c0 494 }
c4d5f83a 495 }
1aa99e6b 496 }
4757a243 497 send = s + len;
1aa99e6b 498 start = s;
4757a243 499
a4fc7abc 500 svp = hv_fetchs(hv, "FINAL", FALSE);
45005bfb 501 if (svp) {
4757a243 502 final = SvUV(*svp);
45005bfb
JH
503 havefinal = TRUE;
504 }
4757a243 505
9b877dbb
IH
506 if (grows) {
507 /* d needs to be bigger than s, in case e.g. upgrading is required */
a02a5408 508 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
9b877dbb
IH
509 dend = d + len * 3;
510 dstart = d;
511 }
512 else {
513 dstart = d = s;
514 dend = d + len;
515 }
4757a243
LW
516
517 if (squash) {
518 UV puv = 0xfeedface;
519 while (s < send) {
f54cb97a 520 UV uv = swash_fetch(rv, s, TRUE);
9041c2e3 521
9b877dbb 522 if (d > dend) {
4373e329
AL
523 const STRLEN clen = d - dstart;
524 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
9b877dbb 525 if (!grows)
7d85a32c 526 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
89ebb4a3 527 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
1aa99e6b
IH
528 d = dstart + clen;
529 dend = dstart + nlen;
4757a243
LW
530 }
531 if (uv < none) {
532 matches++;
94472101 533 s += UTF8SKIP(s);
4757a243 534 if (uv != puv) {
c80e42f3 535 d = uvchr_to_utf8(d, uv);
4757a243
LW
536 puv = uv;
537 }
4757a243
LW
538 continue;
539 }
540 else if (uv == none) { /* "none" is unmapped character */
4373e329 541 const int i = UTF8SKIP(s);
0376ff32 542 Move(s, d, i, U8);
9b877dbb
IH
543 d += i;
544 s += i;
4757a243
LW
545 puv = 0xfeedface;
546 continue;
a0ed51b3 547 }
4757a243
LW
548 else if (uv == extra && !del) {
549 matches++;
45005bfb
JH
550 if (havefinal) {
551 s += UTF8SKIP(s);
552 if (puv != final) {
c80e42f3 553 d = uvchr_to_utf8(d, final);
45005bfb
JH
554 puv = final;
555 }
556 }
557 else {
558 STRLEN len;
c80e42f3 559 uv = utf8n_to_uvchr(s, send - s, &len, UTF8_ALLOW_DEFAULT);
45005bfb 560 if (uv != puv) {
0376ff32 561 Move(s, d, len, U8);
45005bfb
JH
562 d += len;
563 puv = uv;
564 }
565 s += len;
4757a243 566 }
4757a243
LW
567 continue;
568 }
01ec43d0
GS
569 matches++; /* "none+1" is delete character */
570 s += UTF8SKIP(s);
a0ed51b3 571 }
79072805
LW
572 }
573 else {
4757a243 574 while (s < send) {
f54cb97a 575 const UV uv = swash_fetch(rv, s, TRUE);
9b877dbb 576 if (d > dend) {
f54cb97a
AL
577 const STRLEN clen = d - dstart;
578 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
9b877dbb 579 if (!grows)
7d85a32c 580 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
89ebb4a3 581 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
1aa99e6b
IH
582 d = dstart + clen;
583 dend = dstart + nlen;
a0ed51b3 584 }
4757a243
LW
585 if (uv < none) {
586 matches++;
01ec43d0 587 s += UTF8SKIP(s);
c80e42f3 588 d = uvchr_to_utf8(d, uv);
4757a243 589 continue;
a0ed51b3 590 }
4757a243 591 else if (uv == none) { /* "none" is unmapped character */
f54cb97a 592 const int i = UTF8SKIP(s);
0376ff32 593 Move(s, d, i, U8);
9b877dbb
IH
594 d += i;
595 s += i;
4757a243 596 continue;
79072805 597 }
4757a243
LW
598 else if (uv == extra && !del) {
599 matches++;
01ec43d0 600 s += UTF8SKIP(s);
c80e42f3 601 d = uvchr_to_utf8(d, final);
4757a243
LW
602 continue;
603 }
01ec43d0
GS
604 matches++; /* "none+1" is delete character */
605 s += UTF8SKIP(s);
79072805 606 }
4757a243 607 }
16ec844d 608 if (grows || hibit) {
9b877dbb
IH
609 sv_setpvn(sv, (char*)dstart, d - dstart);
610 Safefree(dstart);
16ec844d
IH
611 if (grows && hibit)
612 Safefree(start);
9b877dbb
IH
613 }
614 else {
615 *d = '\0';
616 SvCUR_set(sv, d - dstart);
617 }
1aa99e6b 618 SvUTF8_on(sv);
4757a243
LW
619 SvSETMAGIC(sv);
620
621 return matches;
622}
623
624I32
864dbfa3 625Perl_do_trans(pTHX_ SV *sv)
4757a243 626{
97aff369 627 dVAR;
4757a243 628 STRLEN len;
4373e329 629 const I32 hasutf = (PL_op->op_private &
036b4402 630 (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF));
4757a243 631
7918f24d
NC
632 PERL_ARGS_ASSERT_DO_TRANS;
633
3788ef8f 634 if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL)) {
cb077ed2 635 Perl_croak_no_modify();
2233f375 636 }
10516c54 637 (void)SvPV_const(sv, len);
4757a243
LW
638 if (!len)
639 return 0;
d59e14db 640 if (!(PL_op->op_private & OPpTRANS_IDENTICAL)) {
4499db73 641 if (!SvPOKp(sv) || SvTHINKFIRST(sv))
b4cc4d79 642 (void)SvPV_force_nomg(sv, len);
2de7b02f 643 (void)SvPOK_only_UTF8(sv);
d59e14db 644 }
4757a243 645
cea2e8a9 646 DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
4757a243 647
a77e643a
RGS
648 switch (PL_op->op_private & ~hasutf & (
649 OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF|OPpTRANS_IDENTICAL|
650 OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
4757a243 651 case 0:
01ec43d0
GS
652 if (hasutf)
653 return do_trans_simple_utf8(sv);
654 else
655 return do_trans_simple(sv);
4757a243
LW
656
657 case OPpTRANS_IDENTICAL:
05d340b8 658 case OPpTRANS_IDENTICAL|OPpTRANS_COMPLEMENT:
01ec43d0
GS
659 if (hasutf)
660 return do_trans_count_utf8(sv);
661 else
662 return do_trans_count(sv);
4757a243
LW
663
664 default:
01ec43d0 665 if (hasutf)
036b4402 666 return do_trans_complex_utf8(sv);
4757a243 667 else
036b4402 668 return do_trans_complex(sv);
79072805 669 }
79072805
LW
670}
671
672void
5aaab254 673Perl_do_join(pTHX_ SV *sv, SV *delim, SV **mark, SV **sp)
79072805 674{
97aff369 675 dVAR;
53c1dcc0 676 SV ** const oldmark = mark;
eb578fdb
KW
677 I32 items = sp - mark;
678 STRLEN len;
463ee0b2 679 STRLEN delimlen;
79072805 680
7918f24d
NC
681 PERL_ARGS_ASSERT_DO_JOIN;
682
b9ac451d 683 (void) SvPV_const(delim, delimlen); /* stringify and get the delimlen */
5ba99574 684 /* SvCUR assumes it's SvPOK() and woe betide you if it's not. */
516a5887 685
79072805
LW
686 mark++;
687 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
862a34c6 688 SvUPGRADE(sv, SVt_PV);
79072805
LW
689 if (SvLEN(sv) < len + items) { /* current length is way too short */
690 while (items-- > 0) {
1426bbf4 691 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
f54cb97a 692 STRLEN tmplen;
5c144d81 693 SvPV_const(*mark, tmplen);
463ee0b2 694 len += tmplen;
79072805
LW
695 }
696 mark++;
697 }
698 SvGROW(sv, len + 1); /* so try to pre-extend */
699
700 mark = oldmark;
db7c17d7 701 items = sp - mark;
79072805
LW
702 ++mark;
703 }
704
76f68e9b 705 sv_setpvs(sv, "");
e4803c42 706 /* sv_setpv retains old UTF8ness [perl #24846] */
fb622db0 707 SvUTF8_off(sv);
e4803c42 708
284167a5 709 if (TAINTING_get && SvMAGICAL(sv))
8d6d96c1
HS
710 SvTAINTED_off(sv);
711
463ee0b2 712 if (items-- > 0) {
92d29cee
JH
713 if (*mark)
714 sv_catsv(sv, *mark);
463ee0b2
LW
715 mark++;
716 }
8d6d96c1 717
c512ce4f 718 if (delimlen) {
79072805 719 for (; items > 0; items--,mark++) {
760209f8 720 sv_catsv_nomg(sv,delim);
79072805
LW
721 sv_catsv(sv,*mark);
722 }
723 }
724 else {
725 for (; items > 0; items--,mark++)
726 sv_catsv(sv,*mark);
727 }
728 SvSETMAGIC(sv);
729}
730
731void
864dbfa3 732Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg)
79072805 733{
97aff369 734 dVAR;
46fc3d4c 735 STRLEN patlen;
53c1dcc0 736 const char * const pat = SvPV_const(*sarg, patlen);
46fc3d4c
PP
737 bool do_taint = FALSE;
738
7918f24d
NC
739 PERL_ARGS_ASSERT_DO_SPRINTF;
740
e06d98fb
DM
741 if (SvTAINTED(*sarg))
742 TAINT_PROPER(
743 (PL_op && PL_op->op_type < OP_max)
744 ? (PL_op->op_type == OP_PRTF)
745 ? "printf"
746 : PL_op_name[PL_op->op_type]
747 : "(unknown)"
748 );
5b781b5b 749 SvUTF8_off(sv);
2cf2cfc6
A
750 if (DO_UTF8(*sarg))
751 SvUTF8_on(sv);
4608196e 752 sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, len - 1, &do_taint);
79072805 753 SvSETMAGIC(sv);
46fc3d4c
PP
754 if (do_taint)
755 SvTAINTED_on(sv);
79072805
LW
756}
757
33b45480 758/* currently converts input to bytes if possible, but doesn't sweat failure */
81e118e0 759UV
fcb8da00 760Perl_do_vecget(pTHX_ SV *sv, SSize_t offset, int size)
81e118e0 761{
97aff369 762 dVAR;
bbb8a7e0 763 STRLEN srclen, len, uoffset, bitoffs = 0;
032061d2
BF
764 const unsigned char *s = (const unsigned char *) SvPV_flags_const(sv, srclen,
765 SV_GMAGIC | ((PL_op->op_flags & OPf_MOD || LVRET)
766 ? SV_UNDEF_RETURNS_NULL : 0));
81e118e0
JH
767 UV retnum = 0;
768
032061d2
BF
769 if (!s) {
770 s = (const unsigned char *)"";
771 }
772
7918f24d
NC
773 PERL_ARGS_ASSERT_DO_VECGET;
774
a50d7633 775 if (offset < 0)
61f9802b 776 return 0;
8e84507e 777 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
a50d7633 778 Perl_croak(aTHX_ "Illegal number of bits in vec");
246fae53 779
dcad2880 780 if (SvUTF8(sv))
33b45480 781 (void) Perl_sv_utf8_downgrade(aTHX_ sv, TRUE);
246fae53 782
bbb8a7e0
MHM
783 if (size < 8) {
784 bitoffs = ((offset%8)*size)%8;
785 uoffset = offset/(8/size);
786 }
787 else if (size > 8)
788 uoffset = offset*(size/8);
789 else
790 uoffset = offset;
791
792 len = uoffset + (bitoffs + size + 7)/8; /* required number of bytes */
81e118e0
JH
793 if (len > srclen) {
794 if (size <= 8)
795 retnum = 0;
796 else {
81e118e0 797 if (size == 16) {
bb7a0f54 798 if (uoffset >= srclen)
81e118e0
JH
799 retnum = 0;
800 else
bb7a0f54 801 retnum = (UV) s[uoffset] << 8;
81e118e0
JH
802 }
803 else if (size == 32) {
bb7a0f54 804 if (uoffset >= srclen)
81e118e0 805 retnum = 0;
bb7a0f54 806 else if (uoffset + 1 >= srclen)
81e118e0 807 retnum =
bb7a0f54
MHM
808 ((UV) s[uoffset ] << 24);
809 else if (uoffset + 2 >= srclen)
81e118e0 810 retnum =
bb7a0f54
MHM
811 ((UV) s[uoffset ] << 24) +
812 ((UV) s[uoffset + 1] << 16);
81e118e0
JH
813 else
814 retnum =
bb7a0f54
MHM
815 ((UV) s[uoffset ] << 24) +
816 ((UV) s[uoffset + 1] << 16) +
817 ( s[uoffset + 2] << 8);
81e118e0 818 }
d7d93a81 819#ifdef UV_IS_QUAD
c5a0f51a 820 else if (size == 64) {
a2a5de95
NC
821 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
822 "Bit vector size > 32 non-portable");
bb7a0f54 823 if (uoffset >= srclen)
c5a0f51a 824 retnum = 0;
bb7a0f54 825 else if (uoffset + 1 >= srclen)
c5a0f51a 826 retnum =
bb7a0f54
MHM
827 (UV) s[uoffset ] << 56;
828 else if (uoffset + 2 >= srclen)
c5a0f51a 829 retnum =
bb7a0f54
MHM
830 ((UV) s[uoffset ] << 56) +
831 ((UV) s[uoffset + 1] << 48);
832 else if (uoffset + 3 >= srclen)
c5a0f51a 833 retnum =
bb7a0f54
MHM
834 ((UV) s[uoffset ] << 56) +
835 ((UV) s[uoffset + 1] << 48) +
836 ((UV) s[uoffset + 2] << 40);
837 else if (uoffset + 4 >= srclen)
c5a0f51a 838 retnum =
bb7a0f54
MHM
839 ((UV) s[uoffset ] << 56) +
840 ((UV) s[uoffset + 1] << 48) +
841 ((UV) s[uoffset + 2] << 40) +
842 ((UV) s[uoffset + 3] << 32);
843 else if (uoffset + 5 >= srclen)
c5a0f51a 844 retnum =
bb7a0f54
MHM
845 ((UV) s[uoffset ] << 56) +
846 ((UV) s[uoffset + 1] << 48) +
847 ((UV) s[uoffset + 2] << 40) +
848 ((UV) s[uoffset + 3] << 32) +
849 ( s[uoffset + 4] << 24);
850 else if (uoffset + 6 >= srclen)
c5a0f51a 851 retnum =
bb7a0f54
MHM
852 ((UV) s[uoffset ] << 56) +
853 ((UV) s[uoffset + 1] << 48) +
854 ((UV) s[uoffset + 2] << 40) +
855 ((UV) s[uoffset + 3] << 32) +
856 ((UV) s[uoffset + 4] << 24) +
857 ((UV) s[uoffset + 5] << 16);
c5a0f51a 858 else
8e84507e 859 retnum =
bb7a0f54
MHM
860 ((UV) s[uoffset ] << 56) +
861 ((UV) s[uoffset + 1] << 48) +
862 ((UV) s[uoffset + 2] << 40) +
863 ((UV) s[uoffset + 3] << 32) +
864 ((UV) s[uoffset + 4] << 24) +
865 ((UV) s[uoffset + 5] << 16) +
866 ( s[uoffset + 6] << 8);
c5a0f51a
JH
867 }
868#endif
81e118e0
JH
869 }
870 }
871 else if (size < 8)
bbb8a7e0 872 retnum = (s[uoffset] >> bitoffs) & ((1 << size) - 1);
81e118e0 873 else {
81e118e0 874 if (size == 8)
bb7a0f54 875 retnum = s[uoffset];
81e118e0
JH
876 else if (size == 16)
877 retnum =
bb7a0f54
MHM
878 ((UV) s[uoffset] << 8) +
879 s[uoffset + 1];
81e118e0
JH
880 else if (size == 32)
881 retnum =
bb7a0f54
MHM
882 ((UV) s[uoffset ] << 24) +
883 ((UV) s[uoffset + 1] << 16) +
884 ( s[uoffset + 2] << 8) +
885 s[uoffset + 3];
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");
c5a0f51a 890 retnum =
bb7a0f54
MHM
891 ((UV) s[uoffset ] << 56) +
892 ((UV) s[uoffset + 1] << 48) +
893 ((UV) s[uoffset + 2] << 40) +
894 ((UV) s[uoffset + 3] << 32) +
895 ((UV) s[uoffset + 4] << 24) +
896 ((UV) s[uoffset + 5] << 16) +
897 ( s[uoffset + 6] << 8) +
898 s[uoffset + 7];
c5a0f51a
JH
899 }
900#endif
81e118e0
JH
901 }
902
903 return retnum;
904}
905
33b45480
SB
906/* currently converts input to bytes if possible but doesn't sweat failures,
907 * although it does ensure that the string it clobbers is not marked as
908 * utf8-valid any more
909 */
79072805 910void
864dbfa3 911Perl_do_vecset(pTHX_ SV *sv)
79072805 912{
97aff369 913 dVAR;
eb578fdb
KW
914 SSize_t offset, bitoffs = 0;
915 int size;
916 unsigned char *s;
917 UV lval;
79072805 918 I32 mask;
a0d0e21e
LW
919 STRLEN targlen;
920 STRLEN len;
c4420975 921 SV * const targ = LvTARG(sv);
79072805 922
7918f24d
NC
923 PERL_ARGS_ASSERT_DO_VECSET;
924
8990e307
LW
925 if (!targ)
926 return;
032061d2
BF
927 s = (unsigned char*)SvPV_force_flags(targ, targlen,
928 SV_GMAGIC | SV_UNDEF_RETURNS_NULL);
246fae53 929 if (SvUTF8(targ)) {
33b45480
SB
930 /* This is handled by the SvPOK_only below...
931 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
932 SvUTF8_off(targ);
933 */
934 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
246fae53
MG
935 }
936
4ebbc975 937 (void)SvPOK_only(targ);
81e118e0 938 lval = SvUV(sv);
79072805 939 offset = LvTARGOFF(sv);
fe58ced6 940 if (offset < 0)
ed9aa3b7 941 Perl_croak(aTHX_ "Negative offset to vec in lvalue context");
79072805 942 size = LvTARGLEN(sv);
8e84507e 943 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
a50d7633 944 Perl_croak(aTHX_ "Illegal number of bits in vec");
8e84507e 945
bbb8a7e0
MHM
946 if (size < 8) {
947 bitoffs = ((offset%8)*size)%8;
948 offset /= 8/size;
949 }
950 else if (size > 8)
951 offset *= size/8;
952
953 len = offset + (bitoffs + size + 7)/8; /* required number of bytes */
a0d0e21e
LW
954 if (len > targlen) {
955 s = (unsigned char*)SvGROW(targ, len + 1);
12ae5dfc 956 (void)memzero((char *)(s + targlen), len - targlen + 1);
a0d0e21e
LW
957 SvCUR_set(targ, len);
958 }
8e84507e 959
79072805
LW
960 if (size < 8) {
961 mask = (1 << size) - 1;
79072805 962 lval &= mask;
bbb8a7e0
MHM
963 s[offset] &= ~(mask << bitoffs);
964 s[offset] |= lval << bitoffs;
79072805
LW
965 }
966 else {
967 if (size == 8)
eb160463 968 s[offset ] = (U8)( lval & 0xff);
79072805 969 else if (size == 16) {
eb160463
GS
970 s[offset ] = (U8)((lval >> 8) & 0xff);
971 s[offset+1] = (U8)( lval & 0xff);
79072805
LW
972 }
973 else if (size == 32) {
eb160463
GS
974 s[offset ] = (U8)((lval >> 24) & 0xff);
975 s[offset+1] = (U8)((lval >> 16) & 0xff);
976 s[offset+2] = (U8)((lval >> 8) & 0xff);
977 s[offset+3] = (U8)( lval & 0xff);
c5a0f51a 978 }
d7d93a81 979#ifdef UV_IS_QUAD
c5a0f51a 980 else if (size == 64) {
a2a5de95
NC
981 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
982 "Bit vector size > 32 non-portable");
eb160463
GS
983 s[offset ] = (U8)((lval >> 56) & 0xff);
984 s[offset+1] = (U8)((lval >> 48) & 0xff);
985 s[offset+2] = (U8)((lval >> 40) & 0xff);
986 s[offset+3] = (U8)((lval >> 32) & 0xff);
987 s[offset+4] = (U8)((lval >> 24) & 0xff);
988 s[offset+5] = (U8)((lval >> 16) & 0xff);
989 s[offset+6] = (U8)((lval >> 8) & 0xff);
990 s[offset+7] = (U8)( lval & 0xff);
79072805 991 }
dc1e3f56 992#endif
79072805 993 }
7bb043c3 994 SvSETMAGIC(targ);
79072805
LW
995}
996
997void
864dbfa3 998Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
79072805 999{
97aff369 1000 dVAR;
79072805 1001#ifdef LIBERAL
eb578fdb
KW
1002 long *dl;
1003 long *ll;
1004 long *rl;
79072805 1005#endif
eb578fdb 1006 char *dc;
463ee0b2
LW
1007 STRLEN leftlen;
1008 STRLEN rightlen;
eb578fdb
KW
1009 const char *lc;
1010 const char *rc;
1011 STRLEN len;
bb7a0f54 1012 STRLEN lensave;
e62f0680
NC
1013 const char *lsave;
1014 const char *rsave;
12abf4f0
NC
1015 bool left_utf;
1016 bool right_utf;
bb7a0f54 1017 STRLEN needlen = 0;
0c57e439 1018
7918f24d 1019 PERL_ARGS_ASSERT_DO_VOP;
79072805 1020
6b349a5c 1021 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv)))
76f68e9b 1022 sv_setpvs(sv, ""); /* avoid undef warning on |= and ^= */
8c8eee82
BM
1023 if (sv == left) {
1024 lsave = lc = SvPV_force_nomg(left, leftlen);
1025 }
1026 else {
1027 lsave = lc = SvPV_nomg_const(left, leftlen);
1028 SvPV_force_nomg_nolen(sv);
1029 }
e62f0680 1030 rsave = rc = SvPV_nomg_const(right, rightlen);
12abf4f0
NC
1031
1032 /* This need to come after SvPV to ensure that string overloading has
1033 fired off. */
1034
1035 left_utf = DO_UTF8(left);
1036 right_utf = DO_UTF8(right);
1037
1038 if (left_utf && !right_utf) {
1039 /* Avoid triggering overloading again by using temporaries.
1040 Maybe there should be a variant of sv_utf8_upgrade that takes pvn
1041 */
59cd0e26 1042 right = newSVpvn_flags(rsave, rightlen, SVs_TEMP);
12abf4f0
NC
1043 sv_utf8_upgrade(right);
1044 rsave = rc = SvPV_nomg_const(right, rightlen);
1045 right_utf = TRUE;
1046 }
1047 else if (!left_utf && right_utf) {
59cd0e26 1048 left = newSVpvn_flags(lsave, leftlen, SVs_TEMP);
12abf4f0
NC
1049 sv_utf8_upgrade(left);
1050 lsave = lc = SvPV_nomg_const(left, leftlen);
1051 left_utf = TRUE;
1052 }
1053
93a17b20 1054 len = leftlen < rightlen ? leftlen : rightlen;
a0d0e21e 1055 lensave = len;
9fdd7463
JH
1056 SvCUR_set(sv, len);
1057 (void)SvPOK_only(sv);
c9b3c8d0
JH
1058 if ((left_utf || right_utf) && (sv == left || sv == right)) {
1059 needlen = optype == OP_BIT_AND ? len : leftlen + rightlen;
a02a5408 1060 Newxz(dc, needlen + 1, char);
c9b3c8d0
JH
1061 }
1062 else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
2596d9fe 1063 dc = SvPV_force_nomg_nolen(sv);
bb7a0f54
MHM
1064 if (SvLEN(sv) < len + 1) {
1065 dc = SvGROW(sv, len + 1);
ff68c719
PP
1066 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1067 }
c9b3c8d0
JH
1068 if (optype != OP_BIT_AND && (left_utf || right_utf))
1069 dc = SvGROW(sv, leftlen + rightlen + 1);
ff68c719
PP
1070 }
1071 else {
bb7a0f54
MHM
1072 needlen = optype == OP_BIT_AND
1073 ? len : (leftlen > rightlen ? leftlen : rightlen);
a02a5408 1074 Newxz(dc, needlen + 1, char);
aa0a69cb 1075 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
ff68c719 1076 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
79072805 1077 }
0c57e439
GS
1078 if (left_utf || right_utf) {
1079 UV duc, luc, ruc;
794a0d33
JH
1080 char *dcorig = dc;
1081 char *dcsave = NULL;
0c57e439
GS
1082 STRLEN lulen = leftlen;
1083 STRLEN rulen = rightlen;
ba210ebe 1084 STRLEN ulen;
0c57e439
GS
1085
1086 switch (optype) {
1087 case OP_BIT_AND:
1088 while (lulen && rulen) {
9041c2e3 1089 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439
GS
1090 lc += ulen;
1091 lulen -= ulen;
9041c2e3 1092 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439
GS
1093 rc += ulen;
1094 rulen -= ulen;
1095 duc = luc & ruc;
9041c2e3 1096 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
0c57e439 1097 }
3ce4c532
NC
1098 if (sv == left || sv == right)
1099 (void)sv_usepvn(sv, dcorig, needlen);
794a0d33 1100 SvCUR_set(sv, dc - dcorig);
0c57e439
GS
1101 break;
1102 case OP_BIT_XOR:
1103 while (lulen && rulen) {
9041c2e3 1104 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439
GS
1105 lc += ulen;
1106 lulen -= ulen;
9041c2e3 1107 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439
GS
1108 rc += ulen;
1109 rulen -= ulen;
1110 duc = luc ^ ruc;
9041c2e3 1111 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
0c57e439
GS
1112 }
1113 goto mop_up_utf;
1114 case OP_BIT_OR:
1115 while (lulen && rulen) {
9041c2e3 1116 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439
GS
1117 lc += ulen;
1118 lulen -= ulen;
9041c2e3 1119 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439
GS
1120 rc += ulen;
1121 rulen -= ulen;
1122 duc = luc | ruc;
9041c2e3 1123 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
0c57e439
GS
1124 }
1125 mop_up_utf:
794a0d33
JH
1126 if (rulen)
1127 dcsave = savepvn(rc, rulen);
1128 else if (lulen)
1129 dcsave = savepvn(lc, lulen);
3ce4c532 1130 if (sv == left || sv == right)
4bac9ae4 1131 (void)sv_usepvn(sv, dcorig, needlen); /* uses Renew(); defaults to nomg */
794a0d33 1132 SvCUR_set(sv, dc - dcorig);
0c57e439 1133 if (rulen)
4bac9ae4 1134 sv_catpvn_nomg(sv, dcsave, rulen);
0c57e439 1135 else if (lulen)
4bac9ae4 1136 sv_catpvn_nomg(sv, dcsave, lulen);
0c57e439
GS
1137 else
1138 *SvEND(sv) = '\0';
794a0d33 1139 Safefree(dcsave);
0c57e439 1140 break;
41547412
NC
1141 default:
1142 if (sv == left || sv == right)
1143 Safefree(dcorig);
c395bd6c
AL
1144 Perl_croak(aTHX_ "panic: do_vop called for op %u (%s)",
1145 (unsigned)optype, PL_op_name[optype]);
0c57e439
GS
1146 }
1147 SvUTF8_on(sv);
1148 goto finish;
1149 }
1150 else
79072805
LW
1151#ifdef LIBERAL
1152 if (len >= sizeof(long)*4 &&
bb7a0f54
MHM
1153 !((unsigned long)dc % sizeof(long)) &&
1154 !((unsigned long)lc % sizeof(long)) &&
1155 !((unsigned long)rc % sizeof(long))) /* It's almost always aligned... */
79072805 1156 {
bb7a0f54 1157 const STRLEN remainder = len % (sizeof(long)*4);
79072805
LW
1158 len /= (sizeof(long)*4);
1159
1160 dl = (long*)dc;
1161 ll = (long*)lc;
1162 rl = (long*)rc;
1163
1164 switch (optype) {
1165 case OP_BIT_AND:
1166 while (len--) {
1167 *dl++ = *ll++ & *rl++;
1168 *dl++ = *ll++ & *rl++;
1169 *dl++ = *ll++ & *rl++;
1170 *dl++ = *ll++ & *rl++;
1171 }
1172 break;
a0d0e21e 1173 case OP_BIT_XOR:
79072805
LW
1174 while (len--) {
1175 *dl++ = *ll++ ^ *rl++;
1176 *dl++ = *ll++ ^ *rl++;
1177 *dl++ = *ll++ ^ *rl++;
1178 *dl++ = *ll++ ^ *rl++;
1179 }
1180 break;
1181 case OP_BIT_OR:
1182 while (len--) {
1183 *dl++ = *ll++ | *rl++;
1184 *dl++ = *ll++ | *rl++;
1185 *dl++ = *ll++ | *rl++;
1186 *dl++ = *ll++ | *rl++;
1187 }
1188 }
1189
1190 dc = (char*)dl;
1191 lc = (char*)ll;
1192 rc = (char*)rl;
1193
1194 len = remainder;
1195 }
1196#endif
a0d0e21e 1197 {
a0d0e21e
LW
1198 switch (optype) {
1199 case OP_BIT_AND:
1200 while (len--)
1201 *dc++ = *lc++ & *rc++;
1a787b95 1202 *dc = '\0';
a0d0e21e
LW
1203 break;
1204 case OP_BIT_XOR:
1205 while (len--)
1206 *dc++ = *lc++ ^ *rc++;
1207 goto mop_up;
1208 case OP_BIT_OR:
1209 while (len--)
1210 *dc++ = *lc++ | *rc++;
1211 mop_up:
1212 len = lensave;
bb7a0f54 1213 if (rightlen > len)
4bac9ae4 1214 sv_catpvn_nomg(sv, rsave + len, rightlen - len);
eb160463 1215 else if (leftlen > (STRLEN)len)
4bac9ae4 1216 sv_catpvn_nomg(sv, lsave + len, leftlen - len);
4633a7c4
LW
1217 else
1218 *SvEND(sv) = '\0';
a0d0e21e
LW
1219 break;
1220 }
79072805 1221 }
0c57e439 1222finish:
fb73857a 1223 SvTAINT(sv);
79072805 1224}
463ee0b2
LW
1225
1226OP *
cea2e8a9 1227Perl_do_kv(pTHX)
463ee0b2 1228{
97aff369 1229 dVAR;
39644a26 1230 dSP;
73ff03e8 1231 HV * const keys = MUTABLE_HV(POPs);
eb578fdb 1232 HE *entry;
f54cb97a
AL
1233 const I32 gimme = GIMME_V;
1234 const I32 dokv = (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV);
cba5a3b0
DG
1235 /* op_type is OP_RKEYS/OP_RVALUES if pp_rkeys delegated to here */
1236 const I32 dokeys = dokv || (PL_op->op_type == OP_KEYS || PL_op->op_type == OP_RKEYS);
1237 const I32 dovalues = dokv || (PL_op->op_type == OP_VALUES || PL_op->op_type == OP_RVALUES);
463ee0b2 1238
800e9ae0 1239 (void)hv_iterinit(keys); /* always reset iterator regardless */
748a9306 1240
54310121 1241 if (gimme == G_VOID)
aa689395
PP
1242 RETURN;
1243
54310121 1244 if (gimme == G_SCALAR) {
78f9721b 1245 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
2154eca7
EB
1246 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
1247 sv_magic(ret, NULL, PERL_MAGIC_nkeys, NULL, 0);
1248 LvTYPE(ret) = 'k';
1249 LvTARG(ret) = SvREFCNT_inc_simple(keys);
1250 PUSHs(ret);
81714fb9 1251 }
463ee0b2 1252 else {
2154eca7
EB
1253 IV i;
1254 dTARGET;
1255
1256 if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) ) {
1b95d04f 1257 i = HvUSEDKEYS(keys);
2154eca7
EB
1258 }
1259 else {
1260 i = 0;
1261 while (hv_iternext(keys)) i++;
1262 }
1263 PUSHi( i );
463ee0b2 1264 }
463ee0b2
LW
1265 RETURN;
1266 }
1267
1b95d04f 1268 EXTEND(SP, HvUSEDKEYS(keys) * (dokeys + dovalues));
463ee0b2 1269
463ee0b2 1270 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
155aba94 1271 while ((entry = hv_iternext(keys))) {
463ee0b2 1272 SPAGAIN;
574c8022 1273 if (dokeys) {
890ce7af 1274 SV* const sv = hv_iterkeysv(entry);
574c8022
JH
1275 XPUSHs(sv); /* won't clobber stack_sp */
1276 }
463ee0b2 1277 if (dovalues) {
f54cb97a 1278 SV *tmpstr;
463ee0b2 1279 PUTBACK;
73ff03e8 1280 tmpstr = hv_iterval(keys,entry);
cea2e8a9 1281 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
46fc3d4c 1282 (unsigned long)HeHASH(entry),
cdd3ba14 1283 (int)HvMAX(keys)+1,
800e9ae0 1284 (unsigned long)(HeHASH(entry) & HvMAX(keys))));
463ee0b2 1285 SPAGAIN;
46fc3d4c 1286 XPUSHs(tmpstr);
463ee0b2
LW
1287 }
1288 PUTBACK;
1289 }
1290 return NORMAL;
1291}
4e35701f 1292
af3babe4
NC
1293/*
1294 * Local variables:
1295 * c-indentation-style: bsd
1296 * c-basic-offset: 4
14d04a33 1297 * indent-tabs-mode: nil
af3babe4
NC
1298 * End:
1299 *
14d04a33 1300 * ex: set ts=8 sts=4 sw=4 et:
37442d52 1301 */