query/include/matador/utils/string.hpp

117 lines
3.1 KiB
C++

#ifndef STRING_HPP
#define STRING_HPP
#include "matador/utils/convert.hpp"
#include "matador/utils/export.hpp"
#include "matador/utils/types.hpp"
#include <list>
#include <string>
namespace matador::utils {
/**
* Converts each byte of the given binary data
* into is hex string representation and return
* all bytes of blob as string.
*
* @param data Binary data to be converted
* @return Binary data as string
*/
MATADOR_UTILS_API std::string to_string(const blob &data);
/**
* 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.
*/
MATADOR_UTILS_API size_t split(const std::string &str, char delim, std::vector<std::string> &values);
/**
* 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.
* @return The vector with split strings.
*/
MATADOR_UTILS_API std::vector<std::string> split(const std::string &str, char delim);
/**
* Splits a string by a delimiter and
* add the string tokens to a list. The
* size of the list is returned.
*
* @param str The string to split.
* @param delim The delimiter character.
* @param values The result list.
* @return The size of the list.
*/
MATADOR_UTILS_API size_t split(const std::string &str, char delim, std::list<std::string> &values);
/**
* @fn std::string trim(const std::string& str, const std::string&)
* Trims a string by removing leading and trailing characters
* The default characters are spaces and tabs
*
* @param str The string to be trimmed
* @param whitespace The trimming characters
* @return the trimmed string
*/
MATADOR_UTILS_API std::string trim(const std::string& str, const std::string& whitespace = " \t");
/**
* 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
*/
MATADOR_UTILS_API void replace_all(std::string &in, const std::string &from, const std::string &to);
template <typename Type >
std::string to_string(const Type &value) {
const auto res = to<std::string>(value);
if (res.is_ok()) {
return *res;
}
return "";
}
/**
* Joins a range of elements as string within a list
* with a given delimiter and writes it to the
* given stream
*
* @tparam Range Type og the range (e.g. map, list, vector, etc)
* @param range The range with the elements to join_left
* @param delim The delimiter for the elements
* @return The ostream reference
*/
template < class Range >
std::string join(const Range &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 //STRING_HPP