diff options
author | Robin Jarry <robin@jarry.cc> | 2022-12-21 11:31:04 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-01-04 22:57:31 +0100 |
commit | 5677f93ff8e0f212be112a110fcfe09663c84f98 (patch) | |
tree | 9f47f430b312d1f0dc1acdd3735cfd76ad21e0f0 /worker/lib/search.go | |
parent | 36fded03e762da97edde61559c8bf60d5749d6a2 (diff) | |
download | aerc-5677f93ff8e0f212be112a110fcfe09663c84f98.tar.gz |
model: change flags array to bitmask
Using a list of integers is not optimal. Use a bit mask instead.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'worker/lib/search.go')
-rw-r--r-- | worker/lib/search.go | 49 |
1 files changed, 17 insertions, 32 deletions
diff --git a/worker/lib/search.go b/worker/lib/search.go index c09feff6..0a124e3c 100644 --- a/worker/lib/search.go +++ b/worker/lib/search.go @@ -18,8 +18,8 @@ type searchCriteria struct { Body []string Text []string - WithFlags []models.Flag - WithoutFlags []models.Flag + WithFlags models.Flags + WithoutFlags models.Flags startDate, endDate time.Time } @@ -36,13 +36,13 @@ func GetSearchCriteria(args []string) (*searchCriteria, error) { for _, opt := range opts { switch opt.Option { case 'r': - criteria.WithFlags = append(criteria.WithFlags, models.SeenFlag) + criteria.WithFlags |= models.SeenFlag case 'u': - criteria.WithoutFlags = append(criteria.WithoutFlags, models.SeenFlag) + criteria.WithoutFlags |= models.SeenFlag case 'x': - criteria.WithFlags = append(criteria.WithFlags, getParsedFlag(opt.Value)) + criteria.WithFlags |= getParsedFlag(opt.Value) case 'X': - criteria.WithoutFlags = append(criteria.WithoutFlags, getParsedFlag(opt.Value)) + criteria.WithoutFlags |= getParsedFlag(opt.Value) case 'H': // TODO case 'f': @@ -80,8 +80,8 @@ func GetSearchCriteria(args []string) (*searchCriteria, error) { return criteria, nil } -func getParsedFlag(name string) models.Flag { - var f models.Flag +func getParsedFlag(name string) models.Flags { + var f models.Flags switch strings.ToLower(name) { case "seen": f = models.SeenFlag @@ -117,7 +117,7 @@ func searchMessage(message RawMessage, criteria *searchCriteria, // setup parts of the message to use in the search // this is so that we try to minimise reading unnecessary parts var ( - flags []models.Flag + flags models.Flags header *models.MessageInfo body string all string @@ -188,18 +188,14 @@ func searchMessage(message RawMessage, criteria *searchCriteria, } } } - if criteria.WithFlags != nil { - for _, searchFlag := range criteria.WithFlags { - if !containsFlag(flags, searchFlag) { - return false, nil - } + if criteria.WithFlags != 0 { + if !flags.Has(criteria.WithFlags) { + return false, nil } } - if criteria.WithoutFlags != nil { - for _, searchFlag := range criteria.WithoutFlags { - if containsFlag(flags, searchFlag) { - return false, nil - } + if criteria.WithoutFlags != 0 { + if flags.Has(criteria.WithoutFlags) { + return false, nil } } if parts&DATE > 0 { @@ -221,17 +217,6 @@ func searchMessage(message RawMessage, criteria *searchCriteria, return true, nil } -// containsFlag returns true if searchFlag appears in flags -func containsFlag(flags []models.Flag, searchFlag models.Flag) bool { - match := false - for _, flag := range flags { - if searchFlag == flag { - match = true - } - } - return match -} - // containsSmartCase is a smarter version of strings.Contains for searching. // Is case-insensitive unless substr contains an upper case character func containsSmartCase(s string, substr string) bool { @@ -278,10 +263,10 @@ func getRequiredParts(criteria *searchCriteria) MsgParts { if criteria.Text != nil && len(criteria.Text) > 0 { required |= ALL } - if criteria.WithFlags != nil && len(criteria.WithFlags) > 0 { + if criteria.WithFlags != 0 { required |= FLAGS } - if criteria.WithoutFlags != nil && len(criteria.WithoutFlags) > 0 { + if criteria.WithoutFlags != 0 { required |= FLAGS } |