3 * Copyright (C) 2012 by Larry Wall and others
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.
8 * This file is a home for static inline functions that cannot go in other
9 * headers files, because they depend on proto.h (included after most other
10 * headers) or struct definitions.
12 * Each section names the header file that the functions "belong" to.
15 /* ------------------------------- av.h ------------------------------- */
17 PERL_STATIC_INLINE SSize_t
18 S_av_top_index(pTHX_ AV *av)
20 PERL_ARGS_ASSERT_AV_TOP_INDEX;
21 assert(SvTYPE(av) == SVt_PVAV);
26 /* ------------------------------- cv.h ------------------------------- */
28 PERL_STATIC_INLINE GV *
32 ? Perl_cvgv_from_hek(aTHX_ sv)
33 : ((XPVCV*)MUTABLE_PTR(SvANY(sv)))->xcv_gv_u.xcv_gv;
36 PERL_STATIC_INLINE I32 *
37 S_CvDEPTHp(const CV * const sv)
39 assert(SvTYPE(sv) == SVt_PVCV || SvTYPE(sv) == SVt_PVFM);
40 return &((XPVCV*)SvANY(sv))->xcv_depth;
44 CvPROTO returns the prototype as stored, which is not necessarily what
45 the interpreter should be using. Specifically, the interpreter assumes
46 that spaces have been stripped, which has been the case if the prototype
47 was added by toke.c, but is generally not the case if it was added elsewhere.
48 Since we can't enforce the spacelessness at assignment time, this routine
49 provides a temporary copy at parse time with spaces removed.
50 I<orig> is the start of the original buffer, I<len> is the length of the
51 prototype and will be updated when this returns.
55 PERL_STATIC_INLINE char *
56 S_strip_spaces(pTHX_ const char * orig, STRLEN * const len)
60 tmpsv = newSVpvn_flags(orig, *len, SVs_TEMP);
68 *len = tmps - SvPVX(tmpsv);
73 /* ------------------------------- mg.h ------------------------------- */
75 #if defined(PERL_CORE) || defined(PERL_EXT)
76 /* assumes get-magic and stringification have already occurred */
77 PERL_STATIC_INLINE STRLEN
78 S_MgBYTEPOS(pTHX_ MAGIC *mg, SV *sv, const char *s, STRLEN len)
80 assert(mg->mg_type == PERL_MAGIC_regex_global);
81 assert(mg->mg_len != -1);
82 if (mg->mg_flags & MGf_BYTES || !DO_UTF8(sv))
83 return (STRLEN)mg->mg_len;
85 const STRLEN pos = (STRLEN)mg->mg_len;
86 /* Without this check, we may read past the end of the buffer: */
87 if (pos > sv_or_pv_len_utf8(sv, s, len)) return len+1;
88 return sv_or_pv_pos_u2b(sv, s, pos, NULL);
93 /* ------------------------------- pad.h ------------------------------ */
95 #if defined(PERL_IN_PAD_C) || defined(PERL_IN_OP_C)
96 PERL_STATIC_INLINE bool
97 PadnameIN_SCOPE(const PADNAME * const pn, const U32 seq)
99 /* is seq within the range _LOW to _HIGH ?
100 * This is complicated by the fact that PL_cop_seqmax
101 * may have wrapped around at some point */
102 if (COP_SEQ_RANGE_LOW(pn) == PERL_PADSEQ_INTRO)
103 return FALSE; /* not yet introduced */
105 if (COP_SEQ_RANGE_HIGH(pn) == PERL_PADSEQ_INTRO) {
106 /* in compiling scope */
108 (seq > COP_SEQ_RANGE_LOW(pn))
109 ? (seq - COP_SEQ_RANGE_LOW(pn) < (U32_MAX >> 1))
110 : (COP_SEQ_RANGE_LOW(pn) - seq > (U32_MAX >> 1))
115 (COP_SEQ_RANGE_LOW(pn) > COP_SEQ_RANGE_HIGH(pn))
117 ( seq > COP_SEQ_RANGE_LOW(pn)
118 || seq <= COP_SEQ_RANGE_HIGH(pn))
120 : ( seq > COP_SEQ_RANGE_LOW(pn)
121 && seq <= COP_SEQ_RANGE_HIGH(pn))
128 /* ------------------------------- pp.h ------------------------------- */
130 PERL_STATIC_INLINE I32
133 DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
134 "MARK top %p %" IVdf "\n",
136 (IV)*PL_markstack_ptr)));
137 return *PL_markstack_ptr;
140 PERL_STATIC_INLINE I32
143 DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
144 "MARK pop %p %" IVdf "\n",
145 (PL_markstack_ptr-1),
146 (IV)*(PL_markstack_ptr-1))));
147 assert((PL_markstack_ptr > PL_markstack) || !"MARK underflow");
148 return *PL_markstack_ptr--;
151 /* ----------------------------- regexp.h ----------------------------- */
153 PERL_STATIC_INLINE struct regexp *
154 S_ReANY(const REGEXP * const re)
156 XPV* const p = (XPV*)SvANY(re);
157 assert(isREGEXP(re));
158 return SvTYPE(re) == SVt_PVLV ? p->xpv_len_u.xpvlenu_rx
159 : (struct regexp *)p;
162 /* ------------------------------- sv.h ------------------------------- */
164 PERL_STATIC_INLINE SV *
165 S_SvREFCNT_inc(SV *sv)
167 if (LIKELY(sv != NULL))
171 PERL_STATIC_INLINE SV *
172 S_SvREFCNT_inc_NN(SV *sv)
177 PERL_STATIC_INLINE void
178 S_SvREFCNT_inc_void(SV *sv)
180 if (LIKELY(sv != NULL))
183 PERL_STATIC_INLINE void
184 S_SvREFCNT_dec(pTHX_ SV *sv)
186 if (LIKELY(sv != NULL)) {
187 U32 rc = SvREFCNT(sv);
189 SvREFCNT(sv) = rc - 1;
191 Perl_sv_free2(aTHX_ sv, rc);
195 PERL_STATIC_INLINE void
196 S_SvREFCNT_dec_NN(pTHX_ SV *sv)
198 U32 rc = SvREFCNT(sv);
200 SvREFCNT(sv) = rc - 1;
202 Perl_sv_free2(aTHX_ sv, rc);
205 PERL_STATIC_INLINE void
209 if (SvOBJECT(SvRV(sv))) HvAMAGIC_on(SvSTASH(SvRV(sv)));
211 PERL_STATIC_INLINE void
214 if (SvROK(sv) && SvOBJECT(SvRV(sv)))
215 HvAMAGIC_off(SvSTASH(SvRV(sv)));
218 PERL_STATIC_INLINE U32
219 S_SvPADSTALE_on(SV *sv)
221 assert(!(SvFLAGS(sv) & SVs_PADTMP));
222 return SvFLAGS(sv) |= SVs_PADSTALE;
224 PERL_STATIC_INLINE U32
225 S_SvPADSTALE_off(SV *sv)
227 assert(!(SvFLAGS(sv) & SVs_PADTMP));
228 return SvFLAGS(sv) &= ~SVs_PADSTALE;
230 #if defined(PERL_CORE) || defined (PERL_EXT)
231 PERL_STATIC_INLINE STRLEN
232 S_sv_or_pv_pos_u2b(pTHX_ SV *sv, const char *pv, STRLEN pos, STRLEN *lenp)
234 PERL_ARGS_ASSERT_SV_OR_PV_POS_U2B;
236 U8 *hopped = utf8_hop((U8 *)pv, pos);
237 if (lenp) *lenp = (STRLEN)(utf8_hop(hopped, *lenp) - hopped);
238 return (STRLEN)(hopped - (U8 *)pv);
240 return sv_pos_u2b_flags(sv,pos,lenp,SV_CONST_RETURN);
244 /* ------------------------------- handy.h ------------------------------- */
246 /* saves machine code for a common noreturn idiom typically used in Newx*() */
247 #ifdef GCC_DIAG_PRAGMA
248 GCC_DIAG_IGNORE(-Wunused-function) /* Intentionally left semicolonless. */
251 S_croak_memory_wrap(void)
253 Perl_croak_nocontext("%s",PL_memory_wrap);
255 #ifdef GCC_DIAG_PRAGMA
256 GCC_DIAG_RESTORE /* Intentionally left semicolonless. */
259 /* ------------------------------- utf8.h ------------------------------- */
262 =head1 Unicode Support
265 PERL_STATIC_INLINE void
266 S_append_utf8_from_native_byte(const U8 byte, U8** dest)
268 /* Takes an input 'byte' (Latin1 or EBCDIC) and appends it to the UTF-8
269 * encoded string at '*dest', updating '*dest' to include it */
271 PERL_ARGS_ASSERT_APPEND_UTF8_FROM_NATIVE_BYTE;
273 if (NATIVE_BYTE_IS_INVARIANT(byte))
276 *((*dest)++) = UTF8_EIGHT_BIT_HI(byte);
277 *((*dest)++) = UTF8_EIGHT_BIT_LO(byte);
282 =for apidoc valid_utf8_to_uvchr
283 Like C<L</utf8_to_uvchr_buf>>, but should only be called when it is known that
284 the next character in the input UTF-8 string C<s> is well-formed (I<e.g.>,
285 it passes C<L</isUTF8_CHAR>>. Surrogates, non-character code points, and
286 non-Unicode code points are allowed.
292 PERL_STATIC_INLINE UV
293 Perl_valid_utf8_to_uvchr(const U8 *s, STRLEN *retlen)
295 const UV expectlen = UTF8SKIP(s);
296 const U8* send = s + expectlen;
299 PERL_ARGS_ASSERT_VALID_UTF8_TO_UVCHR;
305 /* An invariant is trivially returned */
306 if (expectlen == 1) {
310 /* Remove the leading bits that indicate the number of bytes, leaving just
311 * the bits that are part of the value */
312 uv = NATIVE_UTF8_TO_I8(uv) & UTF_START_MASK(expectlen);
314 /* Now, loop through the remaining bytes, accumulating each into the
315 * working total as we go. (I khw tried unrolling the loop for up to 4
316 * bytes, but there was no performance improvement) */
317 for (++s; s < send; s++) {
318 uv = UTF8_ACCUMULATE(uv, *s);
321 return UNI_TO_NATIVE(uv);
326 =for apidoc is_utf8_invariant_string
328 Returns TRUE if the first C<len> bytes of the string C<s> are the same
329 regardless of the UTF-8 encoding of the string (or UTF-EBCDIC encoding on
330 EBCDIC machines); otherwise it returns FALSE. That is, it returns TRUE if they
331 are UTF-8 invariant. On ASCII-ish machines, all the ASCII characters and only
332 the ASCII characters fit this definition. On EBCDIC machines, the ASCII-range
333 characters are invariant, but so also are the C1 controls.
335 If C<len> is 0, it will be calculated using C<strlen(s)>, (which means if you
336 use this option, that C<s> can't have embedded C<NUL> characters and has to
337 have a terminating C<NUL> byte).
340 C<L</is_utf8_string>>,
341 C<L</is_utf8_string_flags>>,
342 C<L</is_utf8_string_loc>>,
343 C<L</is_utf8_string_loc_flags>>,
344 C<L</is_utf8_string_loclen>>,
345 C<L</is_utf8_string_loclen_flags>>,
346 C<L</is_utf8_fixed_width_buf_flags>>,
347 C<L</is_utf8_fixed_width_buf_loc_flags>>,
348 C<L</is_utf8_fixed_width_buf_loclen_flags>>,
349 C<L</is_strict_utf8_string>>,
350 C<L</is_strict_utf8_string_loc>>,
351 C<L</is_strict_utf8_string_loclen>>,
352 C<L</is_c9strict_utf8_string>>,
353 C<L</is_c9strict_utf8_string_loc>>,
355 C<L</is_c9strict_utf8_string_loclen>>.
361 #define is_utf8_invariant_string(s, len) \
362 is_utf8_invariant_string_loc(s, len, NULL)
365 =for apidoc is_utf8_invariant_string_loc
367 Like C<L</is_utf8_invariant_string>> but upon failure, stores the location of
368 the first UTF-8 variant character in the C<ep> pointer; if all characters are
369 UTF-8 invariant, this function does not change the contents of C<*ep>.
375 PERL_STATIC_INLINE bool
376 S_is_utf8_invariant_string_loc(const U8* const s, STRLEN len, const U8 ** ep)
381 PERL_ARGS_ASSERT_IS_UTF8_INVARIANT_STRING_LOC;
384 len = strlen((const char *)s);
391 /* This looks like 0x010101... */
392 #define PERL_COUNT_MULTIPLIER (~ (UINTMAX_C(0)) / 0xFF)
394 /* This looks like 0x808080... */
395 #define PERL_VARIANTS_WORD_MASK (PERL_COUNT_MULTIPLIER * 0x80)
396 #define PERL_WORDSIZE sizeof(PERL_COUNT_MULTIPLIER)
397 #define PERL_WORD_BOUNDARY_MASK (PERL_WORDSIZE - 1)
399 /* Evaluates to 0 if 'x' is at a word boundary; otherwise evaluates to 1, by
400 * or'ing together the lowest bits of 'x'. Hopefully the final term gets
401 * optimized out completely on a 32-bit system, and its mask gets optimized out
402 * on a 64-bit system */
403 #define PERL_IS_SUBWORD_ADDR(x) (1 & ( PTR2nat(x) \
404 | (PTR2nat(x) >> 1) \
405 | ( (PTR2nat(x) >> 2) \
406 & PERL_WORD_BOUNDARY_MASK)))
408 /* Do the word-at-a-time iff there is at least one usable full word. That
409 * means that after advancing to a word boundary, there still is at least a
410 * full word left. The number of bytes needed to advance is 'wordsize -
411 * offset' unless offset is 0. */
412 if ((STRLEN) (send - x) >= PERL_WORDSIZE
414 /* This term is wordsize if subword; 0 if not */
415 + PERL_WORDSIZE * PERL_IS_SUBWORD_ADDR(x)
418 - (PTR2nat(x) & PERL_WORD_BOUNDARY_MASK))
421 /* Process per-byte until reach word boundary. XXX This loop could be
422 * eliminated if we knew that this platform had fast unaligned reads */
423 while (PTR2nat(x) & PERL_WORD_BOUNDARY_MASK) {
424 if (! UTF8_IS_INVARIANT(*x)) {
434 /* Here, we know we have at least one full word to process. Process
435 * per-word as long as we have at least a full word left */
437 if ((* (PERL_UINTMAX_T *) x) & PERL_VARIANTS_WORD_MASK) {
439 /* Found a variant. Just return if caller doesn't want its
445 /* Otherwise fall into final loop to find which byte it is */
449 } while (x + PERL_WORDSIZE <= send);
454 /* Process per-byte */
456 if (! UTF8_IS_INVARIANT(*x)) {
470 #if defined(PERL_CORE) || defined(PERL_EXT)
473 =for apidoc variant_under_utf8_count
475 This function looks at the sequence of bytes between C<s> and C<e>, which are
476 assumed to be encoded in ASCII/Latin1, and returns how many of them would
477 change should the string be translated into UTF-8. Due to the nature of UTF-8,
478 each of these would occupy two bytes instead of the single one in the input
479 string. Thus, this function returns the precise number of bytes the string
480 would expand by when translated to UTF-8.
482 Unlike most of the other functions that have C<utf8> in their name, the input
483 to this function is NOT a UTF-8-encoded string. The function name is slightly
484 I<odd> to emphasize this.
486 This function is internal to Perl because khw thinks that any XS code that
487 would want this is probably operating too close to the internals. Presenting a
488 valid use case could change that.
491 C<L<perlapi/is_utf8_invariant_string>>
493 C<L<perlapi/is_utf8_invariant_string_loc>>,
499 PERL_STATIC_INLINE Size_t
500 S_variant_under_utf8_count(const U8* const s, const U8* const e)
505 PERL_ARGS_ASSERT_VARIANT_UNDER_UTF8_COUNT;
509 if ((STRLEN) (e - x) >= PERL_WORDSIZE
510 + PERL_WORDSIZE * PERL_IS_SUBWORD_ADDR(x)
511 - (PTR2nat(x) & PERL_WORD_BOUNDARY_MASK))
514 /* Process per-byte until reach word boundary. XXX This loop could be
515 * eliminated if we knew that this platform had fast unaligned reads */
516 while (PTR2nat(x) & PERL_WORD_BOUNDARY_MASK) {
517 count += ! UTF8_IS_INVARIANT(*x++);
520 /* Process per-word as long as we have at least a full word left */
523 /* It's easier to look at a 16-bit word size to see how this works.
524 * The expression would be:
526 * (((*x & 0x8080) >> 7) * 0x0101) >> 8;
528 * Suppose the value of *x is the 16 bits
532 * where the 14 bits represented by '_' could be any combination of
533 * 0's or 1's (we don't care), and 'y' is the high bit of one byte,
534 * and 'z' is the high bit for the other (endianness doesn't
535 * matter). On ASCII platforms a byte is variant if the high bit
536 * is set; invariant otherwise. Thus, our goal, the count of
537 * variants in this 2-byte word is
541 * To turn 0by_______z_______ into (y + z) we mask the intial value
542 * with 0x8080 to turn it into
546 * Then right shifting by 7 yields
550 * Viewed as a number, this is
554 * We then multiply by 0x0101 (which is = 2**8 + 1), so
556 * (2**8 * y + z) * (2**8 + 1)
557 * = (2**8 * y * 2**8) + (z * 2**8) + (2**8 * y * 1) + (z * 1)
558 * = (2**16 * y) + (2**8 * (y + z)) + z
560 * However (2**16 * y) doesn't fit in a 16-bit word (unless 'y' is
561 * zero in which case it is 0), and since this is unsigned
562 * multiplication, the C standard says that this component just
563 * gets ignored, so we are left with
565 * = 2**8 * (y + z) + z
567 * We then shift right by 8 bits, which divides by 2**8, and gets
568 * rid of the lone 'z', leaving us with
572 * The same principles apply for longer word sizes. For 32 bit
573 * words we end up with
575 * = 2**24 * (w + x + y + z) + (lots of other expressions
578 * with anything above 2**24 having overflowed and been chopped
579 * off. Shifting right by 24 yields (w + x + y + z)
582 count += ((((* (PERL_UINTMAX_T *) x) & PERL_VARIANTS_WORD_MASK) >> 7)
583 * PERL_COUNT_MULTIPLIER)
584 >> ((PERL_WORDSIZE - 1) * CHARBITS);
586 } while (x + PERL_WORDSIZE <= e);
591 /* Process per-byte */
593 if (! UTF8_IS_INVARIANT(*x)) {
606 #undef PERL_COUNT_MULTIPLIER
607 #undef PERL_WORD_BOUNDARY_MASK
608 #undef PERL_VARIANTS_WORD_MASK
611 =for apidoc is_utf8_string
613 Returns TRUE if the first C<len> bytes of string C<s> form a valid
614 Perl-extended-UTF-8 string; returns FALSE otherwise. If C<len> is 0, it will
615 be calculated using C<strlen(s)> (which means if you use this option, that C<s>
616 can't have embedded C<NUL> characters and has to have a terminating C<NUL>
617 byte). Note that all characters being ASCII constitute 'a valid UTF-8 string'.
619 This function considers Perl's extended UTF-8 to be valid. That means that
620 code points above Unicode, surrogates, and non-character code points are
621 considered valid by this function. Use C<L</is_strict_utf8_string>>,
622 C<L</is_c9strict_utf8_string>>, or C<L</is_utf8_string_flags>> to restrict what
623 code points are considered valid.
626 C<L</is_utf8_invariant_string>>,
627 C<L</is_utf8_invariant_string_loc>>,
628 C<L</is_utf8_string_loc>>,
629 C<L</is_utf8_string_loclen>>,
630 C<L</is_utf8_fixed_width_buf_flags>>,
631 C<L</is_utf8_fixed_width_buf_loc_flags>>,
632 C<L</is_utf8_fixed_width_buf_loclen_flags>>,
637 #define is_utf8_string(s, len) is_utf8_string_loclen(s, len, NULL, NULL)
639 #if defined(PERL_CORE) || defined (PERL_EXT)
642 =for apidoc is_utf8_non_invariant_string
644 Returns TRUE if L<perlapi/is_utf8_invariant_string> returns FALSE for the first
645 C<len> bytes of the string C<s>, but they are, nonetheless, legal Perl-extended
646 UTF-8; otherwise returns FALSE.
648 A TRUE return means that at least one code point represented by the sequence
649 either is a wide character not representable as a single byte, or the
650 representation differs depending on whether the sequence is encoded in UTF-8 or
654 C<L<perlapi/is_utf8_invariant_string>>,
655 C<L<perlapi/is_utf8_string>>
659 This is commonly used to determine if a SV's UTF-8 flag should be turned on.
660 It needn't be if its string is entirely UTF-8 invariant, and it shouldn't be if
661 it otherwise contains invalid UTF-8.
663 It is an internal function because khw thinks that XS code shouldn't be working
664 at this low a level. A valid use case could change that.
668 PERL_STATIC_INLINE bool
669 S_is_utf8_non_invariant_string(const U8* const s, STRLEN len)
671 const U8 * first_variant;
673 PERL_ARGS_ASSERT_IS_UTF8_NON_INVARIANT_STRING;
675 if (is_utf8_invariant_string_loc(s, len, &first_variant)) {
679 return is_utf8_string(first_variant, len - (first_variant - s));
685 =for apidoc is_strict_utf8_string
687 Returns TRUE if the first C<len> bytes of string C<s> form a valid
688 UTF-8-encoded string that is fully interchangeable by any application using
689 Unicode rules; otherwise it returns FALSE. If C<len> is 0, it will be
690 calculated using C<strlen(s)> (which means if you use this option, that C<s>
691 can't have embedded C<NUL> characters and has to have a terminating C<NUL>
692 byte). Note that all characters being ASCII constitute 'a valid UTF-8 string'.
694 This function returns FALSE for strings containing any
695 code points above the Unicode max of 0x10FFFF, surrogate code points, or
696 non-character code points.
699 C<L</is_utf8_invariant_string>>,
700 C<L</is_utf8_invariant_string_loc>>,
701 C<L</is_utf8_string>>,
702 C<L</is_utf8_string_flags>>,
703 C<L</is_utf8_string_loc>>,
704 C<L</is_utf8_string_loc_flags>>,
705 C<L</is_utf8_string_loclen>>,
706 C<L</is_utf8_string_loclen_flags>>,
707 C<L</is_utf8_fixed_width_buf_flags>>,
708 C<L</is_utf8_fixed_width_buf_loc_flags>>,
709 C<L</is_utf8_fixed_width_buf_loclen_flags>>,
710 C<L</is_strict_utf8_string_loc>>,
711 C<L</is_strict_utf8_string_loclen>>,
712 C<L</is_c9strict_utf8_string>>,
713 C<L</is_c9strict_utf8_string_loc>>,
715 C<L</is_c9strict_utf8_string_loclen>>.
720 #define is_strict_utf8_string(s, len) is_strict_utf8_string_loclen(s, len, NULL, NULL)
723 =for apidoc is_c9strict_utf8_string
725 Returns TRUE if the first C<len> bytes of string C<s> form a valid
726 UTF-8-encoded string that conforms to
727 L<Unicode Corrigendum #9|http://www.unicode.org/versions/corrigendum9.html>;
728 otherwise it returns FALSE. If C<len> is 0, it will be calculated using
729 C<strlen(s)> (which means if you use this option, that C<s> can't have embedded
730 C<NUL> characters and has to have a terminating C<NUL> byte). Note that all
731 characters being ASCII constitute 'a valid UTF-8 string'.
733 This function returns FALSE for strings containing any code points above the
734 Unicode max of 0x10FFFF or surrogate code points, but accepts non-character
736 L<Corrigendum #9|http://www.unicode.org/versions/corrigendum9.html>.
739 C<L</is_utf8_invariant_string>>,
740 C<L</is_utf8_invariant_string_loc>>,
741 C<L</is_utf8_string>>,
742 C<L</is_utf8_string_flags>>,
743 C<L</is_utf8_string_loc>>,
744 C<L</is_utf8_string_loc_flags>>,
745 C<L</is_utf8_string_loclen>>,
746 C<L</is_utf8_string_loclen_flags>>,
747 C<L</is_utf8_fixed_width_buf_flags>>,
748 C<L</is_utf8_fixed_width_buf_loc_flags>>,
749 C<L</is_utf8_fixed_width_buf_loclen_flags>>,
750 C<L</is_strict_utf8_string>>,
751 C<L</is_strict_utf8_string_loc>>,
752 C<L</is_strict_utf8_string_loclen>>,
753 C<L</is_c9strict_utf8_string_loc>>,
755 C<L</is_c9strict_utf8_string_loclen>>.
760 #define is_c9strict_utf8_string(s, len) is_c9strict_utf8_string_loclen(s, len, NULL, 0)
763 =for apidoc is_utf8_string_flags
765 Returns TRUE if the first C<len> bytes of string C<s> form a valid
766 UTF-8 string, subject to the restrictions imposed by C<flags>;
767 returns FALSE otherwise. If C<len> is 0, it will be calculated
768 using C<strlen(s)> (which means if you use this option, that C<s> can't have
769 embedded C<NUL> characters and has to have a terminating C<NUL> byte). Note
770 that all characters being ASCII constitute 'a valid UTF-8 string'.
772 If C<flags> is 0, this gives the same results as C<L</is_utf8_string>>; if
773 C<flags> is C<UTF8_DISALLOW_ILLEGAL_INTERCHANGE>, this gives the same results
774 as C<L</is_strict_utf8_string>>; and if C<flags> is
775 C<UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE>, this gives the same results as
776 C<L</is_c9strict_utf8_string>>. Otherwise C<flags> may be any
777 combination of the C<UTF8_DISALLOW_I<foo>> flags understood by
778 C<L</utf8n_to_uvchr>>, with the same meanings.
781 C<L</is_utf8_invariant_string>>,
782 C<L</is_utf8_invariant_string_loc>>,
783 C<L</is_utf8_string>>,
784 C<L</is_utf8_string_loc>>,
785 C<L</is_utf8_string_loc_flags>>,
786 C<L</is_utf8_string_loclen>>,
787 C<L</is_utf8_string_loclen_flags>>,
788 C<L</is_utf8_fixed_width_buf_flags>>,
789 C<L</is_utf8_fixed_width_buf_loc_flags>>,
790 C<L</is_utf8_fixed_width_buf_loclen_flags>>,
791 C<L</is_strict_utf8_string>>,
792 C<L</is_strict_utf8_string_loc>>,
793 C<L</is_strict_utf8_string_loclen>>,
794 C<L</is_c9strict_utf8_string>>,
795 C<L</is_c9strict_utf8_string_loc>>,
797 C<L</is_c9strict_utf8_string_loclen>>.
802 PERL_STATIC_INLINE bool
803 S_is_utf8_string_flags(const U8 *s, STRLEN len, const U32 flags)
805 const U8 * first_variant;
807 PERL_ARGS_ASSERT_IS_UTF8_STRING_FLAGS;
808 assert(0 == (flags & ~(UTF8_DISALLOW_ILLEGAL_INTERCHANGE
809 |UTF8_DISALLOW_PERL_EXTENDED)));
812 len = strlen((const char *)s);
816 return is_utf8_string(s, len);
819 if ((flags & ~UTF8_DISALLOW_PERL_EXTENDED)
820 == UTF8_DISALLOW_ILLEGAL_INTERCHANGE)
822 return is_strict_utf8_string(s, len);
825 if ((flags & ~UTF8_DISALLOW_PERL_EXTENDED)
826 == UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE)
828 return is_c9strict_utf8_string(s, len);
831 if (! is_utf8_invariant_string_loc(s, len, &first_variant)) {
832 const U8* const send = s + len;
833 const U8* x = first_variant;
836 STRLEN cur_len = isUTF8_CHAR_flags(x, send, flags);
837 if (UNLIKELY(! cur_len)) {
849 =for apidoc is_utf8_string_loc
851 Like C<L</is_utf8_string>> but stores the location of the failure (in the
852 case of "utf8ness failure") or the location C<s>+C<len> (in the case of
853 "utf8ness success") in the C<ep> pointer.
855 See also C<L</is_utf8_string_loclen>>.
860 #define is_utf8_string_loc(s, len, ep) is_utf8_string_loclen(s, len, ep, 0)
864 =for apidoc is_utf8_string_loclen
866 Like C<L</is_utf8_string>> but stores the location of the failure (in the
867 case of "utf8ness failure") or the location C<s>+C<len> (in the case of
868 "utf8ness success") in the C<ep> pointer, and the number of UTF-8
869 encoded characters in the C<el> pointer.
871 See also C<L</is_utf8_string_loc>>.
876 PERL_STATIC_INLINE bool
877 Perl_is_utf8_string_loclen(const U8 *s, STRLEN len, const U8 **ep, STRLEN *el)
879 const U8 * first_variant;
881 PERL_ARGS_ASSERT_IS_UTF8_STRING_LOCLEN;
884 len = strlen((const char *) s);
887 if (is_utf8_invariant_string_loc(s, len, &first_variant)) {
899 const U8* const send = s + len;
900 const U8* x = first_variant;
901 STRLEN outlen = first_variant - s;
904 const STRLEN cur_len = isUTF8_CHAR(x, send);
905 if (UNLIKELY(! cur_len)) {
925 =for apidoc is_strict_utf8_string_loc
927 Like C<L</is_strict_utf8_string>> but stores the location of the failure (in the
928 case of "utf8ness failure") or the location C<s>+C<len> (in the case of
929 "utf8ness success") in the C<ep> pointer.
931 See also C<L</is_strict_utf8_string_loclen>>.
936 #define is_strict_utf8_string_loc(s, len, ep) \
937 is_strict_utf8_string_loclen(s, len, ep, 0)
941 =for apidoc is_strict_utf8_string_loclen
943 Like C<L</is_strict_utf8_string>> but stores the location of the failure (in the
944 case of "utf8ness failure") or the location C<s>+C<len> (in the case of
945 "utf8ness success") in the C<ep> pointer, and the number of UTF-8
946 encoded characters in the C<el> pointer.
948 See also C<L</is_strict_utf8_string_loc>>.
953 PERL_STATIC_INLINE bool
954 S_is_strict_utf8_string_loclen(const U8 *s, STRLEN len, const U8 **ep, STRLEN *el)
956 const U8 * first_variant;
958 PERL_ARGS_ASSERT_IS_STRICT_UTF8_STRING_LOCLEN;
961 len = strlen((const char *) s);
964 if (is_utf8_invariant_string_loc(s, len, &first_variant)) {
976 const U8* const send = s + len;
977 const U8* x = first_variant;
978 STRLEN outlen = first_variant - s;
981 const STRLEN cur_len = isSTRICT_UTF8_CHAR(x, send);
982 if (UNLIKELY(! cur_len)) {
1002 =for apidoc is_c9strict_utf8_string_loc
1004 Like C<L</is_c9strict_utf8_string>> but stores the location of the failure (in
1005 the case of "utf8ness failure") or the location C<s>+C<len> (in the case of
1006 "utf8ness success") in the C<ep> pointer.
1008 See also C<L</is_c9strict_utf8_string_loclen>>.
1013 #define is_c9strict_utf8_string_loc(s, len, ep) \
1014 is_c9strict_utf8_string_loclen(s, len, ep, 0)
1018 =for apidoc is_c9strict_utf8_string_loclen
1020 Like C<L</is_c9strict_utf8_string>> but stores the location of the failure (in
1021 the case of "utf8ness failure") or the location C<s>+C<len> (in the case of
1022 "utf8ness success") in the C<ep> pointer, and the number of UTF-8 encoded
1023 characters in the C<el> pointer.
1025 See also C<L</is_c9strict_utf8_string_loc>>.
1030 PERL_STATIC_INLINE bool
1031 S_is_c9strict_utf8_string_loclen(const U8 *s, STRLEN len, const U8 **ep, STRLEN *el)
1033 const U8 * first_variant;
1035 PERL_ARGS_ASSERT_IS_C9STRICT_UTF8_STRING_LOCLEN;
1038 len = strlen((const char *) s);
1041 if (is_utf8_invariant_string_loc(s, len, &first_variant)) {
1053 const U8* const send = s + len;
1054 const U8* x = first_variant;
1055 STRLEN outlen = first_variant - s;
1058 const STRLEN cur_len = isC9_STRICT_UTF8_CHAR(x, send);
1059 if (UNLIKELY(! cur_len)) {
1079 =for apidoc is_utf8_string_loc_flags
1081 Like C<L</is_utf8_string_flags>> but stores the location of the failure (in the
1082 case of "utf8ness failure") or the location C<s>+C<len> (in the case of
1083 "utf8ness success") in the C<ep> pointer.
1085 See also C<L</is_utf8_string_loclen_flags>>.
1090 #define is_utf8_string_loc_flags(s, len, ep, flags) \
1091 is_utf8_string_loclen_flags(s, len, ep, 0, flags)
1094 /* The above 3 actual functions could have been moved into the more general one
1095 * just below, and made #defines that call it with the right 'flags'. They are
1096 * currently kept separate to increase their chances of getting inlined */
1100 =for apidoc is_utf8_string_loclen_flags
1102 Like C<L</is_utf8_string_flags>> but stores the location of the failure (in the
1103 case of "utf8ness failure") or the location C<s>+C<len> (in the case of
1104 "utf8ness success") in the C<ep> pointer, and the number of UTF-8
1105 encoded characters in the C<el> pointer.
1107 See also C<L</is_utf8_string_loc_flags>>.
1112 PERL_STATIC_INLINE bool
1113 S_is_utf8_string_loclen_flags(const U8 *s, STRLEN len, const U8 **ep, STRLEN *el, const U32 flags)
1115 const U8 * first_variant;
1117 PERL_ARGS_ASSERT_IS_UTF8_STRING_LOCLEN_FLAGS;
1118 assert(0 == (flags & ~(UTF8_DISALLOW_ILLEGAL_INTERCHANGE
1119 |UTF8_DISALLOW_PERL_EXTENDED)));
1122 len = strlen((const char *) s);
1126 return is_utf8_string_loclen(s, len, ep, el);
1129 if ((flags & ~UTF8_DISALLOW_PERL_EXTENDED)
1130 == UTF8_DISALLOW_ILLEGAL_INTERCHANGE)
1132 return is_strict_utf8_string_loclen(s, len, ep, el);
1135 if ((flags & ~UTF8_DISALLOW_PERL_EXTENDED)
1136 == UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE)
1138 return is_c9strict_utf8_string_loclen(s, len, ep, el);
1141 if (is_utf8_invariant_string_loc(s, len, &first_variant)) {
1153 const U8* send = s + len;
1154 const U8* x = first_variant;
1155 STRLEN outlen = first_variant - s;
1158 const STRLEN cur_len = isUTF8_CHAR_flags(x, send, flags);
1159 if (UNLIKELY(! cur_len)) {
1178 =for apidoc utf8_distance
1180 Returns the number of UTF-8 characters between the UTF-8 pointers C<a>
1183 WARNING: use only if you *know* that the pointers point inside the
1189 PERL_STATIC_INLINE IV
1190 Perl_utf8_distance(pTHX_ const U8 *a, const U8 *b)
1192 PERL_ARGS_ASSERT_UTF8_DISTANCE;
1194 return (a < b) ? -1 * (IV) utf8_length(a, b) : (IV) utf8_length(b, a);
1198 =for apidoc utf8_hop
1200 Return the UTF-8 pointer C<s> displaced by C<off> characters, either
1201 forward or backward.
1203 WARNING: do not use the following unless you *know* C<off> is within
1204 the UTF-8 data pointed to by C<s> *and* that on entry C<s> is aligned
1205 on the first byte of character or just after the last byte of a character.
1210 PERL_STATIC_INLINE U8 *
1211 Perl_utf8_hop(const U8 *s, SSize_t off)
1213 PERL_ARGS_ASSERT_UTF8_HOP;
1215 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g
1216 * the bitops (especially ~) can create illegal UTF-8.
1217 * In other words: in Perl UTF-8 is not just for Unicode. */
1226 while (UTF8_IS_CONTINUATION(*s))
1230 GCC_DIAG_IGNORE(-Wcast-qual);
1236 =for apidoc utf8_hop_forward
1238 Return the UTF-8 pointer C<s> displaced by up to C<off> characters,
1241 C<off> must be non-negative.
1243 C<s> must be before or equal to C<end>.
1245 When moving forward it will not move beyond C<end>.
1247 Will not exceed this limit even if the string is not valid "UTF-8".
1252 PERL_STATIC_INLINE U8 *
1253 Perl_utf8_hop_forward(const U8 *s, SSize_t off, const U8 *end)
1255 PERL_ARGS_ASSERT_UTF8_HOP_FORWARD;
1257 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g
1258 * the bitops (especially ~) can create illegal UTF-8.
1259 * In other words: in Perl UTF-8 is not just for Unicode. */
1265 STRLEN skip = UTF8SKIP(s);
1266 if ((STRLEN)(end - s) <= skip) {
1267 GCC_DIAG_IGNORE(-Wcast-qual);
1274 GCC_DIAG_IGNORE(-Wcast-qual);
1280 =for apidoc utf8_hop_back
1282 Return the UTF-8 pointer C<s> displaced by up to C<off> characters,
1285 C<off> must be non-positive.
1287 C<s> must be after or equal to C<start>.
1289 When moving backward it will not move before C<start>.
1291 Will not exceed this limit even if the string is not valid "UTF-8".
1296 PERL_STATIC_INLINE U8 *
1297 Perl_utf8_hop_back(const U8 *s, SSize_t off, const U8 *start)
1299 PERL_ARGS_ASSERT_UTF8_HOP_BACK;
1301 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g
1302 * the bitops (especially ~) can create illegal UTF-8.
1303 * In other words: in Perl UTF-8 is not just for Unicode. */
1308 while (off++ && s > start) {
1310 while (UTF8_IS_CONTINUATION(*s) && s > start)
1314 GCC_DIAG_IGNORE(-Wcast-qual);
1320 =for apidoc utf8_hop_safe
1322 Return the UTF-8 pointer C<s> displaced by up to C<off> characters,
1323 either forward or backward.
1325 When moving backward it will not move before C<start>.
1327 When moving forward it will not move beyond C<end>.
1329 Will not exceed those limits even if the string is not valid "UTF-8".
1334 PERL_STATIC_INLINE U8 *
1335 Perl_utf8_hop_safe(const U8 *s, SSize_t off, const U8 *start, const U8 *end)
1337 PERL_ARGS_ASSERT_UTF8_HOP_SAFE;
1339 /* Note: cannot use UTF8_IS_...() too eagerly here since e.g
1340 * the bitops (especially ~) can create illegal UTF-8.
1341 * In other words: in Perl UTF-8 is not just for Unicode. */
1343 assert(start <= s && s <= end);
1346 return utf8_hop_forward(s, off, end);
1349 return utf8_hop_back(s, off, start);
1355 =for apidoc is_utf8_valid_partial_char
1357 Returns 0 if the sequence of bytes starting at C<s> and looking no further than
1358 S<C<e - 1>> is the UTF-8 encoding, as extended by Perl, for one or more code
1359 points. Otherwise, it returns 1 if there exists at least one non-empty
1360 sequence of bytes that when appended to sequence C<s>, starting at position
1361 C<e> causes the entire sequence to be the well-formed UTF-8 of some code point;
1362 otherwise returns 0.
1364 In other words this returns TRUE if C<s> points to a partial UTF-8-encoded code
1367 This is useful when a fixed-length buffer is being tested for being well-formed
1368 UTF-8, but the final few bytes in it don't comprise a full character; that is,
1369 it is split somewhere in the middle of the final code point's UTF-8
1370 representation. (Presumably when the buffer is refreshed with the next chunk
1371 of data, the new first bytes will complete the partial code point.) This
1372 function is used to verify that the final bytes in the current buffer are in
1373 fact the legal beginning of some code point, so that if they aren't, the
1374 failure can be signalled without having to wait for the next read.
1378 #define is_utf8_valid_partial_char(s, e) \
1379 is_utf8_valid_partial_char_flags(s, e, 0)
1383 =for apidoc is_utf8_valid_partial_char_flags
1385 Like C<L</is_utf8_valid_partial_char>>, it returns a boolean giving whether
1386 or not the input is a valid UTF-8 encoded partial character, but it takes an
1387 extra parameter, C<flags>, which can further restrict which code points are
1390 If C<flags> is 0, this behaves identically to
1391 C<L</is_utf8_valid_partial_char>>. Otherwise C<flags> can be any combination
1392 of the C<UTF8_DISALLOW_I<foo>> flags accepted by C<L</utf8n_to_uvchr>>. If
1393 there is any sequence of bytes that can complete the input partial character in
1394 such a way that a non-prohibited character is formed, the function returns
1395 TRUE; otherwise FALSE. Non character code points cannot be determined based on
1396 partial character input. But many of the other possible excluded types can be
1397 determined from just the first one or two bytes.
1402 PERL_STATIC_INLINE bool
1403 S_is_utf8_valid_partial_char_flags(const U8 * const s, const U8 * const e, const U32 flags)
1405 PERL_ARGS_ASSERT_IS_UTF8_VALID_PARTIAL_CHAR_FLAGS;
1407 assert(0 == (flags & ~(UTF8_DISALLOW_ILLEGAL_INTERCHANGE
1408 |UTF8_DISALLOW_PERL_EXTENDED)));
1410 if (s >= e || s + UTF8SKIP(s) <= e) {
1414 return cBOOL(_is_utf8_char_helper(s, e, flags));
1419 =for apidoc is_utf8_fixed_width_buf_flags
1421 Returns TRUE if the fixed-width buffer starting at C<s> with length C<len>
1422 is entirely valid UTF-8, subject to the restrictions given by C<flags>;
1423 otherwise it returns FALSE.
1425 If C<flags> is 0, any well-formed UTF-8, as extended by Perl, is accepted
1426 without restriction. If the final few bytes of the buffer do not form a
1427 complete code point, this will return TRUE anyway, provided that
1428 C<L</is_utf8_valid_partial_char_flags>> returns TRUE for them.
1430 If C<flags> in non-zero, it can be any combination of the
1431 C<UTF8_DISALLOW_I<foo>> flags accepted by C<L</utf8n_to_uvchr>>, and with the
1434 This function differs from C<L</is_utf8_string_flags>> only in that the latter
1435 returns FALSE if the final few bytes of the string don't form a complete code
1440 #define is_utf8_fixed_width_buf_flags(s, len, flags) \
1441 is_utf8_fixed_width_buf_loclen_flags(s, len, 0, 0, flags)
1445 =for apidoc is_utf8_fixed_width_buf_loc_flags
1447 Like C<L</is_utf8_fixed_width_buf_flags>> but stores the location of the
1448 failure in the C<ep> pointer. If the function returns TRUE, C<*ep> will point
1449 to the beginning of any partial character at the end of the buffer; if there is
1450 no partial character C<*ep> will contain C<s>+C<len>.
1452 See also C<L</is_utf8_fixed_width_buf_loclen_flags>>.
1457 #define is_utf8_fixed_width_buf_loc_flags(s, len, loc, flags) \
1458 is_utf8_fixed_width_buf_loclen_flags(s, len, loc, 0, flags)
1462 =for apidoc is_utf8_fixed_width_buf_loclen_flags
1464 Like C<L</is_utf8_fixed_width_buf_loc_flags>> but stores the number of
1465 complete, valid characters found in the C<el> pointer.
1470 PERL_STATIC_INLINE bool
1471 S_is_utf8_fixed_width_buf_loclen_flags(const U8 * const s,
1477 const U8 * maybe_partial;
1479 PERL_ARGS_ASSERT_IS_UTF8_FIXED_WIDTH_BUF_LOCLEN_FLAGS;
1482 ep = &maybe_partial;
1485 /* If it's entirely valid, return that; otherwise see if the only error is
1486 * that the final few bytes are for a partial character */
1487 return is_utf8_string_loclen_flags(s, len, ep, el, flags)
1488 || is_utf8_valid_partial_char_flags(*ep, s + len, flags);
1491 /* ------------------------------- perl.h ----------------------------- */
1494 =head1 Miscellaneous Functions
1496 =for apidoc AiR|bool|is_safe_syscall|const char *pv|STRLEN len|const char *what|const char *op_name
1498 Test that the given C<pv> doesn't contain any internal C<NUL> characters.
1499 If it does, set C<errno> to C<ENOENT>, optionally warn, and return FALSE.
1501 Return TRUE if the name is safe.
1503 Used by the C<IS_SAFE_SYSCALL()> macro.
1508 PERL_STATIC_INLINE bool
1509 S_is_safe_syscall(pTHX_ const char *pv, STRLEN len, const char *what, const char *op_name) {
1510 /* While the Windows CE API provides only UCS-16 (or UTF-16) APIs
1511 * perl itself uses xce*() functions which accept 8-bit strings.
1514 PERL_ARGS_ASSERT_IS_SAFE_SYSCALL;
1518 if (UNLIKELY((null_at = (char *)memchr(pv, 0, len-1)) != NULL)) {
1519 SETERRNO(ENOENT, LIB_INVARG);
1520 Perl_ck_warner(aTHX_ packWARN(WARN_SYSCALLS),
1521 "Invalid \\0 character in %s for %s: %s\\0%s",
1522 what, op_name, pv, null_at+1);
1532 Return true if the supplied filename has a newline character
1533 immediately before the first (hopefully only) NUL.
1535 My original look at this incorrectly used the len from SvPV(), but
1536 that's incorrect, since we allow for a NUL in pv[len-1].
1538 So instead, strlen() and work from there.
1540 This allow for the user reading a filename, forgetting to chomp it,
1543 open my $foo, "$file\0";
1549 PERL_STATIC_INLINE bool
1550 S_should_warn_nl(const char *pv) {
1553 PERL_ARGS_ASSERT_SHOULD_WARN_NL;
1557 return len > 0 && pv[len-1] == '\n';
1562 /* ------------------ pp.c, regcomp.c, toke.c, universal.c ------------ */
1564 #define MAX_CHARSET_NAME_LENGTH 2
1566 PERL_STATIC_INLINE const char *
1567 get_regex_charset_name(const U32 flags, STRLEN* const lenp)
1569 /* Returns a string that corresponds to the name of the regex character set
1570 * given by 'flags', and *lenp is set the length of that string, which
1571 * cannot exceed MAX_CHARSET_NAME_LENGTH characters */
1574 switch (get_regex_charset(flags)) {
1575 case REGEX_DEPENDS_CHARSET: return DEPENDS_PAT_MODS;
1576 case REGEX_LOCALE_CHARSET: return LOCALE_PAT_MODS;
1577 case REGEX_UNICODE_CHARSET: return UNICODE_PAT_MODS;
1578 case REGEX_ASCII_RESTRICTED_CHARSET: return ASCII_RESTRICT_PAT_MODS;
1579 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
1581 return ASCII_MORE_RESTRICT_PAT_MODS;
1583 /* The NOT_REACHED; hides an assert() which has a rather complex
1584 * definition in perl.h. */
1585 NOT_REACHED; /* NOTREACHED */
1586 return "?"; /* Unknown */
1591 Return false if any get magic is on the SV other than taint magic.
1595 PERL_STATIC_INLINE bool
1596 S_sv_only_taint_gmagic(SV *sv) {
1597 MAGIC *mg = SvMAGIC(sv);
1599 PERL_ARGS_ASSERT_SV_ONLY_TAINT_GMAGIC;
1602 if (mg->mg_type != PERL_MAGIC_taint
1603 && !(mg->mg_flags & MGf_GSKIP)
1604 && mg->mg_virtual->svt_get) {
1607 mg = mg->mg_moremagic;
1613 /* ------------------ cop.h ------------------------------------------- */
1616 /* Enter a block. Push a new base context and return its address. */
1618 PERL_STATIC_INLINE PERL_CONTEXT *
1619 S_cx_pushblock(pTHX_ U8 type, U8 gimme, SV** sp, I32 saveix)
1623 PERL_ARGS_ASSERT_CX_PUSHBLOCK;
1628 cx->blk_gimme = gimme;
1629 cx->blk_oldsaveix = saveix;
1630 cx->blk_oldsp = (I32)(sp - PL_stack_base);
1631 cx->blk_oldcop = PL_curcop;
1632 cx->blk_oldmarksp = (I32)(PL_markstack_ptr - PL_markstack);
1633 cx->blk_oldscopesp = PL_scopestack_ix;
1634 cx->blk_oldpm = PL_curpm;
1635 cx->blk_old_tmpsfloor = PL_tmps_floor;
1637 PL_tmps_floor = PL_tmps_ix;
1638 CX_DEBUG(cx, "PUSH");
1643 /* Exit a block (RETURN and LAST). */
1645 PERL_STATIC_INLINE void
1646 S_cx_popblock(pTHX_ PERL_CONTEXT *cx)
1648 PERL_ARGS_ASSERT_CX_POPBLOCK;
1650 CX_DEBUG(cx, "POP");
1651 /* these 3 are common to cx_popblock and cx_topblock */
1652 PL_markstack_ptr = PL_markstack + cx->blk_oldmarksp;
1653 PL_scopestack_ix = cx->blk_oldscopesp;
1654 PL_curpm = cx->blk_oldpm;
1656 /* LEAVE_SCOPE() should have made this true. /(?{})/ cheats
1657 * and leaves a CX entry lying around for repeated use, so
1658 * skip for multicall */ \
1659 assert( (CxTYPE(cx) == CXt_SUB && CxMULTICALL(cx))
1660 || PL_savestack_ix == cx->blk_oldsaveix);
1661 PL_curcop = cx->blk_oldcop;
1662 PL_tmps_floor = cx->blk_old_tmpsfloor;
1665 /* Continue a block elsewhere (e.g. NEXT, REDO, GOTO).
1666 * Whereas cx_popblock() restores the state to the point just before
1667 * cx_pushblock() was called, cx_topblock() restores it to the point just
1668 * *after* cx_pushblock() was called. */
1670 PERL_STATIC_INLINE void
1671 S_cx_topblock(pTHX_ PERL_CONTEXT *cx)
1673 PERL_ARGS_ASSERT_CX_TOPBLOCK;
1675 CX_DEBUG(cx, "TOP");
1676 /* these 3 are common to cx_popblock and cx_topblock */
1677 PL_markstack_ptr = PL_markstack + cx->blk_oldmarksp;
1678 PL_scopestack_ix = cx->blk_oldscopesp;
1679 PL_curpm = cx->blk_oldpm;
1681 PL_stack_sp = PL_stack_base + cx->blk_oldsp;
1685 PERL_STATIC_INLINE void
1686 S_cx_pushsub(pTHX_ PERL_CONTEXT *cx, CV *cv, OP *retop, bool hasargs)
1688 U8 phlags = CX_PUSHSUB_GET_LVALUE_MASK(Perl_was_lvalue_sub);
1690 PERL_ARGS_ASSERT_CX_PUSHSUB;
1692 PERL_DTRACE_PROBE_ENTRY(cv);
1693 cx->blk_sub.cv = cv;
1694 cx->blk_sub.olddepth = CvDEPTH(cv);
1695 cx->blk_sub.prevcomppad = PL_comppad;
1696 cx->cx_type |= (hasargs) ? CXp_HASARGS : 0;
1697 cx->blk_sub.retop = retop;
1698 SvREFCNT_inc_simple_void_NN(cv);
1699 cx->blk_u16 = PL_op->op_private & (phlags|OPpDEREF);
1703 /* subsets of cx_popsub() */
1705 PERL_STATIC_INLINE void
1706 S_cx_popsub_common(pTHX_ PERL_CONTEXT *cx)
1710 PERL_ARGS_ASSERT_CX_POPSUB_COMMON;
1711 assert(CxTYPE(cx) == CXt_SUB);
1713 PL_comppad = cx->blk_sub.prevcomppad;
1714 PL_curpad = LIKELY(PL_comppad) ? AvARRAY(PL_comppad) : NULL;
1715 cv = cx->blk_sub.cv;
1716 CvDEPTH(cv) = cx->blk_sub.olddepth;
1717 cx->blk_sub.cv = NULL;
1722 /* handle the @_ part of leaving a sub */
1724 PERL_STATIC_INLINE void
1725 S_cx_popsub_args(pTHX_ PERL_CONTEXT *cx)
1729 PERL_ARGS_ASSERT_CX_POPSUB_ARGS;
1730 assert(CxTYPE(cx) == CXt_SUB);
1731 assert(AvARRAY(MUTABLE_AV(
1732 PadlistARRAY(CvPADLIST(cx->blk_sub.cv))[
1733 CvDEPTH(cx->blk_sub.cv)])) == PL_curpad);
1735 CX_POP_SAVEARRAY(cx);
1736 av = MUTABLE_AV(PAD_SVl(0));
1737 if (UNLIKELY(AvREAL(av)))
1738 /* abandon @_ if it got reified */
1739 clear_defarray(av, 0);
1746 PERL_STATIC_INLINE void
1747 S_cx_popsub(pTHX_ PERL_CONTEXT *cx)
1749 PERL_ARGS_ASSERT_CX_POPSUB;
1750 assert(CxTYPE(cx) == CXt_SUB);
1752 PERL_DTRACE_PROBE_RETURN(cx->blk_sub.cv);
1756 cx_popsub_common(cx);
1760 PERL_STATIC_INLINE void
1761 S_cx_pushformat(pTHX_ PERL_CONTEXT *cx, CV *cv, OP *retop, GV *gv)
1763 PERL_ARGS_ASSERT_CX_PUSHFORMAT;
1765 cx->blk_format.cv = cv;
1766 cx->blk_format.retop = retop;
1767 cx->blk_format.gv = gv;
1768 cx->blk_format.dfoutgv = PL_defoutgv;
1769 cx->blk_format.prevcomppad = PL_comppad;
1772 SvREFCNT_inc_simple_void_NN(cv);
1774 SvREFCNT_inc_void(cx->blk_format.dfoutgv);
1778 PERL_STATIC_INLINE void
1779 S_cx_popformat(pTHX_ PERL_CONTEXT *cx)
1784 PERL_ARGS_ASSERT_CX_POPFORMAT;
1785 assert(CxTYPE(cx) == CXt_FORMAT);
1787 dfout = cx->blk_format.dfoutgv;
1789 cx->blk_format.dfoutgv = NULL;
1790 SvREFCNT_dec_NN(dfout);
1792 PL_comppad = cx->blk_format.prevcomppad;
1793 PL_curpad = LIKELY(PL_comppad) ? AvARRAY(PL_comppad) : NULL;
1794 cv = cx->blk_format.cv;
1795 cx->blk_format.cv = NULL;
1797 SvREFCNT_dec_NN(cv);
1801 PERL_STATIC_INLINE void
1802 S_cx_pusheval(pTHX_ PERL_CONTEXT *cx, OP *retop, SV *namesv)
1804 PERL_ARGS_ASSERT_CX_PUSHEVAL;
1806 cx->blk_eval.retop = retop;
1807 cx->blk_eval.old_namesv = namesv;
1808 cx->blk_eval.old_eval_root = PL_eval_root;
1809 cx->blk_eval.cur_text = PL_parser ? PL_parser->linestr : NULL;
1810 cx->blk_eval.cv = NULL; /* later set by doeval_compile() */
1811 cx->blk_eval.cur_top_env = PL_top_env;
1813 assert(!(PL_in_eval & ~ 0x3F));
1814 assert(!(PL_op->op_type & ~0x1FF));
1815 cx->blk_u16 = (PL_in_eval & 0x3F) | ((U16)PL_op->op_type << 7);
1819 PERL_STATIC_INLINE void
1820 S_cx_popeval(pTHX_ PERL_CONTEXT *cx)
1824 PERL_ARGS_ASSERT_CX_POPEVAL;
1825 assert(CxTYPE(cx) == CXt_EVAL);
1827 PL_in_eval = CxOLD_IN_EVAL(cx);
1828 assert(!(PL_in_eval & 0xc0));
1829 PL_eval_root = cx->blk_eval.old_eval_root;
1830 sv = cx->blk_eval.cur_text;
1831 if (sv && CxEVAL_TXT_REFCNTED(cx)) {
1832 cx->blk_eval.cur_text = NULL;
1833 SvREFCNT_dec_NN(sv);
1836 sv = cx->blk_eval.old_namesv;
1838 cx->blk_eval.old_namesv = NULL;
1839 SvREFCNT_dec_NN(sv);
1844 /* push a plain loop, i.e.
1846 * while (cond) { block }
1847 * for (init;cond;continue) { block }
1848 * This loop can be last/redo'ed etc.
1851 PERL_STATIC_INLINE void
1852 S_cx_pushloop_plain(pTHX_ PERL_CONTEXT *cx)
1854 PERL_ARGS_ASSERT_CX_PUSHLOOP_PLAIN;
1855 cx->blk_loop.my_op = cLOOP;
1859 /* push a true for loop, i.e.
1860 * for var (list) { block }
1863 PERL_STATIC_INLINE void
1864 S_cx_pushloop_for(pTHX_ PERL_CONTEXT *cx, void *itervarp, SV* itersave)
1866 PERL_ARGS_ASSERT_CX_PUSHLOOP_FOR;
1868 /* this one line is common with cx_pushloop_plain */
1869 cx->blk_loop.my_op = cLOOP;
1871 cx->blk_loop.itervar_u.svp = (SV**)itervarp;
1872 cx->blk_loop.itersave = itersave;
1874 cx->blk_loop.oldcomppad = PL_comppad;
1879 /* pop all loop types, including plain */
1881 PERL_STATIC_INLINE void
1882 S_cx_poploop(pTHX_ PERL_CONTEXT *cx)
1884 PERL_ARGS_ASSERT_CX_POPLOOP;
1886 assert(CxTYPE_is_LOOP(cx));
1887 if ( CxTYPE(cx) == CXt_LOOP_ARY
1888 || CxTYPE(cx) == CXt_LOOP_LAZYSV)
1890 /* Free ary or cur. This assumes that state_u.ary.ary
1891 * aligns with state_u.lazysv.cur. See cx_dup() */
1892 SV *sv = cx->blk_loop.state_u.lazysv.cur;
1893 cx->blk_loop.state_u.lazysv.cur = NULL;
1894 SvREFCNT_dec_NN(sv);
1895 if (CxTYPE(cx) == CXt_LOOP_LAZYSV) {
1896 sv = cx->blk_loop.state_u.lazysv.end;
1897 cx->blk_loop.state_u.lazysv.end = NULL;
1898 SvREFCNT_dec_NN(sv);
1901 if (cx->cx_type & (CXp_FOR_PAD|CXp_FOR_GV)) {
1903 SV **svp = (cx)->blk_loop.itervar_u.svp;
1904 if ((cx->cx_type & CXp_FOR_GV))
1905 svp = &GvSV((GV*)svp);
1907 *svp = cx->blk_loop.itersave;
1908 cx->blk_loop.itersave = NULL;
1909 SvREFCNT_dec(cursv);
1914 PERL_STATIC_INLINE void
1915 S_cx_pushwhen(pTHX_ PERL_CONTEXT *cx)
1917 PERL_ARGS_ASSERT_CX_PUSHWHEN;
1919 cx->blk_givwhen.leave_op = cLOGOP->op_other;
1923 PERL_STATIC_INLINE void
1924 S_cx_popwhen(pTHX_ PERL_CONTEXT *cx)
1926 PERL_ARGS_ASSERT_CX_POPWHEN;
1927 assert(CxTYPE(cx) == CXt_WHEN);
1929 PERL_UNUSED_ARG(cx);
1930 PERL_UNUSED_CONTEXT;
1931 /* currently NOOP */
1935 PERL_STATIC_INLINE void
1936 S_cx_pushgiven(pTHX_ PERL_CONTEXT *cx, SV *orig_defsv)
1938 PERL_ARGS_ASSERT_CX_PUSHGIVEN;
1940 cx->blk_givwhen.leave_op = cLOGOP->op_other;
1941 cx->blk_givwhen.defsv_save = orig_defsv;
1945 PERL_STATIC_INLINE void
1946 S_cx_popgiven(pTHX_ PERL_CONTEXT *cx)
1950 PERL_ARGS_ASSERT_CX_POPGIVEN;
1951 assert(CxTYPE(cx) == CXt_GIVEN);
1953 sv = GvSV(PL_defgv);
1954 GvSV(PL_defgv) = cx->blk_givwhen.defsv_save;
1955 cx->blk_givwhen.defsv_save = NULL;
1959 /* ------------------ util.h ------------------------------------------- */
1962 =head1 Miscellaneous Functions
1966 Returns true if the leading C<len> bytes of the strings C<s1> and C<s2> are the
1968 case-insensitively; false otherwise. Uppercase and lowercase ASCII range bytes
1969 match themselves and their opposite case counterparts. Non-cased and non-ASCII
1970 range bytes match only themselves.
1975 PERL_STATIC_INLINE I32
1976 Perl_foldEQ(const char *s1, const char *s2, I32 len)
1978 const U8 *a = (const U8 *)s1;
1979 const U8 *b = (const U8 *)s2;
1981 PERL_ARGS_ASSERT_FOLDEQ;
1986 if (*a != *b && *a != PL_fold[*b])
1993 PERL_STATIC_INLINE I32
1994 Perl_foldEQ_latin1(const char *s1, const char *s2, I32 len)
1996 /* Compare non-utf8 using Unicode (Latin1) semantics. Does not work on
1997 * MICRO_SIGN, LATIN_SMALL_LETTER_SHARP_S, nor
1998 * LATIN_SMALL_LETTER_Y_WITH_DIAERESIS, and does not check for these. Nor
1999 * does it check that the strings each have at least 'len' characters */
2001 const U8 *a = (const U8 *)s1;
2002 const U8 *b = (const U8 *)s2;
2004 PERL_ARGS_ASSERT_FOLDEQ_LATIN1;
2009 if (*a != *b && *a != PL_fold_latin1[*b]) {
2018 =for apidoc foldEQ_locale
2020 Returns true if the leading C<len> bytes of the strings C<s1> and C<s2> are the
2021 same case-insensitively in the current locale; false otherwise.
2026 PERL_STATIC_INLINE I32
2027 Perl_foldEQ_locale(const char *s1, const char *s2, I32 len)
2030 const U8 *a = (const U8 *)s1;
2031 const U8 *b = (const U8 *)s2;
2033 PERL_ARGS_ASSERT_FOLDEQ_LOCALE;
2038 if (*a != *b && *a != PL_fold_locale[*b])
2045 #if ! defined (HAS_MEMRCHR) && (defined(PERL_CORE) || defined(PERL_EXT))
2047 PERL_STATIC_INLINE void *
2048 S_my_memrchr(const char * s, const char c, const STRLEN len)
2050 /* memrchr(), since many platforms lack it */
2052 const char * t = s + len - 1;
2054 PERL_ARGS_ASSERT_MY_MEMRCHR;
2069 * ex: set ts=8 sts=4 sw=4 et: