util/StringCompare: add StringIsEmpty()

This commit is contained in:
Max Kellermann 2015-11-06 09:37:07 +01:00
parent 42f5ecd4a1
commit c880099deb
13 changed files with 37 additions and 16 deletions

View File

@ -27,6 +27,7 @@
#include "fs/Traits.hxx" #include "fs/Traits.hxx"
#include "fs/Charset.hxx" #include "fs/Charset.hxx"
#include "fs/CheckFile.hxx" #include "fs/CheckFile.hxx"
#include "util/StringCompare.hxx"
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
#include "storage/StorageInterface.hxx" #include "storage/StorageInterface.hxx"
@ -98,7 +99,7 @@ map_fs_to_utf8(Path path_fs)
return std::string(); return std::string();
auto relative = music_dir_fs.Relative(path_fs); auto relative = music_dir_fs.Relative(path_fs);
if (relative == nullptr || *relative == 0) if (relative == nullptr || StringIsEmpty(relative))
return std::string(); return std::string();
path_fs = Path::FromFS(relative); path_fs = Path::FromFS(relative);

View File

@ -70,7 +70,7 @@ spl_global_init(void)
bool bool
spl_valid_name(const char *name_utf8) spl_valid_name(const char *name_utf8)
{ {
if (*name_utf8 == 0) if (StringIsEmpty(name_utf8))
/* empty name not allowed */ /* empty name not allowed */
return false; return false;

View File

@ -31,6 +31,7 @@
#include "archive/ArchiveFile.hxx" #include "archive/ArchiveFile.hxx"
#include "archive/ArchiveVisitor.hxx" #include "archive/ArchiveVisitor.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/StringCompare.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <string> #include <string>
@ -54,7 +55,7 @@ UpdateWalk::UpdateArchiveTree(Directory &directory, const char *name)
//create directories first //create directories first
UpdateArchiveTree(*subdir, tmp + 1); UpdateArchiveTree(*subdir, tmp + 1);
} else { } else {
if (strlen(name) == 0) { if (StringIsEmpty(name)) {
LogWarning(update_domain, LogWarning(update_domain,
"archive returned directory only"); "archive returned directory only");
return; return;

View File

@ -22,6 +22,7 @@
#include "InotifyDomain.hxx" #include "InotifyDomain.hxx"
#include "Service.hxx" #include "Service.hxx"
#include "Log.hxx" #include "Log.hxx"
#include "util/StringCompare.hxx"
#include <string.h> #include <string.h>
@ -59,7 +60,7 @@ path_in(const char *path, const char *possible_parent)
{ {
size_t length = strlen(possible_parent); size_t length = strlen(possible_parent);
return path[0] == 0 || return StringIsEmpty(path) ||
(memcmp(possible_parent, path, length) == 0 && (memcmp(possible_parent, path, length) == 0 &&
(path[length] == 0 || path[length] == '/')); (path[length] == 0 || path[length] == '/'));
} }

View File

@ -38,6 +38,7 @@
#include "fs/Charset.hxx" #include "fs/Charset.hxx"
#include "storage/FileInfo.hxx" #include "storage/FileInfo.hxx"
#include "util/Alloc.hxx" #include "util/Alloc.hxx"
#include "util/StringCompare.hxx"
#include "util/UriUtil.hxx" #include "util/UriUtil.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "Log.hxx" #include "Log.hxx"
@ -435,7 +436,7 @@ UpdateWalk::DirectoryMakeUriParentChecked(Directory &root, const char *uri)
while ((slash = strchr(name_utf8, '/')) != nullptr) { while ((slash = strchr(name_utf8, '/')) != nullptr) {
*slash = 0; *slash = 0;
if (*name_utf8 == 0) if (StringIsEmpty(name_utf8))
continue; continue;
directory = DirectoryMakeChildChecked(*directory, directory = DirectoryMakeChildChecked(*directory,

View File

@ -164,7 +164,7 @@ AlsaInputStream::Create(const char *uri, Mutex &mutex, Cond &cond,
return nullptr; return nullptr;
const char *device = uri + strlen(scheme); const char *device = uri + strlen(scheme);
if (strlen(device) == 0) if (*device == 0)
device = default_device; device = default_device;
/* placeholders - eventually user-requested audio format will /* placeholders - eventually user-requested audio format will

View File

@ -25,6 +25,7 @@
#include "Idle.hxx" #include "Idle.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Macros.hxx" #include "util/Macros.hxx"
#include "util/StringCompare.hxx"
#include <string> #include <string>
#include <map> #include <map>
@ -178,7 +179,7 @@ sticker_load_value(const char *type, const char *uri, const char *name,
assert(uri != nullptr); assert(uri != nullptr);
assert(name != nullptr); assert(name != nullptr);
if (*name == 0) if (StringIsEmpty(name))
return std::string(); return std::string();
if (!BindAll(error, stmt, type, uri, name)) if (!BindAll(error, stmt, type, uri, name))
@ -287,7 +288,7 @@ sticker_store_value(const char *type, const char *uri,
assert(name != nullptr); assert(name != nullptr);
assert(value != nullptr); assert(value != nullptr);
if (*name == 0) if (StringIsEmpty(name))
return false; return false;
return sticker_update_value(type, uri, name, value, error) || return sticker_update_value(type, uri, name, value, error) ||

View File

@ -23,6 +23,7 @@
#include "fs/AllocatedPath.hxx" #include "fs/AllocatedPath.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "util/StringCompare.hxx"
#include <set> #include <set>
@ -164,7 +165,7 @@ CompositeStorage::Directory::Unmount()
bool bool
CompositeStorage::Directory::Unmount(const char *uri) CompositeStorage::Directory::Unmount(const char *uri)
{ {
if (*uri == 0) if (StringIsEmpty(uri))
return Unmount(); return Unmount();
const std::string name = NextSegment(uri); const std::string name = NextSegment(uri);

View File

@ -22,10 +22,11 @@
#include "storage/StoragePlugin.hxx" #include "storage/StoragePlugin.hxx"
#include "storage/StorageInterface.hxx" #include "storage/StorageInterface.hxx"
#include "storage/FileInfo.hxx" #include "storage/FileInfo.hxx"
#include "util/Error.hxx"
#include "fs/FileInfo.hxx" #include "fs/FileInfo.hxx"
#include "fs/AllocatedPath.hxx" #include "fs/AllocatedPath.hxx"
#include "fs/DirectoryReader.hxx" #include "fs/DirectoryReader.hxx"
#include "util/Error.hxx"
#include "util/StringCompare.hxx"
#include <string> #include <string>
@ -108,7 +109,7 @@ LocalStorage::MapUTF8(const char *uri_utf8) const
{ {
assert(uri_utf8 != nullptr); assert(uri_utf8 != nullptr);
if (*uri_utf8 == 0) if (StringIsEmpty(uri_utf8))
return base_utf8; return base_utf8;
return PathTraitsUTF8::Build(base_utf8.c_str(), uri_utf8); return PathTraitsUTF8::Build(base_utf8.c_str(), uri_utf8);
@ -119,7 +120,7 @@ LocalStorage::MapFS(const char *uri_utf8, Error &error) const
{ {
assert(uri_utf8 != nullptr); assert(uri_utf8 != nullptr);
if (*uri_utf8 == 0) if (StringIsEmpty(uri_utf8))
return base_fs; return base_fs;
AllocatedPath path_fs = AllocatedPath::FromUTF8(uri_utf8, error); AllocatedPath path_fs = AllocatedPath::FromUTF8(uri_utf8, error);

View File

@ -30,13 +30,14 @@
#include "lib/nfs/Connection.hxx" #include "lib/nfs/Connection.hxx"
#include "lib/nfs/Glue.hxx" #include "lib/nfs/Glue.hxx"
#include "fs/AllocatedPath.hxx" #include "fs/AllocatedPath.hxx"
#include "util/Error.hxx"
#include "thread/Mutex.hxx" #include "thread/Mutex.hxx"
#include "thread/Cond.hxx" #include "thread/Cond.hxx"
#include "event/Loop.hxx" #include "event/Loop.hxx"
#include "event/Call.hxx" #include "event/Call.hxx"
#include "event/DeferredMonitor.hxx" #include "event/DeferredMonitor.hxx"
#include "event/TimeoutMonitor.hxx" #include "event/TimeoutMonitor.hxx"
#include "util/Error.hxx"
#include "util/StringCompare.hxx"
extern "C" { extern "C" {
#include <nfsc/libnfs.h> #include <nfsc/libnfs.h>
@ -232,7 +233,7 @@ NfsStorage::MapUTF8(const char *uri_utf8) const
{ {
assert(uri_utf8 != nullptr); assert(uri_utf8 != nullptr);
if (*uri_utf8 == 0) if (StringIsEmpty(uri_utf8))
return base; return base;
return PathTraitsUTF8::Build(base.c_str(), uri_utf8); return PathTraitsUTF8::Build(base.c_str(), uri_utf8);

View File

@ -25,8 +25,9 @@
#include "lib/smbclient/Init.hxx" #include "lib/smbclient/Init.hxx"
#include "lib/smbclient/Mutex.hxx" #include "lib/smbclient/Mutex.hxx"
#include "fs/Traits.hxx" #include "fs/Traits.hxx"
#include "util/Error.hxx"
#include "thread/Mutex.hxx" #include "thread/Mutex.hxx"
#include "util/Error.hxx"
#include "util/StringCompare.hxx"
#include <libsmbclient.h> #include <libsmbclient.h>
@ -80,7 +81,7 @@ SmbclientStorage::MapUTF8(const char *uri_utf8) const
{ {
assert(uri_utf8 != nullptr); assert(uri_utf8 != nullptr);
if (*uri_utf8 == 0) if (StringIsEmpty(uri_utf8))
return base; return base;
return PathTraitsUTF8::Build(base.c_str(), uri_utf8); return PathTraitsUTF8::Build(base.c_str(), uri_utf8);

View File

@ -36,6 +36,12 @@
#include "WStringCompare.hxx" #include "WStringCompare.hxx"
#endif #endif
static inline bool
StringIsEmpty(const char *string)
{
return *string == 0;
}
gcc_pure gcc_pure
bool bool
StringStartsWith(const char *haystack, const char *needle); StringStartsWith(const char *haystack, const char *needle);

View File

@ -34,6 +34,12 @@
#include <wchar.h> #include <wchar.h>
static inline bool
StringIsEmpty(const wchar_t *string)
{
return *string == 0;
}
gcc_pure gcc_pure
bool bool
StringStartsWith(const wchar_t *haystack, const wchar_t *needle); StringStartsWith(const wchar_t *haystack, const wchar_t *needle);