From 545af857ba4829ba48ac05d708a7371eefc6cef9 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Wed, 14 Aug 2019 19:57:12 +0200
Subject: [PATCH] lib/xiph/{FlacStreamMetadata,VorbisComments}: merge redundant
 code

---
 src/lib/xiph/FlacStreamMetadata.cxx | 44 +-------------------
 src/lib/xiph/ScanVorbisComment.cxx  | 64 +++++++++++++++++++++++++++++
 src/lib/xiph/ScanVorbisComment.hxx  | 29 +++++++++++++
 src/lib/xiph/VorbisComments.cxx     | 38 +----------------
 src/lib/xiph/meson.build            |  1 +
 5 files changed, 98 insertions(+), 78 deletions(-)
 create mode 100644 src/lib/xiph/ScanVorbisComment.cxx
 create mode 100644 src/lib/xiph/ScanVorbisComment.hxx

diff --git a/src/lib/xiph/FlacStreamMetadata.cxx b/src/lib/xiph/FlacStreamMetadata.cxx
index 399ad807b..432a9292c 100644
--- a/src/lib/xiph/FlacStreamMetadata.cxx
+++ b/src/lib/xiph/FlacStreamMetadata.cxx
@@ -19,14 +19,12 @@
 
 #include "FlacStreamMetadata.hxx"
 #include "FlacAudioFormat.hxx"
-#include "XiphTags.hxx"
+#include "ScanVorbisComment.hxx"
 #include "CheckAudioFormat.hxx"
 #include "MixRampInfo.hxx"
 #include "tag/Handler.hxx"
-#include "tag/Table.hxx"
 #include "tag/Builder.hxx"
 #include "tag/Tag.hxx"
-#include "tag/VorbisComment.hxx"
 #include "tag/ReplayGain.hxx"
 #include "tag/MixRamp.hxx"
 #include "ReplayGainInfo.hxx"
@@ -68,50 +66,12 @@ flac_parse_mixramp(const FLAC__StreamMetadata_VorbisComment &vc)
 	return mix_ramp;
 }
 
-/**
- * Check if the comment's name equals the passed name, and if so, copy
- * the comment value into the tag.
- */
-static bool
-flac_copy_comment(StringView comment,
-		  StringView name, TagType tag_type,
-		  TagHandler &handler) noexcept
-{
-	const auto value = GetVorbisCommentValue(comment, name);
-	if (!value.IsNull()) {
-		handler.OnTag(tag_type, value);
-		return true;
-	}
-
-	return false;
-}
-
-static void
-flac_scan_comment(StringView comment, TagHandler &handler) noexcept
-{
-	if (handler.WantPair()) {
-		const auto split = comment.Split('=');
-		if (!split.first.empty() && !split.second.IsNull())
-			handler.OnPair(split.first, split.second);
-	}
-
-	for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i)
-		if (flac_copy_comment(comment, i->name, i->type, handler))
-			return;
-
-	for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
-		if (flac_copy_comment(comment,
-				      tag_item_names[i], (TagType)i,
-				      handler))
-			return;
-}
-
 static void
 flac_scan_comments(const FLAC__StreamMetadata_VorbisComment *comment,
 		   TagHandler &handler) noexcept
 {
 	for (unsigned i = 0; i < comment->num_comments; ++i)
-		flac_scan_comment(ToStringView(comment->comments[i]), handler);
+		ScanVorbisComment(ToStringView(comment->comments[i]), handler);
 }
 
 gcc_pure
diff --git a/src/lib/xiph/ScanVorbisComment.cxx b/src/lib/xiph/ScanVorbisComment.cxx
new file mode 100644
index 000000000..0dea570d4
--- /dev/null
+++ b/src/lib/xiph/ScanVorbisComment.cxx
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2003-2019 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "ScanVorbisComment.hxx"
+#include "XiphTags.hxx"
+#include "tag/Table.hxx"
+#include "tag/Handler.hxx"
+#include "tag/VorbisComment.hxx"
+#include "util/StringView.hxx"
+
+/**
+ * Check if the comment's name equals the passed name, and if so, copy
+ * the comment value into the tag.
+ */
+static bool
+vorbis_copy_comment(StringView comment,
+		    StringView name, TagType tag_type,
+		    TagHandler &handler) noexcept
+{
+	const auto value = GetVorbisCommentValue(comment, name);
+	if (!value.IsNull()) {
+		handler.OnTag(tag_type, value);
+		return true;
+	}
+
+	return false;
+}
+
+void
+ScanVorbisComment(StringView comment, TagHandler &handler) noexcept
+{
+	if (handler.WantPair()) {
+		const auto split = comment.Split('=');
+		if (!split.first.empty() && !split.second.IsNull())
+			handler.OnPair(split.first, split.second);
+	}
+
+	for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i)
+		if (vorbis_copy_comment(comment, i->name, i->type,
+					handler))
+			return;
+
+	for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
+		if (vorbis_copy_comment(comment,
+					tag_item_names[i], TagType(i),
+					handler))
+			return;
+}
diff --git a/src/lib/xiph/ScanVorbisComment.hxx b/src/lib/xiph/ScanVorbisComment.hxx
new file mode 100644
index 000000000..b9a1e64a9
--- /dev/null
+++ b/src/lib/xiph/ScanVorbisComment.hxx
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2003-2019 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPD_SCAN_VORBIS_COMMENT_HXX
+#define MPD_SCAN_VORBIS_COMMENT_HXX
+
+struct StringView;
+class TagHandler;
+
+void
+ScanVorbisComment(StringView comment, TagHandler &handler) noexcept;
+
+#endif
diff --git a/src/lib/xiph/VorbisComments.cxx b/src/lib/xiph/VorbisComments.cxx
index dfa026000..a8929b5e2 100644
--- a/src/lib/xiph/VorbisComments.cxx
+++ b/src/lib/xiph/VorbisComments.cxx
@@ -19,8 +19,7 @@
 
 #include "VorbisComments.hxx"
 #include "VorbisPicture.hxx"
-#include "XiphTags.hxx"
-#include "tag/Table.hxx"
+#include "ScanVorbisComment.hxx"
 #include "tag/Handler.hxx"
 #include "tag/Builder.hxx"
 #include "tag/Tag.hxx"
@@ -64,24 +63,6 @@ VorbisCommentToReplayGain(ReplayGainInfo &rgi,
 	return found;
 }
 
-/**
- * Check if the comment's name equals the passed name, and if so, copy
- * the comment value into the tag.
- */
-static bool
-vorbis_copy_comment(StringView comment,
-		    StringView name, TagType tag_type,
-		    TagHandler &handler) noexcept
-{
-	const auto value = GetVorbisCommentValue(comment, name);
-	if (!value.IsNull()) {
-		handler.OnTag(tag_type, value);
-		return true;
-	}
-
-	return false;
-}
-
 static void
 vorbis_scan_comment(StringView comment, TagHandler &handler) noexcept
 {
@@ -91,22 +72,7 @@ vorbis_scan_comment(StringView comment, TagHandler &handler) noexcept
 	if (!picture_b64.IsNull())
 		return ScanVorbisPicture(picture_b64, handler);
 
-	if (handler.WantPair()) {
-		const auto split = comment.Split('=');
-		if (!split.first.empty() && !split.second.IsNull())
-			handler.OnPair(split.first, split.second);
-	}
-
-	for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i)
-		if (vorbis_copy_comment(comment, i->name, i->type,
-					handler))
-			return;
-
-	for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
-		if (vorbis_copy_comment(comment,
-					tag_item_names[i], TagType(i),
-					handler))
-			return;
+	ScanVorbisComment(comment, handler);
 }
 
 void
diff --git a/src/lib/xiph/meson.build b/src/lib/xiph/meson.build
index bdb489f9e..a2fc382fa 100644
--- a/src/lib/xiph/meson.build
+++ b/src/lib/xiph/meson.build
@@ -46,6 +46,7 @@ endif
 
 xiph = static_library(
   'xiph',
+  'ScanVorbisComment.cxx',
   'VorbisComments.cxx',
   'VorbisPicture.cxx',
   'XiphTags.cxx',
-- 
2.24.1