Util/WStringCompare: use struct WStringView

This commit is contained in:
Max Kellermann 2017-09-12 19:18:28 +02:00
parent eff821c1ca
commit 8d9347edc5
2 changed files with 15 additions and 31 deletions

View File

@ -18,17 +18,6 @@
*/ */
#include "WStringCompare.hxx" #include "WStringCompare.hxx"
#include "WStringAPI.hxx"
#include <assert.h>
#include <string.h>
bool
StringStartsWith(const wchar_t *haystack, const wchar_t *needle) noexcept
{
const size_t length = StringLength(needle);
return StringIsEqual(haystack, needle, length);
}
bool bool
StringEndsWith(const wchar_t *haystack, const wchar_t *needle) noexcept StringEndsWith(const wchar_t *haystack, const wchar_t *needle) noexcept
@ -40,21 +29,6 @@ StringEndsWith(const wchar_t *haystack, const wchar_t *needle) noexcept
StringIsEqual(haystack + haystack_length - needle_length, needle); StringIsEqual(haystack + haystack_length - needle_length, needle);
} }
const wchar_t *
StringAfterPrefix(const wchar_t *string, const wchar_t *prefix) noexcept
{
#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 = StringLength(prefix);
return StringIsEqual(string, prefix, prefix_length)
? string + prefix_length
: nullptr;
}
const wchar_t * const wchar_t *
FindStringSuffix(const wchar_t *p, const wchar_t *suffix) noexcept FindStringSuffix(const wchar_t *p, const wchar_t *suffix) noexcept
{ {

View File

@ -30,6 +30,8 @@
#ifndef WSTRING_COMPARE_HXX #ifndef WSTRING_COMPARE_HXX
#define WSTRING_COMPARE_HXX #define WSTRING_COMPARE_HXX
#include "WStringView.hxx"
#include "WStringAPI.hxx"
#include "Compiler.h" #include "Compiler.h"
#include <wchar.h> #include <wchar.h>
@ -40,9 +42,12 @@ StringIsEmpty(const wchar_t *string) noexcept
return *string == 0; return *string == 0;
} }
gcc_pure gcc_pure gcc_nonnull_all
bool static inline bool
StringStartsWith(const wchar_t *haystack, const wchar_t *needle) noexcept; StringStartsWith(const wchar_t *haystack, WStringView needle) noexcept
{
return StringIsEqual(haystack, needle.data, needle.size);
}
gcc_pure gcc_pure
bool bool
@ -54,8 +59,13 @@ StringEndsWith(const wchar_t *haystack, const wchar_t *needle) noexcept;
* nullptr. * nullptr.
*/ */
gcc_pure gcc_nonnull_all gcc_pure gcc_nonnull_all
const wchar_t * static inline const wchar_t *
StringAfterPrefix(const wchar_t *string, const wchar_t *prefix) noexcept; StringAfterPrefix(const wchar_t *haystack, WStringView needle) noexcept
{
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,