reduce match_field into delete_comments

This commit is contained in:
Frédéric Mangano-Tarumi 2018-12-16 18:41:20 -05:00
parent 7df8c5c426
commit 6797e59417
2 changed files with 6 additions and 19 deletions

View File

@ -122,28 +122,15 @@ ot::dynamic_ogg_packet ot::render_tags(const opus_tags& tags)
return op;
}
/**
* \todo Make the field name case-insensitive?
*/
static int match_field(const char *comment, uint32_t len, const char *field)
{
size_t field_len;
for (field_len = 0; field[field_len] != '\0' && field[field_len] != '='; field_len++);
if (len <= field_len)
return 0;
if (comment[field_len] != '=')
return 0;
if (strncmp(comment, field, field_len) != 0)
return 0;
return 1;
}
void ot::delete_comments(opus_tags& tags, const char* field_name)
void ot::delete_comments(opus_tags& tags, const std::string& field_name)
{
auto field_len = field_name.size();
auto it = tags.comments.begin(), end = tags.comments.end();
while (it != end) {
auto current = it++;
if (match_field(current->data(), current->size(), field_name))
if (current->size() > field_len + 1 &&
(*current)[field_len] == '=' &&
strncmp(current->data(), field_name.data(), field_len) == 0)
tags.comments.erase(current);
}
}

View File

@ -363,7 +363,7 @@ dynamic_ogg_packet render_tags(const opus_tags& tags);
* \todo Move to module cli.
* \todo Accept fields like X=Y to remove only comments X=Y, instead of all X.
*/
void delete_comments(opus_tags& tags, const char* field_name);
void delete_comments(opus_tags& tags, const std::string& field_name);
/** \} */