64 std::filesystem::path temp_dir = std::filesystem::temp_directory_path();
67 auto now = std::chrono::system_clock::now();
69 = std::chrono::duration_cast< std::chrono::milliseconds >(now.time_since_epoch()).count();
74 =
"tempfile_" + std::to_string(timestamp) +
"_" + std::to_string(dis) +
".tmp";
77 std::filesystem::path temp_file_path = temp_dir / filename;
79 return temp_file_path.string();
82 bool endsWith(
const std::string_view& value,
const std::string_view& ending) {
83 if (ending.size() > value.size())
return false;
84 return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
87 std::vector< std::string >
split(std::string_view str, std::string_view delim) {
88 std::vector< std::string > tokens;
89 size_t prev = 0, pos = 0;
91 pos = str.find(delim, prev);
92 if (pos == std::string::npos) pos = str.length();
93 std::string token(str.substr(prev, pos - prev));
94 if (!token.empty()) tokens.push_back(token);
95 prev = pos + delim.length();
96 }
while (pos < str.length() && prev < str.length());
100 std::string
replace(std::string_view s, std::string_view val, std::string_view new_val) {
101 std::string retVal(s);
102 auto pos = retVal.find(val);
103 while (pos != std::string::npos) {
104 std::stringstream sBuff;
105 sBuff << retVal.substr(0, pos) << new_val << retVal.substr(pos + val.size());
106 retVal = sBuff.str();
107 pos = retVal.find(val);
113 if (val.empty())
return false;
115 if ((val[0] ==
'+') || (val[0] ==
'-')) { pos = 1; }
116 if (val.find_first_not_of(
"0123456789", pos) != std::string_view::npos)
return false;
118 if (res !=
nullptr) {
120 const std::size_t from_pos = (val[0] ==
'+') ? 1 : 0;
121 std::from_chars(val.data() + from_pos, val.data() + val.size(), *res);
128 char* endptr =
nullptr;
129 const char* str = s.c_str();
130 double d = std::strtod(str, &endptr);
132 if (*endptr !=
'\0' || endptr == str)
return false;
133 if (res !=
nullptr) *res = d;
std::string getUniqueFileName()
Returns a path to a unique file name.
Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.
bool isIntegerWithResult(std::string_view val, int *res)
return true is a string contains an integer value
bool endsWith(const std::string_view &value, const std::string_view &ending)
Returns true if value ends with ending.
std::vector< std::string > split(std::string_view str, std::string_view delim)
Split str using the delimiter.
bool isNumericalWithResult(std::string_view val, double *res)
return true is a string contains a numerical (double) value
std::string replace(std::string_view s, std::string_view val, std::string_view new_val)
Replace val by new_val in s.
gum is the global namespace for all aGrUM entities
Contains useful methods for random stuff.
Utilities for manipulating strings.
Contains useful methods for random stuff.