This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Warn about small spherical distances.
[perl5.git] / doop.c
CommitLineData
a0d0e21e 1/* doop.c
79072805 2 *
4eb8286e 3 * Copyright (c) 1991-1999, 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
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
LW
26 U8 *s;
27 U8 *send;
28 I32 matches = 0;
463ee0b2 29 STRLEN len;
4757a243
LW
30 short *tbl;
31 I32 ch;
79072805 32
4757a243
LW
33 tbl = (short*)cPVOP->op_pv;
34 if (!tbl)
cea2e8a9 35 Perl_croak(aTHX_ "panic: do_trans");
a0ed51b3 36
4757a243
LW
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
LW
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
LW
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
LW
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
LW
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
LW
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
LW
110 else if (ch == -1) /* -1 is unmapped character */
111 *d++ = *s; /* -2 is delete character */
112 s++;
a0ed51b3 113 }
4757a243
LW
114 }
115 else {
116 while (s < send) {
117 if ((ch = tbl[*s]) >= 0) {
118 *d = ch;
119 matches++;
120 d++;
a0ed51b3 121 }
4757a243
LW
122 else if (ch == -1) /* -1 is unmapped character */
123 *d++ = *s; /* -2 is delete character */
124 s++;
5d06d08e 125 }
4757a243
LW
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
LW
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
LW
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
LW
209 }
210
211 return matches;
212}
213
942e002e 214STATIC I32
cea2e8a9 215S_do_trans_UC_simple(pTHX_ SV *sv)
4757a243
LW
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
LW
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
LW
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
LW
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
LW
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
LW
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
LW
364{
365 dTHR;
366 U8 *s;
367 U8 *send;
368 U8 *d;
369 U8 *dst;
370 I32 matches;
371 STRLEN len;
a0ed51b3 372
4757a243
LW
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
LW
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
LW
445 }
446 else {
4757a243
LW
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
LW
476 for (i = UTF8SKIP(s); i; --i)
477 *d++ = *s++;
a0ed51b3 478 }
4757a243
LW
479 else {
480 I32 ulen;
481 *d++ = (U8)utf8_to_uv(s, &ulen);
482 s += ulen;
a0ed51b3 483 }
a0ed51b3 484 }
4757a243
LW
485 else { /* must be to_utf only */
486 d = uv_to_utf8(d, s[-1]);
487 }
488 puv = 0xfeedface;
489 continue;
a0ed51b3 490 }
4757a243
LW
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
LW
508 }
509 else {
4757a243
LW
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
LW
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
LW
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
LW
550 else { /* must be to_utf only */
551 d = uv_to_utf8(d, s[-1]);
552 }
553 continue;
79072805 554 }
4757a243
LW
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
LW
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
LW
585 STRLEN len;
586
587 if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL))
cea2e8a9 588 Perl_croak(aTHX_ PL_no_modify);
4757a243
LW
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
LW
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
LW
630}
631
632void
864dbfa3 633Perl_do_join(pTHX_ register SV *sv, SV *del, register SV **mark, register SV **sp)
79072805
LW
634{
635 SV **oldmark = mark;
636 register I32 items = sp - mark;
79072805 637 register STRLEN len;
463ee0b2
LW
638 STRLEN delimlen;
639 register char *delim = SvPV(del, delimlen);
640 STRLEN tmplen;
79072805
LW
641
642 mark++;
643 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
07f14f54 644 (void)SvUPGRADE(sv, SVt_PV);
79072805
LW
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
LW
648 SvPV(*mark, tmplen);
649 len += tmplen;
79072805
LW
650 }
651 mark++;
652 }
653 SvGROW(sv, len + 1); /* so try to pre-extend */
654
655 mark = oldmark;
db7c17d7 656 items = sp - mark;
79072805
LW
657 ++mark;
658 }
659
463ee0b2 660 if (items-- > 0) {
8990e307
LW
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
LW
669 mark++;
670 }
79072805
LW
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
PP
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
PP
696 if (do_taint)
697 SvTAINTED_on(sv);
79072805
LW
698}
699
700void
864dbfa3 701Perl_do_vecset(pTHX_ SV *sv)
79072805
LW
702{
703 SV *targ = LvTARG(sv);
704 register I32 offset;
705 register I32 size;
8990e307
LW
706 register unsigned char *s;
707 register unsigned long lval;
79072805 708 I32 mask;
a0d0e21e
LW
709 STRLEN targlen;
710 STRLEN len;
79072805 711
8990e307
LW
712 if (!targ)
713 return;
a0d0e21e 714 s = (unsigned char*)SvPV_force(targ, targlen);
8990e307 715 lval = U_L(SvNV(sv));
79072805
LW
716 offset = LvTARGOFF(sv);
717 size = LvTARGLEN(sv);
a0d0e21e
LW
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
LW
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
LW
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 }
7bb043c3 749 SvSETMAGIC(targ);
79072805
LW
750}
751
752void
864dbfa3 753Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
79072805 754{
463ee0b2 755 STRLEN len;
a0d0e21e 756 char *s;
c485e607 757 dTHR;
a0d0e21e 758
79072805 759 if (SvTYPE(sv) == SVt_PVAV) {
a0d0e21e
LW
760 register I32 i;
761 I32 max;
762 AV* av = (AV*)sv;
763 max = AvFILL(av);
764 for (i = 0; i <= max; i++) {
765 sv = (SV*)av_fetch(av, i, FALSE);
3280af22 766 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
a0d0e21e
LW
767 do_chop(astr, sv);
768 }
769 return;
79072805 770 }
aa854799 771 else if (SvTYPE(sv) == SVt_PVHV) {
a0d0e21e
LW
772 HV* hv = (HV*)sv;
773 HE* entry;
774 (void)hv_iterinit(hv);
775 /*SUPPRESS 560*/
776 while (entry = hv_iternext(hv))
777 do_chop(astr,hv_iterval(hv,entry));
778 return;
79072805 779 }
aa854799 780 else if (SvREADONLY(sv))
cea2e8a9 781 Perl_croak(aTHX_ PL_no_modify);
a0d0e21e 782 s = SvPV(sv, len);
748a9306 783 if (len && !SvPOK(sv))
a0d0e21e 784 s = SvPV_force(sv, len);
a0ed51b3
LW
785 if (IN_UTF8) {
786 if (s && len) {
787 char *send = s + len;
788 char *start = s;
789 s = send - 1;
790 while ((*s & 0xc0) == 0x80)
791 --s;
0453d815
PM
792 if (UTF8SKIP(s) != send - s && ckWARN_d(WARN_UTF8))
793 Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character");
a0ed51b3
LW
794 sv_setpvn(astr, s, send - s);
795 *s = '\0';
796 SvCUR_set(sv, s - start);
797 SvNIOK_off(sv);
798 }
799 else
800 sv_setpvn(astr, "", 0);
801 }
802 else
a0d0e21e
LW
803 if (s && len) {
804 s += --len;
805 sv_setpvn(astr, s, 1);
806 *s = '\0';
807 SvCUR_set(sv, len);
808 SvNIOK_off(sv);
79072805
LW
809 }
810 else
a0d0e21e
LW
811 sv_setpvn(astr, "", 0);
812 SvSETMAGIC(sv);
813}
814
815I32
864dbfa3 816Perl_do_chomp(pTHX_ register SV *sv)
a0d0e21e 817{
aeea060c 818 dTHR;
c07a80fd 819 register I32 count;
a0d0e21e
LW
820 STRLEN len;
821 char *s;
c07a80fd 822
3280af22 823 if (RsSNARF(PL_rs))
c07a80fd 824 return 0;
4c5a6083
GS
825 if (RsRECORD(PL_rs))
826 return 0;
c07a80fd 827 count = 0;
a0d0e21e
LW
828 if (SvTYPE(sv) == SVt_PVAV) {
829 register I32 i;
830 I32 max;
831 AV* av = (AV*)sv;
832 max = AvFILL(av);
833 for (i = 0; i <= max; i++) {
834 sv = (SV*)av_fetch(av, i, FALSE);
3280af22 835 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
a0d0e21e
LW
836 count += do_chomp(sv);
837 }
838 return count;
839 }
aa854799 840 else if (SvTYPE(sv) == SVt_PVHV) {
a0d0e21e
LW
841 HV* hv = (HV*)sv;
842 HE* entry;
843 (void)hv_iterinit(hv);
844 /*SUPPRESS 560*/
845 while (entry = hv_iternext(hv))
846 count += do_chomp(hv_iterval(hv,entry));
847 return count;
848 }
aa854799 849 else if (SvREADONLY(sv))
cea2e8a9 850 Perl_croak(aTHX_ PL_no_modify);
a0d0e21e
LW
851 s = SvPV(sv, len);
852 if (len && !SvPOKp(sv))
853 s = SvPV_force(sv, len);
854 if (s && len) {
855 s += --len;
3280af22 856 if (RsPARA(PL_rs)) {
a0d0e21e
LW
857 if (*s != '\n')
858 goto nope;
859 ++count;
860 while (len && s[-1] == '\n') {
861 --len;
862 --s;
863 ++count;
864 }
865 }
a0d0e21e 866 else {
c07a80fd 867 STRLEN rslen;
3280af22 868 char *rsptr = SvPV(PL_rs, rslen);
c07a80fd
PP
869 if (rslen == 1) {
870 if (*s != *rsptr)
871 goto nope;
872 ++count;
873 }
874 else {
8c2cee6f 875 if (len < rslen - 1)
c07a80fd
PP
876 goto nope;
877 len -= rslen - 1;
878 s -= rslen - 1;
36477c24 879 if (memNE(s, rsptr, rslen))
c07a80fd
PP
880 goto nope;
881 count += rslen;
882 }
a0d0e21e 883 }
a0d0e21e
LW
884 *s = '\0';
885 SvCUR_set(sv, len);
886 SvNIOK_off(sv);
887 }
888 nope:
889 SvSETMAGIC(sv);
890 return count;
891}
79072805
LW
892
893void
864dbfa3 894Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
79072805 895{
aeea060c 896 dTHR; /* just for taint */
79072805
LW
897#ifdef LIBERAL
898 register long *dl;
899 register long *ll;
900 register long *rl;
901#endif
902 register char *dc;
463ee0b2
LW
903 STRLEN leftlen;
904 STRLEN rightlen;
7a4c00b4
PP
905 register char *lc;
906 register char *rc;
79072805 907 register I32 len;
a0d0e21e 908 I32 lensave;
7a4c00b4
PP
909 char *lsave;
910 char *rsave;
79072805 911
1fbd88dc
CS
912 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
913 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
7a4c00b4
PP
914 lsave = lc = SvPV(left, leftlen);
915 rsave = rc = SvPV(right, rightlen);
93a17b20 916 len = leftlen < rightlen ? leftlen : rightlen;
a0d0e21e 917 lensave = len;
7a4c00b4 918 if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
2d8e6c8d
GS
919 STRLEN n_a;
920 dc = SvPV_force(sv, n_a);
ff68c719
PP
921 if (SvCUR(sv) < len) {
922 dc = SvGROW(sv, len + 1);
923 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
924 }
925 }
926 else {
927 I32 needlen = ((optype == OP_BIT_AND)
928 ? len : (leftlen > rightlen ? leftlen : rightlen));
929 Newz(801, dc, needlen + 1, char);
930 (void)sv_usepvn(sv, dc, needlen);
931 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
79072805 932 }
a0d0e21e
LW
933 SvCUR_set(sv, len);
934 (void)SvPOK_only(sv);
79072805
LW
935#ifdef LIBERAL
936 if (len >= sizeof(long)*4 &&
937 !((long)dc % sizeof(long)) &&
938 !((long)lc % sizeof(long)) &&
939 !((long)rc % sizeof(long))) /* It's almost always aligned... */
940 {
941 I32 remainder = len % (sizeof(long)*4);
942 len /= (sizeof(long)*4);
943
944 dl = (long*)dc;
945 ll = (long*)lc;
946 rl = (long*)rc;
947
948 switch (optype) {
949 case OP_BIT_AND:
950 while (len--) {
951 *dl++ = *ll++ & *rl++;
952 *dl++ = *ll++ & *rl++;
953 *dl++ = *ll++ & *rl++;
954 *dl++ = *ll++ & *rl++;
955 }
956 break;
a0d0e21e 957 case OP_BIT_XOR:
79072805
LW
958 while (len--) {
959 *dl++ = *ll++ ^ *rl++;
960 *dl++ = *ll++ ^ *rl++;
961 *dl++ = *ll++ ^ *rl++;
962 *dl++ = *ll++ ^ *rl++;
963 }
964 break;
965 case OP_BIT_OR:
966 while (len--) {
967 *dl++ = *ll++ | *rl++;
968 *dl++ = *ll++ | *rl++;
969 *dl++ = *ll++ | *rl++;
970 *dl++ = *ll++ | *rl++;
971 }
972 }
973
974 dc = (char*)dl;
975 lc = (char*)ll;
976 rc = (char*)rl;
977
978 len = remainder;
979 }
980#endif
a0d0e21e 981 {
a0d0e21e
LW
982 switch (optype) {
983 case OP_BIT_AND:
984 while (len--)
985 *dc++ = *lc++ & *rc++;
986 break;
987 case OP_BIT_XOR:
988 while (len--)
989 *dc++ = *lc++ ^ *rc++;
990 goto mop_up;
991 case OP_BIT_OR:
992 while (len--)
993 *dc++ = *lc++ | *rc++;
994 mop_up:
995 len = lensave;
996 if (rightlen > len)
997 sv_catpvn(sv, rsave + len, rightlen - len);
998 else if (leftlen > len)
999 sv_catpvn(sv, lsave + len, leftlen - len);
4633a7c4
LW
1000 else
1001 *SvEND(sv) = '\0';
a0d0e21e
LW
1002 break;
1003 }
79072805 1004 }
fb73857a 1005 SvTAINT(sv);
79072805 1006}
463ee0b2
LW
1007
1008OP *
cea2e8a9 1009Perl_do_kv(pTHX)
463ee0b2 1010{
4e35701f 1011 djSP;
463ee0b2 1012 HV *hv = (HV*)POPs;
800e9ae0 1013 HV *keys;
463ee0b2 1014 register HE *entry;
463ee0b2 1015 SV *tmpstr;
54310121 1016 I32 gimme = GIMME_V;
533c011a
NIS
1017 I32 dokeys = (PL_op->op_type == OP_KEYS);
1018 I32 dovalues = (PL_op->op_type == OP_VALUES);
c750a3ec
MB
1019 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
1020
533c011a 1021 if (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV)
a0d0e21e 1022 dokeys = dovalues = TRUE;
463ee0b2 1023
85581909 1024 if (!hv) {
533c011a 1025 if (PL_op->op_flags & OPf_MOD) { /* lvalue */
85581909
SB
1026 dTARGET; /* make sure to clear its target here */
1027 if (SvTYPE(TARG) == SVt_PVLV)
1028 LvTARG(TARG) = Nullsv;
1029 PUSHs(TARG);
1030 }
463ee0b2 1031 RETURN;
85581909 1032 }
748a9306 1033
800e9ae0
JP
1034 keys = realhv ? hv : avhv_keys((AV*)hv);
1035 (void)hv_iterinit(keys); /* always reset iterator regardless */
748a9306 1036
54310121 1037 if (gimme == G_VOID)
aa689395
PP
1038 RETURN;
1039
54310121 1040 if (gimme == G_SCALAR) {
6ee623d5 1041 IV i;
463ee0b2
LW
1042 dTARGET;
1043
533c011a 1044 if (PL_op->op_flags & OPf_MOD) { /* lvalue */
85581909
SB
1045 if (SvTYPE(TARG) < SVt_PVLV) {
1046 sv_upgrade(TARG, SVt_PVLV);
1047 sv_magic(TARG, Nullsv, 'k', Nullch, 0);
1048 }
1049 LvTYPE(TARG) = 'k';
800e9ae0 1050 if (LvTARG(TARG) != (SV*)keys) {
6ff81951
GS
1051 if (LvTARG(TARG))
1052 SvREFCNT_dec(LvTARG(TARG));
800e9ae0 1053 LvTARG(TARG) = SvREFCNT_inc(keys);
6ff81951 1054 }
85581909
SB
1055 PUSHs(TARG);
1056 RETURN;
1057 }
1058
33c27489 1059 if (! SvTIED_mg((SV*)keys, 'P'))
800e9ae0 1060 i = HvKEYS(keys);
463ee0b2
LW
1061 else {
1062 i = 0;
463ee0b2 1063 /*SUPPRESS 560*/
800e9ae0 1064 while (hv_iternext(keys)) i++;
463ee0b2
LW
1065 }
1066 PUSHi( i );
1067 RETURN;
1068 }
1069
8ed4b672 1070 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
463ee0b2 1071
463ee0b2 1072 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
800e9ae0 1073 while (entry = hv_iternext(keys)) {
463ee0b2 1074 SPAGAIN;
8c2cee6f
PP
1075 if (dokeys)
1076 XPUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
463ee0b2 1077 if (dovalues) {
463ee0b2 1078 PUTBACK;
b6429b1b
GS
1079 tmpstr = realhv ?
1080 hv_iterval(hv,entry) : avhv_iterval((AV*)hv,entry);
cea2e8a9 1081 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
46fc3d4c 1082 (unsigned long)HeHASH(entry),
800e9ae0
JP
1083 HvMAX(keys)+1,
1084 (unsigned long)(HeHASH(entry) & HvMAX(keys))));
463ee0b2 1085 SPAGAIN;
46fc3d4c 1086 XPUSHs(tmpstr);
463ee0b2
LW
1087 }
1088 PUTBACK;
1089 }
1090 return NORMAL;
1091}
4e35701f 1092