lib/curl/Adapter: use std::string_view internally

This commit is contained in:
Max Kellermann 2022-07-01 16:47:46 +02:00
parent af951dc08a
commit 88a66df9d6

View File

@ -32,12 +32,14 @@
#include "Handler.hxx" #include "Handler.hxx"
#include "util/CharUtil.hxx" #include "util/CharUtil.hxx"
#include "util/RuntimeError.hxx" #include "util/RuntimeError.hxx"
#include "util/StringSplit.hxx"
#include "util/StringStrip.hxx" #include "util/StringStrip.hxx"
#include "util/StringView.hxx"
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
using std::string_view_literals::operator""sv;
void void
CurlResponseHandlerAdapter::Install(CurlEasy &easy) CurlResponseHandlerAdapter::Install(CurlEasy &easy)
{ {
@ -109,19 +111,17 @@ CurlResponseHandlerAdapter::Done(CURLcode result) noexcept
[[gnu::pure]] [[gnu::pure]]
static bool static bool
IsResponseBoundaryHeader(StringView s) noexcept IsResponseBoundaryHeader(std::string_view s) noexcept
{ {
return s.size > 5 && (s.StartsWith("HTTP/") || return s.size() > 5 && (s.starts_with("HTTP/"sv) ||
/* the proprietary "ICY 200 OK" is /* the proprietary "ICY 200 OK" is
emitted by Shoutcast */ emitted by Shoutcast */
s.StartsWith("ICY 2")); s.starts_with("ICY 2"sv));
} }
inline void inline void
CurlResponseHandlerAdapter::HeaderFunction(std::string_view _s) noexcept CurlResponseHandlerAdapter::HeaderFunction(std::string_view s) noexcept
{ {
const StringView s{_s};
if (state > State::HEADERS) if (state > State::HEADERS)
return; return;
@ -132,27 +132,15 @@ CurlResponseHandlerAdapter::HeaderFunction(std::string_view _s) noexcept
return; return;
} }
const char *header = s.data; auto [_name, value] = Split(StripRight(s), ':');
const char *end = StripRight(header, header + s.size); if (_name.empty() || value.data() == nullptr)
const char *value = s.Find(':');
if (value == nullptr)
return; return;
std::string name(header, value); std::string name{_name};
std::transform(name.begin(), name.end(), name.begin(), std::transform(name.begin(), name.end(), name.begin(),
static_cast<char(*)(char)>(ToLowerASCII)); static_cast<char(*)(char)>(ToLowerASCII));
/* skip the colon */ headers.emplace(std::move(name), value);
++value;
/* strip the value */
value = StripLeft(value, end);
end = StripRight(value, end);
headers.emplace(std::move(name), std::string(value, end));
} }
std::size_t std::size_t