more complete support for implicit thread/interpreter pointer,
[p5sagit/p5-mst-13.2.git] / doop.c
CommitLineData
a0d0e21e 1/* doop.c
79072805 2 *
4eb8286e 3 * Copyright (c) 1991-1999, Larry Wall
79072805 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 8 */
9
10/*
11 * "'So that was the job I felt I had to do when I started,' thought Sam."
79072805 12 */
13
14#include "EXTERN.h"
864dbfa3 15#define PERL_IN_DOOP_C
79072805 16#include "perl.h"
17
18#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
19#include <signal.h>
20#endif
21
942e002e 22STATIC I32
cea2e8a9 23S_do_trans_CC_simple(pTHX_ SV *sv)
79072805 24{
11343788 25 dTHR;
4757a243 26 U8 *s;
27 U8 *send;
28 I32 matches = 0;
463ee0b2 29 STRLEN len;
4757a243 30 short *tbl;
31 I32 ch;
79072805 32
4757a243 33 tbl = (short*)cPVOP->op_pv;
34 if (!tbl)
cea2e8a9 35 Perl_croak(aTHX_ "panic: do_trans");
a0ed51b3 36
4757a243 37 s = (U8*)SvPV(sv, len);
38 send = s + len;
39
40 while (s < send) {
41 if ((ch = tbl[*s]) >= 0) {
42 matches++;
43 *s = ch;
79072805 44 }
4757a243 45 s++;
46 }
47 SvSETMAGIC(sv);
48
49 return matches;
50}
51
942e002e 52STATIC I32
cea2e8a9 53S_do_trans_CC_count(pTHX_ SV *sv)
4757a243 54{
55 dTHR;
56 U8 *s;
57 U8 *send;
58 I32 matches = 0;
59 STRLEN len;
60 short *tbl;
61
62 tbl = (short*)cPVOP->op_pv;
63 if (!tbl)
cea2e8a9 64 Perl_croak(aTHX_ "panic: do_trans");
4757a243 65
66 s = (U8*)SvPV(sv, len);
67 send = s + len;
68
69 while (s < send) {
70 if (tbl[*s] >= 0)
71 matches++;
72 s++;
73 }
74
75 return matches;
76}
77
942e002e 78STATIC I32
cea2e8a9 79S_do_trans_CC_complex(pTHX_ SV *sv)
4757a243 80{
81 dTHR;
82 U8 *s;
83 U8 *send;
84 U8 *d;
85 I32 matches = 0;
86 STRLEN len;
87 short *tbl;
88 I32 ch;
89
90 tbl = (short*)cPVOP->op_pv;
91 if (!tbl)
cea2e8a9 92 Perl_croak(aTHX_ "panic: do_trans");
4757a243 93
94 s = (U8*)SvPV(sv, len);
95 send = s + len;
96
97 d = s;
98 if (PL_op->op_private & OPpTRANS_SQUASH) {
99 U8* p = send;
100
101 while (s < send) {
102 if ((ch = tbl[*s]) >= 0) {
103 *d = ch;
104 matches++;
105 if (p == d - 1 && *p == *d)
106 matches--;
a0ed51b3 107 else
4757a243 108 p = d++;
a0ed51b3 109 }
4757a243 110 else if (ch == -1) /* -1 is unmapped character */
111 *d++ = *s; /* -2 is delete character */
112 s++;
a0ed51b3 113 }
4757a243 114 }
115 else {
116 while (s < send) {
117 if ((ch = tbl[*s]) >= 0) {
118 *d = ch;
119 matches++;
120 d++;
a0ed51b3 121 }
4757a243 122 else if (ch == -1) /* -1 is unmapped character */
123 *d++ = *s; /* -2 is delete character */
124 s++;
5d06d08e 125 }
4757a243 126 }
127 matches += send - d; /* account for disappeared chars */
128 *d = '\0';
129 SvCUR_set(sv, d - (U8*)SvPVX(sv));
130 SvSETMAGIC(sv);
131
132 return matches;
133}
134
942e002e 135STATIC I32
cea2e8a9 136S_do_trans_UU_simple(pTHX_ SV *sv)
4757a243 137{
138 dTHR;
139 U8 *s;
140 U8 *send;
141 U8 *d;
142 I32 matches = 0;
143 STRLEN len;
144
145 SV* rv = (SV*)cSVOP->op_sv;
146 HV* hv = (HV*)SvRV(rv);
147 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
148 UV none = svp ? SvUV(*svp) : 0x7fffffff;
149 UV extra = none + 1;
150 UV final;
151 UV uv;
152
153 s = (U8*)SvPV(sv, len);
154 send = s + len;
155
156 svp = hv_fetch(hv, "FINAL", 5, FALSE);
157 if (svp)
158 final = SvUV(*svp);
159
160 d = s;
161 while (s < send) {
162 if ((uv = swash_fetch(rv, s)) < none) {
163 s += UTF8SKIP(s);
164 matches++;
165 d = uv_to_utf8(d, uv);
166 }
167 else if (uv == none) {
168 int i;
169 for (i = UTF8SKIP(s); i; i--)
170 *d++ = *s++;
171 }
172 else if (uv == extra) {
173 s += UTF8SKIP(s);
174 matches++;
175 d = uv_to_utf8(d, final);
176 }
177 else
178 s += UTF8SKIP(s);
179 }
180 *d = '\0';
181 SvCUR_set(sv, d - (U8*)SvPVX(sv));
182 SvSETMAGIC(sv);
183
184 return matches;
185}
186
942e002e 187STATIC I32
cea2e8a9 188S_do_trans_UU_count(pTHX_ SV *sv)
4757a243 189{
190 dTHR;
191 U8 *s;
192 U8 *send;
193 I32 matches = 0;
194 STRLEN len;
195
196 SV* rv = (SV*)cSVOP->op_sv;
197 HV* hv = (HV*)SvRV(rv);
198 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
199 UV none = svp ? SvUV(*svp) : 0x7fffffff;
200 UV uv;
201
202 s = (U8*)SvPV(sv, len);
203 send = s + len;
204
205 while (s < send) {
834a4ddd 206 if ((uv = swash_fetch(rv, s)) < none)
4757a243 207 matches++;
834a4ddd 208 s += UTF8SKIP(s);
4757a243 209 }
210
211 return matches;
212}
213
942e002e 214STATIC I32
cea2e8a9 215S_do_trans_UC_simple(pTHX_ SV *sv)
4757a243 216{
217 dTHR;
218 U8 *s;
219 U8 *send;
220 U8 *d;
221 I32 matches = 0;
222 STRLEN len;
223
224 SV* rv = (SV*)cSVOP->op_sv;
225 HV* hv = (HV*)SvRV(rv);
226 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
227 UV none = svp ? SvUV(*svp) : 0x7fffffff;
228 UV extra = none + 1;
229 UV final;
230 UV uv;
231
232 s = (U8*)SvPV(sv, len);
233 send = s + len;
234
235 svp = hv_fetch(hv, "FINAL", 5, FALSE);
236 if (svp)
237 final = SvUV(*svp);
238
239 d = s;
240 while (s < send) {
241 if ((uv = swash_fetch(rv, s)) < none) {
242 s += UTF8SKIP(s);
243 matches++;
244 *d++ = (U8)uv;
245 }
246 else if (uv == none) {
247 I32 ulen;
248 uv = utf8_to_uv(s, &ulen);
249 s += ulen;
250 *d++ = (U8)uv;
251 }
252 else if (uv == extra) {
253 s += UTF8SKIP(s);
254 matches++;
255 *d++ = (U8)final;
256 }
257 else
258 s += UTF8SKIP(s);
259 }
260 *d = '\0';
261 SvCUR_set(sv, d - (U8*)SvPVX(sv));
262 SvSETMAGIC(sv);
263
264 return matches;
265}
266
942e002e 267STATIC I32
cea2e8a9 268S_do_trans_CU_simple(pTHX_ SV *sv)
4757a243 269{
270 dTHR;
271 U8 *s;
272 U8 *send;
273 U8 *d;
274 U8 *dst;
275 I32 matches = 0;
276 STRLEN len;
277
278 SV* rv = (SV*)cSVOP->op_sv;
279 HV* hv = (HV*)SvRV(rv);
280 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
281 UV none = svp ? SvUV(*svp) : 0x7fffffff;
282 UV extra = none + 1;
283 UV final;
284 UV uv;
285 U8 tmpbuf[10];
286 I32 bits = 16;
287
288 s = (U8*)SvPV(sv, len);
289 send = s + len;
290
291 svp = hv_fetch(hv, "BITS", 4, FALSE);
292 if (svp)
293 bits = (I32)SvIV(*svp);
294
295 svp = hv_fetch(hv, "FINAL", 5, FALSE);
296 if (svp)
297 final = SvUV(*svp);
298
299 Newz(801, d, len * (bits >> 3) + 1, U8);
300 dst = d;
301
302 while (s < send) {
303 uv = *s++;
304 if (uv < 0x80)
305 tmpbuf[0] = uv;
306 else {
307 tmpbuf[0] = (( uv >> 6) | 0xc0);
308 tmpbuf[1] = (( uv & 0x3f) | 0x80);
a0ed51b3 309 }
4757a243 310
311 if ((uv = swash_fetch(rv, tmpbuf)) < none) {
312 matches++;
313 d = uv_to_utf8(d, uv);
314 }
315 else if (uv == none)
316 d = uv_to_utf8(d, s[-1]);
317 else if (uv == extra) {
318 matches++;
319 d = uv_to_utf8(d, final);
320 }
321 }
322 *d = '\0';
323 sv_usepvn_mg(sv, (char*)dst, d - dst);
324
325 return matches;
326}
327
328/* utf-8 to latin-1 */
329
942e002e 330STATIC I32
cea2e8a9 331S_do_trans_UC_trivial(pTHX_ SV *sv)
4757a243 332{
333 dTHR;
334 U8 *s;
335 U8 *send;
336 U8 *d;
337 STRLEN len;
338
339 s = (U8*)SvPV(sv, len);
340 send = s + len;
341
342 d = s;
343 while (s < send) {
344 if (*s < 0x80)
345 *d++ = *s++;
a0ed51b3 346 else {
4757a243 347 I32 ulen;
348 UV uv = utf8_to_uv(s, &ulen);
349 s += ulen;
350 *d++ = (U8)uv;
351 }
352 }
353 *d = '\0';
354 SvCUR_set(sv, d - (U8*)SvPVX(sv));
355 SvSETMAGIC(sv);
a0ed51b3 356
4757a243 357 return SvCUR(sv);
358}
a0ed51b3 359
4757a243 360/* latin-1 to utf-8 */
a0ed51b3 361
942e002e 362STATIC I32
cea2e8a9 363S_do_trans_CU_trivial(pTHX_ SV *sv)
4757a243 364{
365 dTHR;
366 U8 *s;
367 U8 *send;
368 U8 *d;
369 U8 *dst;
370 I32 matches;
371 STRLEN len;
a0ed51b3 372
4757a243 373 s = (U8*)SvPV(sv, len);
374 send = s + len;
375
376 Newz(801, d, len * 2 + 1, U8);
377 dst = d;
378
379 matches = send - s;
380
381 while (s < send) {
382 if (*s < 0x80)
383 *d++ = *s++;
384 else {
385 UV uv = *s++;
386 *d++ = (( uv >> 6) | 0xc0);
387 *d++ = (( uv & 0x3f) | 0x80);
388 }
389 }
390 *d = '\0';
391 sv_usepvn_mg(sv, (char*)dst, d - dst);
392
393 return matches;
394}
395
942e002e 396STATIC I32
cea2e8a9 397S_do_trans_UU_complex(pTHX_ SV *sv)
4757a243 398{
399 dTHR;
400 U8 *s;
401 U8 *send;
402 U8 *d;
403 I32 matches = 0;
404 I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
405 I32 from_utf = PL_op->op_private & OPpTRANS_FROM_UTF;
406 I32 to_utf = PL_op->op_private & OPpTRANS_TO_UTF;
407 I32 del = PL_op->op_private & OPpTRANS_DELETE;
408 SV* rv = (SV*)cSVOP->op_sv;
409 HV* hv = (HV*)SvRV(rv);
410 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
411 UV none = svp ? SvUV(*svp) : 0x7fffffff;
412 UV extra = none + 1;
413 UV final;
414 UV uv;
415 STRLEN len;
416 U8 *dst;
417
418 s = (U8*)SvPV(sv, len);
419 send = s + len;
420
421 svp = hv_fetch(hv, "FINAL", 5, FALSE);
422 if (svp)
423 final = SvUV(*svp);
424
425 if (PL_op->op_private & OPpTRANS_GROWS) {
426 I32 bits = 16;
427
428 svp = hv_fetch(hv, "BITS", 4, FALSE);
429 if (svp)
430 bits = (I32)SvIV(*svp);
431
432 Newz(801, d, len * (bits >> 3) + 1, U8);
433 dst = d;
434 }
435 else {
436 d = s;
437 dst = 0;
438 }
439
440 if (squash) {
441 UV puv = 0xfeedface;
442 while (s < send) {
443 if (from_utf) {
444 uv = swash_fetch(rv, s);
a0ed51b3 445 }
446 else {
4757a243 447 U8 tmpbuf[2];
448 uv = *s++;
449 if (uv < 0x80)
450 tmpbuf[0] = uv;
451 else {
452 tmpbuf[0] = (( uv >> 6) | 0xc0);
453 tmpbuf[1] = (( uv & 0x3f) | 0x80);
454 }
455 uv = swash_fetch(rv, tmpbuf);
456 }
457 if (uv < none) {
458 matches++;
459 if (uv != puv) {
460 if (uv >= 0x80 && to_utf)
461 d = uv_to_utf8(d, uv);
462 else
463 *d++ = (U8)uv;
464 puv = uv;
465 }
466 if (from_utf)
467 s += UTF8SKIP(s);
468 continue;
469 }
470 else if (uv == none) { /* "none" is unmapped character */
471 if (from_utf) {
472 if (*s < 0x80)
473 *d++ = *s++;
474 else if (to_utf) {
a0ed51b3 475 int i;
4757a243 476 for (i = UTF8SKIP(s); i; --i)
477 *d++ = *s++;
a0ed51b3 478 }
4757a243 479 else {
480 I32 ulen;
481 *d++ = (U8)utf8_to_uv(s, &ulen);
482 s += ulen;
a0ed51b3 483 }
a0ed51b3 484 }
4757a243 485 else { /* must be to_utf only */
486 d = uv_to_utf8(d, s[-1]);
487 }
488 puv = 0xfeedface;
489 continue;
a0ed51b3 490 }
4757a243 491 else if (uv == extra && !del) {
492 matches++;
493 if (uv != puv) {
494 if (final >= 0x80 && to_utf)
495 d = uv_to_utf8(d, final);
496 else
497 *d++ = (U8)final;
498 puv = final;
499 }
500 if (from_utf)
501 s += UTF8SKIP(s);
502 continue;
503 }
504 matches++; /* "none+1" is delete character */
505 if (from_utf)
506 s += UTF8SKIP(s);
a0ed51b3 507 }
79072805 508 }
509 else {
4757a243 510 while (s < send) {
511 if (from_utf) {
512 uv = swash_fetch(rv, s);
513 }
514 else {
515 U8 tmpbuf[2];
516 uv = *s++;
517 if (uv < 0x80)
518 tmpbuf[0] = uv;
519 else {
520 tmpbuf[0] = (( uv >> 6) | 0xc0);
521 tmpbuf[1] = (( uv & 0x3f) | 0x80);
a0ed51b3 522 }
4757a243 523 uv = swash_fetch(rv, tmpbuf);
a0ed51b3 524 }
4757a243 525 if (uv < none) {
526 matches++;
527 if (uv >= 0x80 && to_utf)
528 d = uv_to_utf8(d, uv);
529 else
530 *d++ = (U8)uv;
531 if (from_utf)
532 s += UTF8SKIP(s);
533 continue;
a0ed51b3 534 }
4757a243 535 else if (uv == none) { /* "none" is unmapped character */
536 if (from_utf) {
537 if (*s < 0x80)
538 *d++ = *s++;
539 else if (to_utf) {
540 int i;
541 for (i = UTF8SKIP(s); i; --i)
542 *d++ = *s++;
543 }
544 else {
545 I32 ulen;
546 *d++ = (U8)utf8_to_uv(s, &ulen);
547 s += ulen;
a0ed51b3 548 }
79072805 549 }
4757a243 550 else { /* must be to_utf only */
551 d = uv_to_utf8(d, s[-1]);
552 }
553 continue;
79072805 554 }
4757a243 555 else if (uv == extra && !del) {
556 matches++;
557 if (final >= 0x80 && to_utf)
558 d = uv_to_utf8(d, final);
559 else
560 *d++ = (U8)final;
561 if (from_utf)
562 s += UTF8SKIP(s);
563 continue;
564 }
565 matches++; /* "none+1" is delete character */
566 if (from_utf)
567 s += UTF8SKIP(s);
79072805 568 }
4757a243 569 }
570 if (dst)
571 sv_usepvn(sv, (char*)dst, d - dst);
572 else {
573 *d = '\0';
574 SvCUR_set(sv, d - (U8*)SvPVX(sv));
575 }
576 SvSETMAGIC(sv);
577
578 return matches;
579}
580
581I32
864dbfa3 582Perl_do_trans(pTHX_ SV *sv)
4757a243 583{
46124e9e 584 dTHR;
4757a243 585 STRLEN len;
586
587 if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL))
cea2e8a9 588 Perl_croak(aTHX_ PL_no_modify);
4757a243 589
590 (void)SvPV(sv, len);
591 if (!len)
592 return 0;
593 if (!SvPOKp(sv))
594 (void)SvPV_force(sv, len);
595 (void)SvPOK_only(sv);
596
cea2e8a9 597 DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
4757a243 598
599 switch (PL_op->op_private & 63) {
600 case 0:
601 return do_trans_CC_simple(sv);
602
603 case OPpTRANS_FROM_UTF:
604 return do_trans_UC_simple(sv);
605
606 case OPpTRANS_TO_UTF:
607 return do_trans_CU_simple(sv);
608
609 case OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF:
610 return do_trans_UU_simple(sv);
611
612 case OPpTRANS_IDENTICAL:
613 return do_trans_CC_count(sv);
614
615 case OPpTRANS_FROM_UTF|OPpTRANS_IDENTICAL:
616 return do_trans_UC_trivial(sv);
617
618 case OPpTRANS_TO_UTF|OPpTRANS_IDENTICAL:
619 return do_trans_CU_trivial(sv);
620
621 case OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF|OPpTRANS_IDENTICAL:
622 return do_trans_UU_count(sv);
623
624 default:
625 if (PL_op->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF))
626 return do_trans_UU_complex(sv); /* could be UC or CU too */
627 else
628 return do_trans_CC_complex(sv);
79072805 629 }
79072805 630}
631
632void
864dbfa3 633Perl_do_join(pTHX_ register SV *sv, SV *del, register SV **mark, register SV **sp)
79072805 634{
635 SV **oldmark = mark;
636 register I32 items = sp - mark;
79072805 637 register STRLEN len;
463ee0b2 638 STRLEN delimlen;
639 register char *delim = SvPV(del, delimlen);
640 STRLEN tmplen;
79072805 641
642 mark++;
643 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
07f14f54 644 (void)SvUPGRADE(sv, SVt_PV);
79072805 645 if (SvLEN(sv) < len + items) { /* current length is way too short */
646 while (items-- > 0) {
48c036b1 647 if (*mark && !SvGMAGICAL(*mark) && SvOK(*mark)) {
463ee0b2 648 SvPV(*mark, tmplen);
649 len += tmplen;
79072805 650 }
651 mark++;
652 }
653 SvGROW(sv, len + 1); /* so try to pre-extend */
654
655 mark = oldmark;
db7c17d7 656 items = sp - mark;
79072805 657 ++mark;
658 }
659
463ee0b2 660 if (items-- > 0) {
8990e307 661 char *s;
662
663 if (*mark) {
664 s = SvPV(*mark, tmplen);
665 sv_setpvn(sv, s, tmplen);
666 }
667 else
668 sv_setpv(sv, "");
463ee0b2 669 mark++;
670 }
79072805 671 else
672 sv_setpv(sv,"");
673 len = delimlen;
674 if (len) {
675 for (; items > 0; items--,mark++) {
676 sv_catpvn(sv,delim,len);
677 sv_catsv(sv,*mark);
678 }
679 }
680 else {
681 for (; items > 0; items--,mark++)
682 sv_catsv(sv,*mark);
683 }
684 SvSETMAGIC(sv);
685}
686
687void
864dbfa3 688Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg)
79072805 689{
46fc3d4c 690 STRLEN patlen;
691 char *pat = SvPV(*sarg, patlen);
692 bool do_taint = FALSE;
693
694 sv_vsetpvfn(sv, pat, patlen, Null(va_list*), sarg + 1, len - 1, &do_taint);
79072805 695 SvSETMAGIC(sv);
46fc3d4c 696 if (do_taint)
697 SvTAINTED_on(sv);
79072805 698}
699
700void
864dbfa3 701Perl_do_vecset(pTHX_ SV *sv)
79072805 702{
703 SV *targ = LvTARG(sv);
704 register I32 offset;
705 register I32 size;
8990e307 706 register unsigned char *s;
707 register unsigned long lval;
79072805 708 I32 mask;
a0d0e21e 709 STRLEN targlen;
710 STRLEN len;
79072805 711
8990e307 712 if (!targ)
713 return;
a0d0e21e 714 s = (unsigned char*)SvPV_force(targ, targlen);
8990e307 715 lval = U_L(SvNV(sv));
79072805 716 offset = LvTARGOFF(sv);
717 size = LvTARGLEN(sv);
a0d0e21e 718
719 len = (offset + size + 7) / 8;
720 if (len > targlen) {
721 s = (unsigned char*)SvGROW(targ, len + 1);
722 (void)memzero(s + targlen, len - targlen + 1);
723 SvCUR_set(targ, len);
724 }
725
79072805 726 if (size < 8) {
727 mask = (1 << size) - 1;
728 size = offset & 7;
729 lval &= mask;
730 offset >>= 3;
731 s[offset] &= ~(mask << size);
732 s[offset] |= lval << size;
733 }
734 else {
a0d0e21e 735 offset >>= 3;
79072805 736 if (size == 8)
737 s[offset] = lval & 255;
738 else if (size == 16) {
739 s[offset] = (lval >> 8) & 255;
740 s[offset+1] = lval & 255;
741 }
742 else if (size == 32) {
743 s[offset] = (lval >> 24) & 255;
744 s[offset+1] = (lval >> 16) & 255;
745 s[offset+2] = (lval >> 8) & 255;
746 s[offset+3] = lval & 255;
747 }
748 }
749}
750
751void
864dbfa3 752Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
79072805 753{
463ee0b2 754 STRLEN len;
a0d0e21e 755 char *s;
c485e607 756 dTHR;
a0d0e21e 757
79072805 758 if (SvTYPE(sv) == SVt_PVAV) {
a0d0e21e 759 register I32 i;
760 I32 max;
761 AV* av = (AV*)sv;
762 max = AvFILL(av);
763 for (i = 0; i <= max; i++) {
764 sv = (SV*)av_fetch(av, i, FALSE);
3280af22 765 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
a0d0e21e 766 do_chop(astr, sv);
767 }
768 return;
79072805 769 }
aa854799 770 else if (SvTYPE(sv) == SVt_PVHV) {
a0d0e21e 771 HV* hv = (HV*)sv;
772 HE* entry;
773 (void)hv_iterinit(hv);
774 /*SUPPRESS 560*/
775 while (entry = hv_iternext(hv))
776 do_chop(astr,hv_iterval(hv,entry));
777 return;
79072805 778 }
aa854799 779 else if (SvREADONLY(sv))
cea2e8a9 780 Perl_croak(aTHX_ PL_no_modify);
a0d0e21e 781 s = SvPV(sv, len);
748a9306 782 if (len && !SvPOK(sv))
a0d0e21e 783 s = SvPV_force(sv, len);
a0ed51b3 784 if (IN_UTF8) {
785 if (s && len) {
786 char *send = s + len;
787 char *start = s;
788 s = send - 1;
789 while ((*s & 0xc0) == 0x80)
790 --s;
791 if (UTF8SKIP(s) != send - s)
cea2e8a9 792 Perl_warn(aTHX_ "Malformed UTF-8 character");
a0ed51b3 793 sv_setpvn(astr, s, send - s);
794 *s = '\0';
795 SvCUR_set(sv, s - start);
796 SvNIOK_off(sv);
797 }
798 else
799 sv_setpvn(astr, "", 0);
800 }
801 else
a0d0e21e 802 if (s && len) {
803 s += --len;
804 sv_setpvn(astr, s, 1);
805 *s = '\0';
806 SvCUR_set(sv, len);
807 SvNIOK_off(sv);
79072805 808 }
809 else
a0d0e21e 810 sv_setpvn(astr, "", 0);
811 SvSETMAGIC(sv);
812}
813
814I32
864dbfa3 815Perl_do_chomp(pTHX_ register SV *sv)
a0d0e21e 816{
aeea060c 817 dTHR;
c07a80fd 818 register I32 count;
a0d0e21e 819 STRLEN len;
820 char *s;
c07a80fd 821
3280af22 822 if (RsSNARF(PL_rs))
c07a80fd 823 return 0;
4c5a6083 824 if (RsRECORD(PL_rs))
825 return 0;
c07a80fd 826 count = 0;
a0d0e21e 827 if (SvTYPE(sv) == SVt_PVAV) {
828 register I32 i;
829 I32 max;
830 AV* av = (AV*)sv;
831 max = AvFILL(av);
832 for (i = 0; i <= max; i++) {
833 sv = (SV*)av_fetch(av, i, FALSE);
3280af22 834 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
a0d0e21e 835 count += do_chomp(sv);
836 }
837 return count;
838 }
aa854799 839 else if (SvTYPE(sv) == SVt_PVHV) {
a0d0e21e 840 HV* hv = (HV*)sv;
841 HE* entry;
842 (void)hv_iterinit(hv);
843 /*SUPPRESS 560*/
844 while (entry = hv_iternext(hv))
845 count += do_chomp(hv_iterval(hv,entry));
846 return count;
847 }
aa854799 848 else if (SvREADONLY(sv))
cea2e8a9 849 Perl_croak(aTHX_ PL_no_modify);
a0d0e21e 850 s = SvPV(sv, len);
851 if (len && !SvPOKp(sv))
852 s = SvPV_force(sv, len);
853 if (s && len) {
854 s += --len;
3280af22 855 if (RsPARA(PL_rs)) {
a0d0e21e 856 if (*s != '\n')
857 goto nope;
858 ++count;
859 while (len && s[-1] == '\n') {
860 --len;
861 --s;
862 ++count;
863 }
864 }
a0d0e21e 865 else {
c07a80fd 866 STRLEN rslen;
3280af22 867 char *rsptr = SvPV(PL_rs, rslen);
c07a80fd 868 if (rslen == 1) {
869 if (*s != *rsptr)
870 goto nope;
871 ++count;
872 }
873 else {
8c2cee6f 874 if (len < rslen - 1)
c07a80fd 875 goto nope;
876 len -= rslen - 1;
877 s -= rslen - 1;
36477c24 878 if (memNE(s, rsptr, rslen))
c07a80fd 879 goto nope;
880 count += rslen;
881 }
a0d0e21e 882 }
a0d0e21e 883 *s = '\0';
884 SvCUR_set(sv, len);
885 SvNIOK_off(sv);
886 }
887 nope:
888 SvSETMAGIC(sv);
889 return count;
890}
79072805 891
892void
864dbfa3 893Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
79072805 894{
aeea060c 895 dTHR; /* just for taint */
79072805 896#ifdef LIBERAL
897 register long *dl;
898 register long *ll;
899 register long *rl;
900#endif
901 register char *dc;
463ee0b2 902 STRLEN leftlen;
903 STRLEN rightlen;
7a4c00b4 904 register char *lc;
905 register char *rc;
79072805 906 register I32 len;
a0d0e21e 907 I32 lensave;
7a4c00b4 908 char *lsave;
909 char *rsave;
79072805 910
1fbd88dc 911 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
912 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
7a4c00b4 913 lsave = lc = SvPV(left, leftlen);
914 rsave = rc = SvPV(right, rightlen);
93a17b20 915 len = leftlen < rightlen ? leftlen : rightlen;
a0d0e21e 916 lensave = len;
7a4c00b4 917 if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
2d8e6c8d 918 STRLEN n_a;
919 dc = SvPV_force(sv, n_a);
ff68c719 920 if (SvCUR(sv) < len) {
921 dc = SvGROW(sv, len + 1);
922 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
923 }
924 }
925 else {
926 I32 needlen = ((optype == OP_BIT_AND)
927 ? len : (leftlen > rightlen ? leftlen : rightlen));
928 Newz(801, dc, needlen + 1, char);
929 (void)sv_usepvn(sv, dc, needlen);
930 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
79072805 931 }
a0d0e21e 932 SvCUR_set(sv, len);
933 (void)SvPOK_only(sv);
79072805 934#ifdef LIBERAL
935 if (len >= sizeof(long)*4 &&
936 !((long)dc % sizeof(long)) &&
937 !((long)lc % sizeof(long)) &&
938 !((long)rc % sizeof(long))) /* It's almost always aligned... */
939 {
940 I32 remainder = len % (sizeof(long)*4);
941 len /= (sizeof(long)*4);
942
943 dl = (long*)dc;
944 ll = (long*)lc;
945 rl = (long*)rc;
946
947 switch (optype) {
948 case OP_BIT_AND:
949 while (len--) {
950 *dl++ = *ll++ & *rl++;
951 *dl++ = *ll++ & *rl++;
952 *dl++ = *ll++ & *rl++;
953 *dl++ = *ll++ & *rl++;
954 }
955 break;
a0d0e21e 956 case OP_BIT_XOR:
79072805 957 while (len--) {
958 *dl++ = *ll++ ^ *rl++;
959 *dl++ = *ll++ ^ *rl++;
960 *dl++ = *ll++ ^ *rl++;
961 *dl++ = *ll++ ^ *rl++;
962 }
963 break;
964 case OP_BIT_OR:
965 while (len--) {
966 *dl++ = *ll++ | *rl++;
967 *dl++ = *ll++ | *rl++;
968 *dl++ = *ll++ | *rl++;
969 *dl++ = *ll++ | *rl++;
970 }
971 }
972
973 dc = (char*)dl;
974 lc = (char*)ll;
975 rc = (char*)rl;
976
977 len = remainder;
978 }
979#endif
a0d0e21e 980 {
a0d0e21e 981 switch (optype) {
982 case OP_BIT_AND:
983 while (len--)
984 *dc++ = *lc++ & *rc++;
985 break;
986 case OP_BIT_XOR:
987 while (len--)
988 *dc++ = *lc++ ^ *rc++;
989 goto mop_up;
990 case OP_BIT_OR:
991 while (len--)
992 *dc++ = *lc++ | *rc++;
993 mop_up:
994 len = lensave;
995 if (rightlen > len)
996 sv_catpvn(sv, rsave + len, rightlen - len);
997 else if (leftlen > len)
998 sv_catpvn(sv, lsave + len, leftlen - len);
4633a7c4 999 else
1000 *SvEND(sv) = '\0';
a0d0e21e 1001 break;
1002 }
79072805 1003 }
fb73857a 1004 SvTAINT(sv);
79072805 1005}
463ee0b2 1006
1007OP *
cea2e8a9 1008Perl_do_kv(pTHX)
463ee0b2 1009{
4e35701f 1010 djSP;
463ee0b2 1011 HV *hv = (HV*)POPs;
800e9ae0 1012 HV *keys;
463ee0b2 1013 register HE *entry;
463ee0b2 1014 SV *tmpstr;
54310121 1015 I32 gimme = GIMME_V;
533c011a 1016 I32 dokeys = (PL_op->op_type == OP_KEYS);
1017 I32 dovalues = (PL_op->op_type == OP_VALUES);
c750a3ec 1018 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
1019
533c011a 1020 if (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV)
a0d0e21e 1021 dokeys = dovalues = TRUE;
463ee0b2 1022
85581909 1023 if (!hv) {
533c011a 1024 if (PL_op->op_flags & OPf_MOD) { /* lvalue */
85581909 1025 dTARGET; /* make sure to clear its target here */
1026 if (SvTYPE(TARG) == SVt_PVLV)
1027 LvTARG(TARG) = Nullsv;
1028 PUSHs(TARG);
1029 }
463ee0b2 1030 RETURN;
85581909 1031 }
748a9306 1032
800e9ae0 1033 keys = realhv ? hv : avhv_keys((AV*)hv);
1034 (void)hv_iterinit(keys); /* always reset iterator regardless */
748a9306 1035
54310121 1036 if (gimme == G_VOID)
aa689395 1037 RETURN;
1038
54310121 1039 if (gimme == G_SCALAR) {
6ee623d5 1040 IV i;
463ee0b2 1041 dTARGET;
1042
533c011a 1043 if (PL_op->op_flags & OPf_MOD) { /* lvalue */
85581909 1044 if (SvTYPE(TARG) < SVt_PVLV) {
1045 sv_upgrade(TARG, SVt_PVLV);
1046 sv_magic(TARG, Nullsv, 'k', Nullch, 0);
1047 }
1048 LvTYPE(TARG) = 'k';
800e9ae0 1049 if (LvTARG(TARG) != (SV*)keys) {
6ff81951 1050 if (LvTARG(TARG))
1051 SvREFCNT_dec(LvTARG(TARG));
800e9ae0 1052 LvTARG(TARG) = SvREFCNT_inc(keys);
6ff81951 1053 }
85581909 1054 PUSHs(TARG);
1055 RETURN;
1056 }
1057
33c27489 1058 if (! SvTIED_mg((SV*)keys, 'P'))
800e9ae0 1059 i = HvKEYS(keys);
463ee0b2 1060 else {
1061 i = 0;
463ee0b2 1062 /*SUPPRESS 560*/
800e9ae0 1063 while (hv_iternext(keys)) i++;
463ee0b2 1064 }
1065 PUSHi( i );
1066 RETURN;
1067 }
1068
8ed4b672 1069 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
463ee0b2 1070
463ee0b2 1071 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
800e9ae0 1072 while (entry = hv_iternext(keys)) {
463ee0b2 1073 SPAGAIN;
8c2cee6f 1074 if (dokeys)
1075 XPUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
463ee0b2 1076 if (dovalues) {
463ee0b2 1077 PUTBACK;
b6429b1b 1078 tmpstr = realhv ?
1079 hv_iterval(hv,entry) : avhv_iterval((AV*)hv,entry);
cea2e8a9 1080 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
46fc3d4c 1081 (unsigned long)HeHASH(entry),
800e9ae0 1082 HvMAX(keys)+1,
1083 (unsigned long)(HeHASH(entry) & HvMAX(keys))));
463ee0b2 1084 SPAGAIN;
46fc3d4c 1085 XPUSHs(tmpstr);
463ee0b2 1086 }
1087 PUTBACK;
1088 }
1089 return NORMAL;
1090}
4e35701f 1091