> Is there a reason not to do
> 
>       return (cs->ptr[(uch)c] & cs->mask) != 0;
> 
> This would allow us to get rid of the !! construct in regcomp.c

Why not. What about that?

Index: regcomp.c
===================================================================
RCS file: /OpenBSD/src/lib/libc/regex/regcomp.c,v
retrieving revision 1.41
diff -u -p -r1.41 regcomp.c
--- regcomp.c   31 Dec 2020 17:24:05 -0000      1.41
+++ regcomp.c   3 Jan 2021 16:43:50 -0000
@@ -1101,7 +1099,7 @@ freezeset(struct parse *p, cset *cs)
                if (cs2->hash == h && cs2 != cs) {
                        /* maybe */
                        for (i = 0; i < css; i++)
-                               if (!!CHIN(cs2, i) != !!CHIN(cs, i))
+                               if (CHIN(cs2, i) != CHIN(cs, i))
                                        break;          /* no */
                        if (i == css)
                                break;                  /* yes */
Index: regex2.h
===================================================================
RCS file: /OpenBSD/src/lib/libc/regex/regex2.h,v
retrieving revision 1.10
diff -u -p -r1.10 regex2.h
--- regex2.h    31 Dec 2020 17:20:19 -0000      1.10
+++ regex2.h    3 Jan 2021 16:43:50 -0000
@@ -107,10 +107,26 @@ typedef struct {
        uch mask;               /* bit within array */
        uch hash;               /* hash code */
 } cset;
-/* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
-#define        CHadd(cs, c)    ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash 
+= (c))
-#define        CHsub(cs, c)    ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash 
-= (c))
-#define        CHIN(cs, c)     ((cs)->ptr[(uch)(c)] & (cs)->mask)
+
+static inline void
+CHadd(cset *cs, char c)
+{
+       cs->ptr[(uch)c] |= cs->mask;
+       cs->hash += c;
+}
+
+static inline void
+CHsub(cset *cs, char c)
+{
+       cs->ptr[(uch)c] &= ~cs->mask;
+       cs->hash -= c;
+}
+
+static inline int
+CHIN(const cset *cs, char c)
+{
+       return (cs->ptr[(uch)c] & cs->mask) != 0;
+}
 
 /*
  * main compiled-expression structure

Reply via email to