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