Fix various trim functions (#271)

Trim doesn't handle the case where the whole string should be trimmed,
due to being composed of only trimmable chars.
For example the string "\n" when trimmed with chars " \t\n\r" should
yield the empty string "" and not return "\n".
Some test cases were fixed and some were added.
This commit is contained in:
kingscallop
2022-01-01 18:38:58 +00:00
committed by GitHub
parent 5fea2103d1
commit 0ccbdcaa0c
2 changed files with 16 additions and 4 deletions

View File

@@ -480,7 +480,7 @@ namespace bx
}
}
return _str;
return StringView(_str.getTerm(), _str.getTerm() );
}
StringView strLTrimSpace(const StringView& _str)
@@ -523,7 +523,9 @@ namespace bx
{
return StringView(ptr, ii + 1);
}
}
}
return StringView(_str.getPtr(), _str.getPtr());
}
return _str;
@@ -542,6 +544,8 @@ namespace bx
return StringView(ptr, ii + 1);
}
}
return StringView(_str.getPtr(), _str.getPtr());
}
return _str;

View File

@@ -475,12 +475,16 @@ TEST_CASE("StringView", "")
TEST_CASE("Trim", "")
{
REQUIRE(bx::strLTrim("a", "a").isEmpty() );
REQUIRE(bx::strRTrim("a", "a").isEmpty() );
REQUIRE(bx::strTrim("a", "a").isEmpty() );
REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "ab"), "vgd") );
REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "vagbd"), "abvgd") );
REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "vagbd"), "") );
REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "vgd"), "abvgd") );
REQUIRE(0 == bx::strCmp(bx::strLTrim("/555333/podmac/", "/"), "555333/podmac/") );
REQUIRE(0 == bx::strCmp(bx::strRTrim("abvgd", "vagbd"), "abvgd") );
REQUIRE(0 == bx::strCmp(bx::strRTrim("abvgd", "vagbd"), "") );
REQUIRE(0 == bx::strCmp(bx::strRTrim("abvgd", "abv"), "abvgd") );
REQUIRE(0 == bx::strCmp(bx::strRTrim("/555333/podmac/", "/"), "/555333/podmac") );
@@ -501,6 +505,10 @@ TEST_CASE("TrimSpace", "")
REQUIRE(bx::strRTrimSpace("").isEmpty() );
REQUIRE(bx::strTrimSpace( "").isEmpty() );
REQUIRE(bx::strLTrimSpace("\n").isEmpty() );
REQUIRE(bx::strRTrimSpace("\n").isEmpty() );
REQUIRE(bx::strTrimSpace( "\n").isEmpty() );
const bx::StringView t0("1389");
const bx::StringView t1(" 1389");
const bx::StringView t2("1389 ");