Fix return value of get_token_list (#1271)

Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
This commit is contained in:
Mike Bloom 2026-01-21 13:40:19 -05:00 committed by GitHub
parent 2c78c3d151
commit b82bf3b79d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 3 deletions

View File

@ -782,9 +782,9 @@ namespace etl
etl::optional<string_view_t> token;
size_t count = 0;
while ((count != output.max_size()) &&
(count != max_n_tokens) &&
(token = etl::get_token(input, delimiters, token, ignore_empty_tokens)))
while ((token = etl::get_token(input, delimiters, token, ignore_empty_tokens)) &&
(count != output.max_size()) &&
(count != max_n_tokens))
{
output.push_back(token.value());
++count;

View File

@ -1115,6 +1115,48 @@ namespace
CHECK_EQUAL(std::string("mat"), std::string(views[5].begin(), views[5].end()));
}
//*************************************************************************
//before the issue fix,
//the return value was false when output vector max size equaled the number of tokens
//it should be true
TEST(test_issue_1270_output_vector_max_size_equals_num_tokens)
{
String text(STR(",,,The,cat,sat,,on,the,mat"));
etl::vector<StringView, 6> views;
bool all_views_found = etl::get_token_list(text, views, STR(","), true);
CHECK_TRUE(all_views_found);
CHECK_EQUAL(6, views.size());
CHECK_EQUAL(std::string("The"), std::string(views[0].begin(), views[0].end()));
CHECK_EQUAL(std::string("cat"), std::string(views[1].begin(), views[1].end()));
CHECK_EQUAL(std::string("sat"), std::string(views[2].begin(), views[2].end()));
CHECK_EQUAL(std::string("on"), std::string(views[3].begin(), views[3].end()));
CHECK_EQUAL(std::string("the"), std::string(views[4].begin(), views[4].end()));
CHECK_EQUAL(std::string("mat"), std::string(views[5].begin(), views[5].end()));
}
//*************************************************************************
//before the issue fix,
//the return value was false when max number of tokens equaled the number of tokens
//it should be true
TEST(test_issue_1270_max_tokens_equals_num_tokens)
{
String text(STR(",,,The,cat,sat,,on,the,mat"));
VectorOfViews7 views;
bool all_views_found = etl::get_token_list(text, views, STR(","), true, 6);
CHECK_TRUE(all_views_found);
CHECK_EQUAL(6, views.size());
CHECK_EQUAL(std::string("The"), std::string(views[0].begin(), views[0].end()));
CHECK_EQUAL(std::string("cat"), std::string(views[1].begin(), views[1].end()));
CHECK_EQUAL(std::string("sat"), std::string(views[2].begin(), views[2].end()));
CHECK_EQUAL(std::string("on"), std::string(views[3].begin(), views[3].end()));
CHECK_EQUAL(std::string("the"), std::string(views[4].begin(), views[4].end()));
CHECK_EQUAL(std::string("mat"), std::string(views[5].begin(), views[5].end()));
}
//*************************************************************************
TEST(test_get_token_list_to_vector_of_string_view_all_but_1_tokens_captured_ignore_empty_tokens)
{