summaryrefslogtreecommitdiff
path: root/inline.h
diff options
context:
space:
mode:
authorKarl Williamson <khw@cpan.org>2022-04-25 06:01:39 -0600
committerKarl Williamson <khw@cpan.org>2022-05-28 08:52:16 -0600
commitdec3ff31f81782e2cb89a00e8be63635ae97430a (patch)
tree2c4a547a29593e7ad8954fb485eb6f3eb297d727 /inline.h
parent97039a44551f5544ceab6baea4f466fec5d89cbd (diff)
downloadperl-dec3ff31f81782e2cb89a00e8be63635ae97430a.tar.gz
Evaluate SvPVXtrue's argument just once
by making the macro instead an inline function
Diffstat (limited to 'inline.h')
-rw-r--r--inline.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/inline.h b/inline.h
index d4794aa81a..406652073b 100644
--- a/inline.h
+++ b/inline.h
@@ -316,6 +316,41 @@ Perl_ReANY(const REGEXP * const re)
/* ------------------------------- sv.h ------------------------------- */
+/*
+=for apidoc_section $SV
+=for apidoc Am|bool|SvPVXtrue|SV * sv
+
+Returns a boolean as to whether or not C<sv> contains a PV that is considered
+TRUE. FALSE is returned if C<sv> doesn't contain a PV, or if the PV it does
+contain is zero length, or consists of just the single character '0'. Every
+other PV value is considered TRUE.
+
+As of Perl v5.37.1, C<sv> is evaluated exactly once; in earlier releases, it
+could be evaluated more than once.
+
+=cut
+*/
+
+PERL_STATIC_INLINE bool
+Perl_SvPVXtrue(pTHX_ SV *sv)
+{
+ PERL_ARGS_ASSERT_SVPVXTRUE;
+
+ if (! (XPV *) SvANY(sv)) {
+ return false;
+ }
+
+ if ( ((XPV *) SvANY(sv))->xpv_cur > 1) { /* length > 1 */
+ return true;
+ }
+
+ if (( (XPV *) SvANY(sv))->xpv_cur == 0) {
+ return false;
+ }
+
+ return *sv->sv_u.svu_pv != '0';
+}
+
PERL_STATIC_INLINE bool
Perl_SvTRUE(pTHX_ SV *sv)
{