Skip to content

Commit

Permalink
improved trim by removing useless str.empty check
Browse files Browse the repository at this point in the history
  • Loading branch information
martysama0134 committed Apr 28, 2019
1 parent a3d21b4 commit 43f2fad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
6 changes: 2 additions & 4 deletions include/msl/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,14 @@ inline const char * whitespaces()
//! @brief ltrim_in_place left trims in place 'str' of any of the 'chars' characters
inline std::string & ltrim_in_place(std::string & str, const char * chars = whitespaces())
{
if (!str.empty())
str.erase(0, str.find_first_not_of(chars));
str.erase(0, str.find_first_not_of(chars));
return str;
}

//! @brief rtrim_in_place right trims in place 'str' of any of the 'chars' characters
inline std::string & rtrim_in_place(std::string & str, const char * chars = whitespaces())
{
if (!str.empty())
str.erase(str.find_last_not_of(chars) + 1);
str.erase(str.find_last_not_of(chars) + 1);
return str;
}

Expand Down
14 changes: 12 additions & 2 deletions msl/msl_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ int main()
std::cout << '"' << msl::trim(s) << '"' << '\n';
std::cout << '"' << msl::trim_in_place(s) << '"' << '\n';
}
{
auto s = " a"s;
std::cout << '"' << msl::trim(s) << '"' << '\n';
std::cout << '"' << msl::trim_in_place(s) << '"' << '\n';
}
{
auto s = "b "s;
std::cout << '"' << msl::trim(s) << '"' << '\n';
std::cout << '"' << msl::trim_in_place(s) << '"' << '\n';
}
{
auto s = " aaa bbb ccc "s;
std::cout << '"' << msl::trim(s) << '"' << '\n';
Expand All @@ -51,11 +61,11 @@ int main()
msl::bench([] {
auto s = "\n\n \n aaa bbb ccc \t\t \t \t \n"s;
auto r = msl::trim(s);
}); //146ms
}); //142ms
msl::bench([] {
auto s = "\n\n \n aaa bbb ccc \t\t \t \t \n"s;
auto r = msl::trim_in_place(s);
}); //101ms
}); //99ms
}
// string_replace
if constexpr (false)
Expand Down

0 comments on commit 43f2fad

Please sign in to comment.