playlist/cue/CueParser: more std::string_view

This commit is contained in:
Max Kellermann 2022-06-30 18:00:59 +02:00
parent e921c0b40b
commit 21e4c25e61
1 changed files with 54 additions and 55 deletions

View File

@ -20,77 +20,78 @@
#include "CueParser.hxx" #include "CueParser.hxx"
#include "tag/ParseName.hxx" #include "tag/ParseName.hxx"
#include "util/CharUtil.hxx" #include "util/CharUtil.hxx"
#include "util/StringView.hxx" #include "util/StringStrip.hxx"
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
static StringView using std::string_view_literals::operator""sv;
cue_next_word(StringView &src) noexcept
static std::string_view
cue_next_word(std::string_view &src) noexcept
{ {
assert(!src.empty()); assert(!src.empty());
assert(!IsWhitespaceNotNull(src.front())); assert(!IsWhitespaceNotNull(src.front()));
auto end = std::find_if(src.begin(), src.end(), auto end = std::find_if(src.begin(), src.end(),
[](char ch){ return IsWhitespaceOrNull(ch); }); [](char ch){ return IsWhitespaceOrNull(ch); });
StringView word(src.begin(), end); auto word = src.substr(0, std::distance(src.begin(), end));
src = src.substr(end); src = src.substr(std::distance(src.begin(), end));
return word; return word;
} }
static StringView static std::string_view
cue_next_quoted(StringView &src) noexcept cue_next_quoted(std::string_view &src) noexcept
{ {
assert(src.data[-1] == '"'); assert(src.data()[-1] == '"');
auto end = src.Find('"'); auto end = src.find('"');
if (end == nullptr) if (end == src.npos)
/* syntax error - ignore it silently */ /* syntax error - ignore it silently */
return std::exchange(src, nullptr); return std::exchange(src, nullptr);
StringView value(src.data, end); auto value = src.substr(0, end);
src = src.substr(end + 1); src = src.substr(end + 1);
return value; return value;
} }
static StringView static std::string_view
cue_next_token(StringView &src) noexcept cue_next_token(std::string_view &src) noexcept
{ {
src.StripLeft(); src = StripLeft(src);
if (src.empty()) if (src.empty())
return nullptr; return {};
return cue_next_word(src); return cue_next_word(src);
} }
static StringView static std::string_view
cue_next_value(StringView &src) noexcept cue_next_value(std::string_view &src) noexcept
{ {
src.StripLeft(); src = StripLeft(src);
if (src.empty()) if (src.empty())
return nullptr; return {};
if (src.front() == '"') { if (src.front() == '"') {
src.pop_front(); src.remove_prefix(1);
return cue_next_quoted(src); return cue_next_quoted(src);
} else } else
return cue_next_word(src); return cue_next_word(src);
} }
static void static void
cue_add_tag(TagBuilder &tag, TagType type, StringView src) noexcept cue_add_tag(TagBuilder &tag, TagType type, std::string_view src) noexcept
{ {
auto value = cue_next_value(src); auto value = cue_next_value(src);
if (value != nullptr) if (value.data() != nullptr)
tag.AddItem(type, value); tag.AddItem(type, value);
} }
static void static void
cue_parse_rem(StringView src, TagBuilder &tag) noexcept cue_parse_rem(std::string_view src, TagBuilder &tag) noexcept
{ {
auto type = cue_next_token(src); auto type = cue_next_token(src);
if (type == nullptr) if (type.data() == nullptr)
return; return;
TagType type2 = tag_name_parse_i(type); TagType type2 = tag_name_parse_i(type);
@ -116,10 +117,10 @@ IsDigit(std::string_view s) noexcept
} }
static unsigned static unsigned
cue_next_unsigned(StringView &src) noexcept cue_next_unsigned(std::string_view &src) noexcept
{ {
if (!IsDigit(src)) { if (!IsDigit(src)) {
src = nullptr; src = {};
return 0; return 0;
} }
@ -127,7 +128,7 @@ cue_next_unsigned(StringView &src) noexcept
do { do {
char ch = src.front(); char ch = src.front();
src.pop_front(); src.remove_prefix(1);
value = value * 10u + unsigned(ch - '0'); value = value * 10u + unsigned(ch - '0');
} while (IsDigit(src)); } while (IsDigit(src));
@ -136,20 +137,20 @@ cue_next_unsigned(StringView &src) noexcept
} }
static int static int
cue_parse_position(StringView src) noexcept cue_parse_position(std::string_view src) noexcept
{ {
unsigned minutes = cue_next_unsigned(src); unsigned minutes = cue_next_unsigned(src);
if (src.empty() || src.front() != ':') if (src.empty() || src.front() != ':')
return -1; return -1;
src.pop_front(); src.remove_prefix(1);
unsigned seconds = cue_next_unsigned(src); unsigned seconds = cue_next_unsigned(src);
if (src.empty() || src.front() != ':') if (src.empty() || src.front() != ':')
return -1; return -1;
src.pop_front(); src.remove_prefix(1);
unsigned long frames = cue_next_unsigned(src); unsigned long frames = cue_next_unsigned(src);
if (src == nullptr || !src.empty()) if (src.data() == nullptr || !src.empty())
return -1; return -1;
return minutes * 60000 + seconds * 1000 + frames * 1000 / 75; return minutes * 60000 + seconds * 1000 + frames * 1000 / 75;
@ -174,21 +175,19 @@ CueParser::Commit() noexcept
} }
void void
CueParser::Feed(std::string_view _src) noexcept CueParser::Feed(std::string_view src) noexcept
{ {
assert(!end); assert(!end);
StringView src{_src}; const auto command = cue_next_token(src);
if (command.data() == nullptr)
auto command = cue_next_token(src);
if (command == nullptr)
return; return;
if (command.Equals("REM")) { if (command == "REM"sv) {
TagBuilder *tag = GetCurrentTag(); TagBuilder *tag = GetCurrentTag();
if (tag != nullptr) if (tag != nullptr)
cue_parse_rem(src, *tag); cue_parse_rem(src, *tag);
} else if (command.Equals("PERFORMER")) { } else if (command == "PERFORMER"sv) {
/* MPD knows a "performer" tag, but it is not a good /* MPD knows a "performer" tag, but it is not a good
match for this CUE tag; from the Hydrogenaudio match for this CUE tag; from the Hydrogenaudio
Knowledgebase: "At top-level this will specify the Knowledgebase: "At top-level this will specify the
@ -202,26 +201,26 @@ CueParser::Feed(std::string_view _src) noexcept
TagBuilder *tag = GetCurrentTag(); TagBuilder *tag = GetCurrentTag();
if (tag != nullptr) if (tag != nullptr)
cue_add_tag(*tag, type, src); cue_add_tag(*tag, type, src);
} else if (command.Equals("TITLE")) { } else if (command == "TITLE"sv) {
if (state == HEADER) if (state == HEADER)
cue_add_tag(header_tag, TAG_ALBUM, src); cue_add_tag(header_tag, TAG_ALBUM, src);
else if (state == TRACK) else if (state == TRACK)
cue_add_tag(song_tag, TAG_TITLE, src); cue_add_tag(song_tag, TAG_TITLE, src);
} else if (command.Equals("FILE")) { } else if (command == "FILE"sv) {
Commit(); Commit();
const auto new_filename = cue_next_value(src); const auto new_filename = cue_next_value(src);
if (new_filename == nullptr) if (new_filename.data() == nullptr)
return; return;
const auto type = cue_next_token(src); const auto type = cue_next_token(src);
if (type == nullptr) if (type.data() == nullptr)
return; return;
if (!type.Equals("WAVE") && if (type != "WAVE"sv &&
!type.Equals("FLAC") && /* non-standard */ type != "FLAC"sv && /* non-standard */
!type.Equals("MP3") && type != "MP3"sv &&
!type.Equals("AIFF")) { type != "AIFF"sv) {
state = IGNORE_FILE; state = IGNORE_FILE;
return; return;
} }
@ -230,18 +229,18 @@ CueParser::Feed(std::string_view _src) noexcept
filename = new_filename; filename = new_filename;
} else if (state == IGNORE_FILE) { } else if (state == IGNORE_FILE) {
return; return;
} else if (command.Equals("TRACK")) { } else if (command == "TRACK"sv) {
Commit(); Commit();
const auto nr = cue_next_token(src); const auto nr = cue_next_token(src);
if (nr == nullptr) if (nr.data() == nullptr)
return; return;
const auto type = cue_next_token(src); const auto type = cue_next_token(src);
if (type == nullptr) if (type.data() == nullptr)
return; return;
if (!type.Equals("AUDIO")) { if (type != "AUDIO"sv) {
state = IGNORE_TRACK; state = IGNORE_TRACK;
return; return;
} }
@ -256,16 +255,16 @@ CueParser::Feed(std::string_view _src) noexcept
} else if (state == IGNORE_TRACK) { } else if (state == IGNORE_TRACK) {
return; return;
} else if (state == TRACK && command.Equals("INDEX")) { } else if (state == TRACK && command == "INDEX"sv) {
if (ignore_index) if (ignore_index)
return; return;
const auto nr = cue_next_token(src); const auto nr = cue_next_token(src);
if (nr == nullptr) if (nr.data() == nullptr)
return; return;
const auto position = cue_next_token(src); const auto position = cue_next_token(src);
if (position == nullptr) if (position.data() == nullptr)
return; return;
int position_ms = cue_parse_position(position); int position_ms = cue_parse_position(position);
@ -278,7 +277,7 @@ CueParser::Feed(std::string_view _src) noexcept
if (current != nullptr) if (current != nullptr)
current->SetStartTime(SongTime::FromMS(position_ms)); current->SetStartTime(SongTime::FromMS(position_ms));
if (!nr.Equals("00") || previous == nullptr) if (nr != "00"sv || previous == nullptr)
ignore_index = true; ignore_index = true;
} }
} }