aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
utils_string_inl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 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-2025 *
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#pragma once
41
42
49
50#include <algorithm>
51
52// to ease IDE parser
54
55namespace gum {
56 INLINE
57
58 std::string toLower(std::string str) {
59 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
60 return str;
61 }
62
63 INLINE
64
65 void ltrim(std::string& s) {
66 s.erase(s.begin(),
67 std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); }));
68 }
69
70 INLINE
71
72 void rtrim(std::string& s) {
73 s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); })
74 .base(),
75 s.end());
76 }
77
78 INLINE
79
80 void trim(std::string& s) {
81 ltrim(s);
82 rtrim(s);
83 }
84
85 INLINE
86
87 std::string remove_newline(const std::string& s) {
88 std::string res{s};
89 std::erase(res, '\n');
90 std::erase(res, '\r');
91 return res;
92 }
93
94 INLINE
95 bool isInteger(const std::string& val) { return isIntegerWithResult(val, nullptr); }
96
97 INLINE
98 bool isNumerical(const std::string& val) { return isNumericalWithResult(val, nullptr); }
99
100 INLINE
101
102 std::string trim_copy(const std::string& s) {
103 std::string res{s};
104 trim(res);
105 return res;
106 }
107} /* namespace gum */
bool isNumericalWithResult(const std::string &val, double *res)
return true is a string contains a numerical (double) value
void ltrim(std::string &s)
trim from start (in place)
bool isNumerical(const std::string &val)
return true is a string contains a numerical (double) value
bool isIntegerWithResult(const std::string &val, int *res)
return true is a string contains an integer value
bool isInteger(const std::string &val)
return true is a string contains an integer value
std::string toLower(std::string str)
Returns the lowercase version of str.
void trim(std::string &s)
trim from both ends (in place)
std::string remove_newline(const std::string &s)
remove all newlines in a string
void rtrim(std::string &s)
trim from end (in place)
std::string trim_copy(const std::string &s)
trim from both ends (copying)
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Utilities for manipulating strings.