CoolProp  6.6.0
An open-source fluid property and humid air property database
CPstrings.h
Go to the documentation of this file.
1 
2 #ifndef COOLPROP_STRINGS_H
3 #define COOLPROP_STRINGS_H
4 
5 #include <iterator>
6 #include <algorithm>
7 #include <functional>
8 #include <cctype>
9 #include <vector>
10 
11 #if !defined(NO_FMTLIB)
12 # ifndef FMT_HEADER_ONLY
13 # define FMT_HEADER_ONLY
14 # endif
15 # include "fmt/format.h" // For addition of the string formatting functions and macros from fmtlib
16 # include "fmt/printf.h" // For sprintf
17 # undef FMT_HEADER_ONLY
18 #else
19 # include <vector>
20 # include <string>
21 #endif
22 
23 #include "Exceptions.h"
24 
25 #if !defined(__powerpc__)
28 inline void StringToWString(const std::string& s, std::wstring& ws) {
29  ws = std::wstring(s.begin(), s.end());
30 }
31 #endif
32 
34 // trim from start
35 #ifdef HAS_MOVE_SEMANTICS //More robust c++11 detection https://stackoverflow.com/questions/10717502/is-there-a-preprocessor-directive-for-detecting-c11x-support
36 // #ifdef __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
37 inline std::string& strlstrip(std::string& s) {
38  s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
39  return s;
40 }
41 #else
42 inline std::string& strlstrip(std::string& s) {
43  s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); }));
44  return s;
45 }
46 #endif
47 // trim from end
48 #ifdef HAS_MOVE_SEMANTICS //More robust c++11 detection https://stackoverflow.com/questions/10717502/is-there-a-preprocessor-directive-for-detecting-c11x-support
49 // #ifdef __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
50 inline std::string& strrstrip(std::string& s) {
51  s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
52  return s;
53 }
54 #else
55 inline std::string& strrstrip(std::string& s) {
56  s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), s.end());
57  return s;
58 }
59 #endif
60 // trim from both ends
61 inline std::string& strstrip(std::string& s) {
62  return strlstrip(strrstrip(s));
63 }
65 inline bool endswith(const std::string& s1, const std::string& s2) {
66  // Impossible to match a string longer than the given string
67  if (s2.size() > s1.size()) {
68  return false;
69  }
70  long lhs = static_cast<long>(s1.rfind(s2));
71  long rhs = static_cast<long>(s1.size()) - static_cast<long>(s2.size());
72  return lhs == rhs;
73 }
74 
75 #if defined(NO_FMTLIB)
76 // Missing string formatting function, this old guy is needed for ancient gcc compilers on PowerPC for VxWorks
77 inline std::string format(const char* fmt, ...);
78 #elif FMT_VERSION >= 50000
79 template <typename... Args>
80 inline std::string format(const char* format_str, const Args&... args) {
81  return fmt::sprintf(format_str, args...);
82 }
83 #else
84 inline std::string format(const char* format, fmt::ArgList args) {
85  return fmt::sprintf(format, args);
86 }
87 FMT_VARIADIC(std::string, format, const char*)
88 #endif
89 
90 // Missing string split - like in Python
91 std::vector<std::string> strsplit(const std::string& s, char del);
92 
93 inline std::string upper(std::string str) {
94  std::transform(str.begin(), str.end(), str.begin(), ::toupper);
95  return str;
96 }
97 
98 inline std::string lower(std::string str) {
99  std::transform(str.begin(), str.end(), str.begin(), ::tolower);
100  return str;
101 }
102 
103 std::string strjoin(const std::vector<std::string>& strings, const std::string& delim);
104 
106 inline bool strstartswith(const std::string& s, const std::string& other) {
107  return s.find(other) == 0;
108 };
109 
116 inline double string2double(const std::string& s) {
117  std::string mys = s; //copy
118  // replace D with e (FORTRAN style scientific definition)
119  if (mys.find("D") != std::string::npos) {
120  std::size_t pos = mys.find("D"), len = 1;
121  mys.replace(pos, len, "e");
122  }
123  // replace d with e (FORTRAN style scientific definition)
124  if (mys.find("d") != std::string::npos) {
125  std::size_t pos = mys.find("d"), len = 1;
126  mys.replace(pos, len, "e");
127  }
128 
129  const char* cs = mys.c_str();
130  char* pEnd;
131  double val = strtod(cs, &pEnd);
132  if ((pEnd - &(cs[0])) != static_cast<int>(s.size())) {
133  // Found a character that is not able to be converted to number
134  throw CoolProp::ValueError(format("Unable to convert this string to a number:%s", cs));
135  } else {
136  return val;
137  }
138 }
139 
140 #endif