avoid crashes when regex doesn't compile

This commit is contained in:
Mikael Fangel 2023-04-07 09:38:40 +02:00 committed by Maas Lalani
parent be3e250892
commit 562bcaa9df
No known key found for this signature in database
GPG key ID: 5A6ED5CBF1A0A000

View file

@ -41,14 +41,22 @@ func (s *search) Execute(m *model) {
return
}
s.query = regexp.MustCompile(s.input.Value())
var err error
s.query, err = regexp.Compile(s.input.Value())
if err != nil {
s.query = nil
return
}
query := regexp.MustCompile(fmt.Sprintf("(%s)", s.query.String()))
m.content = query.ReplaceAllString(m.content, m.matchStyle.Render("$1"))
// Recompile the regex to match the an replace the highlights.
leftPad, _ := utils.LipglossPadding(m.matchStyle)
matchingString := regexp.QuoteMeta(m.matchStyle.Render()[:leftPad]) + s.query.String() + regexp.QuoteMeta(m.matchStyle.Render()[leftPad:])
s.query = regexp.MustCompile(matchingString)
s.query, err = regexp.Compile(matchingString)
if err != nil {
s.query = nil
}
}
func (s *search) Done() {