Sfoglia il codice sorgente

Added hasPrefix, and hasSuffix string functions.

Бранимир Караџић 4 anni fa
parent
commit
e7a74d8768
3 ha cambiato i file con 36 aggiunte e 0 eliminazioni
  1. 16 0
      include/bx/inline/string.inl
  2. 6 0
      include/bx/string.h
  3. 14 0
      tests/string_test.cpp

+ 16 - 0
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

+ 6 - 0
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);
 

+ 14 - 0
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", "") );
+}