From: <man...@us...> - 2014-09-29 13:43:35
|
Revision: 4326 http://sourceforge.net/p/modplug/code/4326 Author: manxorist Date: 2014-09-29 13:43:27 +0000 (Mon, 29 Sep 2014) Log Message: ----------- [Ref] Avoid source code duplication and implement RTrim, LTrim, Trim and Replace as templates. Modified Paths: -------------- trunk/OpenMPT/common/mptString.h trunk/OpenMPT/test/test.cpp Modified: trunk/OpenMPT/common/mptString.h =================================================================== --- trunk/OpenMPT/common/mptString.h 2014-09-29 12:38:02 UTC (rev 4325) +++ trunk/OpenMPT/common/mptString.h 2014-09-29 13:43:27 UTC (rev 4326) @@ -30,79 +30,70 @@ { +template <typename Tstring> struct Traits { + static const char * GetDefaultWhitespace() { return " \n\r\t"; } +}; + +template <> struct Traits<std::string> { + static const char * GetDefaultWhitespace() { return " \n\r\t"; } +}; + +template <> struct Traits<std::wstring> { + static const wchar_t * GetDefaultWhitespace() { return L" \n\r\t"; } +}; + + // Remove whitespace at start of string -static inline std::string LTrim(std::string str, const std::string &whitespace = " \n\r\t") -//----------------------------------------------------------------------------------------- +template <typename Tstring> +inline Tstring LTrim(Tstring str, const Tstring &whitespace = mpt::String::Traits<Tstring>::GetDefaultWhitespace()) +//----------------------------------------------------------------------------------------------------------------- { - std::string::size_type pos = str.find_first_not_of(whitespace); - if(pos != std::string::npos) + typename Tstring::size_type pos = str.find_first_not_of(whitespace); + if(pos != Tstring::npos) { str.erase(str.begin(), str.begin() + pos); - } else if(pos == std::string::npos && str.length() > 0 && str.find_last_of(whitespace) == str.length() - 1) + } else if(pos == Tstring::npos && str.length() > 0 && str.find_last_of(whitespace) == str.length() - 1) { - return std::string(); + return Tstring(); } return str; } -static inline std::wstring LTrim(std::wstring str, const std::wstring &whitespace = L" \n\r\t") -{ - std::wstring::size_type pos = str.find_first_not_of(whitespace); - if(pos != std::wstring::npos) - { - str.erase(str.begin(), str.begin() + pos); - } else if(pos == std::wstring::npos && str.length() > 0 && str.find_last_of(whitespace) == str.length() - 1) - { - return std::wstring(); - } - return str; -} // Remove whitespace at end of string -static inline std::string RTrim(std::string str, const std::string &whitespace = " \n\r\t") -//----------------------------------------------------------------------------------------- +template <typename Tstring> +inline Tstring RTrim(Tstring str, const Tstring &whitespace = mpt::String::Traits<Tstring>::GetDefaultWhitespace()) +//----------------------------------------------------------------------------------------------------------------- { - std::string::size_type pos = str.find_last_not_of(whitespace); - if(pos != std::string::npos) + typename Tstring::size_type pos = str.find_last_not_of(whitespace); + if(pos != Tstring::npos) { str.erase(str.begin() + pos + 1, str.end()); - } else if(pos == std::string::npos && str.length() > 0 && str.find_first_of(whitespace) == 0) + } else if(pos == Tstring::npos && str.length() > 0 && str.find_first_of(whitespace) == 0) { - return std::string(); + return Tstring(); } return str; } -static inline std::wstring RTrim(std::wstring str, const std::wstring &whitespace = L" \n\r\t") -{ - std::wstring::size_type pos = str.find_last_not_of(whitespace); - if(pos != std::wstring::npos) - { - str.erase(str.begin() + pos + 1, str.end()); - } else if(pos == std::wstring::npos && str.length() > 0 && str.find_first_of(whitespace) == 0) - { - return std::wstring(); - } - return str; -} // Remove whitespace at start and end of string -static inline std::string Trim(std::string str, const std::string &whitespace = " \n\r\t") -//---------------------------------------------------------------------------------------- +template <typename Tstring> +inline Tstring Trim(Tstring str, const Tstring &whitespace = mpt::String::Traits<Tstring>::GetDefaultWhitespace()) +//---------------------------------------------------------------------------------------------------------------- { return RTrim(LTrim(str, whitespace), whitespace); } -static inline std::wstring Trim(std::wstring str, const std::wstring &whitespace = L" \n\r\t") -{ - return RTrim(LTrim(str, whitespace), whitespace); -} -static inline std::string Replace(std::string str, const std::string &oldStr, const std::string &newStr) -//------------------------------------------------------------------------------------------------------ +template <typename Tstring, typename Tstring2, typename Tstring3> +inline Tstring Replace(Tstring str, const Tstring2 &oldStr_, const Tstring3 &newStr_) +//----------------------------------------------------------------------------------- { std::size_t pos = 0; - while((pos = str.find(oldStr, pos)) != std::string::npos) + const Tstring oldStr = oldStr_; + const Tstring newStr = newStr_; + while((pos = str.find(oldStr, pos)) != Tstring::npos) { str.replace(pos, oldStr.length(), newStr); pos += newStr.length(); @@ -111,18 +102,6 @@ } -static inline std::wstring Replace(std::wstring str, const std::wstring &oldStr, const std::wstring &newStr) -{ - std::size_t pos = 0; - while((pos = str.find(oldStr, pos)) != std::string::npos) - { - str.replace(pos, oldStr.length(), newStr); - pos += newStr.length(); - } - return str; -} - - } // namespace String Modified: trunk/OpenMPT/test/test.cpp =================================================================== --- trunk/OpenMPT/test/test.cpp 2014-09-29 12:38:02 UTC (rev 4325) +++ trunk/OpenMPT/test/test.cpp 2014-09-29 13:43:27 UTC (rev 4326) @@ -531,9 +531,9 @@ VERIFY_EQUAL( mpt::saturate_cast<uint32>(static_cast<double>(std::numeric_limits<int64>::max())), std::numeric_limits<uint32>::max() ); - VERIFY_EQUAL( mpt::String::LTrim(" "), "" ); - VERIFY_EQUAL( mpt::String::RTrim(" "), "" ); - VERIFY_EQUAL( mpt::String::Trim(" "), "" ); + VERIFY_EQUAL( mpt::String::LTrim(std::string(" ")), "" ); + VERIFY_EQUAL( mpt::String::RTrim(std::string(" ")), "" ); + VERIFY_EQUAL( mpt::String::Trim(std::string(" ")), "" ); // weird things with std::string containing \0 in the middle and trimming \0 VERIFY_EQUAL( std::string("\0\ta\0b ",6).length(), (std::size_t)6 ); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |