From e7d35b0e1d68962b317c202d6e492f79bd5ba751 Mon Sep 17 00:00:00 2001 From: chatlanin Date: Wed, 20 Jul 2022 11:13:55 +0300 Subject: [PATCH] add new split string implemantation --- bin/main.cpp | 4 ++++ src/string/string.cpp | 16 ---------------- src/string/string.hpp | 18 +++++++++++++++++- tests/string.cpp | 4 ++++ 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/bin/main.cpp b/bin/main.cpp index ace7fc2..9c29168 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -48,6 +48,10 @@ int main(int argc, char *argv[]) hack::string::v_str v = hack::string::split_str(str, ','); hack::log log; for (const auto& c : v) log(c); + + std::string str_2 { "qqq,aaa:eee,ggg" }; + hack::string::v_str v_2 = hack::string::split_str(str_2, ",:"); + for (const auto& c : v_2) log(c); } {// ex: renge::within diff --git a/src/string/string.cpp b/src/string/string.cpp index 70914d4..33fd61f 100644 --- a/src/string/string.cpp +++ b/src/string/string.cpp @@ -2,20 +2,4 @@ namespace hack::string { - v_str split_str(const std::string& str, char t) - { - v_str v; - - std::string::size_type begin = 0; - std::string::size_type end = str.find_first_of(t); - - while(end != std::string::npos) - { - v.emplace_back(str.substr(begin, end - begin)); - begin = ++end; - end = str.find_first_of(t, begin); - } - v.emplace_back(str.substr(begin)); - return v; - } } diff --git a/src/string/string.hpp b/src/string/string.hpp index 952d286..6a2b921 100644 --- a/src/string/string.hpp +++ b/src/string/string.hpp @@ -10,5 +10,21 @@ namespace hack::string { using v_str = std::vector; - v_str split_str(const std::string& str, char t); + template + v_str split_str(const std::string& str, T t) + { + v_str v; + + std::string::size_type begin = 0; + std::string::size_type end = str.find_first_of(t); + + while(end != std::string::npos) + { + v.emplace_back(str.substr(begin, end - begin)); + begin = ++end; + end = str.find_first_of(t, begin); + } + v.emplace_back(str.substr(begin)); + return v; + } } diff --git a/tests/string.cpp b/tests/string.cpp index 0cffff2..0d7d2d5 100644 --- a/tests/string.cpp +++ b/tests/string.cpp @@ -9,4 +9,8 @@ TEST(split_str, check) { std::vector v { "asdf", "qwer", "zxcv" }; ASSERT_EQ(hack::string::split_str("asdf,qwer,zxcv", ','), v); + + std::vector v1 { "qqq", "aaa", "eee", "sss" }; + ASSERT_EQ(hack::string::split_str("qqq,aaa:eee,sss", ":,"), v1); + }