From 5675431eaf74a32799764d70ba917971f8fc762f Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 6 Jun 2019 13:32:39 +0200 Subject: [PATCH] tag/ApeTag: use IterableSplitString Eliminates yet another string copy. --- src/tag/ApeTag.cxx | 46 ++++++---------------------------------------- src/tag/ApeTag.hxx | 2 +- 2 files changed, 7 insertions(+), 41 deletions(-) diff --git a/src/tag/ApeTag.cxx b/src/tag/ApeTag.cxx index 443bd835a..21192689c 100644 --- a/src/tag/ApeTag.cxx +++ b/src/tag/ApeTag.cxx @@ -1,5 +1,5 @@ /* - * Copyright 2003-2018 The Music Player Daemon Project + * Copyright 2003-2019 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -23,10 +23,7 @@ #include "Table.hxx" #include "Handler.hxx" #include "util/StringView.hxx" - -#include - -#include +#include "util/IterableSplitString.hxx" static constexpr struct tag_table ape_tags[] = { { "album artist", TAG_ALBUM_ARTIST }, @@ -44,32 +41,6 @@ tag_ape_name_parse(const char *name) return type; } -/** - * 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 -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 */ @@ -82,21 +53,16 @@ tag_ape_import_item(unsigned long flags, if ((flags & (0x3 << 1)) != 0) return false; - const auto begin = value.begin(); - const auto end = value.end(); - if (handler.WantPair()) - ForEachValue(begin, end, [&handler, key](const char *_value) { - handler.OnPair(key, _value); - }); + for (const auto &i : IterableSplitString(value, '\0')) + handler.OnPair(key, i); TagType type = tag_ape_name_parse(key); if (type == TAG_NUM_OF_ITEM_TYPES) return false; - ForEachValue(begin, end, [&handler, type](const char *_value) { - handler.OnTag(type, _value); - }); + for (const auto &i : IterableSplitString(value, '\0')) + handler.OnTag(type, i); return true; } diff --git a/src/tag/ApeTag.hxx b/src/tag/ApeTag.hxx index 32cc114a9..37c1b9105 100644 --- a/src/tag/ApeTag.hxx +++ b/src/tag/ApeTag.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2003-2018 The Music Player Daemon Project + * Copyright 2003-2019 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify