5
#include "diff.h"
6
#include "diffcore.h"
7
#include "commit.h"
8
+#include "quote.h"
9
10
static int grep_source_load(struct grep_source *gs);
11
static int grep_source_is_binary(struct grep_source *gs);
398
return 1;
399
}
400
401
+static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
402
+{
403
+ struct strbuf sb = STRBUF_INIT;
404
+ int err;
405
+ int regflags;
406
+
407
+ basic_regex_quote_buf(&sb, p->pattern);
408
+ regflags = opt->regflags & ~REG_EXTENDED;
409
+ if (opt->ignore_case)
410
+ regflags |= REG_ICASE;
411
+ err = regcomp(&p->regexp, sb.buf, regflags);
412
+ if (opt->debug)
413
+ fprintf(stderr, "fixed %s\n", sb.buf);
414
+ strbuf_release(&sb);
415
+ if (err) {
416
+ char errbuf[1024];
417
+ regerror(err, &p->regexp, errbuf, sizeof(errbuf));
418
+ regfree(&p->regexp);
419
+ compile_regexp_failed(p, errbuf);
420
+ }
421
+}
422
+
423
static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
424
{
425
int icase, ascii_only;
430
icase = opt->regflags & REG_ICASE || p->ignore_case;
431
ascii_only = !has_non_ascii(p->pattern);
432
433
+ /*
434
+ * Even when -F (fixed) asks us to do a non-regexp search, we
435
+ * may not be able to correctly case-fold when -i
436
+ * (ignore-case) is asked (in which case, we'll synthesize a
437
+ * regexp to match the pattern that matches regexp special
438
+ * characters literally, while ignoring case differences). On
439
+ * the other hand, even without -F, if the pattern does not
440
+ * have any regexp special characters and there is no need for
441
+ * case-folding search, we can internally turn it into a
442
+ * simple string match using kws. p->fixed tells us if we
443
+ * want to use kws.
444
+ */
445
if (opt->fixed)
411
- p->fixed = 1;
446
+ p->fixed = !icase || ascii_only;
447
else if ((!icase || ascii_only) &&
448
is_fixed(p->pattern, p->patternlen))
449
p->fixed = 1;
458
kwsincr(p->kws, p->pattern, p->patternlen);
459
kwsprep(p->kws);
460
return;
461
+ } else if (opt->fixed) {
462
+ /*
463
+ * We come here when the pattern has the non-ascii
464
+ * characters we cannot case-fold, and asked to
465
+ * ignore-case.
466
+ */
467
+ compile_fixed_regexp(p, opt);
468
+ return;
469
}
470
471
if (opt->pcre) {