fix: bug correction in awards where a reference regex was not checked
This commit is contained in:
+28
-4
@@ -13,6 +13,7 @@
|
||||
package award
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
@@ -257,6 +258,21 @@ type RefMeta struct {
|
||||
}
|
||||
|
||||
// NewRefList builds the engine's reference view from (code, meta) pairs.
|
||||
// compileAwardRE compiles an award / reference regex. Award patterns are run
|
||||
// over free-text log fields (QTH, NOTES) whose capitalization is arbitrary — a
|
||||
// log may hold "Tokyo", "TOKYO" or "TOKIO" — so we match case-insensitively by
|
||||
// default. An author who set their own flag group (e.g. "(?s)") is respected.
|
||||
func compileAwardRE(p string) (*regexp.Regexp, error) {
|
||||
p = strings.TrimSpace(p)
|
||||
if p == "" {
|
||||
return nil, errors.New("empty pattern")
|
||||
}
|
||||
if !strings.HasPrefix(p, "(?") {
|
||||
p = "(?i)" + p
|
||||
}
|
||||
return regexp.Compile(p)
|
||||
}
|
||||
|
||||
func NewRefList(metas []RefMeta) refList {
|
||||
rl := refList{byCode: make(map[string]RefMeta, len(metas))}
|
||||
for _, m := range metas {
|
||||
@@ -265,7 +281,7 @@ func NewRefList(metas []RefMeta) refList {
|
||||
continue
|
||||
}
|
||||
if p := strings.TrimSpace(m.Pattern); p != "" {
|
||||
if re, err := regexp.Compile(p); err == nil {
|
||||
if re, err := compileAwardRE(p); err == nil {
|
||||
m.re = re
|
||||
rl.withPattern = append(rl.withPattern, code)
|
||||
}
|
||||
@@ -297,7 +313,7 @@ func Compute(defs []Def, qsos []qso.QSO, refMetas map[string][]RefMeta, nameOf N
|
||||
perr := make([]string, len(defs))
|
||||
for i := range defs {
|
||||
if p := strings.TrimSpace(defs[i].Pattern); p != "" {
|
||||
re, err := regexp.Compile(p)
|
||||
re, err := compileAwardRE(p)
|
||||
if err != nil {
|
||||
perr[i] = "bad pattern: " + err.Error()
|
||||
} else {
|
||||
@@ -432,7 +448,7 @@ func MatchQSO(d Def, metas []RefMeta, q *qso.QSO) []string {
|
||||
}
|
||||
var re *regexp.Regexp
|
||||
if p := strings.TrimSpace(d.Pattern); p != "" {
|
||||
if c, err := regexp.Compile(p); err == nil {
|
||||
if c, err := compileAwardRE(p); err == nil {
|
||||
re = c
|
||||
} else {
|
||||
return nil
|
||||
@@ -565,6 +581,14 @@ func searchOne(field, matchBy string, re *regexp.Regexp, exact bool, leading, tr
|
||||
found = append(found, nc.code)
|
||||
}
|
||||
}
|
||||
// A reference may also declare its own regex to broaden matching beyond
|
||||
// its plain name (e.g. Tokyo's `\bTok[iy]o\b` also catches "TOKIO"). Test
|
||||
// those too — the name match and the pattern are OR'd.
|
||||
for _, code := range rl.withPattern {
|
||||
if m := rl.byCode[code]; m.re != nil && m.re.MatchString(raw) {
|
||||
found = append(found, code)
|
||||
}
|
||||
}
|
||||
case predefined && !exact:
|
||||
// "Search reference inside the field": look up each token of the field in
|
||||
// the list — O(tokens), not O(all references) — plus test the few
|
||||
@@ -602,7 +626,7 @@ func candidates(d *Def, re *regexp.Regexp, q *qso.QSO, rl refList, hasList bool)
|
||||
r := &d.OrRules[i]
|
||||
var rre *regexp.Regexp
|
||||
if p := strings.TrimSpace(r.Pattern); p != "" {
|
||||
c, err := regexp.Compile(p)
|
||||
c, err := compileAwardRE(p)
|
||||
if err != nil {
|
||||
continue // skip a rule with a bad regex rather than failing the award
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user