util/UriQueryParser: use std::string_view

This commit is contained in:
Max Kellermann 2022-07-01 11:08:20 +02:00
parent ca46b4d7a7
commit 6d23ac67f9
5 changed files with 22 additions and 38 deletions

View File

@ -35,10 +35,8 @@ IcyInputStream::IcyInputStream(InputStreamPtr _input,
if (fragment != nullptr) { if (fragment != nullptr) {
const auto charset = UriFindRawQueryParameter(fragment, const auto charset = UriFindRawQueryParameter(fragment,
"charset"); "charset");
if (charset != nullptr) { if (charset.data() != nullptr)
const std::string copy(charset.data, charset.size); parser->SetCharset(std::string{charset}.c_str());
parser->SetCharset(copy.c_str());
}
} }
#endif #endif
} }

View File

@ -195,11 +195,10 @@ CreateIcuConverterForUri(const char *uri)
return nullptr; return nullptr;
const auto charset = UriFindRawQueryParameter(fragment, "charset"); const auto charset = UriFindRawQueryParameter(fragment, "charset");
if (charset == nullptr) if (charset.data() == nullptr)
return nullptr; return nullptr;
const std::string copy(charset.data, charset.size); return IcuConverter::Create(std::string{charset}.c_str());
return IcuConverter::Create(copy.c_str());
} }
#endif #endif

View File

@ -30,20 +30,20 @@
#include "UriQueryParser.hxx" #include "UriQueryParser.hxx"
#include "IterableSplitString.hxx" #include "IterableSplitString.hxx"
StringView using std::string_view_literals::operator""sv;
UriFindRawQueryParameter(StringView query_string, StringView name) noexcept
{
for (StringView i : IterableSplitString(query_string, '&')) {
if (i.StartsWith(name)) {
if (i.size == name.size)
return "";
if (i[name.size] == '=') { std::string_view
i.skip_front(name.size + 1); UriFindRawQueryParameter(std::string_view query_string, std::string_view name) noexcept
return i; {
} 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 {};
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2008-2019 Max Kellermann <max.kellermann@gmail.com> * Copyright 2008-2022 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -27,10 +27,9 @@
* OF THE POSSIBILITY OF SUCH DAMAGE. * OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef URI_QUERY_PARSER_HXX #pragma once
#define URI_QUERY_PARSER_HXX
struct StringView; #include <string_view>
/** /**
* Find the first query parameter with the given name and return its * 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 * or nullptr if the parameter does not exist
*/ */
[[gnu::pure]] [[gnu::pure]]
StringView std::string_view
UriFindRawQueryParameter(StringView query_string, StringView name) noexcept; UriFindRawQueryParameter(std::string_view query_string, std::string_view name) noexcept;
#endif

View File

@ -3,23 +3,13 @@
*/ */
#include "util/UriQueryParser.hxx" #include "util/UriQueryParser.hxx"
#include "util/StringView.hxx"
#include <gtest/gtest.h> #include <gtest/gtest.h>
static bool
operator==(StringView a, StringView b)
{
if (a.IsNull() || b.IsNull())
return a.IsNull() == b.IsNull();
return a.Equals(b);
}
TEST(UriQueryParser, UriFindRawQueryParameter) TEST(UriQueryParser, UriFindRawQueryParameter)
{ {
const char *q = "foo=1&bar=2&quoted=%20%00+%%&empty1&empty2="; const char *q = "foo=1&bar=2&quoted=%20%00+%%&empty1&empty2=";
EXPECT_EQ(UriFindRawQueryParameter(q, "doesntexist"), EXPECT_EQ(UriFindRawQueryParameter(q, "doesntexist").data(),
(const char *)nullptr); (const char *)nullptr);
EXPECT_EQ(UriFindRawQueryParameter(q, "foo"), EXPECT_EQ(UriFindRawQueryParameter(q, "foo"),
"1"); "1");