mirror of
https://github.com/FEX-Emu/FEX.git
synced 2026-10-06 08:00:15 +02:00
StringUtils: Handle strings entirely composed of whitespace in trims
Previously this wouldn't handle fully whitespaced strings.
This commit is contained in:
1 parent
ce27754b9d
commit
4afa49824e
2 files changed
+14
-8
No files matched your search
@@ -5,21 +5,21 @@
|
||||
namespace FEXCore::StringUtils {
|
||||
// Trim the left side of the string of whitespace and new lines
|
||||
inline fextl::string LeftTrim(fextl::string String, std::string_view TrimTokens = " \t\n\r\f\v") {
|
||||
size_t pos = fextl::string::npos;
|
||||
if ((pos = String.find_first_not_of(TrimTokens)) != fextl::string::npos) {
|
||||
String.erase(0, pos);
|
||||
const size_t pos = String.find_first_not_of(TrimTokens);
|
||||
if (pos == fextl::string::npos) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return String;
|
||||
return String.erase(0, pos);
|
||||
}
|
||||
|
||||
// Trim the right side of the string of whitespace and new lines
|
||||
inline fextl::string RightTrim(fextl::string String, std::string_view TrimTokens = " \t\n\r\f\v") {
|
||||
size_t pos = fextl::string::npos;
|
||||
if ((pos = String.find_last_not_of(TrimTokens)) != fextl::string::npos) {
|
||||
String.erase(String.begin() + pos + 1, String.end());
|
||||
const size_t pos = String.find_last_not_of(TrimTokens);
|
||||
if (pos == fextl::string::npos) {
|
||||
return "";
|
||||
}
|
||||
|
||||
String.erase(String.begin() + pos + 1, String.end());
|
||||
return String;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@ using namespace FEXCore::StringUtils;
|
||||
|
||||
TEST_CASE("ltrim") {
|
||||
CHECK(LeftTrim("") == "");
|
||||
CHECK(LeftTrim(" ") == "");
|
||||
CHECK(LeftTrim(" \t\n\r\f\v") == "");
|
||||
CHECK(LeftTrim("FEXInterpreter") == "FEXInterpreter");
|
||||
|
||||
CHECK(LeftTrim("FEXInterpreter\n") == "FEXInterpreter\n");
|
||||
@@ -31,6 +33,8 @@ TEST_CASE("ltrim") {
|
||||
|
||||
TEST_CASE("rtrim") {
|
||||
CHECK(RightTrim("") == "");
|
||||
CHECK(RightTrim(" ") == "");
|
||||
CHECK(RightTrim(" \t\n\r\f\v") == "");
|
||||
CHECK(RightTrim("FEXInterpreter") == "FEXInterpreter");
|
||||
|
||||
CHECK(RightTrim("FEXInterpreter\n") == "FEXInterpreter");
|
||||
@@ -57,6 +61,8 @@ TEST_CASE("rtrim") {
|
||||
|
||||
TEST_CASE("trim") {
|
||||
CHECK(Trim("") == "");
|
||||
CHECK(Trim(" ") == "");
|
||||
CHECK(Trim(" \t\n\r\f\v") == "");
|
||||
CHECK(Trim("FEXInterpreter") == "FEXInterpreter");
|
||||
|
||||
CHECK(Trim("FEXInterpreter\n") == "FEXInterpreter");
|
||||
|
||||
Reference in new issue
Block a user