This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
handy.h: add _memEQs() and _memNEs() for use with constant strings
authorYves Orton <demerphq@gmail.com>
Wed, 19 Oct 2016 08:32:29 +0000 (10:32 +0200)
committerYves Orton <demerphq@gmail.com>
Wed, 19 Oct 2016 11:27:59 +0000 (13:27 +0200)
memEQs() is already defined, and requires a length parameter for
the first pointer argument. However some times we do not have
this length handy and simply want to use the length of the constant
string instead.

In an ideal world, to be compatible with more s suffix macros,
IMO the existing memEQs() should have been called something like
memEQsl() maybe, and the ones I am adding would get the memEQs(
name, but it didnt work out like that.

handy.h

diff --git a/handy.h b/handy.h
index 8111309..d44a226 100644 (file)
--- a/handy.h
+++ b/handy.h
@@ -505,9 +505,15 @@ Returns zero if non-equal, or non-zero if equal.
 
 /* memEQ and memNE where second comparand is a string constant */
 #define memEQs(s1, l, s2) \
-       (sizeof(s2)-1 == l && memEQ(s1, ("" s2 ""), (sizeof(s2)-1)))
+        (((sizeof(s2)-1) == (l)) && memEQ((s1), ("" s2 ""), (sizeof(s2)-1)))
 #define memNEs(s1, l, s2) !memEQs(s1, l, s2)
 
+/* memEQ and memNE where second comparand is a string constant
+ * and we can assume the length of s1 is at least that of the string */
+#define _memEQs(s1, s2) \
+        (memEQ((s1), ("" s2 ""), (sizeof(s2)-1)))
+#define _memNEs(s1, s2) (memNE((s1),("" s2 ""),(sizeof(s2)-1)))
+
 #define memLT(s1,s2,l) (memcmp(s1,s2,l) < 0)
 #define memLE(s1,s2,l) (memcmp(s1,s2,l) <= 0)
 #define memGT(s1,s2,l) (memcmp(s1,s2,l) > 0)