add split string function

This commit is contained in:
chatlanin
2022-03-01 12:03:12 +03:00
parent e872067709
commit d7bff82e02
5 changed files with 35 additions and 35 deletions

View File

@@ -1 +1 @@
![This is an image](./hack.png) ![hack++'s logo](./hack.png)

View File

@@ -1,6 +1,11 @@
#include "src/string/string.hpp" #include <iostream>
#include "string/string.hpp"
int main(int argc, char *argv[]) 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;
} }

View File

@@ -1,32 +1,23 @@
#pragma once #include "string.hpp"
#include <string> namespace hack
#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
{ {
// implementation error exception v_str split_str(const std::string& str, char t)
// example: if (1 < 0) ERROR_EXCEPTION("it's very strange");
// try {}
// catch(tools::error::error_exception& e) {}
struct error_exception : public std::exception
{ {
error_exception(std::string in, std::string out, std::string error, std::string trace) : in { in }, out { out }, error { error }, trace { trace } {}; v_str v;
std::string in;
std::string out; std::string::size_type begin = 0;
std::string error; std::string::size_type end = str.find_first_of(t);
std::string trace;
const char* what () const throw () { return in.c_str(); } 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;
}
}

View File

@@ -1,7 +1,11 @@
#pragma once #pragma once
#include <string> #include <string>
#include <vector>
namespace hack namespace hack
{ {
using v_str = std::vector<std::string>;
v_str split_str(const std::string& str, char t);
} }

View File

@@ -4,10 +4,10 @@
#include "string/string.hpp" #include "string/string.hpp"
using v_str = std::vector<std::string>;
TEST(split_str, check__func) TEST(split_str, check)
{ {
// v_str res {"asdf","qwer","zxcv"}; hack::v_str v { "asdf", "qwer", "zxcv" };
// ASSERT_EQ(tools::func::split_str("asdf,qwer,zxcv", ','), res);
ASSERT_EQ(hack::split_str("asdf,qwer,zxcv", ','), v);
} }