From a7440ce2779734a59a7495ef44b9d62da4aa9eee Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 16 May 2020 20:19:40 +0200 Subject: filter: properly search for tags The old code used get_taglist_string() and split the resulting string at commas to get the list of tags. This was wrong for two reasons: 1) It was buggy. Every tag but the first would start with a leading space and thus not be found. 2) It was inefficient. The tag list was concatenated, just to be split again. Turn the tag list directly into a QStringList and remove whitespace for good measure. Fixes #2842. Reported-by: Hartley Horwitz Signed-off-by: Berthold Stoeger --- core/divefilter.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'core') diff --git a/core/divefilter.cpp b/core/divefilter.cpp index 89fbd731d..fae6c1760 100644 --- a/core/divefilter.cpp +++ b/core/divefilter.cpp @@ -2,7 +2,8 @@ #include "divefilter.h" #include "divelist.h" -#include "qthelper.h" +#include "gettextfromc.h" +#include "tag.h" #include "subsurface-qt/divelistnotifier.h" static void updateDiveStatus(dive *d, bool newStatus, ShownChange &change) @@ -15,6 +16,15 @@ static void updateDiveStatus(dive *d, bool newStatus, ShownChange &change) } } +static QStringList getTagList(const dive *d) +{ + QStringList res; + for (const tag_entry *tag = d->tag_list; tag; tag = tag->next) + res.push_back(QString(tag->tag->name).trimmed()); + res.append(gettextFromC::tr(divemode_text_ui[d->dc.divemode])); + return res; +} + #ifdef SUBSURFACE_MOBILE // Check if a string-list contains at least one string that starts with the second argument. @@ -31,16 +41,14 @@ static bool check(const QStringList &items, const QStringList &list) { return listContainsSuperstring(list, item); }); } -bool hasTags(const QStringList &tags, const struct dive *d) +static bool hasTags(const QStringList &tags, const struct dive *d) { if (tags.isEmpty()) return true; - QStringList dive_tags = get_taglist_string(d->tag_list).split(","); - dive_tags.append(gettextFromC::tr(divemode_text_ui[d->dc.divemode])); - return check(tags, dive_tags); + return check(tags, getTagList(d)); } -bool hasPersons(const QStringList &people, const struct dive *d) +static bool hasPersons(const QStringList &people, const struct dive *d) { if (people.isEmpty()) return true; @@ -220,9 +228,7 @@ namespace { { if (tags.isEmpty()) return true; - QStringList dive_tags = get_taglist_string(d->tag_list).split(","); - dive_tags.append(gettextFromC::tr(divemode_text_ui[d->dc.divemode])); - return check(tags, dive_tags, mode, stringMode); + return check(tags, getTagList(d), mode, stringMode); } bool hasPersons(const QStringList &people, const struct dive *d, FilterData::Mode mode, StringFilterMode stringMode) -- cgit v1.2.3-70-g09d2