From 303f35307e899b28b2d7eaab7e48857180fec619 Mon Sep 17 00:00:00 2001 From: jack-mechrevo <1281261856@qq.com> Date: Sat, 29 Feb 2020 10:13:50 +0800 Subject: [PATCH] update --- jlib/util/str_util.h | 54 +++++++++++------------------- test/test_strutil/test_strutil.cpp | 24 ++++++------- 2 files changed, 31 insertions(+), 47 deletions(-) diff --git a/jlib/util/str_util.h b/jlib/util/str_util.h index 5cd7e63..889c9b5 100644 --- a/jlib/util/str_util.h +++ b/jlib/util/str_util.h @@ -12,43 +12,29 @@ namespace jlib // Taken from https://stackoverflow.com/a/217605/2963736 // Thanks https://stackoverflow.com/users/13430/evan-teran -// trim from start (in place) template <typename StringType> inline void ltrim(StringType& s) { s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); })); } -// trim from end (in place) template <typename StringType> inline void rtrim(StringType& s) { s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }).base(), s.end()); } -// trim from both ends (in place) template <typename StringType> -inline void trim(StringType& s) { - ltrim(s); rtrim(s); -} +inline void trim(StringType& s) { ltrim(s); rtrim(s); } -// trim from start (copying) template <typename StringType> -inline StringType ltrim_copy(StringType s) { - ltrim(s); return s; -} +inline StringType ltrim_copy(StringType s) { ltrim(s); return s; } -// trim from end (copying) template <typename StringType> -inline StringType rtrim_copy(StringType s) { - rtrim(s); return s; -} +inline StringType rtrim_copy(StringType s) { rtrim(s); return s; } -// trim from both ends (copying) template <typename StringType> -inline StringType trim_copy(StringType s) { - trim(s); return s; -} +inline StringType trim_copy(StringType s) { trim(s); return s; } /**************************** join ***************************/ @@ -58,7 +44,7 @@ inline StringType trim_copy(StringType s) { * @note StringContainer 必须为上述StringTypeçš„iterable容器类,如vector,list */ template <typename StringType, typename StringContainer> -StringType join(const StringContainer& container, const StringType& conjunction = StringType()) +inline StringType join(const StringContainer& container, const StringType& conjunction = StringType()) { StringType result; auto itBegin = container.cbegin(); @@ -75,25 +61,23 @@ StringType join(const StringContainer& container, const StringType& conjunction } -/**************************** remove_all ***************************/ +/**************************** erase_all ***************************/ // taken from https://stackoverflow.com/a/20326454 -/** - * @brief 从å—符串str内移除所有cå—符 - */ -inline void remove_all(std::string& str, char c) { + +inline void erase_all(std::string& str, char c) { str.erase(std::remove(str.begin(), str.end(), c), str.end()); } -inline void remove_all(std::wstring& str, wchar_t c) { +inline void erase_all(std::wstring& str, wchar_t c) { str.erase(std::remove(str.begin(), str.end(), c), str.end()); } -inline std::string remove_all_copy(std::string str, char c) { +inline std::string erase_all_copy(std::string str, char c) { str.erase(std::remove(str.begin(), str.end(), c), str.end()); return str; } -inline std::wstring remove_all_copy(std::wstring str, wchar_t c) { +inline std::wstring erase_all_copy(std::wstring str, wchar_t c) { str.erase(std::remove(str.begin(), str.end(), c), str.end()); return str; } @@ -101,38 +85,38 @@ inline std::wstring remove_all_copy(std::wstring str, wchar_t c) { /**************************** case-conv ***************************/ -void to_upper(std::string& str) { +inline void to_upper(std::string& str) { std::for_each(str.begin(), str.end(), [](char& c) { c = ::toupper(c); }); } -void to_upper(std::wstring& str) { +inline void to_upper(std::wstring& str) { std::for_each(str.begin(), str.end(), [](wchar_t& c) { c = ::toupper(c); }); } -std::string to_upper_copy(std::string str) { +inline std::string to_upper_copy(std::string str) { std::for_each(str.begin(), str.end(), [](char& c) { c = ::toupper(c); }); return str; } -std::wstring to_upper_copy(std::wstring str) { +inline std::wstring to_upper_copy(std::wstring str) { std::for_each(str.begin(), str.end(), [](wchar_t& c) { c = ::toupper(c); }); return str; } -void to_lower(std::string& str) { +inline void to_lower(std::string& str) { std::for_each(str.begin(), str.end(), [](char& c) { c = ::tolower(c); }); } -void to_lower(std::wstring& str) { +inline void to_lower(std::wstring& str) { std::for_each(str.begin(), str.end(), [](wchar_t& c) { c = ::tolower(c); }); } -std::string to_lower_copy(std::string str) { +inline std::string to_lower_copy(std::string str) { std::for_each(str.begin(), str.end(), [](char& c) { c = ::tolower(c); }); return str; } -std::wstring to_lower_copy(std::wstring str) { +inline std::wstring to_lower_copy(std::wstring str) { std::for_each(str.begin(), str.end(), [](wchar_t& c) { c = ::tolower(c); }); return str; } diff --git a/test/test_strutil/test_strutil.cpp b/test/test_strutil/test_strutil.cpp index 2cc340b..52245d3 100644 --- a/test/test_strutil/test_strutil.cpp +++ b/test/test_strutil/test_strutil.cpp @@ -41,22 +41,22 @@ int main() // remove_all { string str = "aabbcc"; - assert(remove_all_copy(str, 'a') == "bbcc"); - assert(remove_all_copy(str, 'b') == "aacc"); - assert(remove_all_copy(str, 'c') == "aabb"); + assert(erase_all_copy(str, 'a') == "bbcc"); + assert(erase_all_copy(str, 'b') == "aacc"); + assert(erase_all_copy(str, 'c') == "aabb"); - remove_all(str, 'a'); assert(str == "bbcc"); - remove_all(str, 'b'); assert(str == "cc"); - remove_all(str, 'c'); assert(str == ""); + erase_all(str, 'a'); assert(str == "bbcc"); + erase_all(str, 'b'); assert(str == "cc"); + erase_all(str, 'c'); assert(str == ""); wstring wstr = L"aabbcc"; - assert(remove_all_copy(wstr, L'a') == L"bbcc"); - assert(remove_all_copy(wstr, L'b') == L"aacc"); - assert(remove_all_copy(wstr, L'c') == L"aabb"); + assert(erase_all_copy(wstr, L'a') == L"bbcc"); + assert(erase_all_copy(wstr, L'b') == L"aacc"); + assert(erase_all_copy(wstr, L'c') == L"aabb"); - remove_all(wstr, L'a'); assert(wstr == L"bbcc"); - remove_all(wstr, L'b'); assert(wstr == L"cc"); - remove_all(wstr, L'c'); assert(wstr == L""); + erase_all(wstr, L'a'); assert(wstr == L"bbcc"); + erase_all(wstr, L'b'); assert(wstr == L"cc"); + erase_all(wstr, L'c'); assert(wstr == L""); } // case-conv -- 2.18.1