aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
utils_string.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
49#include <charconv>
50#include <chrono>
51#include <filesystem>
52#include <sstream>
53#include <string>
54
57
58namespace gum {
59
60 // get a unique file name using std::filesystem
61
62 std::string getUniqueFileName() {
63 // Get the temporary directory path
64 std::filesystem::path temp_dir = std::filesystem::temp_directory_path();
65
66 // Generate a unique file name using a timestamp and a random number
67 auto now = std::chrono::system_clock::now();
68 auto timestamp
69 = std::chrono::duration_cast< std::chrono::milliseconds >(now.time_since_epoch()).count();
70
71 auto dis = gum::randomValue(1000);
72
73 std::string filename
74 = "tempfile_" + std::to_string(timestamp) + "_" + std::to_string(dis) + ".tmp";
75
76 // Combine the directory path and the file name
77 std::filesystem::path temp_file_path = temp_dir / filename;
78
79 return temp_file_path.string();
80 }
81
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());
85 }
86
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;
90 do {
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());
97 return tokens;
98 }
99
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);
108 }
109 return retVal;
110 }
111
112 bool isIntegerWithResult(std::string_view val, int* res) {
113 if (val.empty()) return false;
114 std::size_t pos = 0;
115 if ((val[0] == '+') || (val[0] == '-')) { pos = 1; }
116 if (val.find_first_not_of("0123456789", pos) != std::string_view::npos) return false;
117
118 if (res != nullptr) {
119 // from_chars handles '-' natively but not '+'
120 const std::size_t from_pos = (val[0] == '+') ? 1 : 0;
121 std::from_chars(val.data() + from_pos, val.data() + val.size(), *res);
122 }
123 return true;
124 }
125
126 bool isNumericalWithResult(std::string_view val, double* res) {
127 std::string s(val);
128 char* endptr = nullptr;
129 const char* str = s.c_str();
130 double d = std::strtod(str, &endptr);
131
132 if (*endptr != '\0' || endptr == str) return false;
133 if (res != nullptr) *res = d;
134
135 return true;
136 }
137
138
139} /* namespace gum */
140
141#ifdef GUM_NO_INLINE
143#endif // GUM_NO_INLINE
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
Definition agrum.h:46
Contains useful methods for random stuff.
Utilities for manipulating strings.
Contains useful methods for random stuff.