Skip to content

Commit

Permalink
Remove blank characters from the given string
Browse files Browse the repository at this point in the history
  • Loading branch information
chronoxor committed Dec 23, 2023
1 parent eff12f9 commit 0241552
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
2 changes: 2 additions & 0 deletions examples/string_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ int main(int argc, char** argv)
std::cout << '"' << CppCommon::StringUtils::ToRTrim(" foobar ") << '"' << std::endl;
std::cout << '"' << CppCommon::StringUtils::ToTrim(" foobar ") << '"' << std::endl;

std::cout << '"' << CppCommon::StringUtils::RemoveBlank(" \t foo \t bar \t ") << '"' << std::endl;

std::cout << CppCommon::StringUtils::Contains("a foo a bar a baz", '!') << std::endl;
std::cout << CppCommon::StringUtils::Contains("a foo a bar a baz", 'z') << std::endl;
std::cout << CppCommon::StringUtils::Contains("a foo a bar a baz", "foo") << std::endl;
Expand Down
14 changes: 14 additions & 0 deletions include/string/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ class StringUtils
*/
static std::string& Trim(std::string& str);

//! Remove blank characters from the given string
/*!
\param str - String to remove blank characters
\return String with removed blank characters
*/
static std::string RemoveBlank(std::string_view str);
//! Remove blank characters from the given string
/*!
\param str - String to remove blank characters
\return The same string with removed blank characters
*/
static std::string& RemoveBlank(std::string& str);

//! Compare two strings case sensitive version
/*!
\param str1 - First string to compare
Expand Down Expand Up @@ -310,6 +323,7 @@ class StringUtils
static T FromString(std::string_view str);

private:
static bool IsBlankInternal(char ch);
static char ToLowerInternal(char ch);
static char ToUpperInternal(char ch);
};
Expand Down
28 changes: 23 additions & 5 deletions include/string/string_utils.inl
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,31 @@

namespace CppCommon {

inline bool StringUtils::IsBlank(char ch)
inline bool StringUtils::IsBlankInternal(char ch)
{
return std::isspace(ch);
}

inline char StringUtils::ToLowerInternal(char ch)
inline bool StringUtils::IsBlank(char ch)
{
return (char)std::tolower(ch);
return IsBlankInternal(ch);
}

inline char StringUtils::ToUpperInternal(char ch)
inline char StringUtils::ToLowerInternal(char ch)
{
return (char)std::toupper(ch);
return (char)std::tolower(ch);
}

inline char StringUtils::ToLower(char ch)
{
return ToLowerInternal(ch);
}

inline char StringUtils::ToUpperInternal(char ch)
{
return (char)std::toupper(ch);
}

inline char StringUtils::ToUpper(char ch)
{
return ToUpperInternal(ch);
Expand Down Expand Up @@ -64,6 +69,19 @@ inline std::string& StringUtils::Trim(std::string& str)
return LTrim(RTrim(str));
}

inline std::string StringUtils::RemoveBlank(std::string_view str)
{
std::string result(str);
RemoveBlank(result);
return result;
}

inline std::string& StringUtils::RemoveBlank(std::string& str)
{
str.erase(std::remove_if(str.begin(), str.end(), IsBlankInternal), str.end());
return str;
}

inline bool StringUtils::Contains(std::string_view str, const char ch)
{
return (str.find(ch) != std::string::npos);
Expand Down
2 changes: 2 additions & 0 deletions tests/test_string_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ TEST_CASE("String utilities", "[CppCommon][String]")
str = "foobar";
REQUIRE(CppCommon::StringUtils::Trim(str) == "foobar");

REQUIRE(CppCommon::StringUtils::RemoveBlank(" \t foo\r\nbar \t ") == "foobar");

REQUIRE(!CppCommon::StringUtils::Contains("a foo a bar a baz", '!'));
REQUIRE(CppCommon::StringUtils::Contains("a foo a bar a baz", 'z'));
REQUIRE(CppCommon::StringUtils::Contains("a foo a bar a baz", "foo"));
Expand Down

0 comments on commit 0241552

Please sign in to comment.