util/StringUtil: add StringEndsWith()

Replaces g_str_has_suffix().
This commit is contained in:
Max Kellermann 2014-02-17 23:29:08 +01:00
parent 3a818b6d45
commit 2b21312b36
3 changed files with 17 additions and 3 deletions

View File

@ -36,11 +36,10 @@
#include "fs/Charset.hxx" #include "fs/Charset.hxx"
#include "fs/FileSystem.hxx" #include "fs/FileSystem.hxx"
#include "fs/DirectoryReader.hxx" #include "fs/DirectoryReader.hxx"
#include "util/StringUtil.hxx"
#include "util/UriUtil.hxx" #include "util/UriUtil.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include <glib.h>
#include <assert.h> #include <assert.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <string.h> #include <string.h>
@ -147,7 +146,7 @@ LoadPlaylistFileInfo(PlaylistInfo &info,
memchr(name_fs_str, '\n', name_length) != nullptr) memchr(name_fs_str, '\n', name_length) != nullptr)
return false; return false;
if (!g_str_has_suffix(name_fs_str, PLAYLIST_FILE_SUFFIX)) if (!StringEndsWith(name_fs_str, PLAYLIST_FILE_SUFFIX))
return false; return false;
const auto path_fs = AllocatedPath::Build(parent_path_fs, name_fs); const auto path_fs = AllocatedPath::Build(parent_path_fs, name_fs);

View File

@ -54,6 +54,17 @@ StringStartsWith(const char *haystack, const char *needle)
return memcmp(haystack, needle, length) == 0; return memcmp(haystack, needle, length) == 0;
} }
bool
StringEndsWith(const char *haystack, const char *needle)
{
const size_t haystack_length = strlen(haystack);
const size_t needle_length = strlen(needle);
return haystack_length >= needle_length &&
memcmp(haystack + haystack_length - needle_length,
needle, needle_length) == 0;
}
bool bool
string_array_contains(const char *const* haystack, const char *needle) string_array_contains(const char *const* haystack, const char *needle)
{ {

View File

@ -51,6 +51,10 @@ gcc_pure
bool bool
StringStartsWith(const char *haystack, const char *needle); StringStartsWith(const char *haystack, const char *needle);
gcc_pure
bool
StringEndsWith(const char *haystack, const char *needle);
/** /**
* Checks whether a string array contains the specified string. * Checks whether a string array contains the specified string.
* *