Commit 303f3530 authored by captainwong's avatar captainwong

update

parent a2deb999
......@@ -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;
}
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment