Skip to content

Commit 01747c7

Browse files
committed
Added <algorithm> header.
Added support for concepts, if enabled and supported by the compiler.
1 parent 3abbf0c commit 01747c7

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

include/libdbc/utils/utils.hpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#ifndef UTILS_HPP
33
#define UTILS_HPP
44

5+
#include <algorithm>
56
#include <iostream>
67
#include <sstream>
78
#include <string>
@@ -10,6 +11,12 @@
1011
# include <string_view>
1112
#endif // __cplusplus >= 201703L
1213

14+
#if __cpp_concepts >= 201907
15+
#include <concepts>
16+
template<typename Str>
17+
concept procsys_stringable = requires(Str s) { { s.data() + s.size() } -> std::convertible_to<const char*>; };
18+
#endif // __cpp_concepts >= 201907
19+
1320
namespace Utils {
1421

1522
class StreamHandler {
@@ -31,7 +38,11 @@ class StreamHandler {
3138
};
3239

3340
#if __cplusplus >= 201703
34-
template<typename T>
41+
# if __cpp_concepts >= 201907
42+
template<procsys_stringable T>
43+
# else
44+
template<typename T>
45+
# endif // __cpp_concepts >= 201907
3546
constexpr auto trim(const T& value) {
3647
#else
3748
template<typename T>
@@ -43,18 +54,12 @@ std::string trim(const T& value) {
4354
return trimmedValue;
4455
}
4556

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-
5657
#if __cplusplus >= 201703
57-
template<typename T>
58+
# if __cpp_concepts >= 201907
59+
template<procsys_stringable T>
60+
# else
61+
template<typename T>
62+
# endif // __cpp_concepts >= 201907
5863
constexpr auto trim(const T& value, std::initializer_list<char>& trimChars) {
5964
#else
6065
template<typename T>
@@ -66,6 +71,15 @@ std::string trim(const T& value, std::initializer_list<char>& trimChars) {
6671
return trimmedValue;
6772
}
6873

74+
#if __cplusplus >= 201703L
75+
# if __cpp_concepts >= 201907
76+
template<procsys_stringable T>
77+
# else
78+
template<typename T>
79+
# endif // __cpp_concepts >= 201907
80+
constexpr auto trim(const T& value, const std::string_view& trimChars) { return trim(value, std::initializer_list<char>(trimChars.begin(), trimChars.end())); }
81+
#endif // __cplusplus >= 201703L
82+
6983
class String {
7084
public:
7185
static std::string trim(const std::string& line) { return Utils::trim(line); }

0 commit comments

Comments
 (0)