This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[ID 19990823.009] [PATCH perl5.005_61] typo in perl.h
[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
81e118e0
JH
700UV
701Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size)
702{
703 STRLEN srclen, len;
704 unsigned char *s = (unsigned char *) SvPV(sv, srclen);
705 UV retnum = 0;
706
a50d7633 707 if (offset < 0)
81e118e0 708 return retnum;
a50d7633
JH
709 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
710 Perl_croak(aTHX_ "Illegal number of bits in vec");
81e118e0
JH
711 offset *= size; /* turn into bit offset */
712 len = (offset + size + 7) / 8; /* required number of bytes */
713 if (len > srclen) {
714 if (size <= 8)
715 retnum = 0;
716 else {
717 offset >>= 3; /* turn into byte offset */
718 if (size == 16) {
719 if (offset >= srclen)
720 retnum = 0;
721 else
722 retnum = (UV) s[offset] << 8;
723 }
724 else if (size == 32) {
725 if (offset >= srclen)
726 retnum = 0;
727 else if (offset + 1 >= srclen)
728 retnum =
729 ((UV) s[offset ] << 24);
730 else if (offset + 2 >= srclen)
731 retnum =
732 ((UV) s[offset ] << 24) +
733 ((UV) s[offset + 1] << 16);
734 else
735 retnum =
736 ((UV) s[offset ] << 24) +
737 ((UV) s[offset + 1] << 16) +
738 ( s[offset + 2] << 8);
739 }
81e118e0
JH
740 }
741 }
742 else if (size < 8)
743 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
744 else {
745 offset >>= 3; /* turn into byte offset */
746 if (size == 8)
747 retnum = s[offset];
748 else if (size == 16)
749 retnum =
750 ((UV) s[offset] << 8) +
751 s[offset + 1];
752 else if (size == 32)
753 retnum =
754 ((UV) s[offset ] << 24) +
755 ((UV) s[offset + 1] << 16) +
756 ( s[offset + 2] << 8) +
757 s[offset + 3];
81e118e0
JH
758 }
759
760 return retnum;
761}
762
79072805 763void
864dbfa3 764Perl_do_vecset(pTHX_ SV *sv)
79072805
LW
765{
766 SV *targ = LvTARG(sv);
767 register I32 offset;
768 register I32 size;
8990e307 769 register unsigned char *s;
81e118e0 770 register UV lval;
79072805 771 I32 mask;
a0d0e21e
LW
772 STRLEN targlen;
773 STRLEN len;
79072805 774
8990e307
LW
775 if (!targ)
776 return;
a0d0e21e 777 s = (unsigned char*)SvPV_force(targ, targlen);
81e118e0 778 lval = SvUV(sv);
79072805
LW
779 offset = LvTARGOFF(sv);
780 size = LvTARGLEN(sv);
a50d7633
JH
781 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
782 Perl_croak(aTHX_ "Illegal number of bits in vec");
a0d0e21e 783
81e118e0
JH
784 offset *= size; /* turn into bit offset */
785 len = (offset + size + 7) / 8; /* required number of bytes */
a0d0e21e
LW
786 if (len > targlen) {
787 s = (unsigned char*)SvGROW(targ, len + 1);
788 (void)memzero(s + targlen, len - targlen + 1);
789 SvCUR_set(targ, len);
790 }
791
79072805
LW
792 if (size < 8) {
793 mask = (1 << size) - 1;
794 size = offset & 7;
795 lval &= mask;
81e118e0 796 offset >>= 3; /* turn into byte offset */
79072805
LW
797 s[offset] &= ~(mask << size);
798 s[offset] |= lval << size;
799 }
800 else {
81e118e0 801 offset >>= 3; /* turn into byte offset */
79072805
LW
802 if (size == 8)
803 s[offset] = lval & 255;
804 else if (size == 16) {
805 s[offset] = (lval >> 8) & 255;
806 s[offset+1] = lval & 255;
807 }
808 else if (size == 32) {
809 s[offset] = (lval >> 24) & 255;
810 s[offset+1] = (lval >> 16) & 255;
811 s[offset+2] = (lval >> 8) & 255;
812 s[offset+3] = lval & 255;
813 }
814 }
7bb043c3 815 SvSETMAGIC(targ);
79072805
LW
816}
817
818void
864dbfa3 819Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
79072805 820{
463ee0b2 821 STRLEN len;
a0d0e21e 822 char *s;
c485e607 823 dTHR;
a0d0e21e 824
79072805 825 if (SvTYPE(sv) == SVt_PVAV) {
a0d0e21e
LW
826 register I32 i;
827 I32 max;
828 AV* av = (AV*)sv;
829 max = AvFILL(av);
830 for (i = 0; i <= max; i++) {
831 sv = (SV*)av_fetch(av, i, FALSE);
3280af22 832 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
a0d0e21e
LW
833 do_chop(astr, sv);
834 }
835 return;
79072805 836 }
aa854799 837 else if (SvTYPE(sv) == SVt_PVHV) {
a0d0e21e
LW
838 HV* hv = (HV*)sv;
839 HE* entry;
840 (void)hv_iterinit(hv);
841 /*SUPPRESS 560*/
842 while (entry = hv_iternext(hv))
843 do_chop(astr,hv_iterval(hv,entry));
844 return;
79072805 845 }
aa854799 846 else if (SvREADONLY(sv))
cea2e8a9 847 Perl_croak(aTHX_ PL_no_modify);
a0d0e21e 848 s = SvPV(sv, len);
748a9306 849 if (len && !SvPOK(sv))
a0d0e21e 850 s = SvPV_force(sv, len);
a0ed51b3
LW
851 if (IN_UTF8) {
852 if (s && len) {
853 char *send = s + len;
854 char *start = s;
855 s = send - 1;
856 while ((*s & 0xc0) == 0x80)
857 --s;
0453d815
PM
858 if (UTF8SKIP(s) != send - s && ckWARN_d(WARN_UTF8))
859 Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character");
a0ed51b3
LW
860 sv_setpvn(astr, s, send - s);
861 *s = '\0';
862 SvCUR_set(sv, s - start);
863 SvNIOK_off(sv);
864 }
865 else
866 sv_setpvn(astr, "", 0);
867 }
868 else
a0d0e21e
LW
869 if (s && len) {
870 s += --len;
871 sv_setpvn(astr, s, 1);
872 *s = '\0';
873 SvCUR_set(sv, len);
874 SvNIOK_off(sv);
79072805
LW
875 }
876 else
a0d0e21e
LW
877 sv_setpvn(astr, "", 0);
878 SvSETMAGIC(sv);
879}
880
881I32
864dbfa3 882Perl_do_chomp(pTHX_ register SV *sv)
a0d0e21e 883{
aeea060c 884 dTHR;
c07a80fd 885 register I32 count;
a0d0e21e
LW
886 STRLEN len;
887 char *s;
c07a80fd 888
3280af22 889 if (RsSNARF(PL_rs))
c07a80fd 890 return 0;
4c5a6083
GS
891 if (RsRECORD(PL_rs))
892 return 0;
c07a80fd 893 count = 0;
a0d0e21e
LW
894 if (SvTYPE(sv) == SVt_PVAV) {
895 register I32 i;
896 I32 max;
897 AV* av = (AV*)sv;
898 max = AvFILL(av);
899 for (i = 0; i <= max; i++) {
900 sv = (SV*)av_fetch(av, i, FALSE);
3280af22 901 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
a0d0e21e
LW
902 count += do_chomp(sv);
903 }
904 return count;
905 }
aa854799 906 else if (SvTYPE(sv) == SVt_PVHV) {
a0d0e21e
LW
907 HV* hv = (HV*)sv;
908 HE* entry;
909 (void)hv_iterinit(hv);
910 /*SUPPRESS 560*/
911 while (entry = hv_iternext(hv))
912 count += do_chomp(hv_iterval(hv,entry));
913 return count;
914 }
aa854799 915 else if (SvREADONLY(sv))
cea2e8a9 916 Perl_croak(aTHX_ PL_no_modify);
a0d0e21e
LW
917 s = SvPV(sv, len);
918 if (len && !SvPOKp(sv))
919 s = SvPV_force(sv, len);
920 if (s && len) {
921 s += --len;
3280af22 922 if (RsPARA(PL_rs)) {
a0d0e21e
LW
923 if (*s != '\n')
924 goto nope;
925 ++count;
926 while (len && s[-1] == '\n') {
927 --len;
928 --s;
929 ++count;
930 }
931 }
a0d0e21e 932 else {
c07a80fd 933 STRLEN rslen;
3280af22 934 char *rsptr = SvPV(PL_rs, rslen);
c07a80fd
PP
935 if (rslen == 1) {
936 if (*s != *rsptr)
937 goto nope;
938 ++count;
939 }
940 else {
8c2cee6f 941 if (len < rslen - 1)
c07a80fd
PP
942 goto nope;
943 len -= rslen - 1;
944 s -= rslen - 1;
36477c24 945 if (memNE(s, rsptr, rslen))
c07a80fd
PP
946 goto nope;
947 count += rslen;
948 }
a0d0e21e 949 }
a0d0e21e
LW
950 *s = '\0';
951 SvCUR_set(sv, len);
952 SvNIOK_off(sv);
953 }
954 nope:
955 SvSETMAGIC(sv);
956 return count;
957}
79072805
LW
958
959void
864dbfa3 960Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
79072805 961{
aeea060c 962 dTHR; /* just for taint */
79072805
LW
963#ifdef LIBERAL
964 register long *dl;
965 register long *ll;
966 register long *rl;
967#endif
968 register char *dc;
463ee0b2
LW
969 STRLEN leftlen;
970 STRLEN rightlen;
7a4c00b4
PP
971 register char *lc;
972 register char *rc;
79072805 973 register I32 len;
a0d0e21e 974 I32 lensave;
7a4c00b4
PP
975 char *lsave;
976 char *rsave;
79072805 977
1fbd88dc
CS
978 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
979 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
7a4c00b4
PP
980 lsave = lc = SvPV(left, leftlen);
981 rsave = rc = SvPV(right, rightlen);
93a17b20 982 len = leftlen < rightlen ? leftlen : rightlen;
a0d0e21e 983 lensave = len;
7a4c00b4 984 if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
2d8e6c8d
GS
985 STRLEN n_a;
986 dc = SvPV_force(sv, n_a);
ff68c719
PP
987 if (SvCUR(sv) < len) {
988 dc = SvGROW(sv, len + 1);
989 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
990 }
991 }
992 else {
993 I32 needlen = ((optype == OP_BIT_AND)
994 ? len : (leftlen > rightlen ? leftlen : rightlen));
995 Newz(801, dc, needlen + 1, char);
996 (void)sv_usepvn(sv, dc, needlen);
997 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
79072805 998 }
a0d0e21e
LW
999 SvCUR_set(sv, len);
1000 (void)SvPOK_only(sv);
79072805
LW
1001#ifdef LIBERAL
1002 if (len >= sizeof(long)*4 &&
1003 !((long)dc % sizeof(long)) &&
1004 !((long)lc % sizeof(long)) &&
1005 !((long)rc % sizeof(long))) /* It's almost always aligned... */
1006 {
1007 I32 remainder = len % (sizeof(long)*4);
1008 len /= (sizeof(long)*4);
1009
1010 dl = (long*)dc;
1011 ll = (long*)lc;
1012 rl = (long*)rc;
1013
1014 switch (optype) {
1015 case OP_BIT_AND:
1016 while (len--) {
1017 *dl++ = *ll++ & *rl++;
1018 *dl++ = *ll++ & *rl++;
1019 *dl++ = *ll++ & *rl++;
1020 *dl++ = *ll++ & *rl++;
1021 }
1022 break;
a0d0e21e 1023 case OP_BIT_XOR:
79072805
LW
1024 while (len--) {
1025 *dl++ = *ll++ ^ *rl++;
1026 *dl++ = *ll++ ^ *rl++;
1027 *dl++ = *ll++ ^ *rl++;
1028 *dl++ = *ll++ ^ *rl++;
1029 }
1030 break;
1031 case OP_BIT_OR:
1032 while (len--) {
1033 *dl++ = *ll++ | *rl++;
1034 *dl++ = *ll++ | *rl++;
1035 *dl++ = *ll++ | *rl++;
1036 *dl++ = *ll++ | *rl++;
1037 }
1038 }
1039
1040 dc = (char*)dl;
1041 lc = (char*)ll;
1042 rc = (char*)rl;
1043
1044 len = remainder;
1045 }
1046#endif
a0d0e21e 1047 {
a0d0e21e
LW
1048 switch (optype) {
1049 case OP_BIT_AND:
1050 while (len--)
1051 *dc++ = *lc++ & *rc++;
1052 break;
1053 case OP_BIT_XOR:
1054 while (len--)
1055 *dc++ = *lc++ ^ *rc++;
1056 goto mop_up;
1057 case OP_BIT_OR:
1058 while (len--)
1059 *dc++ = *lc++ | *rc++;
1060 mop_up:
1061 len = lensave;
1062 if (rightlen > len)
1063 sv_catpvn(sv, rsave + len, rightlen - len);
1064 else if (leftlen > len)
1065 sv_catpvn(sv, lsave + len, leftlen - len);
4633a7c4
LW
1066 else
1067 *SvEND(sv) = '\0';
a0d0e21e
LW
1068 break;
1069 }
79072805 1070 }
fb73857a 1071 SvTAINT(sv);
79072805 1072}
463ee0b2
LW
1073
1074OP *
cea2e8a9 1075Perl_do_kv(pTHX)
463ee0b2 1076{
4e35701f 1077 djSP;
463ee0b2 1078 HV *hv = (HV*)POPs;
800e9ae0 1079 HV *keys;
463ee0b2 1080 register HE *entry;
463ee0b2 1081 SV *tmpstr;
54310121 1082 I32 gimme = GIMME_V;
533c011a
NIS
1083 I32 dokeys = (PL_op->op_type == OP_KEYS);
1084 I32 dovalues = (PL_op->op_type == OP_VALUES);
c750a3ec
MB
1085 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
1086
533c011a 1087 if (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV)
a0d0e21e 1088 dokeys = dovalues = TRUE;
463ee0b2 1089
85581909 1090 if (!hv) {
533c011a 1091 if (PL_op->op_flags & OPf_MOD) { /* lvalue */
85581909
SB
1092 dTARGET; /* make sure to clear its target here */
1093 if (SvTYPE(TARG) == SVt_PVLV)
1094 LvTARG(TARG) = Nullsv;
1095 PUSHs(TARG);
1096 }
463ee0b2 1097 RETURN;
85581909 1098 }
748a9306 1099
800e9ae0
JP
1100 keys = realhv ? hv : avhv_keys((AV*)hv);
1101 (void)hv_iterinit(keys); /* always reset iterator regardless */
748a9306 1102
54310121 1103 if (gimme == G_VOID)
aa689395
PP
1104 RETURN;
1105
54310121 1106 if (gimme == G_SCALAR) {
6ee623d5 1107 IV i;
463ee0b2
LW
1108 dTARGET;
1109
533c011a 1110 if (PL_op->op_flags & OPf_MOD) { /* lvalue */
85581909
SB
1111 if (SvTYPE(TARG) < SVt_PVLV) {
1112 sv_upgrade(TARG, SVt_PVLV);
1113 sv_magic(TARG, Nullsv, 'k', Nullch, 0);
1114 }
1115 LvTYPE(TARG) = 'k';
800e9ae0 1116 if (LvTARG(TARG) != (SV*)keys) {
6ff81951
GS
1117 if (LvTARG(TARG))
1118 SvREFCNT_dec(LvTARG(TARG));
800e9ae0 1119 LvTARG(TARG) = SvREFCNT_inc(keys);
6ff81951 1120 }
85581909
SB
1121 PUSHs(TARG);
1122 RETURN;
1123 }
1124
33c27489 1125 if (! SvTIED_mg((SV*)keys, 'P'))
800e9ae0 1126 i = HvKEYS(keys);
463ee0b2
LW
1127 else {
1128 i = 0;
463ee0b2 1129 /*SUPPRESS 560*/
800e9ae0 1130 while (hv_iternext(keys)) i++;
463ee0b2
LW
1131 }
1132 PUSHi( i );
1133 RETURN;
1134 }
1135
8ed4b672 1136 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
463ee0b2 1137
463ee0b2 1138 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
800e9ae0 1139 while (entry = hv_iternext(keys)) {
463ee0b2 1140 SPAGAIN;
8c2cee6f
PP
1141 if (dokeys)
1142 XPUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
463ee0b2 1143 if (dovalues) {
463ee0b2 1144 PUTBACK;
b6429b1b
GS
1145 tmpstr = realhv ?
1146 hv_iterval(hv,entry) : avhv_iterval((AV*)hv,entry);
cea2e8a9 1147 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
46fc3d4c 1148 (unsigned long)HeHASH(entry),
800e9ae0
JP
1149 HvMAX(keys)+1,
1150 (unsigned long)(HeHASH(entry) & HvMAX(keys))));
463ee0b2 1151 SPAGAIN;
46fc3d4c 1152 XPUSHs(tmpstr);
463ee0b2
LW
1153 }
1154 PUTBACK;
1155 }
1156 return NORMAL;
1157}
4e35701f 1158