Sunday, June 6, 2010

STLSoft 1.9.98 changes to stlsoft::split()

As of STLSoft 1.9.98, stlsoft::split() has been enhanced to be able to split into between two and six fragments. Previously you would have use intermediates to split into more fields, as in:


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

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 field0;
stlsoft::string_view field1;
stlsoft::string_view field2;
stlsoft::string_view field3;

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: