util/StringCompare: use StringView to simplify inline implementations

This commit is contained in:
Max Kellermann 2015-11-06 09:30:54 +01:00
parent 0d1a54262c
commit 4d15db0134
2 changed files with 14 additions and 31 deletions

View File

@ -28,17 +28,6 @@
*/ */
#include "StringCompare.hxx" #include "StringCompare.hxx"
#include "StringAPI.hxx"
#include <assert.h>
#include <string.h>
bool
StringStartsWith(const char *haystack, const char *needle)
{
const size_t length = StringLength(needle);
return StringIsEqual(haystack, needle, length);
}
bool bool
StringEndsWith(const char *haystack, const char *needle) StringEndsWith(const char *haystack, const char *needle)
@ -51,21 +40,6 @@ StringEndsWith(const char *haystack, const char *needle)
needle, needle_length) == 0; needle, needle_length) == 0;
} }
const char *
StringAfterPrefix(const char *string, const char *prefix)
{
#if !CLANG_CHECK_VERSION(3,6)
/* disabled on clang due to -Wtautological-pointer-compare */
assert(string != nullptr);
assert(prefix != nullptr);
#endif
size_t prefix_length = strlen(prefix);
return StringIsEqual(string, prefix, prefix_length)
? string + prefix_length
: nullptr;
}
const char * const char *
FindStringSuffix(const char *p, const char *suffix) FindStringSuffix(const char *p, const char *suffix)
{ {

View File

@ -30,6 +30,7 @@
#ifndef STRING_COMPARE_HXX #ifndef STRING_COMPARE_HXX
#define STRING_COMPARE_HXX #define STRING_COMPARE_HXX
#include "StringView.hxx"
#include "Compiler.h" #include "Compiler.h"
#ifdef _UNICODE #ifdef _UNICODE
@ -42,9 +43,12 @@ StringIsEmpty(const char *string)
return *string == 0; return *string == 0;
} }
gcc_pure gcc_pure gcc_nonnull_all
bool static inline bool
StringStartsWith(const char *haystack, const char *needle); StringStartsWith(const char *haystack, StringView needle)
{
return strncmp(haystack, needle.data, needle.size) == 0;
}
gcc_pure gcc_pure
bool bool
@ -56,8 +60,13 @@ StringEndsWith(const char *haystack, const char *needle);
* nullptr. * nullptr.
*/ */
gcc_pure gcc_nonnull_all gcc_pure gcc_nonnull_all
const char * static inline const char *
StringAfterPrefix(const char *string, const char *prefix); StringAfterPrefix(const char *haystack, StringView needle)
{
return StringStartsWith(haystack, needle)
? haystack + needle.size
: nullptr;
}
/** /**
* Check if the given string ends with the specified suffix. If yes, * Check if the given string ends with the specified suffix. If yes,