Преглед изворни кода

Added strRTrimSpace and strTrimSpace.

Бранимир Караџић пре 4 година
родитељ
комит
99d4cb78ad
3 измењених фајлова са 56 додато и 0 уклоњено
  1. 6 0
      include/bx/string.h
  2. 23 0
      src/string.cpp
  3. 27 0
      tests/string_test.cpp

+ 6 - 0
include/bx/string.h

@@ -251,9 +251,15 @@ namespace bx
 	/// Returns string view with characters _chars trimmed from right.
 	/// Returns string view with characters _chars trimmed from right.
 	StringView strRTrim(const StringView& _str, const StringView& _chars);
 	StringView strRTrim(const StringView& _str, const StringView& _chars);
 
 
+	/// Returns string view with whitespace characters trimmed from right.
+	StringView strRTrimSpace(const StringView& _str);
+
 	/// Returns string view with characters _chars trimmed from left and right.
 	/// Returns string view with characters _chars trimmed from left and right.
 	StringView strTrim(const StringView& _str, const StringView& _chars);
 	StringView strTrim(const StringView& _str, const StringView& _chars);
 
 
+	/// Returns string view with whitespace characters trimmed from left and right.
+	StringView strTrimSpace(const StringView& _str);
+
 	/// Find new line. Returns pointer after new line terminator.
 	/// Find new line. Returns pointer after new line terminator.
 	StringView strFindNl(const StringView& _str);
 	StringView strFindNl(const StringView& _str);
 
 

+ 23 - 0
src/string.cpp

@@ -530,11 +530,34 @@ namespace bx
 		return _str;
 		return _str;
 	}
 	}
 
 
+	StringView strRTrimSpace(const StringView& _str)
+	{
+		if (!_str.isEmpty() )
+		{
+			const char* ptr = _str.getPtr();
+
+			for (int32_t len = _str.getLength(), ii = len - 1; 0 <= ii; --ii)
+			{
+				if (!isSpace(ptr[ii]) )
+				{
+					return StringView(ptr, ii + 1);
+				}
+			}
+		}
+
+		return _str;
+	}
+
 	StringView strTrim(const StringView& _str, const StringView& _chars)
 	StringView strTrim(const StringView& _str, const StringView& _chars)
 	{
 	{
 		return strLTrim(strRTrim(_str, _chars), _chars);
 		return strLTrim(strRTrim(_str, _chars), _chars);
 	}
 	}
 
 
+	StringView strTrimSpace(const StringView& _str)
+	{
+		return strLTrimSpace(strRTrimSpace(_str) );
+	}
+
 	constexpr uint32_t kFindStep = 1024;
 	constexpr uint32_t kFindStep = 1024;
 
 
 	StringView strFindNl(const StringView& _str)
 	StringView strFindNl(const StringView& _str)

+ 27 - 0
tests/string_test.cpp

@@ -494,6 +494,33 @@ TEST_CASE("Trim", "")
 	REQUIRE(0 == bx::strCmp(bx::strTrim(uri.getPath(), "/"), "555333/podmac") );
 	REQUIRE(0 == bx::strCmp(bx::strTrim(uri.getPath(), "/"), "555333/podmac") );
 }
 }
 
 
+TEST_CASE("TrimSpace", "")
+{
+	REQUIRE(bx::strLTrimSpace("").isEmpty() );
+	REQUIRE(bx::strRTrimSpace("").isEmpty() );
+	REQUIRE(bx::strTrimSpace( "").isEmpty() );
+
+	const bx::StringView t0("1389");
+	const bx::StringView t1("    1389");
+	const bx::StringView t2("1389    ");
+	const bx::StringView t3("  1389  ");
+
+	REQUIRE(0 == bx::strCmp(bx::strLTrimSpace(t0), t0) );
+	REQUIRE(0 == bx::strCmp(bx::strLTrimSpace(t1), t0) );
+	REQUIRE(0 == bx::strCmp(bx::strLTrimSpace(t2), t2) );
+	REQUIRE(0 == bx::strCmp(bx::strLTrimSpace(t3), "1389  ") );
+
+	REQUIRE(0 == bx::strCmp(bx::strRTrimSpace(t0), t0) );
+	REQUIRE(0 == bx::strCmp(bx::strRTrimSpace(t1), t1) );
+	REQUIRE(0 == bx::strCmp(bx::strRTrimSpace(t2), t0) );
+	REQUIRE(0 == bx::strCmp(bx::strRTrimSpace(t3), "  1389") );
+
+	REQUIRE(0 == bx::strCmp(bx::strTrimSpace(t0), t0) );
+	REQUIRE(0 == bx::strCmp(bx::strTrimSpace(t1), t0) );
+	REQUIRE(0 == bx::strCmp(bx::strTrimSpace(t2), t0) );
+	REQUIRE(0 == bx::strCmp(bx::strTrimSpace(t3), t0) );
+}
+
 TEST_CASE("strWord", "")
 TEST_CASE("strWord", "")
 {
 {
 	REQUIRE(bx::strWord(" abvgd-1389.0").isEmpty() );
 	REQUIRE(bx::strWord(" abvgd-1389.0").isEmpty() );