From efb2051b302cbe60a52c216c20a693c2cf49b638 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 24 Jul 2018 22:11:14 +0200 Subject: [PATCH] SongFilter: add ToExpression() --- src/SongFilter.cxx | 42 ++++++++++++++++++++++++++++++++++++++++++ src/SongFilter.hxx | 12 ++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/SongFilter.cxx b/src/SongFilter.cxx index cd8b9afd3..51d96486c 100644 --- a/src/SongFilter.cxx +++ b/src/SongFilter.cxx @@ -73,6 +73,27 @@ SongFilter::Item::Item(unsigned _tag, { } +std::string +SongFilter::Item::ToExpression() const noexcept +{ + switch (tag) { + case LOCATE_TAG_FILE_TYPE: + return "(" LOCATE_TAG_FILE_KEY " == \"" + value + "\")"; + + case LOCATE_TAG_BASE_TYPE: + return "(base \"" + value + "\")"; + + case LOCATE_TAG_MODIFIED_SINCE: + return "(modified-since \"" + value + "\")"; + + case LOCATE_TAG_ANY_TYPE: + return "(" LOCATE_TAG_ANY_KEY " == \"" + value + "\")"; + + default: + return std::string("(") + tag_item_names[tag] + " == \"" + value + "\")"; + } +} + bool SongFilter::Item::StringMatchNN(const char *s) const noexcept { @@ -177,6 +198,27 @@ SongFilter::~SongFilter() /* this destructor exists here just so it won't get inlined */ } +std::string +SongFilter::ToExpression() const noexcept +{ + auto i = items.begin(); + const auto end = items.end(); + + if (std::next(i) == end) + return i->ToExpression(); + + std::string e("("); + e += i->ToExpression(); + + for (++i; i != end; ++i) { + e += " AND "; + e += i->ToExpression(); + } + + e.push_back(')'); + return e; +} + static std::chrono::system_clock::time_point ParseTimeStamp(const char *s) { diff --git a/src/SongFilter.hxx b/src/SongFilter.hxx index 8c1008f7d..fbf8aa037 100644 --- a/src/SongFilter.hxx +++ b/src/SongFilter.hxx @@ -70,6 +70,12 @@ public: Item(unsigned tag, std::string &&_value, bool fold_case=false); Item(unsigned tag, std::chrono::system_clock::time_point time); + /** + * Convert this object into an "expression". This is + * only useful for debugging. + */ + std::string ToExpression() const noexcept; + unsigned GetTag() const { return tag; } @@ -129,6 +135,12 @@ public: ~SongFilter(); + /** + * Convert this object into an "expression". This is + * only useful for debugging. + */ + std::string ToExpression() const noexcept; + private: gcc_nonnull(2,3) void Parse(const char *tag, const char *value, bool fold_case=false);