From d7bff82e02161246d41524815e9c12978a2b2d82 Mon Sep 17 00:00:00 2001 From: chatlanin Date: Tue, 1 Mar 2022 12:03:12 +0300 Subject: [PATCH] add split string function --- README.md | 2 +- bin/main.cpp | 9 +++++++-- src/string/string.cpp | 47 +++++++++++++++++-------------------------- src/string/string.hpp | 4 ++++ tests/split_str.cpp | 8 ++++---- 5 files changed, 35 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 3a5ccd2..b8e19b7 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -![This is an image](./hack.png) +![hack++'s logo](./hack.png) diff --git a/bin/main.cpp b/bin/main.cpp index 9359c6f..c4ac633 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -1,6 +1,11 @@ -#include "src/string/string.hpp" +#include + +#include "string/string.hpp" int main(int argc, char *argv[]) { - return 0; + std::string str { "asdf,qwer,zxcv" }; + hack::v_str v = hack::split_str(str, ','); + + for (const auto& c : v) std::cout << c << std::endl; } diff --git a/src/string/string.cpp b/src/string/string.cpp index 82191c7..df3a2e5 100644 --- a/src/string/string.cpp +++ b/src/string/string.cpp @@ -1,32 +1,23 @@ -#pragma once +#include "string.hpp" -#include - -#ifndef ERROR_EXCEPTION -#define ERROR_EXCEPTION_1(in) { auto trace = std::string(LOGGER___TRACE_ON); \ - throw tools::error::error_exception(in, in, "no internal system error", trace); } -#define ERROR_EXCEPTION_2(in, out) { auto trace = std::string(LOGGER___TRACE_ON); \ - throw tools::error::error_exception(in, out, "no internal system err", trace); } -#define ERROR_EXCEPTION_3(in, out, err) { auto trace = std::string(LOGGER___TRACE_ON); \ - throw tools::error::error_exception(in, out, err, trace); } -#define GET_MACRO(_1,_2,_3,NAME, ...) NAME -#define ERROR_EXCEPTION(...) GET_MACRO(__VA_ARGS__, ERROR_EXCEPTION_3, ERROR_EXCEPTION_2, ERROR_EXCEPTION_1)(__VA_ARGS__) -#endif - -namespace tools::error +namespace hack { - // implementation error exception - // example: if (1 < 0) ERROR_EXCEPTION("it's very strange"); - // try {} - // catch(tools::error::error_exception& e) {} - struct error_exception : public std::exception + v_str split_str(const std::string& str, char t) { - error_exception(std::string in, std::string out, std::string error, std::string trace) : in { in }, out { out }, error { error }, trace { trace } {}; - std::string in; - std::string out; - std::string error; - std::string trace; - const char* what () const throw () { return in.c_str(); } - }; -} + 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 fe96435..6b4a87a 100644 --- a/src/string/string.hpp +++ b/src/string/string.hpp @@ -1,7 +1,11 @@ #pragma once #include +#include namespace hack { + using v_str = std::vector; + + v_str split_str(const std::string& str, char t); } diff --git a/tests/split_str.cpp b/tests/split_str.cpp index b2e2627..e644003 100644 --- a/tests/split_str.cpp +++ b/tests/split_str.cpp @@ -4,10 +4,10 @@ #include "string/string.hpp" -using v_str = std::vector; -TEST(split_str, check__func) +TEST(split_str, check) { - // v_str res {"asdf","qwer","zxcv"}; - // ASSERT_EQ(tools::func::split_str("asdf,qwer,zxcv", ','), res); + hack::v_str v { "asdf", "qwer", "zxcv" }; + + ASSERT_EQ(hack::split_str("asdf,qwer,zxcv", ','), v); }