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