query/include/matador/utils/string.hpp

63 lines
1.5 KiB
C++

#ifndef QUERY_STRING_HPP
#define QUERY_STRING_HPP
#include <string>
#include <vector>
namespace matador::utils {
/**
* Splits a string by a delimiter and
* add the string tokens to a vector. The
* size of the vector is returned.
*
* @param str The string to split.
* @param delim The delimiter character.
* @param values The result vector.
* @return The size of the vector.
*/
size_t split(const std::string &str, char delim, std::vector<std::string> &values);
/**
* Replaces all occurrences of string from in given string
* with string to.
*
* @param in Source string where the replacement takes place
* @param from The string to be replaced
* @param to The new string
*/
void replace_all(std::string &in, const std::string &from, const std::string &to);
const std::string& to_string(const std::string &str);
/**
* Joins a range of elements as string within a list
* with a given delimiter and writes it to the
* given stream
*
* @tparam R Type og the range (e.g. map, list, vector, etc)
* @param range The range with the elements to join
* @param delim The delimiter for the elements
* @return The ostream reference
*/
template < class R >
std::string join(R &range, const std::string &delim)
{
std::string result {};
if (range.size() < 2) {
for (const auto &i : range) {
result += to_string(i);
}
} else {
auto it = range.begin();
result += to_string(*it++);
for (;it != range.end(); ++it) {
result += delim + to_string(*it);
}
}
return result;
}
}
#endif //QUERY_STRING_HPP