From 6593b5998a23c46caacf9978cb353a9c878f7501 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 2 Apr 2020 19:23:04 +0200 Subject: [PATCH] db/simple/Directory: pass std::string_view to several methods --- src/db/plugins/simple/Directory.cxx | 12 +++++------- src/db/plugins/simple/Directory.hxx | 13 +++++++------ src/db/update/Archive.cxx | 9 ++++----- src/db/update/Container.cxx | 2 +- src/db/update/Editor.cxx | 2 +- src/db/update/Editor.hxx | 2 +- src/db/update/Playlist.cxx | 4 ++-- src/db/update/VirtualDirectory.cxx | 4 ++-- src/db/update/Walk.hxx | 11 ++++++----- 9 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/db/plugins/simple/Directory.cxx b/src/db/plugins/simple/Directory.cxx index 0c0811540..17ee140b2 100644 --- a/src/db/plugins/simple/Directory.cxx +++ b/src/db/plugins/simple/Directory.cxx @@ -83,11 +83,10 @@ Directory::GetName() const noexcept } Directory * -Directory::CreateChild(const char *name_utf8) noexcept +Directory::CreateChild(std::string_view name_utf8) noexcept { assert(holding_db_lock()); - assert(name_utf8 != nullptr); - assert(*name_utf8 != 0); + assert(!name_utf8.empty()); std::string path_utf8 = IsRoot() ? std::string(name_utf8) @@ -99,12 +98,12 @@ Directory::CreateChild(const char *name_utf8) noexcept } const Directory * -Directory::FindChild(const char *name) const noexcept +Directory::FindChild(std::string_view name) const noexcept { assert(holding_db_lock()); for (const auto &child : children) - if (strcmp(child.GetName(), name) == 0) + if (name.compare(child.GetName()) == 0) return &child; return nullptr; @@ -194,10 +193,9 @@ Directory::RemoveSong(Song *song) noexcept } const Song * -Directory::FindSong(const char *name_utf8) const noexcept +Directory::FindSong(std::string_view name_utf8) const noexcept { assert(holding_db_lock()); - assert(name_utf8 != nullptr); for (auto &song : songs) { assert(&song.parent == this); diff --git a/src/db/plugins/simple/Directory.hxx b/src/db/plugins/simple/Directory.hxx index 33d08d804..a3533807a 100644 --- a/src/db/plugins/simple/Directory.hxx +++ b/src/db/plugins/simple/Directory.hxx @@ -30,6 +30,7 @@ #include #include +#include /** * Virtual directory that is really an archive file or a folder inside @@ -146,16 +147,16 @@ public: * * @param name_utf8 the UTF-8 encoded name of the new sub directory */ - Directory *CreateChild(const char *name_utf8) noexcept; + Directory *CreateChild(std::string_view name_utf8) noexcept; /** * Caller must lock the #db_mutex. */ gcc_pure - const Directory *FindChild(const char *name) const noexcept; + const Directory *FindChild(std::string_view name) const noexcept; gcc_pure - Directory *FindChild(const char *name) noexcept { + Directory *FindChild(std::string_view name) noexcept { const Directory *cthis = this; return const_cast(cthis->FindChild(name)); } @@ -166,7 +167,7 @@ public: * * Caller must lock the #db_mutex. */ - Directory *MakeChild(const char *name_utf8) noexcept { + Directory *MakeChild(std::string_view name_utf8) noexcept { Directory *child = FindChild(name_utf8); if (child == nullptr) child = CreateChild(name_utf8); @@ -247,10 +248,10 @@ public: * Caller must lock the #db_mutex. */ gcc_pure - const Song *FindSong(const char *name_utf8) const noexcept; + const Song *FindSong(std::string_view name_utf8) const noexcept; gcc_pure - Song *FindSong(const char *name_utf8) noexcept { + Song *FindSong(std::string_view name_utf8) noexcept { const Directory *cthis = this; return const_cast(cthis->FindSong(name_utf8)); } diff --git a/src/db/update/Archive.cxx b/src/db/update/Archive.cxx index 5e03e0c7e..74f754d64 100644 --- a/src/db/update/Archive.cxx +++ b/src/db/update/Archive.cxx @@ -38,14 +38,14 @@ #include static Directory * -LockMakeChild(Directory &directory, const char *name) noexcept +LockMakeChild(Directory &directory, std::string_view name) noexcept { const ScopeDatabaseLock protect; return directory.MakeChild(name); } static Song * -LockFindSong(Directory &directory, const char *name) noexcept +LockFindSong(Directory &directory, std::string_view name) noexcept { const ScopeDatabaseLock protect; return directory.FindSong(name); @@ -57,10 +57,9 @@ UpdateWalk::UpdateArchiveTree(ArchiveFile &archive, Directory &directory, { const char *tmp = strchr(name, '/'); if (tmp) { - const std::string child_name(name, tmp); + const std::string_view child_name(name, tmp - name); //add dir is not there already - Directory *subdir = LockMakeChild(directory, - child_name.c_str()); + Directory *subdir = LockMakeChild(directory, child_name); subdir->device = DEVICE_INARCHIVE; //create directories first diff --git a/src/db/update/Container.cxx b/src/db/update/Container.cxx index 6dd84ec09..2dd1571cd 100644 --- a/src/db/update/Container.cxx +++ b/src/db/update/Container.cxx @@ -32,7 +32,7 @@ bool UpdateWalk::UpdateContainerFile(Directory &directory, - const char *name, const char *suffix, + std::string_view name, const char *suffix, const StorageFileInfo &info) noexcept { const DecoderPlugin *_plugin = decoder_plugins_find([suffix](const DecoderPlugin &plugin){ diff --git a/src/db/update/Editor.cxx b/src/db/update/Editor.cxx index c07e767dd..1339037fd 100644 --- a/src/db/update/Editor.cxx +++ b/src/db/update/Editor.cxx @@ -88,7 +88,7 @@ DatabaseEditor::LockDeleteDirectory(Directory *directory) } bool -DatabaseEditor::DeleteNameIn(Directory &parent, const char *name) +DatabaseEditor::DeleteNameIn(Directory &parent, std::string_view name) { const ScopeDatabaseLock protect; diff --git a/src/db/update/Editor.hxx b/src/db/update/Editor.hxx index 2276f14c6..af20046db 100644 --- a/src/db/update/Editor.hxx +++ b/src/db/update/Editor.hxx @@ -59,7 +59,7 @@ public: * * @return true if the database was modified */ - bool DeleteNameIn(Directory &parent, const char *name); + bool DeleteNameIn(Directory &parent, std::string_view name); private: void ClearDirectory(Directory &directory); diff --git a/src/db/update/Playlist.cxx b/src/db/update/Playlist.cxx index 17c2ed9c7..03509522a 100644 --- a/src/db/update/Playlist.cxx +++ b/src/db/update/Playlist.cxx @@ -34,7 +34,7 @@ #include "Log.hxx" void -UpdateWalk::UpdatePlaylistFile(Directory &parent, const char *name, +UpdateWalk::UpdatePlaylistFile(Directory &parent, std::string_view name, const StorageFileInfo &info, const PlaylistPlugin &plugin) noexcept { @@ -88,7 +88,7 @@ UpdateWalk::UpdatePlaylistFile(Directory &parent, const char *name, bool UpdateWalk::UpdatePlaylistFile(Directory &directory, - const char *name, const char *suffix, + std::string_view name, const char *suffix, const StorageFileInfo &info) noexcept { const auto *const plugin = FindPlaylistPluginBySuffix(suffix); diff --git a/src/db/update/VirtualDirectory.cxx b/src/db/update/VirtualDirectory.cxx index c1ee5e92d..c359809da 100644 --- a/src/db/update/VirtualDirectory.cxx +++ b/src/db/update/VirtualDirectory.cxx @@ -23,7 +23,7 @@ #include "storage/FileInfo.hxx" Directory * -UpdateWalk::MakeVirtualDirectoryIfModified(Directory &parent, const char *name, +UpdateWalk::MakeVirtualDirectoryIfModified(Directory &parent, std::string_view name, const StorageFileInfo &info, unsigned virtual_device) noexcept { @@ -53,7 +53,7 @@ UpdateWalk::MakeVirtualDirectoryIfModified(Directory &parent, const char *name, Directory * UpdateWalk::LockMakeVirtualDirectoryIfModified(Directory &parent, - const char *name, + std::string_view name, const StorageFileInfo &info, unsigned virtual_device) noexcept { diff --git a/src/db/update/Walk.hxx b/src/db/update/Walk.hxx index 3d0793e38..447568aae 100644 --- a/src/db/update/Walk.hxx +++ b/src/db/update/Walk.hxx @@ -26,6 +26,7 @@ #include "config.h" #include +#include struct StorageFileInfo; struct Directory; @@ -93,7 +94,7 @@ private: const StorageFileInfo &info) noexcept; bool UpdateContainerFile(Directory &directory, - const char *name, const char *suffix, + std::string_view name, const char *suffix, const StorageFileInfo &info) noexcept; @@ -119,12 +120,12 @@ private: } #endif - void UpdatePlaylistFile(Directory &parent, const char *name, + void UpdatePlaylistFile(Directory &parent, std::string_view name, const StorageFileInfo &info, const PlaylistPlugin &plugin) noexcept; bool UpdatePlaylistFile(Directory &directory, - const char *name, const char *suffix, + std::string_view name, const char *suffix, const StorageFileInfo &info) noexcept; bool UpdateRegularFile(Directory &directory, @@ -151,12 +152,12 @@ private: * specifying the kind of virtual directory */ Directory *MakeVirtualDirectoryIfModified(Directory &parent, - const char *name, + std::string_view name, const StorageFileInfo &info, unsigned virtual_device) noexcept; Directory *LockMakeVirtualDirectoryIfModified(Directory &parent, - const char *name, + std::string_view name, const StorageFileInfo &info, unsigned virtual_device) noexcept;