This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Convert strcmp into strEQ, strNE
[perl5.git] / doop.c
1 /*    doop.c
2  *
3  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4  *    2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall and others
5  *
6  *    You may distribute under the terms of either the GNU General Public
7  *    License or the Artistic License, as specified in the README file.
8  *
9  */
10
11 /*
12  *  'So that was the job I felt I had to do when I started,' thought Sam.
13  *
14  *     [p.934 of _The Lord of the Rings_, VI/iii: "Mount Doom"]
15  */
16
17 /* This file contains some common functions needed to carry out certain
18  * ops. For example, both pp_sprintf() and pp_prtf() call the function
19  * do_sprintf() found in this file.
20  */
21
22 #include "EXTERN.h"
23 #define PERL_IN_DOOP_C
24 #include "perl.h"
25
26 #ifndef PERL_MICRO
27 #include <signal.h>
28 #endif
29
30 STATIC I32
31 S_do_trans_simple(pTHX_ SV * const sv)
32 {
33     I32 matches = 0;
34     STRLEN len;
35     U8 *s = (U8*)SvPV_nomg(sv,len);
36     U8 * const send = s+len;
37     const short * const tbl = (short*)cPVOP->op_pv;
38
39     PERL_ARGS_ASSERT_DO_TRANS_SIMPLE;
40
41     if (!tbl)
42         Perl_croak(aTHX_ "panic: do_trans_simple line %d",__LINE__);
43
44     /* First, take care of non-UTF-8 input strings, because they're easy */
45     if (!SvUTF8(sv)) {
46         while (s < send) {
47             const I32 ch = tbl[*s];
48             if (ch >= 0) {
49                 matches++;
50                 *s = (U8)ch;
51             }
52             s++;
53         }
54         SvSETMAGIC(sv);
55     }
56     else {
57         const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
58         U8 *d;
59         U8 *dstart;
60
61         /* Allow for expansion: $_="a".chr(400); tr/a/\xFE/, FE needs encoding */
62         if (grows)
63             Newx(d, len*2+1, U8);
64         else
65             d = s;
66         dstart = d;
67         while (s < send) {
68             STRLEN ulen;
69             I32 ch;
70
71             /* Need to check this, otherwise 128..255 won't match */
72             const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
73             if (c < 0x100 && (ch = tbl[c]) >= 0) {
74                 matches++;
75                 d = uvchr_to_utf8(d, ch);
76                 s += ulen;
77             }
78             else { /* No match -> copy */
79                 Move(s, d, ulen, U8);
80                 d += ulen;
81                 s += ulen;
82             }
83         }
84         if (grows) {
85             sv_setpvn(sv, (char*)dstart, d - dstart);
86             Safefree(dstart);
87         }
88         else {
89             *d = '\0';
90             SvCUR_set(sv, d - dstart);
91         }
92         SvUTF8_on(sv);
93         SvSETMAGIC(sv);
94     }
95     return matches;
96 }
97
98 STATIC I32
99 S_do_trans_count(pTHX_ SV * const sv)
100 {
101     STRLEN len;
102     const U8 *s = (const U8*)SvPV_nomg_const(sv, len);
103     const U8 * const send = s + len;
104     I32 matches = 0;
105     const short * const tbl = (short*)cPVOP->op_pv;
106
107     PERL_ARGS_ASSERT_DO_TRANS_COUNT;
108
109     if (!tbl)
110         Perl_croak(aTHX_ "panic: do_trans_count line %d",__LINE__);
111
112     if (!SvUTF8(sv)) {
113         while (s < send) {
114             if (tbl[*s++] >= 0)
115                 matches++;
116         }
117     }
118     else {
119         const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
120         while (s < send) {
121             STRLEN ulen;
122             const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
123             if (c < 0x100) {
124                 if (tbl[c] >= 0)
125                     matches++;
126             } else if (complement)
127                 matches++;
128             s += ulen;
129         }
130     }
131
132     return matches;
133 }
134
135 STATIC I32
136 S_do_trans_complex(pTHX_ SV * const sv)
137 {
138     STRLEN len;
139     U8 *s = (U8*)SvPV_nomg(sv, len);
140     U8 * const send = s+len;
141     I32 matches = 0;
142     const short * const tbl = (short*)cPVOP->op_pv;
143
144     PERL_ARGS_ASSERT_DO_TRANS_COMPLEX;
145
146     if (!tbl)
147         Perl_croak(aTHX_ "panic: do_trans_complex line %d",__LINE__);
148
149     if (!SvUTF8(sv)) {
150         U8 *d = s;
151         U8 * const dstart = d;
152
153         if (PL_op->op_private & OPpTRANS_SQUASH) {
154             const U8* p = send;
155             while (s < send) {
156                 const I32 ch = tbl[*s];
157                 if (ch >= 0) {
158                     *d = (U8)ch;
159                     matches++;
160                     if (p != d - 1 || *p != *d)
161                         p = d++;
162                 }
163                 else if (ch == -1)      /* -1 is unmapped character */
164                     *d++ = *s;  
165                 else if (ch == -2)      /* -2 is delete character */
166                     matches++;
167                 s++;
168             }
169         }
170         else {
171             while (s < send) {
172                 const I32 ch = tbl[*s];
173                 if (ch >= 0) {
174                     matches++;
175                     *d++ = (U8)ch;
176                 }
177                 else if (ch == -1)      /* -1 is unmapped character */
178                     *d++ = *s;
179                 else if (ch == -2)      /* -2 is delete character */
180                     matches++;
181                 s++;
182             }
183         }
184         *d = '\0';
185         SvCUR_set(sv, d - dstart);
186     }
187     else { /* is utf8 */
188         const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
189         const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
190         const I32 del = PL_op->op_private & OPpTRANS_DELETE;
191         U8 *d;
192         U8 *dstart;
193         STRLEN rlen = 0;
194
195         if (grows)
196             Newx(d, len*2+1, U8);
197         else
198             d = s;
199         dstart = d;
200         if (complement && !del)
201             rlen = tbl[0x100];
202
203         if (PL_op->op_private & OPpTRANS_SQUASH) {
204             UV pch = 0xfeedface;
205             while (s < send) {
206                 STRLEN len;
207                 const UV comp = utf8n_to_uvchr(s, send - s, &len,
208                                                UTF8_ALLOW_DEFAULT);
209                 I32 ch;
210
211                 if (comp > 0xff) {
212                     if (!complement) {
213                         Move(s, d, len, U8);
214                         d += len;
215                     }
216                     else {
217                         matches++;
218                         if (!del) {
219                             ch = (rlen == 0) ? (I32)comp :
220                                 (comp - 0x100 < rlen) ?
221                                 tbl[comp+1] : tbl[0x100+rlen];
222                             if ((UV)ch != pch) {
223                                 d = uvchr_to_utf8(d, ch);
224                                 pch = (UV)ch;
225                             }
226                             s += len;
227                             continue;
228                         }
229                     }
230                 }
231                 else if ((ch = tbl[comp]) >= 0) {
232                     matches++;
233                     if ((UV)ch != pch) {
234                         d = uvchr_to_utf8(d, ch);
235                         pch = (UV)ch;
236                     }
237                     s += len;
238                     continue;
239                 }
240                 else if (ch == -1) {    /* -1 is unmapped character */
241                     Move(s, d, len, U8);
242                     d += len;
243                 }
244                 else if (ch == -2)      /* -2 is delete character */
245                     matches++;
246                 s += len;
247                 pch = 0xfeedface;
248             }
249         }
250         else {
251             while (s < send) {
252                 STRLEN len;
253                 const UV comp = utf8n_to_uvchr(s, send - s, &len,
254                                                UTF8_ALLOW_DEFAULT);
255                 I32 ch;
256                 if (comp > 0xff) {
257                     if (!complement) {
258                         Move(s, d, len, U8);
259                         d += len;
260                     }
261                     else {
262                         matches++;
263                         if (!del) {
264                             if (comp - 0x100 < rlen)
265                                 d = uvchr_to_utf8(d, tbl[comp+1]);
266                             else
267                                 d = uvchr_to_utf8(d, tbl[0x100+rlen]);
268                         }
269                     }
270                 }
271                 else if ((ch = tbl[comp]) >= 0) {
272                     d = uvchr_to_utf8(d, ch);
273                     matches++;
274                 }
275                 else if (ch == -1) {    /* -1 is unmapped character */
276                     Move(s, d, len, U8);
277                     d += len;
278                 }
279                 else if (ch == -2)      /* -2 is delete character */
280                     matches++;
281                 s += len;
282             }
283         }
284         if (grows) {
285             sv_setpvn(sv, (char*)dstart, d - dstart);
286             Safefree(dstart);
287         }
288         else {
289             *d = '\0';
290             SvCUR_set(sv, d - dstart);
291         }
292         SvUTF8_on(sv);
293     }
294     SvSETMAGIC(sv);
295     return matches;
296 }
297
298 STATIC I32
299 S_do_trans_simple_utf8(pTHX_ SV * const sv)
300 {
301     U8 *s;
302     U8 *send;
303     U8 *d;
304     U8 *start;
305     U8 *dstart, *dend;
306     I32 matches = 0;
307     const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
308     STRLEN len;
309     SV* const  rv =
310 #ifdef USE_ITHREADS
311                     PAD_SVl(cPADOP->op_padix);
312 #else
313                     MUTABLE_SV(cSVOP->op_sv);
314 #endif
315     HV* const  hv = MUTABLE_HV(SvRV(rv));
316     SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
317     const UV none = svp ? SvUV(*svp) : 0x7fffffff;
318     const UV extra = none + 1;
319     UV final = 0;
320     U8 hibit = 0;
321
322     PERL_ARGS_ASSERT_DO_TRANS_SIMPLE_UTF8;
323
324     s = (U8*)SvPV_nomg(sv, len);
325     if (!SvUTF8(sv)) {
326         const U8 *t = s;
327         const U8 * const e = s + len;
328         while (t < e) {
329             const U8 ch = *t++;
330             hibit = !NATIVE_BYTE_IS_INVARIANT(ch);
331             if (hibit) {
332                 s = bytes_to_utf8(s, &len);
333                 break;
334             }
335         }
336     }
337     send = s + len;
338     start = s;
339
340     svp = hv_fetchs(hv, "FINAL", FALSE);
341     if (svp)
342         final = SvUV(*svp);
343
344     if (grows) {
345         /* d needs to be bigger than s, in case e.g. upgrading is required */
346         Newx(d, len * 3 + UTF8_MAXBYTES, U8);
347         dend = d + len * 3;
348         dstart = d;
349     }
350     else {
351         dstart = d = s;
352         dend = d + len;
353     }
354
355     while (s < send) {
356         const UV uv = swash_fetch(rv, s, TRUE);
357         if (uv < none) {
358             s += UTF8SKIP(s);
359             matches++;
360             d = uvchr_to_utf8(d, uv);
361         }
362         else if (uv == none) {
363             const int i = UTF8SKIP(s);
364             Move(s, d, i, U8);
365             d += i;
366             s += i;
367         }
368         else if (uv == extra) {
369             s += UTF8SKIP(s);
370             matches++;
371             d = uvchr_to_utf8(d, final);
372         }
373         else
374             s += UTF8SKIP(s);
375
376         if (d > dend) {
377             const STRLEN clen = d - dstart;
378             const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
379             if (!grows)
380                 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
381             Renew(dstart, nlen + UTF8_MAXBYTES, U8);
382             d = dstart + clen;
383             dend = dstart + nlen;
384         }
385     }
386     if (grows || hibit) {
387         sv_setpvn(sv, (char*)dstart, d - dstart);
388         Safefree(dstart);
389         if (grows && hibit)
390             Safefree(start);
391     }
392     else {
393         *d = '\0';
394         SvCUR_set(sv, d - dstart);
395     }
396     SvSETMAGIC(sv);
397     SvUTF8_on(sv);
398
399     return matches;
400 }
401
402 STATIC I32
403 S_do_trans_count_utf8(pTHX_ SV * const sv)
404 {
405     const U8 *s;
406     const U8 *start = NULL;
407     const U8 *send;
408     I32 matches = 0;
409     STRLEN len;
410     SV* const  rv =
411 #ifdef USE_ITHREADS
412                     PAD_SVl(cPADOP->op_padix);
413 #else
414                     MUTABLE_SV(cSVOP->op_sv);
415 #endif
416     HV* const hv = MUTABLE_HV(SvRV(rv));
417     SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
418     const UV none = svp ? SvUV(*svp) : 0x7fffffff;
419     const UV extra = none + 1;
420     U8 hibit = 0;
421
422     PERL_ARGS_ASSERT_DO_TRANS_COUNT_UTF8;
423
424     s = (const U8*)SvPV_nomg_const(sv, len);
425     if (!SvUTF8(sv)) {
426         const U8 *t = s;
427         const U8 * const e = s + len;
428         while (t < e) {
429             const U8 ch = *t++;
430             hibit = !NATIVE_BYTE_IS_INVARIANT(ch);
431             if (hibit) {
432                 start = s = bytes_to_utf8(s, &len);
433                 break;
434             }
435         }
436     }
437     send = s + len;
438
439     while (s < send) {
440         const UV uv = swash_fetch(rv, s, TRUE);
441         if (uv < none || uv == extra)
442             matches++;
443         s += UTF8SKIP(s);
444     }
445     if (hibit)
446         Safefree(start);
447
448     return matches;
449 }
450
451 STATIC I32
452 S_do_trans_complex_utf8(pTHX_ SV * const sv)
453 {
454     U8 *start, *send;
455     U8 *d;
456     I32 matches = 0;
457     const I32 squash   = PL_op->op_private & OPpTRANS_SQUASH;
458     const I32 del      = PL_op->op_private & OPpTRANS_DELETE;
459     const I32 grows    = PL_op->op_private & OPpTRANS_GROWS;
460     SV* const  rv =
461 #ifdef USE_ITHREADS
462                     PAD_SVl(cPADOP->op_padix);
463 #else
464                     MUTABLE_SV(cSVOP->op_sv);
465 #endif
466     HV * const hv = MUTABLE_HV(SvRV(rv));
467     SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
468     const UV none = svp ? SvUV(*svp) : 0x7fffffff;
469     const UV extra = none + 1;
470     UV final = 0;
471     bool havefinal = FALSE;
472     STRLEN len;
473     U8 *dstart, *dend;
474     U8 hibit = 0;
475     U8 *s = (U8*)SvPV_nomg(sv, len);
476
477     PERL_ARGS_ASSERT_DO_TRANS_COMPLEX_UTF8;
478
479     if (!SvUTF8(sv)) {
480         const U8 *t = s;
481         const U8 * const e = s + len;
482         while (t < e) {
483             const U8 ch = *t++;
484             hibit = !NATIVE_BYTE_IS_INVARIANT(ch);
485             if (hibit) {
486                 s = bytes_to_utf8(s, &len);
487                 break;
488             }
489         }
490     }
491     send = s + len;
492     start = s;
493
494     svp = hv_fetchs(hv, "FINAL", FALSE);
495     if (svp) {
496         final = SvUV(*svp);
497         havefinal = TRUE;
498     }
499
500     if (grows) {
501         /* d needs to be bigger than s, in case e.g. upgrading is required */
502         Newx(d, len * 3 + UTF8_MAXBYTES, U8);
503         dend = d + len * 3;
504         dstart = d;
505     }
506     else {
507         dstart = d = s;
508         dend = d + len;
509     }
510
511     if (squash) {
512         UV puv = 0xfeedface;
513         while (s < send) {
514             UV uv = swash_fetch(rv, s, TRUE);
515         
516             if (d > dend) {
517                 const STRLEN clen = d - dstart;
518                 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
519                 if (!grows)
520                     Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
521                 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
522                 d = dstart + clen;
523                 dend = dstart + nlen;
524             }
525             if (uv < none) {
526                 matches++;
527                 s += UTF8SKIP(s);
528                 if (uv != puv) {
529                     d = uvchr_to_utf8(d, uv);
530                     puv = uv;
531                 }
532                 continue;
533             }
534             else if (uv == none) {      /* "none" is unmapped character */
535                 const int i = UTF8SKIP(s);
536                 Move(s, d, i, U8);
537                 d += i;
538                 s += i;
539                 puv = 0xfeedface;
540                 continue;
541             }
542             else if (uv == extra && !del) {
543                 matches++;
544                 if (havefinal) {
545                     s += UTF8SKIP(s);
546                     if (puv != final) {
547                         d = uvchr_to_utf8(d, final);
548                         puv = final;
549                     }
550                 }
551                 else {
552                     STRLEN len;
553                     uv = utf8n_to_uvchr(s, send - s, &len, UTF8_ALLOW_DEFAULT);
554                     if (uv != puv) {
555                         Move(s, d, len, U8);
556                         d += len;
557                         puv = uv;
558                     }
559                     s += len;
560                 }
561                 continue;
562             }
563             matches++;                  /* "none+1" is delete character */
564             s += UTF8SKIP(s);
565         }
566     }
567     else {
568         while (s < send) {
569             const UV uv = swash_fetch(rv, s, TRUE);
570             if (d > dend) {
571                 const STRLEN clen = d - dstart;
572                 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
573                 if (!grows)
574                     Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
575                 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
576                 d = dstart + clen;
577                 dend = dstart + nlen;
578             }
579             if (uv < none) {
580                 matches++;
581                 s += UTF8SKIP(s);
582                 d = uvchr_to_utf8(d, uv);
583                 continue;
584             }
585             else if (uv == none) {      /* "none" is unmapped character */
586                 const int i = UTF8SKIP(s);
587                 Move(s, d, i, U8);
588                 d += i;
589                 s += i;
590                 continue;
591             }
592             else if (uv == extra && !del) {
593                 matches++;
594                 s += UTF8SKIP(s);
595                 d = uvchr_to_utf8(d, final);
596                 continue;
597             }
598             matches++;                  /* "none+1" is delete character */
599             s += UTF8SKIP(s);
600         }
601     }
602     if (grows || hibit) {
603         sv_setpvn(sv, (char*)dstart, d - dstart);
604         Safefree(dstart);
605         if (grows && hibit)
606             Safefree(start);
607     }
608     else {
609         *d = '\0';
610         SvCUR_set(sv, d - dstart);
611     }
612     SvUTF8_on(sv);
613     SvSETMAGIC(sv);
614
615     return matches;
616 }
617
618 I32
619 Perl_do_trans(pTHX_ SV *sv)
620 {
621     STRLEN len;
622     const I32 flags = PL_op->op_private;
623     const I32 hasutf = flags & (OPpTRANS_FROM_UTF | OPpTRANS_TO_UTF);
624
625     PERL_ARGS_ASSERT_DO_TRANS;
626
627     if (SvREADONLY(sv) && !(flags & OPpTRANS_IDENTICAL)) {
628         Perl_croak_no_modify();
629     }
630     (void)SvPV_const(sv, len);
631     if (!len)
632         return 0;
633     if (!(flags & OPpTRANS_IDENTICAL)) {
634         if (!SvPOKp(sv) || SvTHINKFIRST(sv))
635             (void)SvPV_force_nomg(sv, len);
636         (void)SvPOK_only_UTF8(sv);
637     }
638
639     DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
640
641     /* If we use only OPpTRANS_IDENTICAL to bypass the READONLY check,
642      * we must also rely on it to choose the readonly strategy.
643      */
644     if (flags & OPpTRANS_IDENTICAL) {
645         return hasutf ? do_trans_count_utf8(sv) : do_trans_count(sv);
646     } else if (flags & (OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
647         return hasutf ? do_trans_complex_utf8(sv) : do_trans_complex(sv);
648     } else {
649         return hasutf ? do_trans_simple_utf8(sv) : do_trans_simple(sv);
650     }
651 }
652
653 void
654 Perl_do_join(pTHX_ SV *sv, SV *delim, SV **mark, SV **sp)
655 {
656     SV ** const oldmark = mark;
657     I32 items = sp - mark;
658     STRLEN len;
659     STRLEN delimlen;
660     const char * const delims = SvPV_const(delim, delimlen);
661
662     PERL_ARGS_ASSERT_DO_JOIN;
663
664     mark++;
665     len = (items > 0 ? (delimlen * (items - 1) ) : 0);
666     SvUPGRADE(sv, SVt_PV);
667     if (SvLEN(sv) < len + items) {      /* current length is way too short */
668         while (items-- > 0) {
669             if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
670                 STRLEN tmplen;
671                 SvPV_const(*mark, tmplen);
672                 len += tmplen;
673             }
674             mark++;
675         }
676         SvGROW(sv, len + 1);            /* so try to pre-extend */
677
678         mark = oldmark;
679         items = sp - mark;
680         ++mark;
681     }
682
683     SvPVCLEAR(sv);
684     /* sv_setpv retains old UTF8ness [perl #24846] */
685     SvUTF8_off(sv);
686
687     if (TAINTING_get && SvMAGICAL(sv))
688         SvTAINTED_off(sv);
689
690     if (items-- > 0) {
691         if (*mark)
692             sv_catsv(sv, *mark);
693         mark++;
694     }
695
696     if (delimlen) {
697         const U32 delimflag = DO_UTF8(delim) ? SV_CATUTF8 : SV_CATBYTES;
698         for (; items > 0; items--,mark++) {
699             STRLEN len;
700             const char *s;
701             sv_catpvn_flags(sv,delims,delimlen,delimflag);
702             s = SvPV_const(*mark,len);
703             sv_catpvn_flags(sv,s,len,
704                             DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
705         }
706     }
707     else {
708         for (; items > 0; items--,mark++)
709         {
710             STRLEN len;
711             const char *s = SvPV_const(*mark,len);
712             sv_catpvn_flags(sv,s,len,
713                             DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
714         }
715     }
716     SvSETMAGIC(sv);
717 }
718
719 void
720 Perl_do_sprintf(pTHX_ SV *sv, SSize_t len, SV **sarg)
721 {
722     STRLEN patlen;
723     const char * const pat = SvPV_const(*sarg, patlen);
724     bool do_taint = FALSE;
725
726     PERL_ARGS_ASSERT_DO_SPRINTF;
727     assert(len >= 1);
728
729     if (SvTAINTED(*sarg))
730         TAINT_PROPER(
731                 (PL_op && PL_op->op_type < OP_max)
732                     ? (PL_op->op_type == OP_PRTF)
733                         ? "printf"
734                         : PL_op_name[PL_op->op_type]
735                     : "(unknown)"
736         );
737     SvUTF8_off(sv);
738     if (DO_UTF8(*sarg))
739         SvUTF8_on(sv);
740     sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, (Size_t)(len - 1), &do_taint);
741     SvSETMAGIC(sv);
742     if (do_taint)
743         SvTAINTED_on(sv);
744 }
745
746 /* currently converts input to bytes if possible, but doesn't sweat failure */
747 UV
748 Perl_do_vecget(pTHX_ SV *sv, STRLEN offset, int size)
749 {
750     STRLEN srclen, len, avail, uoffset, bitoffs = 0;
751     const I32 svpv_flags = ((PL_op->op_flags & OPf_MOD || LVRET)
752                                           ? SV_UNDEF_RETURNS_NULL : 0);
753     unsigned char *s = (unsigned char *)
754                             SvPV_flags(sv, srclen, (svpv_flags|SV_GMAGIC));
755     UV retnum = 0;
756
757     if (!s) {
758       s = (unsigned char *)"";
759     }
760     
761     PERL_ARGS_ASSERT_DO_VECGET;
762
763     if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
764         Perl_croak(aTHX_ "Illegal number of bits in vec");
765
766     if (SvUTF8(sv)) {
767         if (Perl_sv_utf8_downgrade(aTHX_ sv, TRUE)) {
768             /* PVX may have changed */
769             s = (unsigned char *) SvPV_flags(sv, srclen, svpv_flags);
770         }
771         else {
772             Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
773                                 "Use of strings with code points over 0xFF as"
774                                 " arguments to vec is deprecated. This will"
775                                 " be a fatal error in Perl 5.32");
776         }
777     }
778
779     if (size < 8) {
780         bitoffs = ((offset%8)*size)%8;
781         uoffset = offset/(8/size);
782     }
783     else if (size > 8) {
784         int n = size/8;
785         if (offset > Size_t_MAX / n - 1) /* would overflow */
786             return 0;
787         uoffset = offset*n;
788     }
789     else
790         uoffset = offset;
791
792     if (uoffset >= srclen)
793         return 0;
794
795     len   = (bitoffs + size + 7)/8; /* required number of bytes */
796     avail = srclen - uoffset;       /* available number of bytes */
797
798     /* Does the byte range overlap the end of the string? If so,
799      * handle specially. */
800     if (avail < len) {
801         if (size <= 8)
802             retnum = 0;
803         else {
804             if (size == 16) {
805                 assert(avail == 1);
806                 retnum = (UV) s[uoffset] <<  8;
807             }
808             else if (size == 32) {
809                 assert(avail >= 1 && avail <= 3);
810                 if (avail == 1)
811                     retnum =
812                         ((UV) s[uoffset    ] << 24);
813                 else if (avail == 2)
814                     retnum =
815                         ((UV) s[uoffset    ] << 24) +
816                         ((UV) s[uoffset + 1] << 16);
817                 else
818                     retnum =
819                         ((UV) s[uoffset    ] << 24) +
820                         ((UV) s[uoffset + 1] << 16) +
821                         (     s[uoffset + 2] <<  8);
822             }
823 #ifdef UV_IS_QUAD
824             else if (size == 64) {
825                 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
826                                "Bit vector size > 32 non-portable");
827                 assert(avail >= 1 && avail <= 7);
828                 if (avail == 1)
829                     retnum =
830                         (UV) s[uoffset     ] << 56;
831                 else if (avail == 2)
832                     retnum =
833                         ((UV) s[uoffset    ] << 56) +
834                         ((UV) s[uoffset + 1] << 48);
835                 else if (avail == 3)
836                     retnum =
837                         ((UV) s[uoffset    ] << 56) +
838                         ((UV) s[uoffset + 1] << 48) +
839                         ((UV) s[uoffset + 2] << 40);
840                 else if (avail == 4)
841                     retnum =
842                         ((UV) s[uoffset    ] << 56) +
843                         ((UV) s[uoffset + 1] << 48) +
844                         ((UV) s[uoffset + 2] << 40) +
845                         ((UV) s[uoffset + 3] << 32);
846                 else if (avail == 5)
847                     retnum =
848                         ((UV) s[uoffset    ] << 56) +
849                         ((UV) s[uoffset + 1] << 48) +
850                         ((UV) s[uoffset + 2] << 40) +
851                         ((UV) s[uoffset + 3] << 32) +
852                         ((UV) s[uoffset + 4] << 24);
853                 else if (avail == 6)
854                     retnum =
855                         ((UV) s[uoffset    ] << 56) +
856                         ((UV) s[uoffset + 1] << 48) +
857                         ((UV) s[uoffset + 2] << 40) +
858                         ((UV) s[uoffset + 3] << 32) +
859                         ((UV) s[uoffset + 4] << 24) +
860                         ((UV) s[uoffset + 5] << 16);
861                 else
862                     retnum =
863                         ((UV) s[uoffset    ] << 56) +
864                         ((UV) s[uoffset + 1] << 48) +
865                         ((UV) s[uoffset + 2] << 40) +
866                         ((UV) s[uoffset + 3] << 32) +
867                         ((UV) s[uoffset + 4] << 24) +
868                         ((UV) s[uoffset + 5] << 16) +
869                         ((UV) s[uoffset + 6] <<  8);
870             }
871 #endif
872         }
873     }
874     else if (size < 8)
875         retnum = (s[uoffset] >> bitoffs) & ((1 << size) - 1);
876     else {
877         if (size == 8)
878             retnum = s[uoffset];
879         else if (size == 16)
880             retnum =
881                 ((UV) s[uoffset] <<      8) +
882                       s[uoffset + 1];
883         else if (size == 32)
884             retnum =
885                 ((UV) s[uoffset    ] << 24) +
886                 ((UV) s[uoffset + 1] << 16) +
887                 (     s[uoffset + 2] <<  8) +
888                       s[uoffset + 3];
889 #ifdef UV_IS_QUAD
890         else if (size == 64) {
891             Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
892                            "Bit vector size > 32 non-portable");
893             retnum =
894                 ((UV) s[uoffset    ] << 56) +
895                 ((UV) s[uoffset + 1] << 48) +
896                 ((UV) s[uoffset + 2] << 40) +
897                 ((UV) s[uoffset + 3] << 32) +
898                 ((UV) s[uoffset + 4] << 24) +
899                 ((UV) s[uoffset + 5] << 16) +
900                 (     s[uoffset + 6] <<  8) +
901                       s[uoffset + 7];
902         }
903 #endif
904     }
905
906     return retnum;
907 }
908
909 /* currently converts input to bytes if possible but doesn't sweat failures,
910  * although it does ensure that the string it clobbers is not marked as
911  * utf8-valid any more
912  */
913 void
914 Perl_do_vecset(pTHX_ SV *sv)
915 {
916     STRLEN offset, bitoffs = 0;
917     int size;
918     unsigned char *s;
919     UV lval;
920     I32 mask;
921     STRLEN targlen;
922     STRLEN len;
923     SV * const targ = LvTARG(sv);
924     char errflags = LvFLAGS(sv);
925
926     PERL_ARGS_ASSERT_DO_VECSET;
927
928     /* some out-of-range errors have been deferred if/until the LV is
929      * actually written to: f(vec($s,-1,8)) is not always fatal */
930     if (errflags) {
931         assert(!(errflags & ~(LVf_NEG_OFF|LVf_OUT_OF_RANGE)));
932         if (errflags & LVf_NEG_OFF)
933             Perl_croak_nocontext("Negative offset to vec in lvalue context");
934         Perl_croak_nocontext("Out of memory!");
935     }
936
937     if (!targ)
938         return;
939     s = (unsigned char*)SvPV_force_flags(targ, targlen,
940                                          SV_GMAGIC | SV_UNDEF_RETURNS_NULL);
941     if (SvUTF8(targ)) {
942         /* This is handled by the SvPOK_only below...
943         if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
944             SvUTF8_off(targ);
945          */
946         (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
947     }
948
949     (void)SvPOK_only(targ);
950     lval = SvUV(sv);
951     offset = LvTARGOFF(sv);
952     size = LvTARGLEN(sv);
953
954     if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
955         Perl_croak(aTHX_ "Illegal number of bits in vec");
956
957     if (size < 8) {
958         bitoffs = ((offset%8)*size)%8;
959         offset /= 8/size;
960     }
961     else if (size > 8) {
962         int n = size/8;
963         if (offset > Size_t_MAX / n - 1) /* would overflow */
964             Perl_croak_nocontext("Out of memory!");
965         offset *= n;
966     }
967
968     len = (bitoffs + size + 7)/8;       /* required number of bytes */
969     if (targlen < offset || targlen - offset < len) {
970         STRLEN newlen = offset > Size_t_MAX - len - 1 ? /* avoid overflow */
971                                         Size_t_MAX : offset + len + 1;
972         s = (unsigned char*)SvGROW(targ, newlen);
973         (void)memzero((char *)(s + targlen), newlen - targlen);
974         SvCUR_set(targ, newlen - 1);
975     }
976
977     if (size < 8) {
978         mask = (1 << size) - 1;
979         lval &= mask;
980         s[offset] &= ~(mask << bitoffs);
981         s[offset] |= lval << bitoffs;
982     }
983     else {
984         if (size == 8)
985             s[offset  ] = (U8)( lval        & 0xff);
986         else if (size == 16) {
987             s[offset  ] = (U8)((lval >>  8) & 0xff);
988             s[offset+1] = (U8)( lval        & 0xff);
989         }
990         else if (size == 32) {
991             s[offset  ] = (U8)((lval >> 24) & 0xff);
992             s[offset+1] = (U8)((lval >> 16) & 0xff);
993             s[offset+2] = (U8)((lval >>  8) & 0xff);
994             s[offset+3] = (U8)( lval        & 0xff);
995         }
996 #ifdef UV_IS_QUAD
997         else if (size == 64) {
998             Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
999                            "Bit vector size > 32 non-portable");
1000             s[offset  ] = (U8)((lval >> 56) & 0xff);
1001             s[offset+1] = (U8)((lval >> 48) & 0xff);
1002             s[offset+2] = (U8)((lval >> 40) & 0xff);
1003             s[offset+3] = (U8)((lval >> 32) & 0xff);
1004             s[offset+4] = (U8)((lval >> 24) & 0xff);
1005             s[offset+5] = (U8)((lval >> 16) & 0xff);
1006             s[offset+6] = (U8)((lval >>  8) & 0xff);
1007             s[offset+7] = (U8)( lval        & 0xff);
1008         }
1009 #endif
1010     }
1011     SvSETMAGIC(targ);
1012 }
1013
1014 void
1015 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1016 {
1017 #ifdef LIBERAL
1018     long *dl;
1019     long *ll;
1020     long *rl;
1021 #endif
1022     char *dc;
1023     STRLEN leftlen;
1024     STRLEN rightlen;
1025     const char *lc;
1026     const char *rc;
1027     STRLEN len;
1028     STRLEN lensave;
1029     const char *lsave;
1030     const char *rsave;
1031     STRLEN needlen = 0;
1032     bool result_needs_to_be_utf8 = FALSE;
1033     bool left_utf8 = FALSE;
1034     bool right_utf8 = FALSE;
1035     U8 * left_non_downgraded = NULL;
1036     U8 * right_non_downgraded = NULL;
1037     Size_t left_non_downgraded_len = 0;
1038     Size_t right_non_downgraded_len = 0;
1039     char * non_downgraded = NULL;
1040     Size_t non_downgraded_len = 0;
1041
1042     PERL_ARGS_ASSERT_DO_VOP;
1043
1044     if (sv != left || (optype != OP_BIT_AND && !SvOK(sv)))
1045         SvPVCLEAR(sv);        /* avoid undef warning on |= and ^= */
1046     if (sv == left) {
1047         lc = SvPV_force_nomg(left, leftlen);
1048     }
1049     else {
1050         lc = SvPV_nomg_const(left, leftlen);
1051         SvPV_force_nomg_nolen(sv);
1052     }
1053     rc = SvPV_nomg_const(right, rightlen);
1054
1055     /* This needs to come after SvPV to ensure that string overloading has
1056        fired off.  */
1057
1058     /* Create downgraded temporaries of any UTF-8 encoded operands */
1059     if (DO_UTF8(left)) {
1060         const U8 * save_lc = (U8 *) lc;
1061
1062         left_utf8 = TRUE;
1063         result_needs_to_be_utf8 = TRUE;
1064
1065         left_non_downgraded_len = leftlen;
1066         lc = (char *) bytes_from_utf8_loc((const U8 *) lc, &leftlen,
1067                                           &left_utf8,
1068                                           (const U8 **) &left_non_downgraded);
1069         /* Calculate the number of trailing unconvertible bytes.  This quantity
1070          * is the original length minus the length of the converted portion. */
1071         left_non_downgraded_len -= left_non_downgraded - save_lc;
1072         SAVEFREEPV(lc);
1073     }
1074     if (DO_UTF8(right)) {
1075         const U8 * save_rc = (U8 *) rc;
1076
1077         right_utf8 = TRUE;
1078         result_needs_to_be_utf8 = TRUE;
1079
1080         right_non_downgraded_len = rightlen;
1081         rc = (char *) bytes_from_utf8_loc((const U8 *) rc, &rightlen,
1082                                           &right_utf8,
1083                                           (const U8 **) &right_non_downgraded);
1084         right_non_downgraded_len -= right_non_downgraded - save_rc;
1085         SAVEFREEPV(rc);
1086     }
1087
1088     /* We set 'len' to the length that the operation actually operates on.  The
1089      * dangling part of the longer operand doesn't actually participate in the
1090      * operation.  What happens is that we pretend that the shorter operand has
1091      * been extended to the right by enough imaginary zeros to match the length
1092      * of the longer one.  But we know in advance the result of the operation
1093      * on zeros without having to do it.  In the case of '&', the result is
1094      * zero, and the dangling portion is simply discarded.  For '|' and '^', the
1095      * result is the same as the other operand, so the dangling part is just
1096      * appended to the final result, unchanged.  We currently accept above-FF
1097      * code points in the dangling portion, as that's how it has long worked,
1098      * and code depends on it staying that way.  But it is now fatal for
1099      * above-FF to appear in the portion that does get operated on.  Hence, any
1100      * above-FF must come only in the longer operand, and only in its dangling
1101      * portion.  That means that at least one of the operands has to be
1102      * entirely non-UTF-8, and the length of that operand has to be before the
1103      * first above-FF in the other */
1104     if (left_utf8) {
1105         if (right_utf8 || rightlen > leftlen) {
1106             Perl_croak(aTHX_ fatal_above_ff_msg, PL_op_desc[optype]);
1107         }
1108         len = rightlen;
1109     }
1110     else if (right_utf8) {
1111         if (leftlen > rightlen) {
1112             Perl_croak(aTHX_ fatal_above_ff_msg, PL_op_desc[optype]);
1113         }
1114         len = leftlen;
1115     }
1116     else {  /* Neither is UTF-8 */
1117         len = leftlen < rightlen ? leftlen : rightlen;
1118     }
1119
1120     lensave = len;
1121     lsave = lc;
1122     rsave = rc;
1123
1124     SvCUR_set(sv, len);
1125     (void)SvPOK_only(sv);
1126     if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1127         dc = SvPV_force_nomg_nolen(sv);
1128         if (SvLEN(sv) < len + 1) {
1129             dc = SvGROW(sv, len + 1);
1130             (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1131         }
1132     }
1133     else {
1134         needlen = optype == OP_BIT_AND
1135                     ? len : (leftlen > rightlen ? leftlen : rightlen);
1136         Newxz(dc, needlen + 1, char);
1137         sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
1138         dc = SvPVX(sv);         /* sv_usepvn() calls Renew() */
1139     }
1140
1141 #ifdef LIBERAL
1142     if (len >= sizeof(long)*4 &&
1143         !((unsigned long)dc % sizeof(long)) &&
1144         !((unsigned long)lc % sizeof(long)) &&
1145         !((unsigned long)rc % sizeof(long)))    /* It's almost always aligned... */
1146     {
1147         const STRLEN remainder = len % (sizeof(long)*4);
1148         len /= (sizeof(long)*4);
1149
1150         dl = (long*)dc;
1151         ll = (long*)lc;
1152         rl = (long*)rc;
1153
1154         switch (optype) {
1155         case OP_BIT_AND:
1156             while (len--) {
1157                 *dl++ = *ll++ & *rl++;
1158                 *dl++ = *ll++ & *rl++;
1159                 *dl++ = *ll++ & *rl++;
1160                 *dl++ = *ll++ & *rl++;
1161             }
1162             break;
1163         case OP_BIT_XOR:
1164             while (len--) {
1165                 *dl++ = *ll++ ^ *rl++;
1166                 *dl++ = *ll++ ^ *rl++;
1167                 *dl++ = *ll++ ^ *rl++;
1168                 *dl++ = *ll++ ^ *rl++;
1169             }
1170             break;
1171         case OP_BIT_OR:
1172             while (len--) {
1173                 *dl++ = *ll++ | *rl++;
1174                 *dl++ = *ll++ | *rl++;
1175                 *dl++ = *ll++ | *rl++;
1176                 *dl++ = *ll++ | *rl++;
1177             }
1178         }
1179
1180         dc = (char*)dl;
1181         lc = (char*)ll;
1182         rc = (char*)rl;
1183
1184         len = remainder;
1185     }
1186 #endif
1187     switch (optype) {
1188     case OP_BIT_AND:
1189         while (len--)
1190             *dc++ = *lc++ & *rc++;
1191         *dc = '\0';
1192         break;
1193     case OP_BIT_XOR:
1194         while (len--)
1195             *dc++ = *lc++ ^ *rc++;
1196         goto mop_up;
1197     case OP_BIT_OR:
1198         while (len--)
1199             *dc++ = *lc++ | *rc++;
1200       mop_up:
1201         len = lensave;
1202         if (rightlen > len) {
1203             if (dc == rc)
1204                 SvCUR(sv) = rightlen;
1205             else
1206                 sv_catpvn_nomg(sv, rsave + len, rightlen - len);
1207         }
1208         else if (leftlen > len) {
1209             if (dc == lc)
1210                 SvCUR(sv) = leftlen;
1211             else
1212                 sv_catpvn_nomg(sv, lsave + len, leftlen - len);
1213         }
1214         *SvEND(sv) = '\0';
1215
1216         /* If there is trailing stuff that couldn't be converted from UTF-8, it
1217          * is appended as-is for the ^ and | operators.  This preserves
1218          * backwards compatibility */
1219         if (right_non_downgraded) {
1220             non_downgraded = (char *) right_non_downgraded;
1221             non_downgraded_len = right_non_downgraded_len;
1222         }
1223         else if (left_non_downgraded) {
1224             non_downgraded = (char *) left_non_downgraded;
1225             non_downgraded_len = left_non_downgraded_len;
1226         }
1227
1228         break;
1229     }
1230
1231     if (result_needs_to_be_utf8) {
1232         sv_utf8_upgrade_nomg(sv);
1233
1234         /* Append any trailing UTF-8 as-is. */
1235         if (non_downgraded) {
1236             sv_catpvn_nomg(sv, non_downgraded, non_downgraded_len);
1237         }
1238     }
1239
1240     SvTAINT(sv);
1241 }
1242
1243
1244 /* Perl_do_kv() may be:
1245  *  * called directly as the pp function for pp_keys() and pp_values();
1246  *  * It may also be called directly when the op is OP_AVHVSWITCH, to
1247  *       implement CORE::keys(), CORE::values().
1248  *
1249  * In all cases it expects an HV on the stack and returns a list of keys,
1250  * values, or key-value pairs, depending on PL_op.
1251  */
1252
1253 OP *
1254 Perl_do_kv(pTHX)
1255 {
1256     dSP;
1257     HV * const keys = MUTABLE_HV(POPs);
1258     const U8 gimme = GIMME_V;
1259
1260     const I32 dokeys   =     (PL_op->op_type == OP_KEYS)
1261                           || (    PL_op->op_type == OP_AVHVSWITCH
1262                               && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1263                                     + OP_EACH == OP_KEYS);
1264
1265     const I32 dovalues =     (PL_op->op_type == OP_VALUES)
1266                           || (    PL_op->op_type == OP_AVHVSWITCH
1267                               && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1268                                      + OP_EACH == OP_VALUES);
1269
1270     assert(   PL_op->op_type == OP_KEYS
1271            || PL_op->op_type == OP_VALUES
1272            || PL_op->op_type == OP_AVHVSWITCH);
1273
1274     assert(!(    PL_op->op_type == OP_VALUES
1275              && (PL_op->op_private & OPpMAYBE_LVSUB)));
1276
1277     (void)hv_iterinit(keys);    /* always reset iterator regardless */
1278
1279     if (gimme == G_VOID)
1280         RETURN;
1281
1282     if (gimme == G_SCALAR) {
1283         if (PL_op->op_flags & OPf_MOD || LVRET) {       /* lvalue */
1284             SV * const ret = sv_2mortal(newSV_type(SVt_PVLV));  /* Not TARG RT#67838 */
1285             sv_magic(ret, NULL, PERL_MAGIC_nkeys, NULL, 0);
1286             LvTYPE(ret) = 'k';
1287             LvTARG(ret) = SvREFCNT_inc_simple(keys);
1288             PUSHs(ret);
1289         }
1290         else {
1291             IV i;
1292             dTARGET;
1293
1294             /* note that in 'scalar(keys %h)' the OP_KEYS is usually
1295              * optimised away and the action is performed directly by the
1296              * padhv or rv2hv op. We now only get here via OP_AVHVSWITCH
1297              * and \&CORE::keys
1298              */
1299             if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) ) {
1300                 i = HvUSEDKEYS(keys);
1301             }
1302             else {
1303                 i = 0;
1304                 while (hv_iternext(keys)) i++;
1305             }
1306             PUSHi( i );
1307         }
1308         RETURN;
1309     }
1310
1311     if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
1312         const I32 flags = is_lvalue_sub();
1313         if (flags && !(flags & OPpENTERSUB_INARGS))
1314             /* diag_listed_as: Can't modify %s in %s */
1315             Perl_croak(aTHX_ "Can't modify keys in list assignment");
1316     }
1317
1318     PUTBACK;
1319     hv_pushkv(keys, (dokeys | (dovalues << 1)));
1320     return NORMAL;
1321 }
1322
1323 /*
1324  * ex: set ts=8 sts=4 sw=4 et:
1325  */