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