aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2013-10-26 15:57:09 +0200
committerMax Kellermann <max@duempel.org>2013-10-26 15:57:09 +0200
commita4d82cfe1eb388b0552a20703116def53046774f (patch)
tree2a47af6a20cf095bbb4f14d8ff5cedd01e0a4b94 /src
parent9f21eee2ec942829fe4e122f309a6bf5dd3df366 (diff)
downloadmpd-a4d82cfe1eb388b0552a20703116def53046774f.tar.gz
mpd-a4d82cfe1eb388b0552a20703116def53046774f.tar.xz
mpd-a4d82cfe1eb388b0552a20703116def53046774f.zip
ApeTag: move code to ForEachValue()
Diffstat (limited to 'src')
-rw-r--r--src/tag/ApeTag.cxx46
1 files changed, 30 insertions, 16 deletions
diff --git a/src/tag/ApeTag.cxx b/src/tag/ApeTag.cxx
index ab7ddbe7a..f33bf1353 100644
--- a/src/tag/ApeTag.cxx
+++ b/src/tag/ApeTag.cxx
@@ -46,6 +46,32 @@ tag_ape_name_parse(const char *name)
}
/**
+ * Invoke the given callback for each string inside the given range.
+ * The strings are separated by null bytes, but the last one may not
+ * be terminated.
+ */
+template<typename C>
+static void
+ForEachValue(const char *value, const char *end, C &&callback)
+{
+ while (true) {
+ const char *n = (const char *)memchr(value, 0, end - value);
+ if (n == nullptr)
+ break;
+
+ if (n > value)
+ callback(value);
+
+ value = n + 1;
+ }
+
+ if (value < end) {
+ const std::string value2(value, end);
+ callback(value2.c_str());
+ }
+}
+
+/**
* @return true if the item was recognized
*/
static bool
@@ -64,23 +90,11 @@ tag_ape_import_item(unsigned long flags,
return false;
const char *end = value + value_length;
- while (true) {
- /* multiple values are separated by null bytes */
- const char *n = (const char *)memchr(value, 0, end - value);
- if (n != nullptr) {
- if (n > value) {
- tag_handler_invoke_tag(handler, handler_ctx,
- type, value);
- }
-
- value = n + 1;
- } else {
- const std::string value2(value, end);
+ ForEachValue(value, end, [handler, handler_ctx,
+ type](const char *_value) {
tag_handler_invoke_tag(handler, handler_ctx,
- type, value2.c_str());
- break;
- }
- }
+ type, _value);
+ });
return true;
}