Branimir Karadžić 8 vuotta sitten
vanhempi
sitoutus
5ebcf40c8c
4 muutettua tiedostoa jossa 70 lisäystä ja 38 poistoa
  1. 16 4
      include/bx/string.h
  2. 2 2
      src/filepath.cpp
  3. 34 14
      src/string.cpp
  4. 18 18
      tests/string_test.cpp

+ 16 - 4
include/bx/string.h

@@ -179,16 +179,28 @@ namespace bx
 	int32_t strCat(char* _dst, int32_t _dstSize, const StringView& _str);
 
 	/// Find character in string. Limit search to _max characters.
-	const char* strFind(const char* _str, char _ch, int32_t _max = INT32_MAX);
+	const char* strFind(const char* _str, int32_t _max, char _ch);
+
+	///
+	const char* strFind(const StringView& _str, char _ch);
 
 	/// Find character in string in reverse. Limit search to _max characters.
-	const char* strRFind(const char* _str, char _ch, int32_t _max = INT32_MAX);
+	const char* strRFind(const char* _str, int32_t _max, char _ch);
+
+	///
+	const char* strRFind(const StringView& _str, char _ch);
 
 	/// Find substring in string. Limit search to _max characters.
-	const char* strFind(const char* _str, const char* _find, int32_t _max = INT32_MAX);
+	const char* strFind(const char* _str, int32_t _max, const char* _find, int32_t _findMax = INT32_MAX);
+
+	///
+	const char* strFind(const StringView& _str, const StringView& _find);
 
 	/// Find substring in string. Case insensitive. Limit search to _max characters.
-	const char* strFindI(const char* _str, const char* _find, int32_t _max = INT32_MAX);
+	const char* strFindI(const char* _str, int32_t _max, const char* _find, int32_t _findMax = INT32_MAX);
+
+	///
+	const char* strFindI(const StringView& _str, const StringView& _find);
 
 	/// Find new line. Returns pointer after new line terminator.
 	const char* strnl(const char* _str);

+ 2 - 2
src/filepath.cpp

@@ -239,7 +239,7 @@ namespace bx
 
 	const StringView FilePath::getPath() const
 	{
-		const char* end = strRFind(m_filePath, '/');
+		const char* end = strRFind(m_filePath, INT32_MAX, '/');
 		if (NULL != end)
 		{
 			return StringView(m_filePath, end+1);
@@ -250,7 +250,7 @@ namespace bx
 
 	const StringView FilePath::getFileName() const
 	{
-		const char* fileName = strRFind(m_filePath, '/');
+		const char* fileName = strRFind(m_filePath, INT32_MAX, '/');
 		if (NULL != fileName)
 		{
 			return StringView(fileName+1);

+ 34 - 14
src/string.cpp

@@ -266,7 +266,7 @@ namespace bx
 		return strCat(_dst, _dstSize, _str.getPtr(), _str.getLength() );
 	}
 
-	const char* strFind(const char* _str, char _ch, int32_t _max)
+	const char* strFind(const char* _str, int32_t _max, char _ch)
 	{
 		for (int32_t ii = 0, len = strLen(_str, _max); ii < len; ++ii)
 		{
@@ -279,7 +279,12 @@ namespace bx
 		return NULL;
 	}
 
-	const char* strRFind(const char* _str, char _ch, int32_t _max)
+	const char* strFind(const StringView& _str, char _ch)
+	{
+		return strFind(_str.getPtr(), _str.getLength(), _ch);
+	}
+
+	const char* strRFind(const char* _str, int32_t _max, char _ch)
 	{
 		for (int32_t ii = strLen(_str, _max); 0 <= ii; --ii)
 		{
@@ -292,8 +297,13 @@ namespace bx
 		return NULL;
 	}
 
+	const char* strRFind(const StringView& _str, char _ch)
+	{
+		return strRFind(_str.getPtr(), _str.getLength(), _ch);
+	}
+
 	template<CharFn fn>
-	static const char* strStr(const char* _str, int32_t _strMax, const char* _find, int32_t _findMax)
+	static const char* strFind(const char* _str, int32_t _strMax, const char* _find, int32_t _findMax)
 	{
 		const char* ptr = _str;
 
@@ -333,27 +343,37 @@ namespace bx
 		return NULL;
 	}
 
-	const char* strFind(const char* _str, const char* _find, int32_t _max)
+	const char* strFind(const char* _str, int32_t _max, const char* _find, int32_t _findMax)
+	{
+		return strFind<toNoop>(_str, _max, _find, _findMax);
+	}
+
+	const char* strFind(const StringView& _str, const StringView& _find)
+	{
+		return strFind(_str.getPtr(), _str.getLength(), _find.getPtr(), _find.getLength() );
+	}
+
+	const char* strFindI(const char* _str, int32_t _max, const char* _find, int32_t _findMax)
 	{
-		return strStr<toNoop>(_str, _max, _find, INT32_MAX);
+		return strFind<toLower>(_str, _max, _find, _findMax);
 	}
 
-	const char* strFindI(const char* _str, const char* _find, int32_t _max)
+	const char* strFindI(const StringView& _str, const StringView& _find)
 	{
-		return strStr<toLower>(_str, _max, _find, INT32_MAX);
+		return strFindI(_str.getPtr(), _str.getLength(), _find.getPtr(), _find.getLength() );
 	}
 
 	const char* strnl(const char* _str)
 	{
 		for (; '\0' != *_str; _str += strLen(_str, 1024) )
 		{
-			const char* eol = strFind(_str, "\r\n", 1024);
+			const char* eol = strFind(_str, 1024, "\r\n");
 			if (NULL != eol)
 			{
 				return eol + 2;
 			}
 
-			eol = strFind(_str, "\n", 1024);
+			eol = strFind(_str, 1024, "\n");
 			if (NULL != eol)
 			{
 				return eol + 1;
@@ -367,13 +387,13 @@ namespace bx
 	{
 		for (; '\0' != *_str; _str += strLen(_str, 1024) )
 		{
-			const char* eol = strFind(_str, "\r\n", 1024);
+			const char* eol = strFind(_str, 1024, "\r\n");
 			if (NULL != eol)
 			{
 				return eol;
 			}
 
-			eol = strFind(_str, "\n", 1024);
+			eol = strFind(_str, 1024, "\n");
 			if (NULL != eol)
 			{
 				return eol;
@@ -443,8 +463,8 @@ namespace bx
 	const char* findIdentifierMatch(const char* _str, const char* _word)
 	{
 		int32_t len = strLen(_word);
-		const char* ptr = strFind(_str, _word);
-		for (; NULL != ptr; ptr = strFind(ptr + len, _word) )
+		const char* ptr = strFind(_str, INT32_MAX, _word);
+		for (; NULL != ptr; ptr = strFind(ptr + len, INT32_MAX, _word) )
 		{
 			if (ptr != _str)
 			{
@@ -628,7 +648,7 @@ namespace bx
 				toUpperUnsafe(str, len);
 			}
 
-			const char* dot = strFind(str, '.');
+			const char* dot = strFind(str, INT32_MAX, '.');
 			if (NULL != dot)
 			{
 				const int32_t precLen = int32_t(

+ 18 - 18
tests/string_test.cpp

@@ -153,19 +153,19 @@ TEST_CASE("strCmpV sort", "")
 TEST_CASE("strRFind", "")
 {
 	const char* test = "test";
-	REQUIRE(NULL == bx::strRFind(test, 's', 0) );
-	REQUIRE(NULL == bx::strRFind(test, 's', 1) );
-	REQUIRE(&test[2] == bx::strRFind(test, 's') );
+	REQUIRE(NULL == bx::strRFind(test, 0, 's') );
+	REQUIRE(NULL == bx::strRFind(test, 1, 's') );
+	REQUIRE(&test[2] == bx::strRFind(test, INT32_MAX, 's') );
 }
 
 TEST_CASE("strFindI", "")
 {
 	const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
 
-	REQUIRE(NULL == bx::strFindI(test, "quick", 8) );
-	REQUIRE(NULL == bx::strFindI(test, "quick1") );
-	REQUIRE(&test[4] == bx::strFindI(test, "quick", 9) );
-	REQUIRE(&test[4] == bx::strFindI(test, "quick") );
+	REQUIRE(NULL == bx::strFindI(test, 8, "quick") );
+	REQUIRE(NULL == bx::strFindI(test, INT32_MAX, "quick1") );
+	REQUIRE(&test[4] == bx::strFindI(test, 9, "quick") );
+	REQUIRE(&test[4] == bx::strFindI(test, INT32_MAX, "quick") );
 }
 
 TEST_CASE("strFind", "")
@@ -173,23 +173,23 @@ TEST_CASE("strFind", "")
 	{
 		const char* test = "test";
 
-		REQUIRE(NULL == bx::strFind(test, 's', 0) );
-		REQUIRE(NULL == bx::strFind(test, 's', 2) );
-		REQUIRE(&test[2] == bx::strFind(test, 's') );
+		REQUIRE(NULL == bx::strFind(test, 0, 's') );
+		REQUIRE(NULL == bx::strFind(test, 2, 's') );
+		REQUIRE(&test[2] == bx::strFind(test, INT32_MAX, 's') );
 	}
 
 	{
 		const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
 
-		REQUIRE(NULL == bx::strFind(test, "quick", 8) );
-		REQUIRE(NULL == bx::strFind(test, "quick1") );
-		REQUIRE(NULL == bx::strFind(test, "quick", 9) );
-		REQUIRE(NULL == bx::strFind(test, "quick") );
+		REQUIRE(NULL == bx::strFind(test, 8, "quick") );
+		REQUIRE(NULL == bx::strFind(test, INT32_MAX, "quick1") );
+		REQUIRE(NULL == bx::strFind(test, 9, "quick") );
+		REQUIRE(NULL == bx::strFind(test, INT32_MAX, "quick") );
 
-		REQUIRE(NULL == bx::strFind(test, "Quick", 8) );
-		REQUIRE(NULL == bx::strFind(test, "Quick1") );
-		REQUIRE(&test[4] == bx::strFind(test, "Quick", 9) );
-		REQUIRE(&test[4] == bx::strFind(test, "Quick") );
+		REQUIRE(NULL == bx::strFind(test, 8, "Quick") );
+		REQUIRE(NULL == bx::strFind(test, INT32_MAX, "Quick1") );
+		REQUIRE(&test[4] == bx::strFind(test, 9, "Quick") );
+		REQUIRE(&test[4] == bx::strFind(test, INT32_MAX, "Quick") );
 	}
 }