diff options
Diffstat (limited to 'utility/Utility.h')
| -rw-r--r-- | utility/Utility.h | 99 |
1 files changed, 59 insertions, 40 deletions
diff --git a/utility/Utility.h b/utility/Utility.h index 93acd82..c32095a 100644 --- a/utility/Utility.h +++ b/utility/Utility.h @@ -34,50 +34,69 @@ #include <list> #include <map> #include <sstream> +#include <numeric> -class CUtility +namespace utility { -public: - /** - * Format the items of a map into a string as a list of key-value pairs. The map must be - * composed of pairs of strings. - * - * @param[in] mapStr A map of strings - * @param[out] strOutput The output string - * @param[in] separator The separator to use between each item - */ - static void asString(const std::list<std::string>& lstr, - std::string& strOutput, - const std::string& separator = "\n"); - /** - * Format the items of a map into a string as a list of key-value pairs. The map must be - * composed of pairs of strings. - * - * @param[in] mapStr A map of strings - * @param[out] strOutput The output string - * @param[in] strItemSeparator The separator to use between each item (key-value pair) - * @param[in] strKeyValueSeparator The separator to use between key and value - */ - static void asString(const std::map<std::string, std::string>& mapStr, - std::string& strOutput, - const std::string& strItemSeparator = ", ", - const std::string& strKeyValueSeparator = ":"); +/** Join all elements in [first, last[ with op. + * + * If their is no element to join, return empty. + * + * Example (joining strings): + * @verbatim + * let op = [](auto l, auto r){ return l + "|" + r; } + * let [first, last[ = list<string>{"1", "2", "3"} + * then join(first, last, op) == "1|2|3" + * @endverbatim + */ +template <class T, class InputIt, class BinaryOperation> +T join(InputIt first, InputIt last, BinaryOperation op, T empty = T{}) +{ + if (first == last) { + return empty; + } + auto init = *first++; - /** Utility to easily convert a builtin type into string - * - * FIXME: Should be replaced by std::to_string after C++11 introduction - */ - template <class T> - static std::string toString(T uiValue) - { - std::ostringstream ostr; + return std::accumulate(first, last, init, op); +} - ostr << uiValue; +/** +* Format the items of a map into a string as a list of key-value pairs. The map must be +* composed of pairs of strings. +* +* @param[in] lstr A list of strings +* @param[in] separator The separator to use between each item +* +* @return the concatenated elements. +*/ +std::string asString(const std::list<std::string> &lstr, const std::string &separator = "\n"); - return ostr.str(); - } +/** + * Format the items of a map into a string as a list of key-value pairs. The map must be + * composed of pairs of strings. + * + * @param[in] mapStr A map of strings + * @param[in] strItemSeparator The separator to use between each item (key-value pair) + * @param[in] strKeyValueSeparator The separator to use between key and value + * + * @returns the pretty-printed map + */ +std::string asString(const std::map<std::string, std::string> &mapStr, + const std::string &strItemSeparator = ", ", + const std::string &strKeyValueSeparator = ":"); + +/** Utility to underline */ +void appendTitle(std::string &strTo, const std::string &strTitle); + +/** + * Checks if a string has the written representation of an hexadecimal + * number (Which is the prefix "0x" or "0X" in C++). + * + * @param[in] strValue value as string + * + * @return true if the string is written as hexa, false otherwise. + */ +bool isHexadecimal(const std::string &strValue); - /** Utility to underline */ - static void appendTitle(std::string& strTo, const std::string& strTitle); -}; +} // utility |
