Tags: make keys case insensitive

This commit is contained in:
rr- 2016-03-03 12:25:32 +01:00
parent 449235ed5a
commit 32c5e2b0a6
2 changed files with 20 additions and 7 deletions

View File

@ -3,6 +3,17 @@
using namespace opustags;
// ASCII only, but for tag keys it's good enough
static bool iequals(const std::string &a, const std::string &b)
{
if (a.size() != b.size())
return false;
for (size_t i = 0; i < a.size(); i++)
if (std::tolower(a[i]) != std::tolower(b[i]))
return false;
return true;
}
const std::vector<Tag> Tags::get_all() const
{
return tags;
@ -11,7 +22,7 @@ const std::vector<Tag> Tags::get_all() const
std::string Tags::get(const std::string &key) const
{
for (auto &tag : tags)
if (tag.key == key)
if (iequals(tag.key, key))
return tag.value;
throw std::runtime_error("Tag '" + key + "' not found.");
}
@ -43,7 +54,7 @@ void Tags::remove(const std::string &key)
tags.begin(),
tags.end(),
std::back_inserter(new_tags),
[&](const Tag &tag) { return tag.key != key; });
[&](const Tag &tag) { return !iequals(tag.key, key); });
tags = new_tags;
}
@ -52,5 +63,5 @@ bool Tags::contains(const std::string &key) const
return std::count_if(
tags.begin(),
tags.end(),
[&](const Tag &tag) { return tag.key == key; }) > 0;
[&](const Tag &tag) { return iequals(tag.key, key); }) > 0;
}

View File

@ -22,7 +22,7 @@ TEST_CASE("Tag manipulation test", "[tags]")
tags.add("b", "2");
REQUIRE(tags.get_all().size() == 2);
tags.clear();
REQUIRE(tags.get_all().size() == 0);
REQUIRE(tags.get_all().empty());
}
SECTION("Maintaing order of insertions") {
@ -66,7 +66,7 @@ TEST_CASE("Tag manipulation test", "[tags]")
tags.add("ARTIST", "You");
tags.add("ARTIST", "Me");
tags.remove("ARTIST");
REQUIRE(tags.get_all().size() == 0);
REQUIRE(tags.get_all().empty());
}
SECTION("Raw set") {
@ -77,7 +77,9 @@ TEST_CASE("Tag manipulation test", "[tags]")
SECTION("Case insensitiveness for keys") {
Tags tags;
tags.add("TiTlE=Boop");
REQUIRE(tags.get("tiTLE") == "Boop");
tags.add("TITLE", "Boop");
REQUIRE(tags.get("title") == "Boop");
tags.remove("titLE");
REQUIRE(tags.get_all().empty());
}
}