My Project
String.hpp
1#ifndef OPM_UTILITY_STRING_HPP
2#define OPM_UTILITY_STRING_HPP
3
4#include <algorithm>
5#include <cctype>
6#include <cstdlib>
7#include <cstring>
8#include <cmath>
9#include <optional>
10#include <sstream>
11#include <string>
12#include <vector>
13
14namespace Opm {
15
16template< typename T, typename U >
17U& uppercase( const T& src, U& dst ) {
18 const auto up = []( char c ) { return std::toupper( c ); };
19 std::transform( std::begin( src ), std::end( src ), std::begin( dst ), up );
20 return dst;
21}
22
23template< typename T >
24typename std::decay< T >::type uppercase( T&& x ) {
25 typename std::decay< T >::type t( std::forward< T >( x ) );
26 return uppercase( t, t );
27}
28
29template<typename T>
30std::string ltrim_copy(const T& s)
31{
32 auto ret = std::string(s.c_str());
33
34 const auto start = ret.find_first_not_of(" \t\n\r\f\v");
35 if (start == std::string::npos)
36 return "";
37
38 return ret.substr(start);
39}
40
41
42template<typename T>
43std::string rtrim_copy(const T& s)
44{
45 auto ret = std::string(s.c_str());
46
47 const auto end = ret.find_last_not_of(" \t\n\r\f\v");
48 if (end == std::string::npos)
49 return "";
50
51 return ret.substr(0, end + 1);
52}
53
54template<typename T>
55std::string trim_copy(const T& s)
56{
57 return ltrim_copy( rtrim_copy(s) );
58}
59
60
61template<typename T>
62void replaceAll(T& data, const T& toSearch, const T& replace)
63{
64 // Get the first occurrence
65 size_t pos = data.find(toSearch);
66
67 // Repeat till end is reached
68 while (pos != std::string::npos)
69 {
70 // Replace this occurrence of Sub String
71 data.replace(pos, toSearch.size(), replace);
72 // Get the next occurrence from the current position
73 pos = data.find(toSearch, pos + replace.size());
74 }
75}
76
77
78inline std::vector<std::string> split_string(const std::string& input,
79 char delimiter)
80{
81 std::vector<std::string> result;
82 std::string token;
83 std::istringstream tokenStream(input);
84 while (std::getline(tokenStream, token, delimiter))
85 result.push_back(token);
86
87 return result;
88}
89
90
91inline std::vector<std::string> split_string(const std::string& input,
92 const std::string& delimiters)
93{
94 std::vector<std::string> result;
95 std::string::size_type start = 0;
96 while (start < input.size()) {
97 auto end = input.find_first_of(delimiters, start);
98 if (end == std::string::npos) {
99 result.push_back(input.substr(start));
100 end = input.size() - 1;
101 } else if (end != start)
102 result.push_back(input.substr(start, end-start));
103
104 start = end + 1;
105 }
106
107 return result;
108}
109
110inline std::string format_double(double d) {
111 double integral_part;
112 const double decimal_part = std::modf(d, &integral_part);
113
114 if (decimal_part == 0)
115 return std::to_string(static_cast<int>(d));
116 else
117 return std::to_string(d);
118}
119
120
121inline std::optional<double> try_parse_double(const std::string& token) {
122 char * end_ptr;
123 auto value = std::strtod(token.c_str(), &end_ptr);
124 if (std::strlen(end_ptr) == 0)
125 return value;
126
127 return std::nullopt;
128}
129
130}
131#endif //OPM_UTILITY_STRING_HPP
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:29