tag/Handler: pass StringView to OnTag() and OnPair()

Eliminates a number of allocations, because callers don't need to copy
the strings to a newly allocated buffer only to null-terminate them.
And most callers don't need to have a null-terminated string.
This commit is contained in:
Max Kellermann
2019-06-06 12:02:55 +02:00
parent 76eb550011
commit 548aa00111
19 changed files with 108 additions and 62 deletions

View File

@@ -30,7 +30,7 @@
#include "tag/ReplayGain.hxx"
#include "tag/MixRamp.hxx"
#include "ReplayGainInfo.hxx"
#include "util/DivideString.hxx"
#include "util/StringView.hxx"
#include <assert.h>
@@ -98,10 +98,10 @@ flac_scan_comment(const FLAC__StreamMetadata_VorbisComment_Entry *entry,
TagHandler &handler) noexcept
{
if (handler.WantPair()) {
const char *comment = (const char *)entry->entry;
const DivideString split(comment, '=');
if (split.IsDefined() && !split.empty())
handler.OnPair(split.GetFirst(), split.GetSecond());
const StringView comment((const char *)entry->entry);
const auto split = StringView(comment).Split('=');
if (!split.first.empty() && !split.second.IsNull())
handler.OnPair(split.first, split.second);
}
for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i)

View File

@@ -26,7 +26,7 @@
#include "tag/VorbisComment.hxx"
#include "tag/ReplayGain.hxx"
#include "ReplayGainInfo.hxx"
#include "util/DivideString.hxx"
#include "util/StringView.hxx"
bool
vorbis_comments_to_replay_gain(ReplayGainInfo &rgi, char **comments) noexcept
@@ -69,9 +69,9 @@ static void
vorbis_scan_comment(const char *comment, TagHandler &handler) noexcept
{
if (handler.WantPair()) {
const DivideString split(comment, '=');
if (split.IsDefined() && !split.empty())
handler.OnPair(split.GetFirst(), split.GetSecond());
const auto split = StringView(comment).Split('=');
if (!split.first.empty() && !split.second.IsNull())
handler.OnPair(split.first, split.second);
}
for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i)