From 166ce0da5aa73cbc0adce4de542ab0ac0be49ff8 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 14 Feb 2022 12:30:54 +0100 Subject: [PATCH] db/VHelper: move CompareTags() to tag/Sort.cxx --- src/db/VHelper.cxx | 36 +--------------------------- src/tag/Sort.cxx | 57 +++++++++++++++++++++++++++++++++++++++++++++ src/tag/Sort.hxx | 29 +++++++++++++++++++++++ src/tag/meson.build | 1 + 4 files changed, 88 insertions(+), 35 deletions(-) create mode 100644 src/tag/Sort.cxx create mode 100644 src/tag/Sort.hxx diff --git a/src/db/VHelper.cxx b/src/db/VHelper.cxx index a5b54f523..de720bc19 100644 --- a/src/db/VHelper.cxx +++ b/src/db/VHelper.cxx @@ -21,14 +21,12 @@ #include "song/DetachedSong.hxx" #include "song/LightSong.hxx" #include "song/Filter.hxx" +#include "tag/Sort.hxx" #include #include #include -#include -#include - DatabaseVisitorHelper::DatabaseVisitorHelper(DatabaseSelection _selection, VisitSong &visit_song) noexcept :selection(std::move(_selection)) @@ -58,38 +56,6 @@ DatabaseVisitorHelper::DatabaseVisitorHelper(DatabaseSelection _selection, DatabaseVisitorHelper::~DatabaseVisitorHelper() noexcept = default; -gcc_pure -static bool -CompareNumeric(const char *a, const char *b) noexcept -{ - long a_value = strtol(a, nullptr, 10); - long b_value = strtol(b, nullptr, 10); - - return a_value < b_value; -} - -gcc_pure -static bool -CompareTags(TagType type, bool descending, const Tag &a, const Tag &b) noexcept -{ - const char *a_value = a.GetSortValue(type); - const char *b_value = b.GetSortValue(type); - - if (descending) { - using std::swap; - swap(a_value, b_value); - } - - switch (type) { - case TAG_DISC: - case TAG_TRACK: - return CompareNumeric(a_value, b_value); - - default: - return strcmp(a_value, b_value) < 0; - } -} - void DatabaseVisitorHelper::Commit() { diff --git a/src/tag/Sort.cxx b/src/tag/Sort.cxx new file mode 100644 index 000000000..957d24990 --- /dev/null +++ b/src/tag/Sort.cxx @@ -0,0 +1,57 @@ +/* + * Copyright 2003-2021 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 "Sort.hxx" +#include "Tag.hxx" + +#include + +#include +#include + +[[gnu::pure]] +static bool +CompareNumeric(const char *a, const char *b) noexcept +{ + long a_value = strtol(a, nullptr, 10); + long b_value = strtol(b, nullptr, 10); + + return a_value < b_value; +} + +bool +CompareTags(TagType type, bool descending, const Tag &a, const Tag &b) noexcept +{ + const char *a_value = a.GetSortValue(type); + const char *b_value = b.GetSortValue(type); + + if (descending) { + using std::swap; + swap(a_value, b_value); + } + + switch (type) { + case TAG_DISC: + case TAG_TRACK: + return CompareNumeric(a_value, b_value); + + default: + return strcmp(a_value, b_value) < 0; + } +} diff --git a/src/tag/Sort.hxx b/src/tag/Sort.hxx new file mode 100644 index 000000000..54b95197a --- /dev/null +++ b/src/tag/Sort.hxx @@ -0,0 +1,29 @@ +/* + * Copyright 2003-2021 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. + */ + +#pragma once + +#include "Type.h" + +struct Tag; + +[[gnu::pure]] +bool +CompareTags(TagType type, bool descending, + const Tag &a, const Tag &b) noexcept; diff --git a/src/tag/meson.build b/src/tag/meson.build index 60e50cd36..9f564db6e 100644 --- a/src/tag/meson.build +++ b/src/tag/meson.build @@ -18,6 +18,7 @@ tag_sources = [ 'Builder.cxx', 'Handler.cxx', 'Settings.cxx', + 'Sort.cxx', 'Config.cxx', 'ParseName.cxx', parse_name_cxx,