std::string line = "abc|def|ghi|jkl";
stlsoft::string_view dummy1;
stlsoft::string_view dummy2;
stlsoft::string_view field0;
stlsoft::string_view field1;
stlsoft::string_view field2;
stlsoft::string_view field3;
if(stlsoft::split(line, '|', field0, dummy1) &&
stlsoft::split(dummy1, '|', field1, dummy2) &&
stlsoft::split(dummy2, '|', field2, field3))
{
. . . // use fields
}
if(stlsoft::split(line, '|', field0, dummy1) &&
stlsoft::split(dummy1, '|', field1, dummy2) &&
stlsoft::split(dummy2, '|', field2, field3))
{
. . . // use fields
}
Although there's no additional memory allocation here - because we're using string views as the intermediate and final fragment types - it's still hard to follow, and doing three separate split operations.
You can now split directly into up to six fields, as in:
std::string line = "abc|def|ghi|jkl";
stlsoft::string_view field1;
stlsoft::string_view field2;
stlsoft::string_view field3;
if(stlsoft::split(line, '|', field0, field1, field2, field3))
{
. . . // use fields
}
if(stlsoft::split(line, '|', field0, field1, field2, field3))
{
. . . // use fields
}
It's marginally more efficient when using string views, and substantially more efficient when using string value types (such as std::string and stlsoft::simple_string). In either cases, it's considerably more transparent.
No comments:
Post a Comment