From 968652bcfb876d429403b7048a8a30d7368907f2 Mon Sep 17 00:00:00 2001 From: Bart Trzynadlowski Date: Thu, 2 Jun 2016 03:14:46 +0000 Subject: [PATCH] Added a Split() function to Util::Format. --- Src/Util/Format.h | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Src/Util/Format.h b/Src/Util/Format.h index d2c2a8e..3fdea00 100644 --- a/Src/Util/Format.h +++ b/Src/Util/Format.h @@ -5,6 +5,7 @@ #include #include #include +#include namespace Util { @@ -42,7 +43,30 @@ namespace Util } return *this; } - + + std::vector Split(char separator) + { + // Very inefficient: lots of intermediate string copies! + std::string str = m_stream.str(); + const char *start = str.c_str(); + const char *end = start; + std::vector parts; + do + { + if (*end == separator || !*end) + { + size_t len = end - start; + if (len) + parts.emplace_back(start, len); + else + parts.emplace_back(); + start = end + 1; + } + ++end; + } while (end[-1]); + return parts; + } + Format(const std::string &str) : m_stream(str) {