input/qobuz: use std::string_view

This commit is contained in:
Max Kellermann 2022-07-01 11:02:10 +02:00
parent c9723ee4b7
commit c05d4cddfb
5 changed files with 46 additions and 36 deletions

View File

@ -22,6 +22,9 @@
#include "util/ConstBuffer.hxx" #include "util/ConstBuffer.hxx"
#include "util/RuntimeError.hxx" #include "util/RuntimeError.hxx"
using std::string_view_literals::operator""sv;
using Wrapper = Yajl::CallbacksWrapper<QobuzErrorParser>; using Wrapper = Yajl::CallbacksWrapper<QobuzErrorParser>;
static constexpr yajl_callbacks qobuz_error_parser_callbacks = { static constexpr yajl_callbacks qobuz_error_parser_callbacks = {
nullptr, nullptr,
@ -60,14 +63,14 @@ QobuzErrorParser::OnEnd()
} }
inline bool inline bool
QobuzErrorParser::String(StringView value) noexcept QobuzErrorParser::String(std::string_view value) noexcept
{ {
switch (state) { switch (state) {
case State::NONE: case State::NONE:
break; break;
case State::MESSAGE: case State::MESSAGE:
message.assign(value.data, value.size); message = value;
break; break;
} }
@ -75,9 +78,9 @@ QobuzErrorParser::String(StringView value) noexcept
} }
inline bool inline bool
QobuzErrorParser::MapKey(StringView value) noexcept QobuzErrorParser::MapKey(std::string_view value) noexcept
{ {
if (value.Equals("message")) if (value == "message"sv)
state = State::MESSAGE; state = State::MESSAGE;
else else
state = State::NONE; state = State::NONE;

View File

@ -23,8 +23,9 @@
#include "lib/curl/Headers.hxx" #include "lib/curl/Headers.hxx"
#include "lib/yajl/ResponseParser.hxx" #include "lib/yajl/ResponseParser.hxx"
#include <string_view>
template<typename T> struct ConstBuffer; template<typename T> struct ConstBuffer;
struct StringView;
/** /**
* Parse an error JSON response. * Parse an error JSON response.
@ -53,8 +54,8 @@ protected:
public: public:
/* yajl callbacks */ /* yajl callbacks */
bool String(StringView value) noexcept; bool String(std::string_view value) noexcept;
bool MapKey(StringView value) noexcept; bool MapKey(std::string_view value) noexcept;
bool EndMap() noexcept; bool EndMap() noexcept;
}; };

View File

@ -25,6 +25,8 @@
#include <cassert> #include <cassert>
using std::string_view_literals::operator""sv;
using Wrapper = Yajl::CallbacksWrapper<QobuzLoginRequest::ResponseParser>; using Wrapper = Yajl::CallbacksWrapper<QobuzLoginRequest::ResponseParser>;
static constexpr yajl_callbacks parse_callbacks = { static constexpr yajl_callbacks parse_callbacks = {
nullptr, nullptr,
@ -59,9 +61,9 @@ public:
QobuzSession &&GetSession(); QobuzSession &&GetSession();
/* yajl callbacks */ /* yajl callbacks */
bool String(StringView value) noexcept; bool String(std::string_view value) noexcept;
bool StartMap() noexcept; bool StartMap() noexcept;
bool MapKey(StringView value) noexcept; bool MapKey(std::string_view value) noexcept;
bool EndMap() noexcept; bool EndMap() noexcept;
}; };
@ -161,7 +163,7 @@ QobuzLoginRequest::OnError(std::exception_ptr e) noexcept
} }
inline bool inline bool
QobuzLoginRequest::ResponseParser::String(StringView value) noexcept QobuzLoginRequest::ResponseParser::String(std::string_view value) noexcept
{ {
switch (state) { switch (state) {
case State::NONE: case State::NONE:
@ -169,11 +171,11 @@ QobuzLoginRequest::ResponseParser::String(StringView value) noexcept
break; break;
case State::DEVICE_ID: case State::DEVICE_ID:
session.device_id.assign(value.data, value.size); session.device_id = value;
break; break;
case State::USER_AUTH_TOKEN: case State::USER_AUTH_TOKEN:
session.user_auth_token.assign(value.data, value.size); session.user_auth_token = value;
break; break;
} }
@ -200,13 +202,13 @@ QobuzLoginRequest::ResponseParser::StartMap() noexcept
} }
inline bool inline bool
QobuzLoginRequest::ResponseParser::MapKey(StringView value) noexcept QobuzLoginRequest::ResponseParser::MapKey(std::string_view value) noexcept
{ {
switch (state) { switch (state) {
case State::NONE: case State::NONE:
if (value.Equals("user_auth_token")) if (value == "user_auth_token"sv)
state = State::USER_AUTH_TOKEN; state = State::USER_AUTH_TOKEN;
else if (value.Equals("device")) { else if (value == "device"sv) {
state = State::DEVICE; state = State::DEVICE;
map_depth = 0; map_depth = 0;
} }
@ -214,7 +216,7 @@ QobuzLoginRequest::ResponseParser::MapKey(StringView value) noexcept
break; break;
case State::DEVICE: case State::DEVICE:
if (value.Equals("id")) if (value == "id"sv)
state = State::DEVICE_ID; state = State::DEVICE_ID;
break; break;

View File

@ -24,6 +24,8 @@
#include "tag/Builder.hxx" #include "tag/Builder.hxx"
#include "tag/Tag.hxx" #include "tag/Tag.hxx"
using std::string_view_literals::operator""sv;
using Wrapper = Yajl::CallbacksWrapper<QobuzTagScanner::ResponseParser>; using Wrapper = Yajl::CallbacksWrapper<QobuzTagScanner::ResponseParser>;
static constexpr yajl_callbacks parse_callbacks = { static constexpr yajl_callbacks parse_callbacks = {
nullptr, nullptr,
@ -68,9 +70,9 @@ public:
/* yajl callbacks */ /* yajl callbacks */
bool Integer(long long value) noexcept; bool Integer(long long value) noexcept;
bool String(StringView value) noexcept; bool String(std::string_view value) noexcept;
bool StartMap() noexcept; bool StartMap() noexcept;
bool MapKey(StringView value) noexcept; bool MapKey(std::string_view value) noexcept;
bool EndMap() noexcept; bool EndMap() noexcept;
}; };
@ -142,7 +144,7 @@ QobuzTagScanner::ResponseParser::Integer(long long value) noexcept
} }
inline bool inline bool
QobuzTagScanner::ResponseParser::String(StringView value) noexcept QobuzTagScanner::ResponseParser::String(std::string_view value) noexcept
{ {
switch (state) { switch (state) {
case State::TITLE: case State::TITLE:
@ -185,19 +187,19 @@ QobuzTagScanner::ResponseParser::StartMap() noexcept
} }
inline bool inline bool
QobuzTagScanner::ResponseParser::MapKey(StringView value) noexcept QobuzTagScanner::ResponseParser::MapKey(std::string_view value) noexcept
{ {
switch (map_depth) { switch (map_depth) {
case 1: case 1:
if (value.Equals("composer")) if (value == "composer"sv)
state = State::COMPOSER; state = State::COMPOSER;
else if (value.Equals("duration")) else if (value == "duration"sv)
state = State::DURATION; state = State::DURATION;
else if (value.Equals("title")) else if (value == "title"sv)
state = State::TITLE; state = State::TITLE;
else if (value.Equals("album")) else if (value == "album"sv)
state = State::ALBUM; state = State::ALBUM;
else if (value.Equals("performer")) else if (value == "performer"sv)
state = State::PERFORMER; state = State::PERFORMER;
else else
state = State::NONE; state = State::NONE;
@ -212,7 +214,7 @@ QobuzTagScanner::ResponseParser::MapKey(StringView value) noexcept
case State::COMPOSER: case State::COMPOSER:
case State::COMPOSER_NAME: case State::COMPOSER_NAME:
if (value.Equals("name")) if (value == "name"sv)
state = State::COMPOSER_NAME; state = State::COMPOSER_NAME;
else else
state = State::COMPOSER; state = State::COMPOSER;
@ -222,9 +224,9 @@ QobuzTagScanner::ResponseParser::MapKey(StringView value) noexcept
case State::ALBUM_TITLE: case State::ALBUM_TITLE:
case State::ALBUM_ARTIST: case State::ALBUM_ARTIST:
case State::ALBUM_ARTIST_NAME: case State::ALBUM_ARTIST_NAME:
if (value.Equals("title")) if (value == "title"sv)
state = State::ALBUM_TITLE; state = State::ALBUM_TITLE;
else if (value.Equals("artist")) else if (value == "artist"sv)
state = State::ALBUM_ARTIST; state = State::ALBUM_ARTIST;
else else
state = State::ALBUM; state = State::ALBUM;
@ -232,7 +234,7 @@ QobuzTagScanner::ResponseParser::MapKey(StringView value) noexcept
case State::PERFORMER: case State::PERFORMER:
case State::PERFORMER_NAME: case State::PERFORMER_NAME:
if (value.Equals("name")) if (value == "name"sv)
state = State::PERFORMER_NAME; state = State::PERFORMER_NAME;
else else
state = State::PERFORMER; state = State::PERFORMER;
@ -247,7 +249,7 @@ QobuzTagScanner::ResponseParser::MapKey(StringView value) noexcept
switch (state) { switch (state) {
case State::ALBUM_ARTIST: case State::ALBUM_ARTIST:
case State::ALBUM_ARTIST_NAME: case State::ALBUM_ARTIST_NAME:
if (value.Equals("name")) if (value == "name"sv)
state = State::ALBUM_ARTIST_NAME; state = State::ALBUM_ARTIST_NAME;
else else
state = State::ALBUM_ARTIST; state = State::ALBUM_ARTIST;

View File

@ -22,6 +22,8 @@
#include "QobuzClient.hxx" #include "QobuzClient.hxx"
#include "lib/yajl/Callbacks.hxx" #include "lib/yajl/Callbacks.hxx"
using std::string_view_literals::operator""sv;
using Wrapper = Yajl::CallbacksWrapper<QobuzTrackRequest::ResponseParser>; using Wrapper = Yajl::CallbacksWrapper<QobuzTrackRequest::ResponseParser>;
static constexpr yajl_callbacks parse_callbacks = { static constexpr yajl_callbacks parse_callbacks = {
nullptr, nullptr,
@ -57,8 +59,8 @@ public:
} }
/* yajl callbacks */ /* yajl callbacks */
bool String(StringView value) noexcept; bool String(std::string_view value) noexcept;
bool MapKey(StringView value) noexcept; bool MapKey(std::string_view value) noexcept;
bool EndMap() noexcept; bool EndMap() noexcept;
}; };
@ -120,14 +122,14 @@ QobuzTrackRequest::OnError(std::exception_ptr e) noexcept
} }
inline bool inline bool
QobuzTrackRequest::ResponseParser::String(StringView value) noexcept QobuzTrackRequest::ResponseParser::String(std::string_view value) noexcept
{ {
switch (state) { switch (state) {
case State::NONE: case State::NONE:
break; break;
case State::URL: case State::URL:
url.assign(value.data, value.size); url = value;
break; break;
} }
@ -135,9 +137,9 @@ QobuzTrackRequest::ResponseParser::String(StringView value) noexcept
} }
inline bool inline bool
QobuzTrackRequest::ResponseParser::MapKey(StringView value) noexcept QobuzTrackRequest::ResponseParser::MapKey(std::string_view value) noexcept
{ {
if (value.Equals("url")) if (value == "url"sv)
state = State::URL; state = State::URL;
else else
state = State::NONE; state = State::NONE;