string_view: expose data and size as functions

Be consistent with C++17.
This commit is contained in:
Frédéric Mangano-Tarumi 2018-11-06 21:00:43 -05:00
parent d9b96d471d
commit 1744cab9ed
3 changed files with 31 additions and 27 deletions

View File

@ -43,9 +43,8 @@ int ot::parse_tags(const char *data, long len, opus_tags *tags)
return -1;
// Vendor
pos = 8;
tags->vendor.size = le32toh(*((uint32_t*) (data + pos)));
tags->vendor.data = data + pos + 4;
pos += 4 + tags->vendor.size;
tags->vendor = ot::string_view(data + pos + 4, le32toh(*((uint32_t*) (data + pos))));
pos += 4 + tags->vendor.size();
if (pos + 4 > len)
return -1;
// Count
@ -72,10 +71,10 @@ int ot::render_tags(opus_tags *tags, ogg_packet *op)
op->e_o_s = 0;
op->granulepos = 0;
op->packetno = 1;
long len = 8 + 4 + tags->vendor.size + 4;
long len = 8 + 4 + tags->vendor.size() + 4;
for (const string_view &comment : tags->comments)
len += 4 + comment.size;
len += tags->extra_data.size;
len += 4 + comment.size();
len += tags->extra_data.size();
op->bytes = len;
char *data = static_cast<char*>(malloc(len));
if (!data)
@ -83,20 +82,20 @@ int ot::render_tags(opus_tags *tags, ogg_packet *op)
op->packet = (unsigned char*) data;
uint32_t n;
memcpy(data, "OpusTags", 8);
n = htole32(tags->vendor.size);
n = htole32(tags->vendor.size());
memcpy(data+8, &n, 4);
memcpy(data+12, tags->vendor.data, tags->vendor.size);
data += 12 + tags->vendor.size;
memcpy(data+12, tags->vendor.data(), tags->vendor.size());
data += 12 + tags->vendor.size();
n = htole32(tags->comments.size());
memcpy(data, &n, 4);
data += 4;
for (const string_view &comment : tags->comments) {
n = htole32(comment.size);
n = htole32(comment.size());
memcpy(data, &n, 4);
memcpy(data+4, comment.data, comment.size);
data += 4 + comment.size;
memcpy(data+4, comment.data(), comment.size());
data += 4 + comment.size();
}
memcpy(data, tags->extra_data.data, tags->extra_data.size);
memcpy(data, tags->extra_data.data(), tags->extra_data.size());
return 0;
}
@ -121,7 +120,7 @@ void ot::delete_tags(opus_tags *tags, const char *field)
auto it = tags->comments.begin(), end = tags->comments.end();
while (it != end) {
auto current = it++;
if (match_field(current->data, current->size, field))
if (match_field(current->data(), current->size(), field))
tags->comments.erase(current);
}
}
@ -139,7 +138,7 @@ int ot::add_tags(opus_tags *tags, const char **tags_to_add, uint32_t count)
void ot::print_tags(opus_tags *tags)
{
for (const string_view &comment : tags->comments) {
fwrite(comment.data, 1, comment.size, stdout);
fwrite(comment.data(), 1, comment.size(), stdout);
puts("");
}
}

View File

@ -13,18 +13,25 @@
namespace ot {
/**
* Non-owning string, similar to what std::string_view is in C++17.
* 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.
*/
struct string_view {
string_view() {};
string_view(const char *data) : data(data), size(strlen(data)) {};
string_view(const char *data, size_t size) : data(data), size(size) {}
class string_view {
public:
string_view() {}
string_view(const char *data) : _data(data), _size(strlen(data)) {}
string_view(const char *data, size_t size) : _data(data), _size(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;
return _size == other._size && memcmp(_data, other._data, _size) == 0;
}
bool operator!=(const string_view &other) const { return !(*this == other); }
const char *data = nullptr;
size_t size = 0;
private:
const char *_data = nullptr;
size_t _size = 0;
};
/**

View File

@ -52,7 +52,7 @@ static bool parse_standard()
++it;
if (*it != ot::string_view("ARTIST=Bar"))
throw failure("bad artist");
if (tags.extra_data.size != 0)
if (tags.extra_data.size() != 0)
throw failure("found mysterious padding data");
return true;
}
@ -92,9 +92,7 @@ static bool recode_padding()
int rc = ot::parse_tags(padded_OpusTags.data(), padded_OpusTags.size(), &tags);
if (rc != 0)
throw failure("ot::parse_tags did not return 0");
if (tags.extra_data.size != 6)
throw failure("unexpected amount of extra bytes");
if (memcmp(tags.extra_data.data, "\0hello", 6) != 0)
if (tags.extra_data != ot::string_view("\0hello", 6))
throw failure("corrupted extra data");
// recode the packet and ensure it's exactly the same
ogg_packet packet;