18#include <boost/algorithm/string/replace.hpp>
31 std::istringstream str_stream(str);
32 std::vector<std::string> items;
33 std::copy(std::istream_iterator<std::string>(str_stream),
34 std::istream_iterator<std::string>(),
35 std::back_inserter(items));
39std::list<std::string>
splitString(
const std::string& str,
char delim)
41 std::list<std::string> strList;
42 std::stringstream ss(str);
44 while (std::getline(ss, item, delim))
46 strList.push_back(item);
53 std::string stringToReplace)
55 boost::replace_all(stringToReplace, searchString,
replaceString);
56 return stringToReplace;
59void trim(std::string& str,
char ch)
61 std::string::size_type pos = str.find_last_not_of(ch);
62 if (pos != std::string::npos)
65 pos = str.find_first_not_of(ch);
66 if (pos != std::string::npos)
73 str.erase(str.begin(), str.end());
80 str.erase(std::unique(str.begin(),
82 [](
char a,
char b) { return a ==
' ' && b ==
' '; }),
86std::string
format(
const char* format_str, ...)
89 va_start(args, format_str);
92 va_copy(args_tmp, args);
93 int char_length = std::vsnprintf(
nullptr, 0, format_str, args_tmp);
96 std::vector<char> buffer(char_length + 1);
97 vsnprintf(buffer.data(), buffer.size(), format_str, args);
100 return std::string(buffer.data());
105 static constexpr char charset[] =
106 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
108 static const auto seed =
static_cast<std::mt19937::result_type
>(
109 std::chrono::system_clock::now().time_since_epoch().count());
110 static std::mt19937 generator{seed};
111 static std::uniform_int_distribution<unsigned short> distribution(
112 0,
sizeof(charset) - 2);
114 std::string s(length, 0);
115 std::generate_n(begin(s), length,
116 [&]() {
return charset[distribution(generator)]; });
122 std::string
const& input_name)
124 std::string result_name = input_name;
125 std::size_t count = 1;
126 while (std::find(existing_names.cbegin(),
127 existing_names.cend(),
128 result_name) != existing_names.end())
131 result_name = input_name +
"-" + std::to_string(count);
void simplify(std::string &str)
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)
void trim(std::string &str, char ch)
std::string format(const char *format_str,...)
std::vector< std::string > splitString(std::string const &str)