std::string slicer by tags (parsing command line params etc.)

60 views
Skip to first unread message

PAVEL SOKOLOV

unread,
Apr 22, 2017, 1:20:57 PM4/22/17
to ISO C++ Standard - Future Proposals
It can be useful for a simple tasks (for a difficult parsing rules regexp can be used). Looks like it's a very frequent task.

Possible implementation on github (makefile included). Supported: one char(wchar) delimiter, char*(wchar*) as delimiter in each_slice(...), any one from char(wchar) as delimiter from char*(wchar*) in any_slice(...). 

Example:

std::string testString( "cpp=1,2,3,4,5,6,7,8,9" );

std::vector< std::string > allSlices;
each_slice( testString.begin(), testString.end(), '=', back_insert_functor<std::vector< std::string >>( allSlices ) );
if ( allSlices.size() == 2 )
{
each_slice( allSlices[ 1 ].begin(), allSlices[ 1 ].end(), ',', []( const std::string& tag )
{
std::cout << tag << std::endl;
} );
}

Result: 1 2 3 4 5 6 7 8 9

Can be easily switched to string_view. Can be a part of std::algorithms or even std::string. Easy and fast. 

ps: back_insert_functor is a wrapper that allows passing OutputIterator as functor.
pss: implementation is not final and may contain bugs.



Richard Thorell

unread,
Apr 22, 2017, 2:55:06 PM4/22/17
to ISO C++ Standard - Future Proposals
Reply all
Reply to author
Forward
0 new messages