develooper Front page | perl.perl5.porters | Postings from June 2017

Re: RFC: Add new string comparison macros in handy.h

Thread Previous | Thread Next
June 2, 2017 12:46
Re: RFC: Add new string comparison macros in handy.h
Message ID:
On 2 June 2017 at 13:30, Ævar Arnfjörð Bjarmason <> wrote:
> On Thu, May 11, 2017 at 5:22 PM, Karl Williamson
> <> wrote:
>>     memSTARTS_WITHs
>>             Test if the string buffer "s1" with length "l1" begins with the
>>             substring given by the string literal "s2", returning non-zero
>> if
>>             so (including if the two are identical); zero otherwise. The
>>             comparison does not include the final "NUL" of "s2". "s1" does
>> not
>>             have to be "NUL"-terminated,
>>                     bool    memSTARTS_WITHs(char* s1, STRLEN l1, char* s2)
> I don't have to use these and don't really care, but just a question:
> Is there a reason for why the prototype for the the mem* functions
> doesn't also pass the STRLEN for the needle as well as the haystack?

The whole point of the 's' family macros is to handle cases where one
of the arguments is a constant string in the C code, and therefore the
length can be computed by the macro. In other words cases like this:

char *pv= SvPV(thing,len);

if (memSTARTS_WITHs(pv,len,"someprefix")) { ... }

That is why I mentioned the variants I did, which I will relist with
better arguments:


> Right now the interface only allows the haystack not the needle to
> contain \0, which seems like a needless arbitrary limitation for
> something that's essentially a fancy strstr() & memmem(). I.e. you
> have feature-parity with strstr() (and extra features like "begins
> with?"), but not with memmem().

With the 's' macros we know the length of the string by using
sizeof(). The 's' macros are composed of the STR_WITH_LEN() macro

#define STR_WITH_LEN(s) "" s "", sizeof(s)-1

the "" s "" thing guarantees the argument is a C string, not a
pointer, and the sizeof(s)-1 tells us its length.

With the api I proposed in a reply to Karl the 'll' variants would
cover the cases you are thinking of.

To recap and refine that proposal:


More specifically the suffixes would be:

'' :  none, both arguments are pv's without a length.
's': second argument is a constant string
'l' : first argument has a length, second argument is a pointer
'ls': first argument has a length, second argument is a constant string
'll':  both arguments are char *'s and have lengths.

Not all suffixes would apply to 'mem', but i think they all apply to 'str'.

Whether we should have 'str' at all is a different question.


perl -Mre=debug -e "/just|another|perl|hacker/"

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About