Skip to content

Commit 3d6e710

Browse files
committed
Replaced String::trim function with generic trim functions which work with modern C++ types, such as string_view.
1 parent 214d652 commit 3d6e710

1 file changed

Lines changed: 41 additions & 1 deletion

File tree

include/libdbc/utils/utils.hpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include <sstream>
77
#include <string>
88

9+
#if __cplusplus >= 201703L
10+
# include <string_view>
11+
#endif // __cplusplus >= 201703L
12+
913
namespace Utils {
1014

1115
class StreamHandler {
@@ -26,9 +30,45 @@ class StreamHandler {
2630
static std::istream& skip_to_next_blank_line(std::istream& stream, std::string& line);
2731
};
2832

33+
#if __cplusplus >= 201703
34+
template<typename T>
35+
constexpr auto trim(const T& value) {
36+
#else
37+
template<typename T>
38+
std::string trim(const T& value) {
39+
#endif // #if __cplusplus >= 201703
40+
T trimmedValue = value;
41+
trimmedValue.erase(trimmedValue.begin(), std::find_if(trimmedValue.begin(), trimmedValue.end(), [](int ch) { return !std::isspace(ch); }));
42+
trimmedValue.erase(std::find_if(trimmedValue.rbegin(), trimmedValue.rend(), [](int ch) { return !std::isspace(ch); }).base(), trimmedValue.end());
43+
return trimmedValue;
44+
}
45+
46+
#if __cplusplus >= 201703L
47+
template<typename T>
48+
constexpr auto trim(const T& value, const std::string_view& trimChars) {
49+
T trimmedValue = value;
50+
trimmedValue.erase(trimmedValue.begin(), std::find_if(trimmedValue.begin(), trimmedValue.end(), [&trimChars](int ch) { return trimChars.find(static_cast<char>(ch)) == std::string_view::npos; }));
51+
trimmedValue.erase(std::find_if(trimmedValue.rbegin(), trimmedValue.rend(), [&trimChars](int ch) { return trimChars.find(static_cast<char>(ch)) == std::string_view::npos; }).base(), trimmedValue.end());
52+
return trimmedValue;
53+
}
54+
#endif // __cplusplus >= 201703L
55+
56+
#if __cplusplus >= 201703
57+
template<typename T>
58+
constexpr auto trim(const T& value, std::initializer_list<char>& trimChars) {
59+
#else
60+
template<typename T>
61+
std::string trim(const T& value, std::initializer_list<char>& trimChars) {
62+
#endif // __cplusplus >= 201703
63+
T trimmedValue = value;
64+
trimmedValue.erase(trimmedValue.begin(), std::find_if(trimmedValue.begin(), trimmedValue.end(), [&trimChars](int ch) { return std::find(trimChars.begin(), trimChars.end(), static_cast<char>(ch)) == trimChars.end(); }));
65+
trimmedValue.erase(std::find_if(trimmedValue.rbegin(), trimmedValue.rend(), [&trimChars](int ch) { return std::find(trimChars.begin(), trimChars.end(), static_cast<char>(ch)) == trimChars.end(); }).base(), trimmedValue.end());
66+
return trimmedValue;
67+
}
68+
2969
class String {
3070
public:
31-
static std::string trim(const std::string& line);
71+
static std::string trim(const std::string& line) { return Utils::trim(line); }
3272

3373
template<class Container>
3474
static void split(const std::string& str, Container& cont, char delim = ' ') {

0 commit comments

Comments
 (0)