From e7a74d8768fef746f603cc17f2570aec23e10c07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Fri, 1 Oct 2021 20:12:44 -0700 Subject: [PATCH] Added hasPrefix, and hasSuffix string functions. --- include/bx/inline/string.inl | 16 ++++++++++++++++ include/bx/string.h | 6 ++++++ tests/string_test.cpp | 14 ++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/include/bx/inline/string.inl b/include/bx/inline/string.inl index 1aa830f..e049b0c 100644 --- a/include/bx/inline/string.inl +++ b/include/bx/inline/string.inl @@ -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 diff --git a/include/bx/string.h b/include/bx/string.h index 7b27cc3..edb257a 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -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); diff --git a/tests/string_test.cpp b/tests/string_test.cpp index 40f58cf..48ee8d8 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -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", "") ); +}