aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
utils_string.cpp
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Utilities for manipulating strings.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27  *
28  */
29 #include <iterator>
30 #include <regex>
31 
32 #include <agrum/tools/core/utils_string.h>
33 
34 namespace gum {
35 
36  std::string getUniqueFileName() {
37 #ifdef HAVE_MKSTEMP
38  char tmpFileName_[] = "fileXXXXXX";
39  int fd = mkstemp(tmpFileName_);
40  close(fd);
41 #else // mainly Windows
42  char tmpFileName_[] = "fileXXXXXX";
43  _mktemp_s(tmpFileName_, strlen(tmpFileName_));
44 #endif
45 
46  return std::string(tmpFileName_);
47  }
48 
49  bool endsWith(std::string const& value, std::string const& ending) {
50  if (ending.size() > value.size()) return false;
51  return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
52  }
53 
54  std::vector< std::string > split(const std::string& str, const std::string& delim) {
55  std::vector< std::string > tokens;
56  size_t prev = 0, pos = 0;
57  do {
58  pos = str.find(delim, prev);
59  if (pos == std::string::npos) pos = str.length();
60  std::string token = str.substr(prev, pos - prev);
61  if (!token.empty()) tokens.push_back(token);
62  prev = pos + delim.length();
63  } while (pos < str.length() && prev < str.length());
64  return tokens;
65  }
66 
67  /** not usable for gcc 4.8
68 std::vector<std::string> split( const std::string& orig,
69  const std::string& delimiter ) {
70 
71  std::regex rgx( delimiter );
72 
73  std::sregex_token_iterator first{begin( orig ), end( orig ), rgx, -1}, last;
74 
75  return {first, last};
76 } */
77 
78  std::string replace(const std::string& s, const std::string& val, const std::string& new_val) {
79  auto retVal = s;
80  auto pos = retVal.find(val);
81  while (pos != std::string::npos) {
82  std::stringstream sBuff;
83  sBuff << s.substr(0, pos) << new_val << s.substr(pos + val.size(), std::string::npos);
84  retVal = sBuff.str();
85  pos = retVal.find(val);
86  }
87  return retVal;
88  }
89 
90 } /* namespace gum */
91 
92 #ifdef GUM_NO_INLINE
93 # include <agrum/tools/core/utils_string_inl.h>
94 #endif // GUM_NO_INLINE