Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools.cpp - split_string function didn't not cover some situations #78

Open
tianjixuetu opened this issue Mar 10, 2024 · 0 comments
Open

Comments

@tianjixuetu
Copy link

/*!
 * 它接受一个std::string类型的字符串str和一个指向C风格字符串的指针sep作为参数。
 * 函数的目的是将str字符串按照sep指定的分隔符进行分割,并将分割后的子字符串存储在一个std::vector<std::string>中。
 * @param str: 待分割的字符串
 * @param sep: 分隔符
 * @return: 分隔之后的vector字符串
 */
std::vector<std::string> split_string(const std::string &str, const char *sep) {
    std::vector<std::string> res{};

    boost::algorithm::split(res, str, boost::is_any_of(sep));
    for ( auto &it: res ) {
        boost::algorithm::trim(it);
    }
    // 去除空字符串
    // res.erase(std::remove(res.begin(), res.end(), ""), res.end());
    return res;
}

for example, test some situation:

TEST_F(ToolsTestSplitString, s3){
    // 测试使用多个字符作为分隔符
    std::string str = "apple::banana::cherry::date";
    const char *sep = "::";
    std::vector<std::string> expected = {"apple", "banana", "cherry", "date"};
    std::vector<std::string> result = split_string(str, sep);
    EXPECT_EQ(expected, result);
}

it will not generate the expected std::vectorstd::string, because some element is "", so maybe it is better to add a code to remove "".
res.erase(std::remove(res.begin(), res.end(), ""), res.end());

@tianjixuetu tianjixuetu changed the title 【bug】tools.cpp - split_string function didn't not cover some situations tools.cpp - split_string function didn't not cover some situations Mar 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant