From 49b9a0bcab97067ea1581315d83f73ed632e67e7 Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Fri, 22 Nov 2024 21:46:34 +0000 Subject: [PATCH] Also add a `valid_identifier_pve()` that takes a start/end pair instead of s/len --- embed.fnc | 4 ++++ embed.h | 1 + proto.h | 5 +++++ toke.c | 39 ++++++++++++++++++++++++++++++--------- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/embed.fnc b/embed.fnc index 1ad7a514c2b3..5b8bacf884eb 100644 --- a/embed.fnc +++ b/embed.fnc @@ -3766,6 +3766,10 @@ EXdpx |bool |validate_proto |NN SV *name \ |NULLOK SV *proto \ |bool warn \ |bool curstash +Adp |bool |valid_identifier_pve \ + |NN const char *s \ + |NN const char *end \ + |U32 flags Adp |bool |valid_identifier_pvn \ |NN const char *s \ |STRLEN len \ diff --git a/embed.h b/embed.h index a4fba3060f81..8b8b09b511a8 100644 --- a/embed.h +++ b/embed.h @@ -865,6 +865,7 @@ # define uvchr_to_utf8_flags(a,b,c) Perl_uvchr_to_utf8_flags(aTHX,a,b,c) # define uvchr_to_utf8_flags_msgs(a,b,c,d) Perl_uvchr_to_utf8_flags_msgs(aTHX,a,b,c,d) # define uvoffuni_to_utf8_flags_msgs(a,b,c,d) Perl_uvoffuni_to_utf8_flags_msgs(aTHX_ a,b,c,d) +# define valid_identifier_pve(a,b,c) Perl_valid_identifier_pve(aTHX_ a,b,c) # define valid_identifier_pvn(a,b,c) Perl_valid_identifier_pvn(aTHX_ a,b,c) # define valid_identifier_sv(a) Perl_valid_identifier_sv(aTHX_ a) # define valid_utf8_to_uvchr Perl_valid_utf8_to_uvchr diff --git a/proto.h b/proto.h index 811a17622bf9..74ea81d1a066 100644 --- a/proto.h +++ b/proto.h @@ -5390,6 +5390,11 @@ Perl_uvoffuni_to_utf8_flags_msgs(pTHX_ U8 *d, UV input_uv, const UV flags, HV ** #define PERL_ARGS_ASSERT_UVOFFUNI_TO_UTF8_FLAGS_MSGS \ assert(d) +PERL_CALLCONV bool +Perl_valid_identifier_pve(pTHX_ const char *s, const char *end, U32 flags); +#define PERL_ARGS_ASSERT_VALID_IDENTIFIER_PVE \ + assert(s); assert(end) + PERL_CALLCONV bool Perl_valid_identifier_pvn(pTHX_ const char *s, STRLEN len, U32 flags); #define PERL_ARGS_ASSERT_VALID_IDENTIFIER_PVN \ diff --git a/toke.c b/toke.c index 7fe55e0e02b5..1fc30f378027 100644 --- a/toke.c +++ b/toke.c @@ -13933,12 +13933,12 @@ Perl_parse_subsignature(pTHX_ U32 flags) } /* -=for apidoc valid_identifier_pvn +=for apidoc valid_identifier_pve -Returns true if the string given by C whose length is C would be -considered valid as a Perl identifier. That is, it must begin with a -character matching C, followed by characters all matching -C. An empty string (i.e. when C is zero) will return false. +Returns true if the string given by C until C would be considered +valid as a Perl identifier. That is, it must begin with a character matching +C, followed by characters all matching C. An empty +string (i.e. when C is C) will return false. If C contains the C bit, then the string is presumed to be encoded in UTF-8, and suitable Unicode character test functions will be used. @@ -13947,12 +13947,11 @@ encoded in UTF-8, and suitable Unicode character test functions will be used. */ bool -Perl_valid_identifier_pvn(pTHX_ const char *s, STRLEN len, U32 flags) +Perl_valid_identifier_pve(pTHX_ const char *s, const char *end, U32 flags) { - PERL_ARGS_ASSERT_VALID_IDENTIFIER_PVN; - const char *end = s + len; + PERL_ARGS_ASSERT_VALID_IDENTIFIER_PVE; - if(!len) + if(end <= s) return false; if(flags & SVf_UTF8) { @@ -13985,6 +13984,28 @@ Perl_valid_identifier_pvn(pTHX_ const char *s, STRLEN len, U32 flags) return false; } +/* +=for apidoc valid_identifier_pvn + +Returns true if the string given by C whose length is C would be +considered valid as a Perl identifier. That is, it must begin with a +character matching C, followed by characters all matching +C. An empty string (i.e. when C is zero) will return false. + +If C contains the C bit, then the string is presumed to be +encoded in UTF-8, and suitable Unicode character test functions will be used. + +=cut +*/ + +bool +Perl_valid_identifier_pvn(pTHX_ const char *s, STRLEN len, U32 flags) +{ + PERL_ARGS_ASSERT_VALID_IDENTIFIER_PVN; + + return valid_identifier_pve(s, s + len, flags); +} + /* =for apidoc valid_identifier_sv