aGrUM
3.3.2
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
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 <locale>
53
#include <sstream>
54
#include <string>
55
56
#include <
agrum/base/core/utils_random.h
>
57
#include <
agrum/base/core/utils_string.h
>
58
59
namespace
gum
{
60
61
// get a unique file name using std::filesystem
62
63
std::string
getUniqueFileName
() {
64
// Get the temporary directory path
65
std::filesystem::path temp_dir = std::filesystem::temp_directory_path();
66
67
// Generate a unique file name using a timestamp and a random number
68
auto
now = std::chrono::system_clock::now();
69
auto
timestamp
70
= std::chrono::duration_cast< std::chrono::milliseconds >(now.time_since_epoch()).count();
71
72
auto
dis =
gum::randomValue
(1000);
73
74
std::string filename
75
=
"tempfile_"
+ std::to_string(timestamp) +
"_"
+ std::to_string(dis) +
".tmp"
;
76
77
// Combine the directory path and the file name
78
std::filesystem::path temp_file_path = temp_dir / filename;
79
80
return
temp_file_path.string();
81
}
82
83
bool
endsWith
(
const
std::string_view& value,
const
std::string_view& ending) {
84
if
(ending.size() > value.size())
return
false
;
85
return
std::equal(ending.rbegin(), ending.rend(), value.rbegin());
86
}
87
88
std::vector< std::string >
split
(std::string_view str, std::string_view delim) {
89
std::vector< std::string > tokens;
90
size_t
prev = 0, pos = 0;
91
do
{
92
pos = str.find(delim, prev);
93
if
(pos == std::string::npos) pos = str.length();
94
std::string token(str.substr(prev, pos - prev));
95
if
(!token.empty()) tokens.push_back(token);
96
prev = pos + delim.length();
97
}
while
(pos < str.length() && prev < str.length());
98
return
tokens;
99
}
100
101
std::string
replace
(std::string_view s, std::string_view val, std::string_view new_val) {
102
std::string retVal(s);
103
auto
pos = retVal.find(val);
104
while
(pos != std::string::npos) {
105
std::stringstream sBuff;
106
sBuff << retVal.substr(0, pos) << new_val << retVal.substr(pos + val.size());
107
retVal = sBuff.str();
108
pos = retVal.find(val);
109
}
110
return
retVal;
111
}
112
113
bool
isIntegerWithResult
(std::string_view val,
int
* res) {
114
if
(val.empty())
return
false
;
115
std::size_t pos = 0;
116
if
((val[0] ==
'+'
) || (val[0] ==
'-'
)) { pos = 1; }
117
if
(val.find_first_not_of(
"0123456789"
, pos) != std::string_view::npos)
return
false
;
118
119
if
(res !=
nullptr
) {
120
// from_chars handles '-' natively but not '+'
121
const
std::size_t from_pos = (val[0] ==
'+'
) ? 1 : 0;
122
std::from_chars(val.data() + from_pos, val.data() + val.size(), *res);
123
}
124
return
true
;
125
}
126
127
bool
isNumericalWithResult
(std::string_view val,
double
* res) {
128
std::string s(val);
129
char
* endptr =
nullptr
;
130
const
char
* str = s.c_str();
131
double
d = std::strtod(str, &endptr);
132
133
if
(*endptr !=
'\0'
|| endptr == str)
return
false
;
134
if
(res !=
nullptr
) *res = d;
135
136
return
true
;
137
}
138
139
bool
parseDecimal
(std::string_view val,
double
& res) {
140
// the standard library accepts more than that (hexadecimal, inf, nan) on some platforms
141
if
(val.empty() || val.find_first_not_of(
"0123456789+-.eE"
) != std::string_view::npos) {
142
return
false
;
143
}
144
std::istringstream in{std::string(val)};
145
in.imbue(std::locale::classic());
146
in >> res;
147
return
!in.fail() && in.peek() == std::istringstream::traits_type::eof();
148
}
149
150
151
}
/* namespace gum */
152
153
#ifdef GUM_NO_INLINE
154
# include <
agrum/base/core/utils_string_inl.h
>
155
#endif
// GUM_NO_INLINE
gum::randomValue
GUM_SHARED_PUBLIC Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.
Definition
utils_random_inl.h:66
gum::isNumericalWithResult
bool isNumericalWithResult(std::string_view val, double *res)
return true is a string contains a numerical (double) value
Definition
utils_string.cpp:127
gum::isIntegerWithResult
bool isIntegerWithResult(std::string_view val, int *res)
return true is a string contains an integer value
Definition
utils_string.cpp:113
gum::endsWith
bool endsWith(const std::string_view &value, const std::string_view &ending)
Returns true if value ends with ending.
Definition
utils_string.cpp:83
gum::parseDecimal
bool parseDecimal(std::string_view val, double &res)
parse a decimal number, whatever the locale of the program
Definition
utils_string.cpp:139
gum::getUniqueFileName
std::string getUniqueFileName()
Returns a path to a unique file name.
Definition
utils_string.cpp:63
gum::replace
std::string replace(std::string_view s, std::string_view val, std::string_view new_val)
Replace val by new_val in s.
Definition
utils_string.cpp:101
gum::split
std::vector< std::string > split(std::string_view str, std::string_view delim)
Split str using the delimiter.
Definition
utils_string.cpp:88
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
utils_random.h
Contains useful methods for random stuff.
utils_string.h
Utilities for manipulating strings.
utils_string_inl.h
Contains useful methods for random stuff.
aGrUM
3.3.2
© PHW&CG&others - 2022
DoXyGeN 1.18.0