add new split string implemantation
This commit is contained in:
parent
7ec159ac5f
commit
e7d35b0e1d
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -10,5 +10,21 @@ namespace hack::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;
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ TEST(split_str, check)
|
||||
{
|
||||
std::vector<std::string> v { "asdf", "qwer", "zxcv" };
|
||||
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);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user