2019-10-20 16:02:28 +02:00
|
|
|
package sheet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-02-15 20:40:33 +01:00
|
|
|
// Search returns lines within a sheet's Text that match the search regex
|
|
|
|
func (s *Sheet) Search(reg *regexp.Regexp) string {
|
2019-10-20 16:02:28 +02:00
|
|
|
|
|
|
|
// record matches
|
2020-02-15 20:40:33 +01:00
|
|
|
matches := []string{}
|
2019-10-20 16:02:28 +02:00
|
|
|
|
|
|
|
// search through the cheatsheet's text line by line
|
|
|
|
// TODO: searching line-by-line is surely the "naive" approach. Revisit this
|
|
|
|
// later with an eye for performance improvements.
|
2020-02-15 16:11:25 +01:00
|
|
|
for _, line := range strings.Split(s.Text, "\n") {
|
2019-10-20 16:02:28 +02:00
|
|
|
|
|
|
|
// exit early if the line doesn't match the regex
|
|
|
|
if !reg.MatchString(line) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// record the match
|
2020-02-15 20:40:33 +01:00
|
|
|
matches = append(matches, line)
|
2019-10-20 16:02:28 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 20:40:33 +01:00
|
|
|
return strings.Join(matches, "\n")
|
2019-10-20 16:02:28 +02:00
|
|
|
}
|