use std::string instead of ot::string_view

String views are cool, but let's play it safe and standard for now. The
impact on performance is insignificant, since most of the job is reading
the ogg file, not actually manipulating tags.
This commit is contained in:
Frédéric Mangano-Tarumi 2018-11-10 11:30:30 -05:00
parent 1b9bd83e8f
commit 2a31c5491b
4 changed files with 14 additions and 35 deletions

View File

@ -48,7 +48,7 @@ ot::status ot::parse_tags(const char *data, long len, opus_tags *tags)
size_t vendor_length = le32toh(*((uint32_t*) (data + pos)));
if (pos + 4 + vendor_length > len)
return status::overflowing_vendor_data;
tags->vendor = ot::string_view(data + pos + 4, vendor_length);
tags->vendor = std::string(data + pos + 4, vendor_length);
pos += 4 + tags->vendor.size();
// Count
if (pos + 4 > len)
@ -67,7 +67,7 @@ ot::status ot::parse_tags(const char *data, long len, opus_tags *tags)
pos += 4 + comment_length;
}
// Extra data
tags->extra_data = ot::string_view(data + pos, static_cast<size_t>(len - pos));
tags->extra_data = std::string(data + pos, static_cast<size_t>(len - pos));
return status::ok;
}
@ -79,7 +79,7 @@ int ot::render_tags(opus_tags *tags, ogg_packet *op)
op->granulepos = 0;
op->packetno = 1;
long len = 8 + 4 + tags->vendor.size() + 4;
for (const string_view &comment : tags->comments)
for (const std::string& comment : tags->comments)
len += 4 + comment.size();
len += tags->extra_data.size();
op->bytes = len;
@ -96,7 +96,7 @@ int ot::render_tags(opus_tags *tags, ogg_packet *op)
n = htole32(tags->comments.size());
memcpy(data, &n, 4);
data += 4;
for (const string_view &comment : tags->comments) {
for (const std::string& comment : tags->comments) {
n = htole32(comment.size());
memcpy(data, &n, 4);
memcpy(data+4, comment.data(), comment.size());

View File

@ -72,7 +72,7 @@ struct opustags_options {
*/
static void print_tags(ot::opus_tags &tags)
{
for (const ot::string_view &comment : tags.comments) {
for (const std::string& comment : tags.comments) {
fwrite(comment.data(), 1, comment.size(), stdout);
puts("");
}

View File

@ -13,29 +13,6 @@
namespace ot {
/**
* Non-owning string.
*
* The interface is meant to be compatible with std::string_view from C++17, so
* that we'll be able to delete it as soon as C++17 is widely supported.
*/
class string_view {
public:
string_view() : _data(nullptr), _size(0) {}
string_view(const char* data) : _data(data), _size(strlen(data)) {}
string_view(const char* data, size_t size) : _data(data), _size(size) {}
string_view(const std::string& str) : _data(str.data()), _size(str.size()) {}
const char* data() const { return _data; }
size_t size() const { return _size; }
bool operator==(const string_view &other) const {
return _size == other._size && memcmp(_data, other._data, _size) == 0;
}
bool operator!=(const string_view& other) const { return !(*this == other); }
private:
const char* _data;
size_t _size;
};
/**
* Possible error status.
*
@ -175,7 +152,7 @@ struct opus_tags {
* OpusTags packets begin with a vendor string, meant to identify the
* implementation of the encoder. It is an arbitrary UTF-8 string.
*/
string_view vendor;
std::string vendor;
/**
* Comments. These are a list of string following the NAME=Value format.
* A comment may also be called a field, or a tag.
@ -183,7 +160,7 @@ struct opus_tags {
* The field name in vorbis comment is case-insensitive and ASCII,
* while the value can be any valid UTF-8 string.
*/
std::list<string_view> comments;
std::list<std::string> comments;
/**
* According to RFC 7845:
* > Immediately following the user comment list, the comment header MAY contain
@ -195,7 +172,7 @@ struct opus_tags {
* In the future, we could add options to manipulate this data: view it, edit it, truncate
* it if it's marked as padding, truncate it unconditionally.
*/
string_view extra_data;
std::string extra_data;
};
status parse_tags(const char *data, long len, opus_tags *tags);

View File

@ -12,6 +12,8 @@
#include <exception>
#include <iostream>
using namespace std::literals::string_literals;
class failure : public std::runtime_error {
public:
failure(const char *message) : std::runtime_error(message) {}
@ -42,15 +44,15 @@ static bool parse_standard()
auto rc = ot::parse_tags(standard_OpusTags, sizeof(standard_OpusTags) - 1, &tags);
if (rc != ot::status::ok)
throw failure("ot::parse_tags did not return ok");
if (tags.vendor != ot::string_view( "opustags test packet"))
if (tags.vendor != "opustags test packet")
throw failure("bad vendor string");
if (tags.comments.size() != 2)
throw failure("bad number of comments");
auto it = tags.comments.begin();
if (*it != ot::string_view("TITLE=Foo"))
if (*it != "TITLE=Foo")
throw failure("bad title");
++it;
if (*it != ot::string_view("ARTIST=Bar"))
if (*it != "ARTIST=Bar")
throw failure("bad artist");
if (tags.extra_data.size() != 0)
throw failure("found mysterious padding data");
@ -139,7 +141,7 @@ static bool recode_padding()
auto rc = ot::parse_tags(padded_OpusTags.data(), padded_OpusTags.size(), &tags);
if (rc != ot::status::ok)
throw failure("ot::parse_tags did not return ok");
if (tags.extra_data != ot::string_view("\0hello", 6))
if (tags.extra_data != "\0hello"s)
throw failure("corrupted extra data");
// recode the packet and ensure it's exactly the same
ogg_packet packet;