From d2cf57dcd9baaab5a8dec2483027078b0714b354 Mon Sep 17 00:00:00 2001 From: Laurence Withers Date: Fri, 7 Jul 2023 10:57:31 +0100 Subject: [PATCH] Use regexp for case-insensitive literal matches This commit switches back to using the regexp engine for case-insensitive literal string matches. This is slower, but at least case-insensitive matches for string literals will function now. The code is a tiny bit shorter and simpler too. Given the aim of the tool is to be useful for ad-hoc searches, efficiency isn't the concern but rather just getting the job done with the minimum of fuss / unexpected behaviour. --- file.go | 6 ------ main.go | 20 ++++++++++++-------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/file.go b/file.go index 615ce10..900c445 100644 --- a/file.go +++ b/file.go @@ -274,12 +274,6 @@ func findMatches(data []byte) (loc []int) { return loc } } - for _, s := range searchBytes { - pos := bytes.Index(data, s) - if pos != -1 { - return []int{pos, pos + len(s)} - } - } return nil } diff --git a/main.go b/main.go index 254c88e..c259bf0 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,6 @@ import ( ) // TODO: -// - it would be better to make fixed patterns case insensitive too. // - configurable defaults for exclude. func main() { @@ -42,10 +41,9 @@ which can be repeated multiple times. -e specifies a regular expression and -Q a fixed pattern. When using either flag, any non-flag arguments are treated as paths to scan. -Search defaults to case-sensitive but the -i flag may be passed to make regular -expression searches case-insensitive. Alternatively, the "(?i)" construct may be -added to a regular expression to make that specific expression case insensitive. -Fixed pattern matches are always case-sensitive. +Search defaults to case-sensitive but the -i flag may be passed to make all +search terms case-insensitive. Alternatively, the "(?i)" construct may be added +to a regular expression to make that specific expression case insensitive. Files and directories can be excluded with the -x option. This supports bash-style globs with '*', '?', '[a-z]', '{this,that}', or '/**/' to match zero or more @@ -58,7 +56,6 @@ var ( searchRegexp []string regexps []*regexp.Regexp searchFixed []string - searchBytes [][]byte searchPath []string excludeList []string binaryFile notPlainTextFlag @@ -116,8 +113,15 @@ func run(c *cobra.Command, args []string) error { regexps = append(regexps, re) } - for _, s := range searchFixed { - searchBytes = append(searchBytes, []byte(s)) + for _, r := range searchFixed { + r = regexp.QuoteMeta(r) + if ignoreCase { + r = "(?i)" + r + } + re, err := regexp.Compile(r) + if err != nil { + } + regexps = append(regexps, re) } var errs []error