diff --git a/src/input/IcyInputStream.cxx b/src/input/IcyInputStream.cxx index 8e21ed69e..26aa3bf20 100644 --- a/src/input/IcyInputStream.cxx +++ b/src/input/IcyInputStream.cxx @@ -35,10 +35,8 @@ IcyInputStream::IcyInputStream(InputStreamPtr _input, if (fragment != nullptr) { const auto charset = UriFindRawQueryParameter(fragment, "charset"); - if (charset != nullptr) { - const std::string copy(charset.data, charset.size); - parser->SetCharset(copy.c_str()); - } + if (charset.data() != nullptr) + parser->SetCharset(std::string{charset}.c_str()); } #endif } diff --git a/src/input/plugins/CurlInputPlugin.cxx b/src/input/plugins/CurlInputPlugin.cxx index 938e243dd..8319d7944 100644 --- a/src/input/plugins/CurlInputPlugin.cxx +++ b/src/input/plugins/CurlInputPlugin.cxx @@ -195,11 +195,10 @@ CreateIcuConverterForUri(const char *uri) return nullptr; const auto charset = UriFindRawQueryParameter(fragment, "charset"); - if (charset == nullptr) + if (charset.data() == nullptr) return nullptr; - const std::string copy(charset.data, charset.size); - return IcuConverter::Create(copy.c_str()); + return IcuConverter::Create(std::string{charset}.c_str()); } #endif diff --git a/src/util/UriQueryParser.cxx b/src/util/UriQueryParser.cxx index f75fec733..879cb55c2 100644 --- a/src/util/UriQueryParser.cxx +++ b/src/util/UriQueryParser.cxx @@ -30,20 +30,20 @@ #include "UriQueryParser.hxx" #include "IterableSplitString.hxx" -StringView -UriFindRawQueryParameter(StringView query_string, StringView name) noexcept -{ - for (StringView i : IterableSplitString(query_string, '&')) { - if (i.StartsWith(name)) { - if (i.size == name.size) - return ""; +using std::string_view_literals::operator""sv; - if (i[name.size] == '=') { - i.skip_front(name.size + 1); - return i; - } +std::string_view +UriFindRawQueryParameter(std::string_view query_string, std::string_view name) noexcept +{ + for (const std::string_view i : IterableSplitString(query_string, '&')) { + if (i.starts_with(name)) { + if (i.size() == name.size()) + return ""sv; + + if (i[name.size()] == '=') + return i.substr(name.size() + 1); } } - return nullptr; + return {}; } diff --git a/src/util/UriQueryParser.hxx b/src/util/UriQueryParser.hxx index c9b52d4a8..1d5f2bd4b 100644 --- a/src/util/UriQueryParser.hxx +++ b/src/util/UriQueryParser.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2008-2019 Max Kellermann + * Copyright 2008-2022 Max Kellermann * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -27,10 +27,9 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef URI_QUERY_PARSER_HXX -#define URI_QUERY_PARSER_HXX +#pragma once -struct StringView; +#include /** * Find the first query parameter with the given name and return its @@ -40,7 +39,5 @@ struct StringView; * or nullptr if the parameter does not exist */ [[gnu::pure]] -StringView -UriFindRawQueryParameter(StringView query_string, StringView name) noexcept; - -#endif +std::string_view +UriFindRawQueryParameter(std::string_view query_string, std::string_view name) noexcept; diff --git a/test/util/TestUriQueryParser.cxx b/test/util/TestUriQueryParser.cxx index 9455f0664..5a9b236ab 100644 --- a/test/util/TestUriQueryParser.cxx +++ b/test/util/TestUriQueryParser.cxx @@ -3,23 +3,13 @@ */ #include "util/UriQueryParser.hxx" -#include "util/StringView.hxx" #include -static bool -operator==(StringView a, StringView b) -{ - if (a.IsNull() || b.IsNull()) - return a.IsNull() == b.IsNull(); - - return a.Equals(b); -} - TEST(UriQueryParser, UriFindRawQueryParameter) { const char *q = "foo=1&bar=2"ed=%20%00+%%&empty1&empty2="; - EXPECT_EQ(UriFindRawQueryParameter(q, "doesntexist"), + EXPECT_EQ(UriFindRawQueryParameter(q, "doesntexist").data(), (const char *)nullptr); EXPECT_EQ(UriFindRawQueryParameter(q, "foo"), "1");