From b3d4b1a28e293d8bb346de5ccda0a3fddda2a0b6 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Thu, 29 May 2014 13:20:51 +0300 Subject: [PATCH 1/1] Added Split utility. --- stglibs/common.lib/include/stg/common.h | 34 ++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/stglibs/common.lib/include/stg/common.h b/stglibs/common.lib/include/stg/common.h index 0a4ef949..1dd8c1c7 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" @@ -96,7 +97,38 @@ std::string & TrimL(std::string & val); std::string & TrimR(std::string & val); std::string & Trim(std::string & val); -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); -- 2.44.2