add new split string implemantation

This commit is contained in:
chatlanin 2022-07-20 11:13:55 +03:00
parent 7ec159ac5f
commit e7d35b0e1d
4 changed files with 25 additions and 17 deletions

View File

@ -48,6 +48,10 @@ int main(int argc, char *argv[])
hack::string::v_str v = hack::string::split_str(str, ','); hack::string::v_str v = hack::string::split_str(str, ',');
hack::log log; hack::log log;
for (const auto& c : v) log(c); 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 {// ex: renge::within

View File

@ -2,20 +2,4 @@
namespace hack::string 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;
}
} }

View File

@ -10,5 +10,21 @@ namespace hack::string
{ {
using v_str = std::vector<std::string>; using v_str = std::vector<std::string>;
v_str split_str(const std::string& str, char t); template<typename T>
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;
}
} }

View File

@ -9,4 +9,8 @@ TEST(split_str, check)
{ {
std::vector<std::string> v { "asdf", "qwer", "zxcv" }; std::vector<std::string> v { "asdf", "qwer", "zxcv" };
ASSERT_EQ(hack::string::split_str("asdf,qwer,zxcv", ','), v); ASSERT_EQ(hack::string::split_str("asdf,qwer,zxcv", ','), v);
std::vector<std::string> v1 { "qqq", "aaa", "eee", "sss" };
ASSERT_EQ(hack::string::split_str("qqq,aaa:eee,sss", ":,"), v1);
} }