OGS
StringTools.cpp
Go to the documentation of this file.
1 
15 #include "StringTools.h"
16 
17 #include <algorithm>
18 #include <boost/algorithm/string/replace.hpp>
19 #include <cctype>
20 #include <chrono>
21 #include <cstdarg>
22 #include <cstdio>
23 #include <iomanip>
24 #include <random>
25 
26 namespace BaseLib
27 {
28 std::vector<std::string> splitString(std::string const& str)
29 {
30  std::istringstream str_stream(str);
31  std::vector<std::string> items;
32  std::copy(std::istream_iterator<std::string>(str_stream),
33  std::istream_iterator<std::string>(),
34  std::back_inserter(items));
35  return items;
36 }
37 
38 std::list<std::string> splitString(const std::string& str, char delim)
39 {
40  std::list<std::string> strList;
41  std::stringstream ss(str);
42  std::string item;
43  while (getline(ss, item, delim))
44  {
45  strList.push_back(item);
46  }
47  return strList;
48 }
49 
50 std::string replaceString(const std::string& searchString,
51  const std::string& replaceString,
52  std::string stringToReplace)
53 {
54  boost::replace_all(stringToReplace, searchString, replaceString);
55  return stringToReplace;
56 }
57 
58 void trim(std::string& str, char ch)
59 {
60  std::string::size_type pos = str.find_last_not_of(ch);
61  if (pos != std::string::npos)
62  {
63  str.erase(pos + 1);
64  pos = str.find_first_not_of(ch);
65  if (pos != std::string::npos)
66  {
67  str.erase(0, pos);
68  }
69  }
70  else
71  {
72  str.erase(str.begin(), str.end());
73  }
74 }
75 
76 void simplify(std::string& str)
77 {
78  trim(str);
79  str.erase(std::unique(str.begin(),
80  str.end(),
81  [](char a, char b) { return a == ' ' && b == ' '; }),
82  str.end());
83 }
84 
85 std::string format(const char* format_str, ...)
86 {
87  va_list args;
88  va_start(args, format_str);
89  // get the number of chars to write
90  va_list args_tmp;
91  va_copy(args_tmp, args);
92  int char_length = std::vsnprintf(nullptr, 0, format_str, args_tmp);
93  va_end(args_tmp);
94  // allocate buffer and store formatted output there
95  std::vector<char> buffer(char_length + 1); // note +1 for null terminator
96  vsnprintf(buffer.data(), buffer.size(), format_str, args);
97  va_end(args);
98 
99  return std::string(buffer.data());
100 }
101 
102 std::string randomString(std::size_t const length)
103 {
104  static constexpr char charset[] =
105  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
106 
107  static const auto seed = static_cast<std::mt19937::result_type>(
108  std::chrono::system_clock::now().time_since_epoch().count());
109  static std::mt19937 generator{seed};
110  static std::uniform_int_distribution<unsigned short> distribution(
111  0, sizeof(charset) - 2);
112 
113  std::string s(length, 0);
114  std::generate_n(
115  begin(s), length, [&]() { return charset[distribution(generator)]; });
116 
117  return s;
118 }
119 
120 std::string getUniqueName(std::vector<std::string> const& existing_names,
121  std::string const& input_name)
122 {
123  std::string result_name = input_name;
124  std::size_t count = 1;
125  while (std::find(existing_names.cbegin(),
126  existing_names.cend(),
127  result_name) != existing_names.end())
128  {
129  count++;
130  result_name = input_name + "-" + std::to_string(count);
131  }
132  return result_name;
133 }
134 
135 } // end namespace BaseLib
Definition of string helper functions.
void simplify(std::string &str)
Definition: StringTools.cpp:76
std::string randomString(std::size_t const length)
Returns a random string of the given length containing just a-z,A-Z,0-9.
std::string getUniqueName(std::vector< std::string > const &existing_names, std::string const &input_name)
Append '-' and a number such that the name is unique.
std::string replaceString(const std::string &searchString, const std::string &replaceString, std::string stringToReplace)
Definition: StringTools.cpp:50
void trim(std::string &str, char ch)
Definition: StringTools.cpp:58
std::string format(const char *format_str,...)
Definition: StringTools.cpp:85
std::vector< std::string > splitString(std::string const &str)
Definition: StringTools.cpp:28
void copy(PETScVector const &x, PETScVector &y)
Definition: LinAlg.cpp:37
static const double s