Replace string with string_view where possible.

This commit is contained in:
tastytea 2020-01-08 16:46:27 +01:00
parent 2ab0018df9
commit 1f78b00205
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
7 changed files with 27 additions and 22 deletions

View File

@ -312,7 +312,7 @@ public:
[[nodiscard]] [[nodiscard]]
inline string_view to_string_view() const inline string_view to_string_view() const
{ {
return _endpoint_map.at(_endpoint).data(); return _endpoint_map.at(_endpoint);
} }
private: private:

View File

@ -33,7 +33,12 @@ using std::string;
using std::string_view; using std::string_view;
using std::variant; using std::variant;
using endpoint_variant = variant<API::endpoint_type,string>; /*!
* @brief An endpoint. Either API::endpoint_type or `std::string_view`.
*
* @since 0.1.0
*/
using endpoint_variant = variant<API::endpoint_type,string_view>;
/*! /*!
* @brief Represents a connection to an instance. Used for requests. * @brief Represents a connection to an instance. Used for requests.
@ -66,7 +71,7 @@ public:
* })}; * })};
* @endcode * @endcode
* *
* @param endpoint Endpoint as API::endpoint_type or `std::string`. * @param endpoint Endpoint as API::endpoint_type or `std::string_view`.
* @param parameters A map of parameters. * @param parameters A map of parameters.
* *
* *
@ -83,7 +88,7 @@ public:
* auto answer{connection.get("/api/v1/instance")}; * auto answer{connection.get("/api/v1/instance")};
* @endcode * @endcode
* *
* @param endpoint Endpoint as API::endpoint_type or `std::string`. * @param endpoint Endpoint as API::endpoint_type or `std::string_view`.
* *
* @since 0.1.0 * @since 0.1.0
*/ */

View File

@ -58,13 +58,13 @@ enum class http_method
* parametermap parameters * parametermap parameters
* { * {
* {"id", "12"}, * {"id", "12"},
* {"poll[options]", vector<string>{"Yes", "No", "Maybe"}} * {"poll[options]", vector<string_view>{"Yes", "No", "Maybe"}}
* }; * };
* @endcode * @endcode
* *
* @since 0.1.0 * @since 0.1.0
*/ */
using parametermap = map<string, variant<string, vector<string>>>; using parametermap = map<string_view, variant<string_view, vector<string_view>>>;
/*! /*!
* @brief Handles the details of network connections. * @brief Handles the details of network connections.

View File

@ -50,8 +50,8 @@ public:
* *
* @since 0.1.0 * @since 0.1.0
*/ */
explicit Instance(const string_view &hostname, explicit Instance(const string_view hostname,
const string_view &access_token); const string_view access_token);
/*! /*!
* @brief Returns the hostname. * @brief Returns the hostname.

View File

@ -29,16 +29,16 @@ Connection::Connection(Instance &instance)
answer_type Connection::get(const endpoint_variant &endpoint, answer_type Connection::get(const endpoint_variant &endpoint,
const parametermap &parameters) const parametermap &parameters)
{ {
string uri{[&] const string uri{[&]
{ {
if (holds_alternative<API::endpoint_type>(endpoint)) if (holds_alternative<API::endpoint_type>(endpoint))
{ {
return string(_baseuri).append( return string(_baseuri).append(
API{std::get<API::endpoint_type>(endpoint)}.to_string_view()); API{std::get<API::endpoint_type>(endpoint)}.to_string_view());
} }
return string(std::get<string_view>(endpoint));
return std::get<string>(endpoint);
}()}; }()};
return make_request(http_method::GET, uri, parameters); return make_request(http_method::GET, uri, parameters);
} }

View File

@ -88,32 +88,32 @@ answer_type CURLWrapper::make_request(const http_method &method, string uri,
if (pos != string::npos) if (pos != string::npos)
{ {
uri.replace(pos, param.first.size() + 2, uri.replace(pos, param.first.size() + 2,
get<string>(param.second)); get<string_view>(param.second));
} }
continue; continue;
} }
static bool first{true}; static bool first{true};
if (first) if (first)
{ {
uri.append("?"); uri += "?";
first = false; first = false;
} }
else else
{ {
uri.append("&"); uri += "&";
} }
if (holds_alternative<string>(param.second)) if (holds_alternative<string_view>(param.second))
{ {
uri.append(param.first + '=' + get<string>(param.second)); ((uri += param.first) += "=") += get<string_view>(param.second);
} }
else else
{ {
for (const auto &arg : get<vector<string>>(param.second)) for (const auto &arg : get<vector<string_view>>(param.second))
{ {
uri.append(param.first + "[]=" + arg); ((uri += param.first) += "[]=") += arg;
if (arg != *get<vector<string>>(param.second).rbegin()) if (arg != *get<vector<string_view>>(param.second).rbegin())
{ {
uri.append("&"); uri += "&";
} }
} }
} }
@ -238,7 +238,7 @@ void CURLWrapper::setup_curl()
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
code = curl_easy_setopt(_connection, CURLOPT_USERAGENT, code = curl_easy_setopt(_connection, CURLOPT_USERAGENT,
string("mastorss/").append(version).c_str()); (string("mastorss/") += version).c_str());
if (code != CURLE_OK) if (code != CURLE_OK)
{ {
throw CURLException{code, "Failed to set User-Agent", throw CURLException{code, "Failed to set User-Agent",

View File

@ -23,7 +23,7 @@ namespace mastodonpp
using std::stoull; using std::stoull;
Instance::Instance(const string_view &hostname, const string_view &access_token) Instance::Instance(const string_view hostname, const string_view access_token)
: _hostname{hostname} : _hostname{hostname}
, _baseuri{"https://" + _hostname} , _baseuri{"https://" + _hostname}
, _access_token{access_token} , _access_token{access_token}