mirror of
https://github.com/bkaradzic/bx.git
synced 2026-02-19 05:23:00 +01:00
Added hasPrefix, and hasSuffix string functions.
This commit is contained in:
@@ -292,4 +292,20 @@ namespace bx
|
||||
return m_line;
|
||||
}
|
||||
|
||||
inline bool hasPrefix(const StringView& _str, const StringView& _prefix)
|
||||
{
|
||||
const int32_t len = _prefix.getLength();
|
||||
return _str.getLength() >= len
|
||||
&& 0 == strCmp(_str, _prefix, len)
|
||||
;
|
||||
}
|
||||
|
||||
inline bool hasSuffix(const StringView& _str, const StringView& _suffix)
|
||||
{
|
||||
const int32_t len = _suffix.getLength();
|
||||
return _str.getLength() >= len
|
||||
&& 0 == strCmp(StringView(_str.getTerm() - len, _str.getTerm() ), _suffix, len)
|
||||
;
|
||||
}
|
||||
|
||||
} // namespace bx
|
||||
|
||||
@@ -227,6 +227,12 @@ namespace bx
|
||||
/// Concatinate string.
|
||||
int32_t strCat(char* _dst, int32_t _dstSize, const StringView& _str, int32_t _num = INT32_MAX);
|
||||
|
||||
/// Test whether the string _str begins with prefix.
|
||||
bool hasPrefix(const StringView& _str, const StringView& _prefix);
|
||||
|
||||
/// Test whether the string _str ends with suffix.
|
||||
bool hasSuffix(const StringView& _str, const StringView& _suffix);
|
||||
|
||||
/// Find character in string. Limit search to _max characters.
|
||||
StringView strFind(const StringView& _str, char _ch);
|
||||
|
||||
|
||||
@@ -535,3 +535,17 @@ TEST_CASE("strFindBlock", "")
|
||||
bx::StringView result = bx::strFindBlock(test1, '{', '}');
|
||||
REQUIRE(19 == result.getLength() );
|
||||
}
|
||||
|
||||
TEST_CASE("hasPrefix", "")
|
||||
{
|
||||
REQUIRE( bx::hasPrefix("abvgd-1389.0", "abv") );
|
||||
REQUIRE(!bx::hasPrefix("abvgd-1389.0", "bvg") );
|
||||
REQUIRE( bx::hasPrefix("abvgd-1389.0", "") );
|
||||
}
|
||||
|
||||
TEST_CASE("hasSuffix", "")
|
||||
{
|
||||
REQUIRE( bx::hasSuffix("abvgd-1389.0", "389.0") );
|
||||
REQUIRE(!bx::hasSuffix("abvgd-1389.0", "1389") );
|
||||
REQUIRE( bx::hasSuffix("abvgd-1389.0", "") );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user