util/MimeType: use IterableSplitString() in ParseMimeTypeParameters()

This commit is contained in:
Max Kellermann 2020-11-04 21:24:53 +01:00
parent 51e5b56b3a
commit afbcac9fb1
2 changed files with 11 additions and 15 deletions

View File

@ -18,7 +18,7 @@
*/ */
#include "MimeType.hxx" #include "MimeType.hxx"
#include "SplitString.hxx" #include "IterableSplitString.hxx"
#include "StringView.hxx" #include "StringView.hxx"
std::string_view std::string_view
@ -28,21 +28,17 @@ GetMimeTypeBase(std::string_view s) noexcept
} }
std::map<std::string, std::string> std::map<std::string, std::string>
ParseMimeTypeParameters(const char *s) noexcept ParseMimeTypeParameters(std::string_view mime_type) noexcept
{ {
/* discard the first segment (the base MIME type) */
const auto params = StringView(mime_type).Split(';').second;
std::map<std::string, std::string> result; std::map<std::string, std::string> result;
for (auto i : IterableSplitString(params, ';')) {
auto l = SplitString(s, ';', true); i.Strip();
if (!l.empty()) auto s = i.Split('=');
l.pop_front(); if (!s.second.IsNull())
result.emplace(s);
for (const auto &i : l) {
const auto eq = i.find('=');
if (eq == i.npos)
continue;
result.insert(std::make_pair(i.substr(0, eq),
i.substr(eq + 1)));
} }
return result; return result;

View File

@ -42,6 +42,6 @@ GetMimeTypeBase(std::string_view s) noexcept;
* "foo/bar; param1=value1; param2=value2" * "foo/bar; param1=value1; param2=value2"
*/ */
std::map<std::string, std::string> std::map<std::string, std::string>
ParseMimeTypeParameters(const char *s) noexcept; ParseMimeTypeParameters(std::string_view mime_type) noexcept;
#endif #endif