Added hasPrefix, and hasSuffix string functions.

This commit is contained in:
Бранимир Караџић
2021-10-01 20:12:44 -07:00
parent 9768d3a728
commit e7a74d8768
3 changed files with 36 additions and 0 deletions

View File

@@ -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

View File

@@ -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);

View File

@@ -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", "") );
}