move single tag parsing outside of Tags class

This commit is contained in:
Frédéric Mangano 2016-03-04 15:21:05 +01:00
parent 4cd0e34d0d
commit cd550d8d80
4 changed files with 23 additions and 16 deletions

View File

@ -122,7 +122,7 @@ void ogg::Stream::parse_opustags(const ogg_packet &op)
uint32_t comment_length = le32toh(*reinterpret_cast<uint32_t*>(data));
if (remaining - 4 < comment_length)
throw std::runtime_error("no space for comment contents");
tags.add(std::string(data + 4, comment_length));
tags.add(parse_tag(std::string(data + 4, comment_length)));
data += 4 + comment_length;
remaining -= 4 + comment_length;
}

View File

@ -27,6 +27,11 @@ std::string Tags::get(const std::string &key) const
throw std::runtime_error("Tag '" + key + "' not found.");
}
void Tags::add(const Tag &tag)
{
tags.push_back(tag);
}
void Tags::add(const std::string &key, const std::string &value)
{
tags.push_back({key, value});
@ -37,16 +42,6 @@ void Tags::clear()
tags.clear();
}
void Tags::add(const std::string &assoc)
{
size_t eq = assoc.find_first_of('=');
if (eq == std::string::npos)
throw std::runtime_error("misconstructed tag");
std::string name = assoc.substr(0, eq);
std::string value = assoc.substr(eq + 1);
add(name, value);
}
void Tags::remove(const std::string &key)
{
std::vector<Tag> new_tags;
@ -65,3 +60,13 @@ bool Tags::contains(const std::string &key) const
tags.end(),
[&](const Tag &tag) { return iequals(tag.key, key); }) > 0;
}
Tag opustags::parse_tag(const std::string &assoc)
{
size_t eq = assoc.find_first_of('=');
if (eq == std::string::npos)
throw std::runtime_error("misconstructed tag");
std::string name = assoc.substr(0, eq);
std::string value = assoc.substr(eq + 1);
return { name, value };
}

View File

@ -19,8 +19,8 @@ namespace opustags {
const std::vector<Tag> get_all() const;
std::string get(const std::string &key) const;
void add(const Tag &tag);
void add(const std::string &key, const std::string &value);
void add(const std::string &assoc); // KEY=value
void remove(const std::string &key);
bool contains(const std::string &key) const;
void clear();
@ -35,4 +35,6 @@ namespace opustags {
std::vector<Tag> tags;
};
Tag parse_tag(const std::string &assoc); // KEY=value
}

View File

@ -69,10 +69,10 @@ TEST_CASE("tag manipulation", "[tags]")
REQUIRE(tags.get_all().empty());
}
SECTION("raw set") {
Tags tags;
tags.add("TITLE=Foo=Bar");
REQUIRE(tags.get("TITLE") == "Foo=Bar");
SECTION("tag parsing") {
Tag t = parse_tag("TITLE=Foo=Bar");
REQUIRE(t.key == "TITLE");
REQUIRE(t.value == "Foo=Bar");
}
SECTION("case insensitiveness for keys") {