diff --git a/mars/comm/strutil.h b/mars/comm/strutil.h index 1391a0c86..6ff3f91df 100644 --- a/mars/comm/strutil.h +++ b/mars/comm/strutil.h @@ -170,6 +170,55 @@ size_t ci_find_substr(const std::string& str, const std::string& sub, size_t pos std::string BufferMD5(const void* buffer, size_t size); std::string MD5DigestToBase16(const uint8_t digest[16]); std::string DigestToBase16(const uint8_t* digest, size_t length); + +template ::value, bool> = true> +std::string to_string(const T& v) { + return std::to_string(v); +} + +template ::value, bool> = true> +std::string to_string(const T& v) { + return std::to_string(static_cast(v)); +} + +// std::string +template ::value, bool> = true> +std::string to_string(const T& v) { + return v; +} + +// char* +template , char*>::value, bool> = true> +std::string to_string(const T& v) { + return std::string(v); +} + +// literal strings +template ::value, bool> = true> +std::string to_string(T const (&v)[N]) { + return std::string(v); +} + +template +std::string to_string(const std::pair & v) { + return "{" + to_string(v.first) + ":" + to_string(v.second) + "}"; +} + +template +std::string join_to_string(const T &stl, + const std::string &seperator = ",", + const std::string &prefix = "", + const std::string &postfix = "") { + if (stl.empty()) { + return {}; + } + std::string rtn = prefix; + for (const auto &it : stl) { + rtn.append(strutil::to_string(it)).append(seperator); + } + return rtn.append(postfix); +} + } // namespace strutil #endif // COMM_STRUTIL_H_