SongFilter: add ToExpression()

This commit is contained in:
Max Kellermann 2018-07-24 22:11:14 +02:00
parent a2b24cb5a2
commit efb2051b30
2 changed files with 54 additions and 0 deletions

View File

@ -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)
{

View File

@ -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);