add split string function
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
#include "src/string/string.hpp"
|
||||
#include <iostream>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -1,32 +1,23 @@
|
||||
#pragma once
|
||||
#include "string.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
#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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace hack
|
||||
{
|
||||
using v_str = std::vector<std::string>;
|
||||
|
||||
v_str split_str(const std::string& str, char t);
|
||||
}
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
|
||||
#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"};
|
||||
// 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user