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