db/simple/Directory: pass std::string_view to several methods

This commit is contained in:
Max Kellermann 2020-04-02 19:23:04 +02:00
parent 386235e2d2
commit 6593b5998a
9 changed files with 29 additions and 30 deletions

View File

@ -83,11 +83,10 @@ Directory::GetName() const noexcept
} }
Directory * Directory *
Directory::CreateChild(const char *name_utf8) noexcept Directory::CreateChild(std::string_view name_utf8) noexcept
{ {
assert(holding_db_lock()); assert(holding_db_lock());
assert(name_utf8 != nullptr); assert(!name_utf8.empty());
assert(*name_utf8 != 0);
std::string path_utf8 = IsRoot() std::string path_utf8 = IsRoot()
? std::string(name_utf8) ? std::string(name_utf8)
@ -99,12 +98,12 @@ Directory::CreateChild(const char *name_utf8) noexcept
} }
const Directory * const Directory *
Directory::FindChild(const char *name) const noexcept Directory::FindChild(std::string_view name) const noexcept
{ {
assert(holding_db_lock()); assert(holding_db_lock());
for (const auto &child : children) for (const auto &child : children)
if (strcmp(child.GetName(), name) == 0) if (name.compare(child.GetName()) == 0)
return &child; return &child;
return nullptr; return nullptr;
@ -194,10 +193,9 @@ Directory::RemoveSong(Song *song) noexcept
} }
const Song * const Song *
Directory::FindSong(const char *name_utf8) const noexcept Directory::FindSong(std::string_view name_utf8) const noexcept
{ {
assert(holding_db_lock()); assert(holding_db_lock());
assert(name_utf8 != nullptr);
for (auto &song : songs) { for (auto &song : songs) {
assert(&song.parent == this); assert(&song.parent == this);

View File

@ -30,6 +30,7 @@
#include <boost/intrusive/list.hpp> #include <boost/intrusive/list.hpp>
#include <string> #include <string>
#include <string_view>
/** /**
* Virtual directory that is really an archive file or a folder inside * 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 * @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. * Caller must lock the #db_mutex.
*/ */
gcc_pure gcc_pure
const Directory *FindChild(const char *name) const noexcept; const Directory *FindChild(std::string_view name) const noexcept;
gcc_pure gcc_pure
Directory *FindChild(const char *name) noexcept { Directory *FindChild(std::string_view name) noexcept {
const Directory *cthis = this; const Directory *cthis = this;
return const_cast<Directory *>(cthis->FindChild(name)); return const_cast<Directory *>(cthis->FindChild(name));
} }
@ -166,7 +167,7 @@ public:
* *
* Caller must lock the #db_mutex. * 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); Directory *child = FindChild(name_utf8);
if (child == nullptr) if (child == nullptr)
child = CreateChild(name_utf8); child = CreateChild(name_utf8);
@ -247,10 +248,10 @@ public:
* Caller must lock the #db_mutex. * Caller must lock the #db_mutex.
*/ */
gcc_pure gcc_pure
const Song *FindSong(const char *name_utf8) const noexcept; const Song *FindSong(std::string_view name_utf8) const noexcept;
gcc_pure gcc_pure
Song *FindSong(const char *name_utf8) noexcept { Song *FindSong(std::string_view name_utf8) noexcept {
const Directory *cthis = this; const Directory *cthis = this;
return const_cast<Song *>(cthis->FindSong(name_utf8)); return const_cast<Song *>(cthis->FindSong(name_utf8));
} }

View File

@ -38,14 +38,14 @@
#include <string.h> #include <string.h>
static Directory * static Directory *
LockMakeChild(Directory &directory, const char *name) noexcept LockMakeChild(Directory &directory, std::string_view name) noexcept
{ {
const ScopeDatabaseLock protect; const ScopeDatabaseLock protect;
return directory.MakeChild(name); return directory.MakeChild(name);
} }
static Song * static Song *
LockFindSong(Directory &directory, const char *name) noexcept LockFindSong(Directory &directory, std::string_view name) noexcept
{ {
const ScopeDatabaseLock protect; const ScopeDatabaseLock protect;
return directory.FindSong(name); return directory.FindSong(name);
@ -57,10 +57,9 @@ UpdateWalk::UpdateArchiveTree(ArchiveFile &archive, Directory &directory,
{ {
const char *tmp = strchr(name, '/'); const char *tmp = strchr(name, '/');
if (tmp) { if (tmp) {
const std::string child_name(name, tmp); const std::string_view child_name(name, tmp - name);
//add dir is not there already //add dir is not there already
Directory *subdir = LockMakeChild(directory, Directory *subdir = LockMakeChild(directory, child_name);
child_name.c_str());
subdir->device = DEVICE_INARCHIVE; subdir->device = DEVICE_INARCHIVE;
//create directories first //create directories first

View File

@ -32,7 +32,7 @@
bool bool
UpdateWalk::UpdateContainerFile(Directory &directory, UpdateWalk::UpdateContainerFile(Directory &directory,
const char *name, const char *suffix, std::string_view name, const char *suffix,
const StorageFileInfo &info) noexcept const StorageFileInfo &info) noexcept
{ {
const DecoderPlugin *_plugin = decoder_plugins_find([suffix](const DecoderPlugin &plugin){ const DecoderPlugin *_plugin = decoder_plugins_find([suffix](const DecoderPlugin &plugin){

View File

@ -88,7 +88,7 @@ DatabaseEditor::LockDeleteDirectory(Directory *directory)
} }
bool bool
DatabaseEditor::DeleteNameIn(Directory &parent, const char *name) DatabaseEditor::DeleteNameIn(Directory &parent, std::string_view name)
{ {
const ScopeDatabaseLock protect; const ScopeDatabaseLock protect;

View File

@ -59,7 +59,7 @@ public:
* *
* @return true if the database was modified * @return true if the database was modified
*/ */
bool DeleteNameIn(Directory &parent, const char *name); bool DeleteNameIn(Directory &parent, std::string_view name);
private: private:
void ClearDirectory(Directory &directory); void ClearDirectory(Directory &directory);

View File

@ -34,7 +34,7 @@
#include "Log.hxx" #include "Log.hxx"
void void
UpdateWalk::UpdatePlaylistFile(Directory &parent, const char *name, UpdateWalk::UpdatePlaylistFile(Directory &parent, std::string_view name,
const StorageFileInfo &info, const StorageFileInfo &info,
const PlaylistPlugin &plugin) noexcept const PlaylistPlugin &plugin) noexcept
{ {
@ -88,7 +88,7 @@ UpdateWalk::UpdatePlaylistFile(Directory &parent, const char *name,
bool bool
UpdateWalk::UpdatePlaylistFile(Directory &directory, UpdateWalk::UpdatePlaylistFile(Directory &directory,
const char *name, const char *suffix, std::string_view name, const char *suffix,
const StorageFileInfo &info) noexcept const StorageFileInfo &info) noexcept
{ {
const auto *const plugin = FindPlaylistPluginBySuffix(suffix); const auto *const plugin = FindPlaylistPluginBySuffix(suffix);

View File

@ -23,7 +23,7 @@
#include "storage/FileInfo.hxx" #include "storage/FileInfo.hxx"
Directory * Directory *
UpdateWalk::MakeVirtualDirectoryIfModified(Directory &parent, const char *name, UpdateWalk::MakeVirtualDirectoryIfModified(Directory &parent, std::string_view name,
const StorageFileInfo &info, const StorageFileInfo &info,
unsigned virtual_device) noexcept unsigned virtual_device) noexcept
{ {
@ -53,7 +53,7 @@ UpdateWalk::MakeVirtualDirectoryIfModified(Directory &parent, const char *name,
Directory * Directory *
UpdateWalk::LockMakeVirtualDirectoryIfModified(Directory &parent, UpdateWalk::LockMakeVirtualDirectoryIfModified(Directory &parent,
const char *name, std::string_view name,
const StorageFileInfo &info, const StorageFileInfo &info,
unsigned virtual_device) noexcept unsigned virtual_device) noexcept
{ {

View File

@ -26,6 +26,7 @@
#include "config.h" #include "config.h"
#include <atomic> #include <atomic>
#include <string_view>
struct StorageFileInfo; struct StorageFileInfo;
struct Directory; struct Directory;
@ -93,7 +94,7 @@ private:
const StorageFileInfo &info) noexcept; const StorageFileInfo &info) noexcept;
bool UpdateContainerFile(Directory &directory, bool UpdateContainerFile(Directory &directory,
const char *name, const char *suffix, std::string_view name, const char *suffix,
const StorageFileInfo &info) noexcept; const StorageFileInfo &info) noexcept;
@ -119,12 +120,12 @@ private:
} }
#endif #endif
void UpdatePlaylistFile(Directory &parent, const char *name, void UpdatePlaylistFile(Directory &parent, std::string_view name,
const StorageFileInfo &info, const StorageFileInfo &info,
const PlaylistPlugin &plugin) noexcept; const PlaylistPlugin &plugin) noexcept;
bool UpdatePlaylistFile(Directory &directory, bool UpdatePlaylistFile(Directory &directory,
const char *name, const char *suffix, std::string_view name, const char *suffix,
const StorageFileInfo &info) noexcept; const StorageFileInfo &info) noexcept;
bool UpdateRegularFile(Directory &directory, bool UpdateRegularFile(Directory &directory,
@ -151,12 +152,12 @@ private:
* specifying the kind of virtual directory * specifying the kind of virtual directory
*/ */
Directory *MakeVirtualDirectoryIfModified(Directory &parent, Directory *MakeVirtualDirectoryIfModified(Directory &parent,
const char *name, std::string_view name,
const StorageFileInfo &info, const StorageFileInfo &info,
unsigned virtual_device) noexcept; unsigned virtual_device) noexcept;
Directory *LockMakeVirtualDirectoryIfModified(Directory &parent, Directory *LockMakeVirtualDirectoryIfModified(Directory &parent,
const char *name, std::string_view name,
const StorageFileInfo &info, const StorageFileInfo &info,
unsigned virtual_device) noexcept; unsigned virtual_device) noexcept;