From: Maxim Mamontov Date: Thu, 29 May 2014 10:20:51 +0000 (+0300) Subject: Added Split utility. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/3ee15695bbd86c97088270b855f810279c2b8275?ds=sidebyside Added Split utility. --- diff --git a/stglibs/common.lib/include/stg/common.h b/stglibs/common.lib/include/stg/common.h index 0437d672..605b9951 100644 --- a/stglibs/common.lib/include/stg/common.h +++ b/stglibs/common.lib/include/stg/common.h @@ -33,6 +33,7 @@ #include #endif #include +#include #include "stg/os_int.h" #include "stg/const.h" @@ -101,7 +102,38 @@ std::string Trim(const std::string & val); std::string ToLower(const std::string & value); std::string ToUpper(const std::string & value); -std::string IconvString(const std::string & source, const std::string & from, const std::string & to); +template +C Split(const std::string & value, char delim, F conv) +{ +C res; +size_t startPos = 0; +size_t pos = value.find_first_of(delim); +while (pos != std::string::npos) + { + res.push_back(conv(value.substr(startPos, pos - startPos))); + startPos = pos + 1; + pos = value.find_first_of(delim, pos); + } +res.push_back(conv(value.substr(startPos, pos - startPos))); +return res; +} + +template +T FromString(const std::string & value) +{ +T res; +std::istringstream stream(value); +stream >> res; +return res; +} + +template +C Split(const std::string & value, char delim) +{ + return Split(value, delim, FromString); +} + +std::string IconvString(const std::string & source, const std::string & from, const std::string & to); int ParseInt(const std::string & str, int * val); int ParseUnsigned(const std::string & str, unsigned * val);