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::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);

View File

@ -30,6 +30,7 @@
#include <boost/intrusive/list.hpp>
#include <string>
#include <string_view>
/**
* 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<Directory *>(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<Song *>(cthis->FindSong(name_utf8));
}

View File

@ -38,14 +38,14 @@
#include <string.h>
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

View File

@ -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){

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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
{

View File

@ -26,6 +26,7 @@
#include "config.h"
#include <atomic>
#include <string_view>
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;