Re: [PATCH v3 2/4] grep/pcre2: simplify boolean spaghetti

2 messages, 2 authors, 2021-01-24 · open the first message on its own page

Re: [PATCH v3 2/4] grep/pcre2: simplify boolean spaghetti

From: Junio C Hamano <hidden>
Date: 2021-01-24 05:36:28

Ævar Arnfjörð Bjarmason  [off-list ref] writes:
    NOT(A && B) is Equivalent to (NOT(A) OR NOT(B))
At this level, however, the left one looks much simpler than the
right one ;-)

 	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
-	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+	    (opt->ignore_case || !(p->fixed || p->is_fixed)))
 		options |= PCRE2_UTF;
In the context of this expression, well, I guess the rewritten one
is probably simpler but can we explain the whole condition in fewer
than three lines?  With or without the rewrite, it still looks too
complicated to me.

Re: [PATCH v3 2/4] grep/pcre2: simplify boolean spaghetti

From: Johannes Sixt <hidden>
Date: 2021-01-24 10:47:13

Am 24.01.21 um 06:33 schrieb Junio C Hamano:
Ævar Arnfjörð Bjarmason  [off-list ref] writes:
quoted
    NOT(A && B) is Equivalent to (NOT(A) OR NOT(B))
At this level, however, the left one looks much simpler than the
right one ;-)

quoted
 	if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) &&
-	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
+	    (opt->ignore_case || !(p->fixed || p->is_fixed)))
 		options |= PCRE2_UTF;
In the context of this expression, well, I guess the rewritten one
is probably simpler but can we explain the whole condition in fewer
than three lines?  With or without the rewrite, it still looks too
complicated to me.
Make the condition

 	if (!opt->ignore_locale &&
	    is_utf8_locale() &&
	    has_non_ascii(p->pattern) &&
	    (opt->ignore_case ||
		(!p->fixed &&
		 !p->is_fixed)))
	{
  		options |= PCRE2_UTF;
	}

With the knowledge of the equivalence

    (A => B)  <=>  (NOT(A) OR B)

(A => B means "if A then B"), the condition makes a lot of sense when
read aloud:

    if
       NOT ignore locale
       AND
       is UTF8
       AND
       has non-ASCII
       AND
         if
            NOT ignore case
         then if also
            NOT fixed
            AND
            NOT is fixed
    then
        ...


The codition amounts to extending a series of conjunctions with more
conjuctions IF a condition is satisfied. That's quite sensible.

You have to swap the polarity of the first condition of || in your head,
though, to achieve that meaning. That works with every OR condition, BTW.

-- Hannes
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help