diff --git a/opm/parser/eclipse/Utility/String.hpp b/opm/parser/eclipse/Utility/String.hpp index 4c303cc1e..5547494a2 100644 --- a/opm/parser/eclipse/Utility/String.hpp +++ b/opm/parser/eclipse/Utility/String.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace Opm { @@ -19,6 +20,18 @@ typename std::decay< T >::type uppercase( T&& x ) { return uppercase( t, t ); } -} +template +std::string trim_copy(const T& s) { +{ + auto ret = std::string(s.c_str()); + + const auto end = ret.find_last_not_of(" \t\n\r\f\v"); + if (end == std::string::npos) + return ""; + + return ret.substr(0, end + 1); +} +} +} #endif //OPM_UTILITY_STRING_HPP diff --git a/tests/test_PaddedOutputString.cpp b/tests/test_PaddedOutputString.cpp index f4a78eed6..5f089f354 100644 --- a/tests/test_PaddedOutputString.cpp +++ b/tests/test_PaddedOutputString.cpp @@ -3,6 +3,7 @@ #include #include +#include // Convenience alias. template @@ -92,4 +93,15 @@ BOOST_AUTO_TEST_CASE (String_Shortening) } } +BOOST_AUTO_TEST_CASE (Trim) { + const auto s1 = PadString<4>{"X"}; + BOOST_CHECK_EQUAL(Opm::trim_copy(s1), std::string{"X"}); + + const auto s2 = PadString<4>{"ABCD"}; + BOOST_CHECK_EQUAL(Opm::trim_copy(s2), std::string{"ABCD"}); + + const auto s3 = PadString<4>{""}; + BOOST_CHECK_EQUAL(Opm::trim_copy(s3), std::string{""}); +} + BOOST_AUTO_TEST_SUITE_END()