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