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.
This commit is contained in:
Laurence Withers 2023-07-07 10:57:31 +01:00
parent 2f3af7fc8e
commit d2cf57dcd9
2 changed files with 12 additions and 14 deletions

View File

@ -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
}

20
main.go
View File

@ -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