This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate change #9233 from maintperl into mainline.
[perl5.git] / doop.c
CommitLineData
a0d0e21e 1/* doop.c
79072805 2 *
bc89e66f 3 * Copyright (c) 1991-2001, Larry Wall
79072805
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
a0d0e21e
LW
8 */
9
10/*
11 * "'So that was the job I felt I had to do when I started,' thought Sam."
79072805
LW
12 */
13
14#include "EXTERN.h"
864dbfa3 15#define PERL_IN_DOOP_C
79072805
LW
16#include "perl.h"
17
64ca3a65 18#ifndef PERL_MICRO
79072805
LW
19#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
20#include <signal.h>
21#endif
64ca3a65 22#endif
79072805 23
942e002e 24STATIC I32
b250498f 25S_do_trans_simple(pTHX_ SV *sv)
79072805 26{
4757a243 27 U8 *s;
b250498f 28 U8 *d;
4757a243 29 U8 *send;
b250498f 30 U8 *dstart;
4757a243 31 I32 matches = 0;
9b877dbb 32 I32 grows = PL_op->op_private & OPpTRANS_GROWS;
463ee0b2 33 STRLEN len;
4757a243
LW
34 short *tbl;
35 I32 ch;
79072805 36
4757a243
LW
37 tbl = (short*)cPVOP->op_pv;
38 if (!tbl)
2269b42e 39 Perl_croak(aTHX_ "panic: do_trans_simple");
a0ed51b3 40
4757a243
LW
41 s = (U8*)SvPV(sv, len);
42 send = s + len;
43
b250498f 44 /* First, take care of non-UTF8 input strings, because they're easy */
1aa99e6b 45 if (!SvUTF8(sv)) {
01ec43d0 46 while (s < send) {
036b4402 47 if ((ch = tbl[*s]) >= 0) {
01ec43d0
GS
48 matches++;
49 *s++ = ch;
50 }
51 else
52 s++;
53 }
54 SvSETMAGIC(sv);
b250498f
GS
55 return matches;
56 }
4757a243 57
b250498f 58 /* Allow for expansion: $_="a".chr(400); tr/a/\xFE/, FE needs encoding */
9b877dbb
IH
59 if (grows)
60 New(0, d, len*2+1, U8);
61 else
62 d = s;
b250498f
GS
63 dstart = d;
64 while (s < send) {
ba210ebe 65 STRLEN ulen;
9b877dbb 66 UV c;
b250498f 67
b250498f 68 /* Need to check this, otherwise 128..255 won't match */
9041c2e3 69 c = utf8n_to_uvchr(s, send - s, &ulen, 0);
9b877dbb 70 if (c < 0x100 && (ch = tbl[c]) >= 0) {
b250498f 71 matches++;
9041c2e3 72 d = uvchr_to_utf8(d, ch);
b250498f 73 s += ulen;
01ec43d0
GS
74 }
75 else { /* No match -> copy */
9b877dbb
IH
76 Copy(s, d, ulen, U8);
77 d += ulen;
78 s += ulen;
b250498f
GS
79 }
80 }
9b877dbb
IH
81 if (grows) {
82 sv_setpvn(sv, (char*)dstart, d - dstart);
83 Safefree(dstart);
84 }
85 else {
86 *d = '\0';
87 SvCUR_set(sv, d - dstart);
88 }
b250498f 89 SvUTF8_on(sv);
b250498f 90 SvSETMAGIC(sv);
4757a243
LW
91 return matches;
92}
93
942e002e 94STATIC I32
036b4402 95S_do_trans_count(pTHX_ SV *sv)/* SPC - OK */
4757a243 96{
4757a243
LW
97 U8 *s;
98 U8 *send;
99 I32 matches = 0;
100 STRLEN len;
101 short *tbl;
8973db79 102 I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
4757a243
LW
103
104 tbl = (short*)cPVOP->op_pv;
105 if (!tbl)
2269b42e 106 Perl_croak(aTHX_ "panic: do_trans_count");
4757a243
LW
107
108 s = (U8*)SvPV(sv, len);
109 send = s + len;
110
1aa99e6b
IH
111 if (!SvUTF8(sv))
112 while (s < send) {
113 if (tbl[*s++] >= 0)
036b4402 114 matches++;
1aa99e6b
IH
115 }
116 else
117 while (s < send) {
118 UV c;
119 STRLEN ulen;
9041c2e3 120 c = utf8n_to_uvchr(s, send - s, &ulen, 0);
8973db79
JH
121 if (c < 0x100) {
122 if (tbl[c] >= 0)
123 matches++;
124 } else if (complement)
1aa99e6b
IH
125 matches++;
126 s += ulen;
127 }
4757a243
LW
128
129 return matches;
130}
131
942e002e 132STATIC I32
b250498f 133S_do_trans_complex(pTHX_ SV *sv)/* SPC - NOT OK */
4757a243 134{
4757a243
LW
135 U8 *s;
136 U8 *send;
137 U8 *d;
5e44153e 138 U8 *dstart;
1aa99e6b 139 I32 isutf8;
4757a243 140 I32 matches = 0;
9b877dbb 141 I32 grows = PL_op->op_private & OPpTRANS_GROWS;
8973db79
JH
142 I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
143 I32 del = PL_op->op_private & OPpTRANS_DELETE;
144 STRLEN len, rlen;
4757a243
LW
145 short *tbl;
146 I32 ch;
147
148 tbl = (short*)cPVOP->op_pv;
149 if (!tbl)
2269b42e 150 Perl_croak(aTHX_ "panic: do_trans_complex");
4757a243
LW
151
152 s = (U8*)SvPV(sv, len);
1aa99e6b 153 isutf8 = SvUTF8(sv);
4757a243
LW
154 send = s + len;
155
1aa99e6b
IH
156 if (!isutf8) {
157 dstart = d = s;
158 if (PL_op->op_private & OPpTRANS_SQUASH) {
159 U8* p = send;
160 while (s < send) {
161 if ((ch = tbl[*s]) >= 0) {
036b4402
GS
162 *d = ch;
163 matches++;
1aa99e6b
IH
164 if (p != d - 1 || *p != *d)
165 p = d++;
166 }
167 else if (ch == -1) /* -1 is unmapped character */
168 *d++ = *s;
169 else if (ch == -2) /* -2 is delete character */
170 matches++;
171 s++;
172 }
a0ed51b3 173 }
1aa99e6b
IH
174 else {
175 while (s < send) {
176 if ((ch = tbl[*s]) >= 0) {
177 matches++;
178 *d++ = ch;
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 }
187 SvCUR_set(sv, d - dstart);
4757a243 188 }
1aa99e6b 189 else { /* isutf8 */
9b877dbb
IH
190 if (grows)
191 New(0, d, len*2+1, U8);
192 else
193 d = s;
1aa99e6b 194 dstart = d;
8973db79
JH
195 if (complement && !del)
196 rlen = tbl[0x100];
1aa99e6b 197
4aecb5b5
JH
198#ifdef MACOS_TRADITIONAL
199#define comp CoMP /* "comp" is a keyword in some compilers ... */
200#endif
201
1aa99e6b 202 if (PL_op->op_private & OPpTRANS_SQUASH) {
1aa99e6b
IH
203 UV pch = 0xfeedface;
204 while (s < send) {
205 STRLEN len;
9041c2e3 206 UV comp = utf8_to_uvchr(s, &len);
1aa99e6b 207
8973db79
JH
208 if (comp > 0xff) {
209 if (!complement) {
210 Copy(s, d, len, U8);
211 d += len;
212 }
213 else {
214 matches++;
215 if (!del) {
05d340b8
JH
216 ch = (rlen == 0) ? comp :
217 (comp - 0x100 < rlen) ?
8973db79
JH
218 tbl[comp+1] : tbl[0x100+rlen];
219 if (ch != pch) {
9041c2e3 220 d = uvchr_to_utf8(d, ch);
8973db79
JH
221 pch = ch;
222 }
223 s += len;
224 continue;
225 }
226 }
9b877dbb 227 }
1aa99e6b
IH
228 else if ((ch = tbl[comp]) >= 0) {
229 matches++;
230 if (ch != pch) {
9041c2e3 231 d = uvchr_to_utf8(d, ch);
1aa99e6b
IH
232 pch = ch;
233 }
234 s += len;
235 continue;
236 }
9b877dbb
IH
237 else if (ch == -1) { /* -1 is unmapped character */
238 Copy(s, d, len, U8);
239 d += len;
240 }
1aa99e6b
IH
241 else if (ch == -2) /* -2 is delete character */
242 matches++;
243 s += len;
244 pch = 0xfeedface;
245 }
5d06d08e 246 }
1aa99e6b
IH
247 else {
248 while (s < send) {
249 STRLEN len;
9041c2e3 250 UV comp = utf8_to_uvchr(s, &len);
8973db79
JH
251 if (comp > 0xff) {
252 if (!complement) {
253 Copy(s, d, len, U8);
254 d += len;
255 }
256 else {
257 matches++;
258 if (!del) {
9041c2e3
NIS
259 if (comp - 0x100 < rlen)
260 d = uvchr_to_utf8(d, tbl[comp+1]);
8973db79 261 else
9041c2e3 262 d = uvchr_to_utf8(d, tbl[0x100+rlen]);
8973db79
JH
263 }
264 }
9b877dbb 265 }
1aa99e6b 266 else if ((ch = tbl[comp]) >= 0) {
9041c2e3 267 d = uvchr_to_utf8(d, ch);
1aa99e6b
IH
268 matches++;
269 }
270 else if (ch == -1) { /* -1 is unmapped character */
9b877dbb
IH
271 Copy(s, d, len, U8);
272 d += len;
1aa99e6b
IH
273 }
274 else if (ch == -2) /* -2 is delete character */
275 matches++;
276 s += len;
277 }
278 }
9b877dbb
IH
279 if (grows) {
280 sv_setpvn(sv, (char*)dstart, d - dstart);
281 Safefree(dstart);
282 }
283 else {
284 *d = '\0';
285 SvCUR_set(sv, d - dstart);
286 }
1aa99e6b 287 SvUTF8_on(sv);
4757a243 288 }
5e44153e 289 SvSETMAGIC(sv);
4757a243
LW
290 return matches;
291}
292
942e002e 293STATIC I32
036b4402 294S_do_trans_simple_utf8(pTHX_ SV *sv)/* SPC - OK */
4757a243 295{
4757a243
LW
296 U8 *s;
297 U8 *send;
298 U8 *d;
036b4402 299 U8 *start;
1aa99e6b 300 U8 *dstart, *dend;
4757a243 301 I32 matches = 0;
9b877dbb 302 I32 grows = PL_op->op_private & OPpTRANS_GROWS;
4757a243
LW
303 STRLEN len;
304
305 SV* rv = (SV*)cSVOP->op_sv;
306 HV* hv = (HV*)SvRV(rv);
307 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
308 UV none = svp ? SvUV(*svp) : 0x7fffffff;
309 UV extra = none + 1;
310 UV final;
311 UV uv;
1aa99e6b
IH
312 I32 isutf8;
313 U8 hibit = 0;
4757a243
LW
314
315 s = (U8*)SvPV(sv, len);
1aa99e6b
IH
316 isutf8 = SvUTF8(sv);
317 if (!isutf8) {
318 U8 *t = s, *e = s + len;
319 while (t < e)
63cd0674 320 if ((hibit = !UTF8_IS_INVARIANT(*t++)))
1aa99e6b
IH
321 break;
322 if (hibit)
323 s = bytes_to_utf8(s, &len);
324 }
4757a243 325 send = s + len;
036b4402 326 start = s;
4757a243
LW
327
328 svp = hv_fetch(hv, "FINAL", 5, FALSE);
329 if (svp)
330 final = SvUV(*svp);
331
9b877dbb
IH
332 if (grows) {
333 /* d needs to be bigger than s, in case e.g. upgrading is required */
334 New(0, d, len*3+UTF8_MAXLEN, U8);
335 dend = d + len * 3;
336 dstart = d;
337 }
338 else {
339 dstart = d = s;
340 dend = d + len;
341 }
1aa99e6b 342
4757a243
LW
343 while (s < send) {
344 if ((uv = swash_fetch(rv, s)) < none) {
345 s += UTF8SKIP(s);
346 matches++;
9041c2e3 347 d = uvchr_to_utf8(d, uv);
4757a243
LW
348 }
349 else if (uv == none) {
1aa99e6b 350 int i = UTF8SKIP(s);
9b877dbb
IH
351 Copy(s, d, i, U8);
352 d += i;
353 s += i;
4757a243
LW
354 }
355 else if (uv == extra) {
1aa99e6b 356 int i = UTF8SKIP(s);
036b4402 357 s += i;
4757a243 358 matches++;
9041c2e3 359 d = uvchr_to_utf8(d, final);
4757a243
LW
360 }
361 else
362 s += UTF8SKIP(s);
1aa99e6b 363
9b877dbb 364 if (d > dend) {
1aa99e6b
IH
365 STRLEN clen = d - dstart;
366 STRLEN nlen = dend - dstart + len + UTF8_MAXLEN;
9b877dbb
IH
367 if (!grows)
368 Perl_croak(aTHX_ "panic: do_trans_complex_utf8");
1aa99e6b
IH
369 Renew(dstart, nlen+UTF8_MAXLEN, U8);
370 d = dstart + clen;
371 dend = dstart + nlen;
372 }
4757a243 373 }
16ec844d 374 if (grows || hibit) {
9b877dbb
IH
375 sv_setpvn(sv, (char*)dstart, d - dstart);
376 Safefree(dstart);
16ec844d
IH
377 if (grows && hibit)
378 Safefree(start);
9b877dbb
IH
379 }
380 else {
381 *d = '\0';
382 SvCUR_set(sv, d - dstart);
383 }
4757a243 384 SvSETMAGIC(sv);
1aa99e6b 385 SvUTF8_on(sv);
9041c2e3 386 /* Downgrading just 'cos it will is suspect - NI-S */
1aa99e6b
IH
387 if (!isutf8 && !(PL_hints & HINT_UTF8))
388 sv_utf8_downgrade(sv, TRUE);
4757a243
LW
389
390 return matches;
391}
392
942e002e 393STATIC I32
036b4402 394S_do_trans_count_utf8(pTHX_ SV *sv)/* SPC - OK */
4757a243 395{
4757a243 396 U8 *s;
1aa99e6b 397 U8 *start, *send;
4757a243
LW
398 I32 matches = 0;
399 STRLEN len;
400
401 SV* rv = (SV*)cSVOP->op_sv;
402 HV* hv = (HV*)SvRV(rv);
403 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
404 UV none = svp ? SvUV(*svp) : 0x7fffffff;
405 UV uv;
1aa99e6b 406 U8 hibit = 0;
4757a243
LW
407
408 s = (U8*)SvPV(sv, len);
1aa99e6b
IH
409 if (!SvUTF8(sv)) {
410 U8 *t = s, *e = s + len;
411 while (t < e)
d742c382 412 if ((hibit = !UTF8_IS_INVARIANT(*t++)))
1aa99e6b
IH
413 break;
414 if (hibit)
415 start = s = bytes_to_utf8(s, &len);
416 }
4757a243
LW
417 send = s + len;
418
419 while (s < send) {
834a4ddd 420 if ((uv = swash_fetch(rv, s)) < none)
4757a243 421 matches++;
834a4ddd 422 s += UTF8SKIP(s);
4757a243 423 }
1aa99e6b
IH
424 if (hibit)
425 Safefree(start);
4757a243
LW
426
427 return matches;
428}
429
942e002e 430STATIC I32
036b4402 431S_do_trans_complex_utf8(pTHX_ SV *sv) /* SPC - NOT OK */
4757a243 432{
4757a243 433 U8 *s;
1aa99e6b 434 U8 *start, *send;
4757a243
LW
435 U8 *d;
436 I32 matches = 0;
437 I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
4757a243 438 I32 del = PL_op->op_private & OPpTRANS_DELETE;
9b877dbb 439 I32 grows = PL_op->op_private & OPpTRANS_GROWS;
4757a243
LW
440 SV* rv = (SV*)cSVOP->op_sv;
441 HV* hv = (HV*)SvRV(rv);
442 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
443 UV none = svp ? SvUV(*svp) : 0x7fffffff;
444 UV extra = none + 1;
445 UV final;
446 UV uv;
447 STRLEN len;
1aa99e6b
IH
448 U8 *dstart, *dend;
449 I32 isutf8;
450 U8 hibit = 0;
4757a243
LW
451
452 s = (U8*)SvPV(sv, len);
1aa99e6b
IH
453 isutf8 = SvUTF8(sv);
454 if (!isutf8) {
455 U8 *t = s, *e = s + len;
456 while (t < e)
d742c382 457 if ((hibit = !UTF8_IS_INVARIANT(*t++)))
1aa99e6b
IH
458 break;
459 if (hibit)
460 s = bytes_to_utf8(s, &len);
461 }
4757a243 462 send = s + len;
1aa99e6b 463 start = s;
4757a243
LW
464
465 svp = hv_fetch(hv, "FINAL", 5, FALSE);
466 if (svp)
467 final = SvUV(*svp);
468
9b877dbb
IH
469 if (grows) {
470 /* d needs to be bigger than s, in case e.g. upgrading is required */
471 New(0, d, len*3+UTF8_MAXLEN, U8);
472 dend = d + len * 3;
473 dstart = d;
474 }
475 else {
476 dstart = d = s;
477 dend = d + len;
478 }
4757a243
LW
479
480 if (squash) {
481 UV puv = 0xfeedface;
482 while (s < send) {
1aa99e6b 483 uv = swash_fetch(rv, s);
9041c2e3 484
9b877dbb
IH
485 if (d > dend) {
486 STRLEN clen = d - dstart;
487 STRLEN nlen = dend - dstart + len + UTF8_MAXLEN;
488 if (!grows)
489 Perl_croak(aTHX_ "panic: do_trans_complex_utf8");
1aa99e6b
IH
490 Renew(dstart, nlen+UTF8_MAXLEN, U8);
491 d = dstart + clen;
492 dend = dstart + nlen;
4757a243
LW
493 }
494 if (uv < none) {
495 matches++;
94472101 496 s += UTF8SKIP(s);
4757a243 497 if (uv != puv) {
9041c2e3 498 d = uvchr_to_utf8(d, uv);
4757a243
LW
499 puv = uv;
500 }
4757a243
LW
501 continue;
502 }
503 else if (uv == none) { /* "none" is unmapped character */
1aa99e6b 504 int i = UTF8SKIP(s);
9b877dbb
IH
505 Copy(s, d, i, U8);
506 d += i;
507 s += i;
4757a243
LW
508 puv = 0xfeedface;
509 continue;
a0ed51b3 510 }
4757a243
LW
511 else if (uv == extra && !del) {
512 matches++;
94472101 513 s += UTF8SKIP(s);
4757a243 514 if (uv != puv) {
9041c2e3 515 d = uvchr_to_utf8(d, final);
4757a243
LW
516 puv = final;
517 }
4757a243
LW
518 continue;
519 }
01ec43d0
GS
520 matches++; /* "none+1" is delete character */
521 s += UTF8SKIP(s);
a0ed51b3 522 }
79072805
LW
523 }
524 else {
4757a243 525 while (s < send) {
1aa99e6b 526 uv = swash_fetch(rv, s);
9b877dbb
IH
527 if (d > dend) {
528 STRLEN clen = d - dstart;
529 STRLEN nlen = dend - dstart + len + UTF8_MAXLEN;
530 if (!grows)
531 Perl_croak(aTHX_ "panic: do_trans_complex_utf8");
1aa99e6b
IH
532 Renew(dstart, nlen+UTF8_MAXLEN, U8);
533 d = dstart + clen;
534 dend = dstart + nlen;
a0ed51b3 535 }
4757a243
LW
536 if (uv < none) {
537 matches++;
01ec43d0 538 s += UTF8SKIP(s);
9041c2e3 539 d = uvchr_to_utf8(d, uv);
4757a243 540 continue;
a0ed51b3 541 }
4757a243 542 else if (uv == none) { /* "none" is unmapped character */
1aa99e6b 543 int i = UTF8SKIP(s);
9b877dbb
IH
544 Copy(s, d, i, U8);
545 d += i;
546 s += i;
4757a243 547 continue;
79072805 548 }
4757a243
LW
549 else if (uv == extra && !del) {
550 matches++;
01ec43d0 551 s += UTF8SKIP(s);
9041c2e3 552 d = uvchr_to_utf8(d, final);
4757a243
LW
553 continue;
554 }
01ec43d0
GS
555 matches++; /* "none+1" is delete character */
556 s += UTF8SKIP(s);
79072805 557 }
4757a243 558 }
16ec844d 559 if (grows || hibit) {
9b877dbb
IH
560 sv_setpvn(sv, (char*)dstart, d - dstart);
561 Safefree(dstart);
16ec844d
IH
562 if (grows && hibit)
563 Safefree(start);
9b877dbb
IH
564 }
565 else {
566 *d = '\0';
567 SvCUR_set(sv, d - dstart);
568 }
1aa99e6b 569 SvUTF8_on(sv);
1aa99e6b
IH
570 if (!isutf8 && !(PL_hints & HINT_UTF8))
571 sv_utf8_downgrade(sv, TRUE);
4757a243
LW
572 SvSETMAGIC(sv);
573
574 return matches;
575}
576
577I32
864dbfa3 578Perl_do_trans(pTHX_ SV *sv)
4757a243
LW
579{
580 STRLEN len;
8e84507e 581 I32 hasutf = (PL_op->op_private &
036b4402 582 (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF));
4757a243
LW
583
584 if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL))
cea2e8a9 585 Perl_croak(aTHX_ PL_no_modify);
4757a243
LW
586
587 (void)SvPV(sv, len);
588 if (!len)
589 return 0;
590 if (!SvPOKp(sv))
591 (void)SvPV_force(sv, len);
2de7b02f
GS
592 if (!(PL_op->op_private & OPpTRANS_IDENTICAL))
593 (void)SvPOK_only_UTF8(sv);
4757a243 594
cea2e8a9 595 DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
4757a243 596
036b4402 597 switch (PL_op->op_private & ~hasutf & 63) {
4757a243 598 case 0:
01ec43d0
GS
599 if (hasutf)
600 return do_trans_simple_utf8(sv);
601 else
602 return do_trans_simple(sv);
4757a243
LW
603
604 case OPpTRANS_IDENTICAL:
05d340b8 605 case OPpTRANS_IDENTICAL|OPpTRANS_COMPLEMENT:
01ec43d0
GS
606 if (hasutf)
607 return do_trans_count_utf8(sv);
608 else
609 return do_trans_count(sv);
4757a243
LW
610
611 default:
01ec43d0 612 if (hasutf)
036b4402 613 return do_trans_complex_utf8(sv);
4757a243 614 else
036b4402 615 return do_trans_complex(sv);
79072805 616 }
79072805
LW
617}
618
619void
864dbfa3 620Perl_do_join(pTHX_ register SV *sv, SV *del, register SV **mark, register SV **sp)
79072805
LW
621{
622 SV **oldmark = mark;
623 register I32 items = sp - mark;
79072805 624 register STRLEN len;
463ee0b2
LW
625 STRLEN delimlen;
626 register char *delim = SvPV(del, delimlen);
627 STRLEN tmplen;
79072805
LW
628
629 mark++;
630 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
07f14f54 631 (void)SvUPGRADE(sv, SVt_PV);
79072805
LW
632 if (SvLEN(sv) < len + items) { /* current length is way too short */
633 while (items-- > 0) {
1426bbf4 634 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
463ee0b2
LW
635 SvPV(*mark, tmplen);
636 len += tmplen;
79072805
LW
637 }
638 mark++;
639 }
640 SvGROW(sv, len + 1); /* so try to pre-extend */
641
642 mark = oldmark;
db7c17d7 643 items = sp - mark;
79072805
LW
644 ++mark;
645 }
646
463ee0b2 647 if (items-- > 0) {
92d29cee
JH
648 sv_setpv(sv, "");
649 if (*mark)
650 sv_catsv(sv, *mark);
463ee0b2
LW
651 mark++;
652 }
79072805
LW
653 else
654 sv_setpv(sv,"");
c512ce4f 655 if (delimlen) {
79072805 656 for (; items > 0; items--,mark++) {
c512ce4f 657 sv_catsv(sv,del);
79072805
LW
658 sv_catsv(sv,*mark);
659 }
660 }
661 else {
662 for (; items > 0; items--,mark++)
663 sv_catsv(sv,*mark);
664 }
665 SvSETMAGIC(sv);
666}
667
668void
864dbfa3 669Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg)
79072805 670{
46fc3d4c
PP
671 STRLEN patlen;
672 char *pat = SvPV(*sarg, patlen);
673 bool do_taint = FALSE;
674
675 sv_vsetpvfn(sv, pat, patlen, Null(va_list*), sarg + 1, len - 1, &do_taint);
79072805 676 SvSETMAGIC(sv);
46fc3d4c
PP
677 if (do_taint)
678 SvTAINTED_on(sv);
79072805
LW
679}
680
33b45480 681/* currently converts input to bytes if possible, but doesn't sweat failure */
81e118e0
JH
682UV
683Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size)
684{
685 STRLEN srclen, len;
686 unsigned char *s = (unsigned char *) SvPV(sv, srclen);
687 UV retnum = 0;
688
a50d7633 689 if (offset < 0)
81e118e0 690 return retnum;
8e84507e 691 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
a50d7633 692 Perl_croak(aTHX_ "Illegal number of bits in vec");
246fae53 693
dcad2880 694 if (SvUTF8(sv))
33b45480 695 (void) Perl_sv_utf8_downgrade(aTHX_ sv, TRUE);
246fae53 696
81e118e0
JH
697 offset *= size; /* turn into bit offset */
698 len = (offset + size + 7) / 8; /* required number of bytes */
699 if (len > srclen) {
700 if (size <= 8)
701 retnum = 0;
702 else {
703 offset >>= 3; /* turn into byte offset */
704 if (size == 16) {
705 if (offset >= srclen)
706 retnum = 0;
707 else
628e1a40 708 retnum = (UV) s[offset] << 8;
81e118e0
JH
709 }
710 else if (size == 32) {
711 if (offset >= srclen)
712 retnum = 0;
713 else if (offset + 1 >= srclen)
714 retnum =
715 ((UV) s[offset ] << 24);
716 else if (offset + 2 >= srclen)
717 retnum =
718 ((UV) s[offset ] << 24) +
719 ((UV) s[offset + 1] << 16);
720 else
721 retnum =
722 ((UV) s[offset ] << 24) +
723 ((UV) s[offset + 1] << 16) +
724 ( s[offset + 2] << 8);
725 }
d7d93a81 726#ifdef UV_IS_QUAD
c5a0f51a 727 else if (size == 64) {
c5a0f51a
JH
728 if (ckWARN(WARN_PORTABLE))
729 Perl_warner(aTHX_ WARN_PORTABLE,
730 "Bit vector size > 32 non-portable");
731 if (offset >= srclen)
732 retnum = 0;
733 else if (offset + 1 >= srclen)
734 retnum =
735 (UV) s[offset ] << 56;
736 else if (offset + 2 >= srclen)
737 retnum =
738 ((UV) s[offset ] << 56) +
739 ((UV) s[offset + 1] << 48);
740 else if (offset + 3 >= srclen)
741 retnum =
742 ((UV) s[offset ] << 56) +
743 ((UV) s[offset + 1] << 48) +
744 ((UV) s[offset + 2] << 40);
745 else if (offset + 4 >= srclen)
746 retnum =
747 ((UV) s[offset ] << 56) +
748 ((UV) s[offset + 1] << 48) +
749 ((UV) s[offset + 2] << 40) +
750 ((UV) s[offset + 3] << 32);
751 else if (offset + 5 >= srclen)
752 retnum =
753 ((UV) s[offset ] << 56) +
754 ((UV) s[offset + 1] << 48) +
755 ((UV) s[offset + 2] << 40) +
756 ((UV) s[offset + 3] << 32) +
757 ( s[offset + 4] << 24);
758 else if (offset + 6 >= srclen)
759 retnum =
760 ((UV) s[offset ] << 56) +
761 ((UV) s[offset + 1] << 48) +
762 ((UV) s[offset + 2] << 40) +
763 ((UV) s[offset + 3] << 32) +
764 ((UV) s[offset + 4] << 24) +
765 ((UV) s[offset + 5] << 16);
766 else
8e84507e 767 retnum =
c5a0f51a
JH
768 ((UV) s[offset ] << 56) +
769 ((UV) s[offset + 1] << 48) +
770 ((UV) s[offset + 2] << 40) +
771 ((UV) s[offset + 3] << 32) +
772 ((UV) s[offset + 4] << 24) +
773 ((UV) s[offset + 5] << 16) +
628e1a40 774 ( s[offset + 6] << 8);
c5a0f51a
JH
775 }
776#endif
81e118e0
JH
777 }
778 }
779 else if (size < 8)
780 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
781 else {
782 offset >>= 3; /* turn into byte offset */
783 if (size == 8)
784 retnum = s[offset];
785 else if (size == 16)
786 retnum =
628e1a40 787 ((UV) s[offset] << 8) +
81e118e0
JH
788 s[offset + 1];
789 else if (size == 32)
790 retnum =
791 ((UV) s[offset ] << 24) +
792 ((UV) s[offset + 1] << 16) +
793 ( s[offset + 2] << 8) +
794 s[offset + 3];
d7d93a81 795#ifdef UV_IS_QUAD
c5a0f51a 796 else if (size == 64) {
c5a0f51a
JH
797 if (ckWARN(WARN_PORTABLE))
798 Perl_warner(aTHX_ WARN_PORTABLE,
799 "Bit vector size > 32 non-portable");
800 retnum =
801 ((UV) s[offset ] << 56) +
802 ((UV) s[offset + 1] << 48) +
803 ((UV) s[offset + 2] << 40) +
804 ((UV) s[offset + 3] << 32) +
805 ((UV) s[offset + 4] << 24) +
806 ((UV) s[offset + 5] << 16) +
628e1a40 807 ( s[offset + 6] << 8) +
c5a0f51a
JH
808 s[offset + 7];
809 }
810#endif
81e118e0
JH
811 }
812
813 return retnum;
814}
815
33b45480
SB
816/* currently converts input to bytes if possible but doesn't sweat failures,
817 * although it does ensure that the string it clobbers is not marked as
818 * utf8-valid any more
819 */
79072805 820void
864dbfa3 821Perl_do_vecset(pTHX_ SV *sv)
79072805
LW
822{
823 SV *targ = LvTARG(sv);
824 register I32 offset;
825 register I32 size;
8990e307 826 register unsigned char *s;
81e118e0 827 register UV lval;
79072805 828 I32 mask;
a0d0e21e
LW
829 STRLEN targlen;
830 STRLEN len;
79072805 831
8990e307
LW
832 if (!targ)
833 return;
a0d0e21e 834 s = (unsigned char*)SvPV_force(targ, targlen);
246fae53 835 if (SvUTF8(targ)) {
33b45480
SB
836 /* This is handled by the SvPOK_only below...
837 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
838 SvUTF8_off(targ);
839 */
840 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
246fae53
MG
841 }
842
4ebbc975 843 (void)SvPOK_only(targ);
81e118e0 844 lval = SvUV(sv);
79072805 845 offset = LvTARGOFF(sv);
fe58ced6
MG
846 if (offset < 0)
847 Perl_croak(aTHX_ "Assigning to negative offset in vec");
79072805 848 size = LvTARGLEN(sv);
8e84507e 849 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
a50d7633 850 Perl_croak(aTHX_ "Illegal number of bits in vec");
8e84507e 851
81e118e0
JH
852 offset *= size; /* turn into bit offset */
853 len = (offset + size + 7) / 8; /* required number of bytes */
a0d0e21e
LW
854 if (len > targlen) {
855 s = (unsigned char*)SvGROW(targ, len + 1);
12ae5dfc 856 (void)memzero((char *)(s + targlen), len - targlen + 1);
a0d0e21e
LW
857 SvCUR_set(targ, len);
858 }
8e84507e 859
79072805
LW
860 if (size < 8) {
861 mask = (1 << size) - 1;
862 size = offset & 7;
863 lval &= mask;
81e118e0 864 offset >>= 3; /* turn into byte offset */
79072805
LW
865 s[offset] &= ~(mask << size);
866 s[offset] |= lval << size;
867 }
868 else {
81e118e0 869 offset >>= 3; /* turn into byte offset */
79072805 870 if (size == 8)
c5a0f51a 871 s[offset ] = lval & 0xff;
79072805 872 else if (size == 16) {
c5a0f51a
JH
873 s[offset ] = (lval >> 8) & 0xff;
874 s[offset+1] = lval & 0xff;
79072805
LW
875 }
876 else if (size == 32) {
c5a0f51a
JH
877 s[offset ] = (lval >> 24) & 0xff;
878 s[offset+1] = (lval >> 16) & 0xff;
879 s[offset+2] = (lval >> 8) & 0xff;
880 s[offset+3] = lval & 0xff;
881 }
d7d93a81 882#ifdef UV_IS_QUAD
c5a0f51a 883 else if (size == 64) {
c5a0f51a
JH
884 if (ckWARN(WARN_PORTABLE))
885 Perl_warner(aTHX_ WARN_PORTABLE,
886 "Bit vector size > 32 non-portable");
887 s[offset ] = (lval >> 56) & 0xff;
888 s[offset+1] = (lval >> 48) & 0xff;
889 s[offset+2] = (lval >> 40) & 0xff;
890 s[offset+3] = (lval >> 32) & 0xff;
891 s[offset+4] = (lval >> 24) & 0xff;
892 s[offset+5] = (lval >> 16) & 0xff;
893 s[offset+6] = (lval >> 8) & 0xff;
894 s[offset+7] = lval & 0xff;
79072805 895 }
dc1e3f56 896#endif
79072805 897 }
7bb043c3 898 SvSETMAGIC(targ);
79072805
LW
899}
900
901void
864dbfa3 902Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
79072805 903{
463ee0b2 904 STRLEN len;
a0d0e21e 905 char *s;
8e84507e 906
79072805 907 if (SvTYPE(sv) == SVt_PVAV) {
a0d0e21e
LW
908 register I32 i;
909 I32 max;
910 AV* av = (AV*)sv;
911 max = AvFILL(av);
912 for (i = 0; i <= max; i++) {
913 sv = (SV*)av_fetch(av, i, FALSE);
3280af22 914 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
a0d0e21e
LW
915 do_chop(astr, sv);
916 }
917 return;
79072805 918 }
aa854799 919 else if (SvTYPE(sv) == SVt_PVHV) {
a0d0e21e
LW
920 HV* hv = (HV*)sv;
921 HE* entry;
922 (void)hv_iterinit(hv);
923 /*SUPPRESS 560*/
155aba94 924 while ((entry = hv_iternext(hv)))
a0d0e21e
LW
925 do_chop(astr,hv_iterval(hv,entry));
926 return;
79072805 927 }
aa854799 928 else if (SvREADONLY(sv))
cea2e8a9 929 Perl_croak(aTHX_ PL_no_modify);
a0d0e21e 930 s = SvPV(sv, len);
748a9306 931 if (len && !SvPOK(sv))
a0d0e21e 932 s = SvPV_force(sv, len);
7e2040f0 933 if (DO_UTF8(sv)) {
a0ed51b3
LW
934 if (s && len) {
935 char *send = s + len;
936 char *start = s;
937 s = send - 1;
a0dbb045
JH
938 while (s > start && UTF8_IS_CONTINUATION(*s))
939 s--;
9041c2e3 940 if (utf8_to_uvchr((U8*)s, 0)) {
a0dbb045
JH
941 sv_setpvn(astr, s, send - s);
942 *s = '\0';
943 SvCUR_set(sv, s - start);
944 SvNIOK_off(sv);
945 SvUTF8_on(astr);
946 }
a0ed51b3
LW
947 }
948 else
949 sv_setpvn(astr, "", 0);
950 }
7e2040f0 951 else if (s && len) {
a0d0e21e
LW
952 s += --len;
953 sv_setpvn(astr, s, 1);
954 *s = '\0';
955 SvCUR_set(sv, len);
2c19a612 956 SvUTF8_off(sv);
a0d0e21e 957 SvNIOK_off(sv);
79072805
LW
958 }
959 else
a0d0e21e
LW
960 sv_setpvn(astr, "", 0);
961 SvSETMAGIC(sv);
7e2040f0 962}
a0d0e21e
LW
963
964I32
864dbfa3 965Perl_do_chomp(pTHX_ register SV *sv)
a0d0e21e 966{
c07a80fd 967 register I32 count;
a0d0e21e
LW
968 STRLEN len;
969 char *s;
c07a80fd 970
3280af22 971 if (RsSNARF(PL_rs))
c07a80fd 972 return 0;
4c5a6083
GS
973 if (RsRECORD(PL_rs))
974 return 0;
c07a80fd 975 count = 0;
a0d0e21e
LW
976 if (SvTYPE(sv) == SVt_PVAV) {
977 register I32 i;
978 I32 max;
979 AV* av = (AV*)sv;
980 max = AvFILL(av);
981 for (i = 0; i <= max; i++) {
982 sv = (SV*)av_fetch(av, i, FALSE);
3280af22 983 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
a0d0e21e
LW
984 count += do_chomp(sv);
985 }
986 return count;
987 }
aa854799 988 else if (SvTYPE(sv) == SVt_PVHV) {
a0d0e21e
LW
989 HV* hv = (HV*)sv;
990 HE* entry;
991 (void)hv_iterinit(hv);
992 /*SUPPRESS 560*/
155aba94 993 while ((entry = hv_iternext(hv)))
a0d0e21e
LW
994 count += do_chomp(hv_iterval(hv,entry));
995 return count;
996 }
aa854799 997 else if (SvREADONLY(sv))
cea2e8a9 998 Perl_croak(aTHX_ PL_no_modify);
a0d0e21e
LW
999 s = SvPV(sv, len);
1000 if (len && !SvPOKp(sv))
1001 s = SvPV_force(sv, len);
1002 if (s && len) {
1003 s += --len;
3280af22 1004 if (RsPARA(PL_rs)) {
a0d0e21e
LW
1005 if (*s != '\n')
1006 goto nope;
1007 ++count;
1008 while (len && s[-1] == '\n') {
1009 --len;
1010 --s;
1011 ++count;
1012 }
1013 }
a0d0e21e 1014 else {
c07a80fd 1015 STRLEN rslen;
3280af22 1016 char *rsptr = SvPV(PL_rs, rslen);
c07a80fd
PP
1017 if (rslen == 1) {
1018 if (*s != *rsptr)
1019 goto nope;
1020 ++count;
1021 }
1022 else {
8c2cee6f 1023 if (len < rslen - 1)
c07a80fd
PP
1024 goto nope;
1025 len -= rslen - 1;
1026 s -= rslen - 1;
36477c24 1027 if (memNE(s, rsptr, rslen))
c07a80fd
PP
1028 goto nope;
1029 count += rslen;
1030 }
a0d0e21e 1031 }
a0d0e21e
LW
1032 *s = '\0';
1033 SvCUR_set(sv, len);
1034 SvNIOK_off(sv);
1035 }
1036 nope:
1037 SvSETMAGIC(sv);
1038 return count;
8e84507e 1039}
79072805
LW
1040
1041void
864dbfa3 1042Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
79072805
LW
1043{
1044#ifdef LIBERAL
1045 register long *dl;
1046 register long *ll;
1047 register long *rl;
1048#endif
1049 register char *dc;
463ee0b2
LW
1050 STRLEN leftlen;
1051 STRLEN rightlen;
7a4c00b4
PP
1052 register char *lc;
1053 register char *rc;
79072805 1054 register I32 len;
a0d0e21e 1055 I32 lensave;
7a4c00b4
PP
1056 char *lsave;
1057 char *rsave;
0c57e439
GS
1058 bool left_utf = DO_UTF8(left);
1059 bool right_utf = DO_UTF8(right);
c9b3c8d0 1060 I32 needlen;
0c57e439
GS
1061
1062 if (left_utf && !right_utf)
1063 sv_utf8_upgrade(right);
a1ca4561 1064 else if (!left_utf && right_utf)
0c57e439 1065 sv_utf8_upgrade(left);
79072805 1066
1fbd88dc
CS
1067 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
1068 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
7a4c00b4
PP
1069 lsave = lc = SvPV(left, leftlen);
1070 rsave = rc = SvPV(right, rightlen);
93a17b20 1071 len = leftlen < rightlen ? leftlen : rightlen;
a0d0e21e 1072 lensave = len;
c9b3c8d0
JH
1073 if ((left_utf || right_utf) && (sv == left || sv == right)) {
1074 needlen = optype == OP_BIT_AND ? len : leftlen + rightlen;
1075 Newz(801, dc, needlen + 1, char);
1076 }
1077 else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
2d8e6c8d
GS
1078 STRLEN n_a;
1079 dc = SvPV_force(sv, n_a);
ff68c719
PP
1080 if (SvCUR(sv) < len) {
1081 dc = SvGROW(sv, len + 1);
1082 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1083 }
c9b3c8d0
JH
1084 if (optype != OP_BIT_AND && (left_utf || right_utf))
1085 dc = SvGROW(sv, leftlen + rightlen + 1);
ff68c719
PP
1086 }
1087 else {
c9b3c8d0
JH
1088 needlen = ((optype == OP_BIT_AND)
1089 ? len : (leftlen > rightlen ? leftlen : rightlen));
ff68c719
PP
1090 Newz(801, dc, needlen + 1, char);
1091 (void)sv_usepvn(sv, dc, needlen);
1092 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
79072805 1093 }
a0d0e21e
LW
1094 SvCUR_set(sv, len);
1095 (void)SvPOK_only(sv);
0c57e439
GS
1096 if (left_utf || right_utf) {
1097 UV duc, luc, ruc;
c9b3c8d0 1098 char *dcsave = dc;
0c57e439
GS
1099 STRLEN lulen = leftlen;
1100 STRLEN rulen = rightlen;
ba210ebe 1101 STRLEN ulen;
0c57e439
GS
1102
1103 switch (optype) {
1104 case OP_BIT_AND:
1105 while (lulen && rulen) {
9041c2e3 1106 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439
GS
1107 lc += ulen;
1108 lulen -= ulen;
9041c2e3 1109 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439
GS
1110 rc += ulen;
1111 rulen -= ulen;
1112 duc = luc & ruc;
9041c2e3 1113 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
0c57e439 1114 }
c9b3c8d0
JH
1115 if (sv == left || sv == right)
1116 (void)sv_usepvn(sv, dcsave, needlen);
1117 SvCUR_set(sv, dc - dcsave);
0c57e439
GS
1118 break;
1119 case OP_BIT_XOR:
1120 while (lulen && rulen) {
9041c2e3 1121 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439
GS
1122 lc += ulen;
1123 lulen -= ulen;
9041c2e3 1124 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439
GS
1125 rc += ulen;
1126 rulen -= ulen;
1127 duc = luc ^ ruc;
9041c2e3 1128 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
0c57e439
GS
1129 }
1130 goto mop_up_utf;
1131 case OP_BIT_OR:
1132 while (lulen && rulen) {
9041c2e3 1133 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439
GS
1134 lc += ulen;
1135 lulen -= ulen;
9041c2e3 1136 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439
GS
1137 rc += ulen;
1138 rulen -= ulen;
1139 duc = luc | ruc;
9041c2e3 1140 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
0c57e439
GS
1141 }
1142 mop_up_utf:
c9b3c8d0
JH
1143 if (sv == left || sv == right)
1144 (void)sv_usepvn(sv, dcsave, needlen);
1145 SvCUR_set(sv, dc - dcsave);
0c57e439
GS
1146 if (rulen)
1147 sv_catpvn(sv, rc, rulen);
1148 else if (lulen)
1149 sv_catpvn(sv, lc, lulen);
1150 else
1151 *SvEND(sv) = '\0';
1152 break;
1153 }
1154 SvUTF8_on(sv);
1155 goto finish;
1156 }
1157 else
79072805
LW
1158#ifdef LIBERAL
1159 if (len >= sizeof(long)*4 &&
1160 !((long)dc % sizeof(long)) &&
1161 !((long)lc % sizeof(long)) &&
1162 !((long)rc % sizeof(long))) /* It's almost always aligned... */
1163 {
1164 I32 remainder = len % (sizeof(long)*4);
1165 len /= (sizeof(long)*4);
1166
1167 dl = (long*)dc;
1168 ll = (long*)lc;
1169 rl = (long*)rc;
1170
1171 switch (optype) {
1172 case OP_BIT_AND:
1173 while (len--) {
1174 *dl++ = *ll++ & *rl++;
1175 *dl++ = *ll++ & *rl++;
1176 *dl++ = *ll++ & *rl++;
1177 *dl++ = *ll++ & *rl++;
1178 }
1179 break;
a0d0e21e 1180 case OP_BIT_XOR:
79072805
LW
1181 while (len--) {
1182 *dl++ = *ll++ ^ *rl++;
1183 *dl++ = *ll++ ^ *rl++;
1184 *dl++ = *ll++ ^ *rl++;
1185 *dl++ = *ll++ ^ *rl++;
1186 }
1187 break;
1188 case OP_BIT_OR:
1189 while (len--) {
1190 *dl++ = *ll++ | *rl++;
1191 *dl++ = *ll++ | *rl++;
1192 *dl++ = *ll++ | *rl++;
1193 *dl++ = *ll++ | *rl++;
1194 }
1195 }
1196
1197 dc = (char*)dl;
1198 lc = (char*)ll;
1199 rc = (char*)rl;
1200
1201 len = remainder;
1202 }
1203#endif
a0d0e21e 1204 {
a0d0e21e
LW
1205 switch (optype) {
1206 case OP_BIT_AND:
1207 while (len--)
1208 *dc++ = *lc++ & *rc++;
1209 break;
1210 case OP_BIT_XOR:
1211 while (len--)
1212 *dc++ = *lc++ ^ *rc++;
1213 goto mop_up;
1214 case OP_BIT_OR:
1215 while (len--)
1216 *dc++ = *lc++ | *rc++;
1217 mop_up:
1218 len = lensave;
1219 if (rightlen > len)
1220 sv_catpvn(sv, rsave + len, rightlen - len);
1221 else if (leftlen > len)
1222 sv_catpvn(sv, lsave + len, leftlen - len);
4633a7c4
LW
1223 else
1224 *SvEND(sv) = '\0';
a0d0e21e
LW
1225 break;
1226 }
79072805 1227 }
0c57e439 1228finish:
fb73857a 1229 SvTAINT(sv);
79072805 1230}
463ee0b2
LW
1231
1232OP *
cea2e8a9 1233Perl_do_kv(pTHX)
463ee0b2 1234{
39644a26 1235 dSP;
463ee0b2 1236 HV *hv = (HV*)POPs;
800e9ae0 1237 HV *keys;
463ee0b2 1238 register HE *entry;
463ee0b2 1239 SV *tmpstr;
54310121 1240 I32 gimme = GIMME_V;
533c011a
NIS
1241 I32 dokeys = (PL_op->op_type == OP_KEYS);
1242 I32 dovalues = (PL_op->op_type == OP_VALUES);
c750a3ec 1243 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
8e84507e
NIS
1244
1245 if (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV)
a0d0e21e 1246 dokeys = dovalues = TRUE;
463ee0b2 1247
85581909 1248 if (!hv) {
78f9721b 1249 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
85581909
SB
1250 dTARGET; /* make sure to clear its target here */
1251 if (SvTYPE(TARG) == SVt_PVLV)
1252 LvTARG(TARG) = Nullsv;
1253 PUSHs(TARG);
1254 }
463ee0b2 1255 RETURN;
85581909 1256 }
748a9306 1257
800e9ae0
JP
1258 keys = realhv ? hv : avhv_keys((AV*)hv);
1259 (void)hv_iterinit(keys); /* always reset iterator regardless */
748a9306 1260
54310121 1261 if (gimme == G_VOID)
aa689395
PP
1262 RETURN;
1263
54310121 1264 if (gimme == G_SCALAR) {
6ee623d5 1265 IV i;
463ee0b2
LW
1266 dTARGET;
1267
78f9721b 1268 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
85581909
SB
1269 if (SvTYPE(TARG) < SVt_PVLV) {
1270 sv_upgrade(TARG, SVt_PVLV);
1271 sv_magic(TARG, Nullsv, 'k', Nullch, 0);
1272 }
1273 LvTYPE(TARG) = 'k';
800e9ae0 1274 if (LvTARG(TARG) != (SV*)keys) {
6ff81951
GS
1275 if (LvTARG(TARG))
1276 SvREFCNT_dec(LvTARG(TARG));
800e9ae0 1277 LvTARG(TARG) = SvREFCNT_inc(keys);
6ff81951 1278 }
85581909
SB
1279 PUSHs(TARG);
1280 RETURN;
1281 }
1282
33c27489 1283 if (! SvTIED_mg((SV*)keys, 'P'))
800e9ae0 1284 i = HvKEYS(keys);
463ee0b2
LW
1285 else {
1286 i = 0;
463ee0b2 1287 /*SUPPRESS 560*/
800e9ae0 1288 while (hv_iternext(keys)) i++;
463ee0b2
LW
1289 }
1290 PUSHi( i );
1291 RETURN;
1292 }
1293
8ed4b672 1294 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
463ee0b2 1295
463ee0b2 1296 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
155aba94 1297 while ((entry = hv_iternext(keys))) {
463ee0b2 1298 SPAGAIN;
6bfc225d 1299 if (dokeys)
8c2cee6f 1300 XPUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
463ee0b2 1301 if (dovalues) {
463ee0b2 1302 PUTBACK;
b6429b1b
GS
1303 tmpstr = realhv ?
1304 hv_iterval(hv,entry) : avhv_iterval((AV*)hv,entry);
cea2e8a9 1305 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
46fc3d4c 1306 (unsigned long)HeHASH(entry),
800e9ae0
JP
1307 HvMAX(keys)+1,
1308 (unsigned long)(HeHASH(entry) & HvMAX(keys))));
463ee0b2 1309 SPAGAIN;
46fc3d4c 1310 XPUSHs(tmpstr);
463ee0b2
LW
1311 }
1312 PUTBACK;
1313 }
1314 return NORMAL;
1315}
4e35701f 1316