String.hpp
Go to the documentation of this file.
1#ifndef OPM_UTILITY_STRING_HPP
2#define OPM_UTILITY_STRING_HPP
3
4#include <algorithm>
5#include <cctype>
6#include <sstream>
7#include <string>
8#include <vector>
9
10namespace Opm {
11
12template< typename T, typename U >
13U& uppercase( const T& src, U& dst ) {
14 const auto up = []( char c ) { return std::toupper( c ); };
15 std::transform( std::begin( src ), std::end( src ), std::begin( dst ), up );
16 return dst;
17}
18
19template< typename T >
20typename std::decay< T >::type uppercase( T&& x ) {
21 typename std::decay< T >::type t( std::forward< T >( x ) );
22 return uppercase( t, t );
23}
24
25template<typename T>
27{
28 auto ret = std::string(s.c_str());
29
30 const auto start = ret.find_first_not_of(" \t\n\r\f\v");
31 if (start == std::string::npos)
32 return "";
33
34 return ret.substr(start);
35}
36
37
38template<typename T>
40{
41 auto ret = std::string(s.c_str());
42
43 const auto end = ret.find_last_not_of(" \t\n\r\f\v");
44 if (end == std::string::npos)
45 return "";
46
47 return ret.substr(0, end + 1);
48}
49
50template<typename T>
52{
53 return ltrim_copy( rtrim_copy(s) );
54}
55
56
57template<typename T>
58void replaceAll(T& data, const T& toSearch, const T& replace)
59{
60 // Get the first occurrence
61 size_t pos = data.find(toSearch);
62
63 // Repeat till end is reached
64 while (pos != std::string::npos)
65 {
66 // Replace this occurrence of Sub String
67 data.replace(pos, toSearch.size(), replace);
68 // Get the next occurrence from the current position
69 pos = data.find(toSearch, pos + replace.size());
70 }
71}
72
73
74inline std::vector<std::string> split_string(const std::string& input,
75 char delimiter)
76{
77 std::vector<std::string> result;
78 std::string token;
79 std::istringstream tokenStream(input);
80 while (std::getline(tokenStream, token, delimiter))
81 result.push_back(token);
82
83 return result;
84}
85
86
87inline std::vector<std::string> split_string(const std::string& input,
88 const std::string& delimiters)
89{
90 std::vector<std::string> result;
91 std::string::size_type start = 0;
92 while (start < input.size()) {
93 auto end = input.find_first_of(delimiters, start);
94 if (end == std::string::npos) {
95 result.push_back(input.substr(start));
96 end = input.size() - 1;
97 } else if (end != start)
98 result.push_back(input.substr(start, end-start));
99
100 start = end + 1;
101 }
102
103 return result;
104}
105
106}
107
108#endif //OPM_UTILITY_STRING_HPP
const char *const string
Definition: cJSON.h:170
not_this_one begin(...)
Definition: A.hpp:4
std::string trim_copy(const T &s)
Definition: String.hpp:51
std::vector< std::string > split_string(const std::string &input, char delimiter)
Definition: String.hpp:74
std::string rtrim_copy(const T &s)
Definition: String.hpp:39
@ end
Definition: ActionValue.hpp:20
std::string ltrim_copy(const T &s)
Definition: String.hpp:26
void replaceAll(T &data, const T &toSearch, const T &replace)
Definition: String.hpp:58
U & uppercase(const T &src, U &dst)
Definition: String.hpp:13
x y t t *t x y t t t x y t t t x *y t *t t x *y t *t t x y t t t x y t t t t(t+t)") define_sfop3(16
x y t t *t x y t t t x y t t t x *y t *t t x *y t *t t x y t t t x y t t t x(y+z)
static std::string data()
Definition: exprtk.hpp:40022