From bb07fd42ce76b1cc6e4ed778d11b68c11c01bd00 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Wed, 4 Nov 2020 20:27:28 +0100
Subject: [PATCH] util/MimeType: migrate GetMimeTypeBase() to std::string_view

---
 src/TagStream.cxx                 | 18 +++++++++---------
 src/playlist/PlaylistRegistry.cxx |  6 ++----
 src/util/MimeType.cxx             | 12 ++++--------
 src/util/MimeType.hxx             |  8 ++++++--
 4 files changed, 21 insertions(+), 23 deletions(-)

diff --git a/src/TagStream.cxx b/src/TagStream.cxx
index 8013161d0..6522cf39b 100644
--- a/src/TagStream.cxx
+++ b/src/TagStream.cxx
@@ -36,9 +36,9 @@
 gcc_pure
 static bool
 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));
 }
 
@@ -48,23 +48,23 @@ tag_stream_scan(InputStream &is, TagHandler &handler)
 	assert(is.IsReady());
 
 	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;
 
-	std::string mime_base;
-	if (mime != nullptr)
-		mime = (mime_base = GetMimeTypeBase(mime)).c_str();
+	std::string_view mime_base{};
+	if (full_mime != nullptr)
+		mime_base = GetMimeTypeBase(full_mime);
 
-	return decoder_plugins_try([suffix, mime, &is,
+	return decoder_plugins_try([suffix, mime_base, &is,
 				    &handler](const DecoderPlugin &plugin){
 			try {
 				is.LockRewind();
 			} catch (...) {
 			}
 
-			return CheckDecoderPlugin(plugin, suffix, mime) &&
+			return CheckDecoderPlugin(plugin, suffix, mime_base) &&
 				plugin.ScanStream(is, handler);
 		});
 }
diff --git a/src/playlist/PlaylistRegistry.cxx b/src/playlist/PlaylistRegistry.cxx
index 166ff136d..3e790290d 100644
--- a/src/playlist/PlaylistRegistry.cxx
+++ b/src/playlist/PlaylistRegistry.cxx
@@ -227,10 +227,8 @@ ExtractMimeTypeMainPart(StringView s) noexcept
 }
 
 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*/
 	return playlist_list_open_stream_mime2(std::move(is),
 					       ExtractMimeTypeMainPart(full_mime));
@@ -266,7 +264,7 @@ playlist_list_open_stream(InputStreamPtr &&is, const char *uri)
 	const char *const mime = is->GetMimeType();
 	if (mime != nullptr) {
 		auto playlist = playlist_list_open_stream_mime(std::move(is),
-							       GetMimeTypeBase(mime).c_str());
+							       GetMimeTypeBase(mime));
 		if (playlist != nullptr)
 			return playlist;
 	}
diff --git a/src/util/MimeType.cxx b/src/util/MimeType.cxx
index f73c37557..13ad9fe8b 100644
--- a/src/util/MimeType.cxx
+++ b/src/util/MimeType.cxx
@@ -19,16 +19,12 @@
 
 #include "MimeType.hxx"
 #include "SplitString.hxx"
+#include "StringView.hxx"
 
-#include <cstring>
-
-std::string
-GetMimeTypeBase(const char *s) noexcept
+std::string_view
+GetMimeTypeBase(std::string_view s) noexcept
 {
-	const char *semicolon = std::strchr(s, ';');
-	return semicolon != nullptr
-		? std::string(s, semicolon)
-		: std::string(s);
+	return StringView(s).Split(';').first;
 }
 
 std::map<std::string, std::string>
diff --git a/src/util/MimeType.hxx b/src/util/MimeType.hxx
index 30c20a3ee..1a20c8cf2 100644
--- a/src/util/MimeType.hxx
+++ b/src/util/MimeType.hxx
@@ -20,7 +20,10 @@
 #ifndef MPD_MIME_TYPE_HXX
 #define MPD_MIME_TYPE_HXX
 
+#include "util/Compiler.h"
+
 #include <string>
+#include <string_view>
 #include <map>
 
 /**
@@ -28,8 +31,9 @@
  * part before the semicolon.  If there is no semicolon, it returns
  * the string as-is.
  */
-std::string
-GetMimeTypeBase(const char *s) noexcept;
+gcc_pure
+std::string_view
+GetMimeTypeBase(std::string_view s) noexcept;
 
 /**
  * Parse the parameters from a MIME type string.  Parameters are