- /*
- * Even when -F (fixed) asks us to do a non-regexp search, we
- * may not be able to correctly case-fold when -i
- * (ignore-case) is asked (in which case, we'll synthesize a
- * regexp to match the pattern that matches regexp special
- * characters literally, while ignoring case differences). On
- * the other hand, even without -F, if the pattern does not
- * have any regexp special characters and there is no need for
- * case-folding search, we can internally turn it into a
- * simple string match using kws. p->fixed tells us if we
- * want to use kws.
- */
- if (opt->fixed ||
- has_null(p->pattern, p->patternlen) ||
- is_fixed(p->pattern, p->patternlen))
- p->fixed = !p->ignore_case || ascii_only;
-
- if (p->fixed) {
- p->kws = kwsalloc(p->ignore_case ? tolower_trans_tbl : NULL);
- kwsincr(p->kws, p->pattern, p->patternlen);
- kwsprep(p->kws);
- return;
- } else if (opt->fixed) {
- /*
- * We come here when the pattern has the non-ascii
- * characters we cannot case-fold, and asked to
- * ignore-case.
- */
+ if (memchr(p->pattern, 0, p->patternlen) && !opt->pcre2)
+ die(_("given pattern contains NULL byte (via -f <file>). This is only supported with -P under PCRE v2"));
+
+ pat_is_fixed = is_fixed(p->pattern, p->patternlen);
+ if (opt->fixed || pat_is_fixed) {
+#ifdef USE_LIBPCRE2
+ opt->pcre2 = 1;
+ if (pat_is_fixed) {
+ compile_pcre2_pattern(p, opt);
+ } else {
+ /*
+ * E.g. t7811-grep-open.sh relies on the
+ * pattern being restored.
+ */
+ char *old_pattern = p->pattern;
+ size_t old_patternlen = p->patternlen;
+ struct strbuf sb = STRBUF_INIT;
+
+ /*
+ * There is the PCRE2_LITERAL flag, but it's
+ * only in PCRE v2 10.30 and later. Needing to
+ * ifdef our way around that and dealing with
+ * it + PCRE2_MULTILINE being an error is more
+ * complex than just quoting this ourselves.
+ */
+ strbuf_add(&sb, "\\Q", 2);
+ strbuf_add(&sb, p->pattern, p->patternlen);
+ strbuf_add(&sb, "\\E", 2);
+
+ p->pattern = sb.buf;
+ p->patternlen = sb.len;
+ compile_pcre2_pattern(p, opt);
+ p->pattern = old_pattern;
+ p->patternlen = old_patternlen;
+ strbuf_release(&sb);
+ }
+#else /* !USE_LIBPCRE2 */