xerus
a general purpose tensor library
stringUtilities.cpp
Go to the documentation of this file.
1 // Xerus - A General Purpose Tensor Library
2 // Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf.
3 //
4 // Xerus is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published
6 // by the Free Software Foundation, either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // Xerus is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with Xerus. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // For further information on Xerus visit https://libXerus.org
18 // or contact us at contact@libXerus.org.
19 
26 #include <memory>
27 #include <sstream>
28 #include <cxxabi.h>
29 
30 namespace xerus {
31  namespace misc {
32 
33  std::string demangle_cxa(const std::string &_cxa) {
34  int status;
35  std::unique_ptr<char, void(*)(void*)> realname(abi::__cxa_demangle(_cxa.data(), nullptr, nullptr, &status), &free);
36  if (status != 0) { return _cxa; }
37 
38  if (realname) {
39  return std::string(realname.get());
40  }
41  return "";
42  }
43 
44  std::string normalize_pathname(const std::string &_name) {
45  std::vector<std::string> oldpath = explode(_name,'/');
46  std::vector<std::string *> newpath;
47  for (std::string &f : oldpath) {
48  if (f.empty()) { continue; }
49  if (f==".." && !newpath.empty() && *newpath.back() != "..") {
50  newpath.pop_back();
51  } else {
52  newpath.push_back(&f);
53  }
54  }
55  std::string ret;
56  for (std::string *f : newpath) {
57  ret += *f;
58  ret += '/';
59  }
60  if (!ret.empty()) { ret.pop_back(); }
61  return ret;
62  }
63 
64  std::vector<std::string> explode(const std::string& _string, const char _delim) {
65  std::vector<std::string> result;
66  std::istringstream iss(_string);
67 
68  std::string token;
69  while(std::getline(iss, token, _delim)) {
70  result.push_back(std::move(token));
71  }
72 
73  return result;
74  }
75 
76  void replace(std::string& _string, const std::string& _search, const std::string& _replace) {
77  size_t pos = 0;
78  while((pos = _string.find(_search, pos)) != std::string::npos){
79  _string.replace(pos, _search.length(), _replace);
80  pos += _replace.length();
81  }
82  }
83 
84  std::string trim(const std::string& _string, const std::string& whitespace) {
85  const size_t strBegin = _string.find_first_not_of(whitespace);
86  if (strBegin == std::string::npos) {
87  return "";
88  }
89  const size_t strEnd = _string.find_last_not_of(whitespace);
90  return _string.substr(strBegin, strEnd - strBegin + 1);
91  }
92 
93  std::string reduce(const std::string& _string, const std::string& whitespace, const std::string& fill) {
94  // Trim first
95  auto trimedString = trim(_string, whitespace);
96 
97  // Replace sub ranges
98  auto beginSpace = trimedString.find_first_of(whitespace);
99  while (beginSpace != std::string::npos) {
100  const auto endSpace = trimedString.find_first_not_of(whitespace, beginSpace);
101  const auto range = endSpace - beginSpace;
102 
103  trimedString.replace(beginSpace, range, fill);
104 
105  const auto newStart = beginSpace + fill.length();
106  beginSpace = trimedString.find_first_of(whitespace, newStart);
107  }
108  return trimedString;
109  }
110  } // namespace misc
111 } // namespace xerus
The main namespace of xerus.
Definition: basic.h:37
std::string XERUS_warn_unused trim(const std::string &_string, const std::string &whitespace=" \\")
: Removes all leading and trailing whitespaces from _string.
std::string XERUS_warn_unused reduce(const std::string &_string, const std::string &whitespace=" \\", const std::string &fill=" ")
: Removes all leading and trailing whitespaces from _string, and reduces all double whitespaces to on...
Header file for some elementary string manipulation routines.
std::string XERUS_warn_unused demangle_cxa(const std::string &_cxa)
Demangles the function and class names created by gcc into a more readable format.
std::string XERUS_warn_unused normalize_pathname(const std::string &_name)
Resolves &#39;folder/..&#39; occurences in pathnames.
void replace(std::string &_string, const std::string &_search, const std::string &_replace)
: Replaces all occurences of _search in _string by _replace.
std::vector< std::string > explode(const std::string &_string, const char _delim)
: Explodes a string at positions indicated by _delim.