Commit bb07fd42 authored by Max Kellermann's avatar Max Kellermann

util/MimeType: migrate GetMimeTypeBase() to std::string_view

parent bab626c3
...@@ -36,9 +36,9 @@ ...@@ -36,9 +36,9 @@
gcc_pure gcc_pure
static bool static bool
CheckDecoderPlugin(const DecoderPlugin &plugin, CheckDecoderPlugin(const DecoderPlugin &plugin,
std::string_view suffix, const char *mime) noexcept std::string_view suffix, std::string_view mime) noexcept
{ {
return (mime != nullptr && plugin.SupportsMimeType(mime)) || return (!mime.empty() && plugin.SupportsMimeType(mime)) ||
(!suffix.empty() && plugin.SupportsSuffix(suffix)); (!suffix.empty() && plugin.SupportsSuffix(suffix));
} }
...@@ -48,23 +48,23 @@ tag_stream_scan(InputStream &is, TagHandler &handler) ...@@ -48,23 +48,23 @@ tag_stream_scan(InputStream &is, TagHandler &handler)
assert(is.IsReady()); assert(is.IsReady());
const auto suffix = uri_get_suffix(is.GetURI()); const auto suffix = uri_get_suffix(is.GetURI());
const char *mime = is.GetMimeType(); const char *full_mime = is.GetMimeType();
if (suffix.empty() && mime == nullptr) if (suffix.empty() && full_mime == nullptr)
return false; return false;
std::string mime_base; std::string_view mime_base{};
if (mime != nullptr) if (full_mime != nullptr)
mime = (mime_base = GetMimeTypeBase(mime)).c_str(); mime_base = GetMimeTypeBase(full_mime);
return decoder_plugins_try([suffix, mime, &is, return decoder_plugins_try([suffix, mime_base, &is,
&handler](const DecoderPlugin &plugin){ &handler](const DecoderPlugin &plugin){
try { try {
is.LockRewind(); is.LockRewind();
} catch (...) { } catch (...) {
} }
return CheckDecoderPlugin(plugin, suffix, mime) && return CheckDecoderPlugin(plugin, suffix, mime_base) &&
plugin.ScanStream(is, handler); plugin.ScanStream(is, handler);
}); });
} }
......
...@@ -227,10 +227,8 @@ ExtractMimeTypeMainPart(StringView s) noexcept ...@@ -227,10 +227,8 @@ ExtractMimeTypeMainPart(StringView s) noexcept
} }
static std::unique_ptr<SongEnumerator> static std::unique_ptr<SongEnumerator>
playlist_list_open_stream_mime(InputStreamPtr &&is, const char *full_mime) playlist_list_open_stream_mime(InputStreamPtr &&is, std::string_view full_mime)
{ {
assert(full_mime != nullptr);
/* probe only the portion before the semicolon*/ /* probe only the portion before the semicolon*/
return playlist_list_open_stream_mime2(std::move(is), return playlist_list_open_stream_mime2(std::move(is),
ExtractMimeTypeMainPart(full_mime)); ExtractMimeTypeMainPart(full_mime));
...@@ -266,7 +264,7 @@ playlist_list_open_stream(InputStreamPtr &&is, const char *uri) ...@@ -266,7 +264,7 @@ playlist_list_open_stream(InputStreamPtr &&is, const char *uri)
const char *const mime = is->GetMimeType(); const char *const mime = is->GetMimeType();
if (mime != nullptr) { if (mime != nullptr) {
auto playlist = playlist_list_open_stream_mime(std::move(is), auto playlist = playlist_list_open_stream_mime(std::move(is),
GetMimeTypeBase(mime).c_str()); GetMimeTypeBase(mime));
if (playlist != nullptr) if (playlist != nullptr)
return playlist; return playlist;
} }
......
...@@ -19,16 +19,12 @@ ...@@ -19,16 +19,12 @@
#include "MimeType.hxx" #include "MimeType.hxx"
#include "SplitString.hxx" #include "SplitString.hxx"
#include "StringView.hxx"
#include <cstring> std::string_view
GetMimeTypeBase(std::string_view s) noexcept
std::string
GetMimeTypeBase(const char *s) noexcept
{ {
const char *semicolon = std::strchr(s, ';'); return StringView(s).Split(';').first;
return semicolon != nullptr
? std::string(s, semicolon)
: std::string(s);
} }
std::map<std::string, std::string> std::map<std::string, std::string>
......
...@@ -20,7 +20,10 @@ ...@@ -20,7 +20,10 @@
#ifndef MPD_MIME_TYPE_HXX #ifndef MPD_MIME_TYPE_HXX
#define MPD_MIME_TYPE_HXX #define MPD_MIME_TYPE_HXX
#include "util/Compiler.h"
#include <string> #include <string>
#include <string_view>
#include <map> #include <map>
/** /**
...@@ -28,8 +31,9 @@ ...@@ -28,8 +31,9 @@
* part before the semicolon. If there is no semicolon, it returns * part before the semicolon. If there is no semicolon, it returns
* the string as-is. * the string as-is.
*/ */
std::string gcc_pure
GetMimeTypeBase(const char *s) noexcept; std::string_view
GetMimeTypeBase(std::string_view s) noexcept;
/** /**
* Parse the parameters from a MIME type string. Parameters are * Parse the parameters from a MIME type string. Parameters are
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment