forked from Alepha/Alepha
Multi-char delimiter string splitting.
This commit is contained in:
@ -151,4 +151,19 @@ namespace Alepha::Hydrogen ::detail:: string_algorithms
|
||||
rv.push_back( std::move( next ) );
|
||||
return rv;
|
||||
}
|
||||
|
||||
std::vector< std::string >
|
||||
exports::split( std::string s, const std::string &delim )
|
||||
{
|
||||
std::vector< std::string > rv;
|
||||
while( true )
|
||||
{
|
||||
const auto pos= s.find( delim );
|
||||
rv.push_back( s.substr( 0, pos ) );
|
||||
if( pos == std::string::npos ) break;
|
||||
s= s.substr( pos + delim.size() );
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
@ -75,6 +75,8 @@ namespace Alepha::Hydrogen ::detail:: string_algorithms
|
||||
|
||||
std::vector< std::string > split( const std::string &s, char token );
|
||||
|
||||
std::vector< std::string > split( std::string s, const std::string &delim );
|
||||
|
||||
/*!
|
||||
* Parses an integral range description into a vector of values.
|
||||
*/
|
||||
|
@ -50,11 +50,29 @@ static auto init= enroll <=[]
|
||||
},
|
||||
};
|
||||
|
||||
"Does the `split` function handle simple cases correctly?"_test <=TableTest< Alepha::split >::Cases
|
||||
"Does the `split` function handle simple cases correctly?"_test <=TableTest
|
||||
<
|
||||
[] ( const std::string text, const char delim ) { return Alepha::split( text, delim ); }
|
||||
>
|
||||
::Cases
|
||||
{
|
||||
{ "Empty string", { "", ':' }, { "" } },
|
||||
{ "Single token", { "item", ':' }, { "item" } },
|
||||
{ "Two tokens", { "first:second", ':' }, { "first", "second" } },
|
||||
{ "Empty string many tokens", { ":::", ':' }, { "", "", "", "" } },
|
||||
};
|
||||
|
||||
"Does the `split` function over multi-token-delimiters handle simple cases correctly?"_test <=TableTest
|
||||
<
|
||||
[] ( const std::string text, const std::string delim ) { return Alepha::split( text, delim ); }
|
||||
>
|
||||
::Cases
|
||||
{
|
||||
{ "Empty string", { "", "::" }, { "" } },
|
||||
{ "Single token", { "item", "::" }, { "item" } },
|
||||
{ "Two tokens", { "first::second", "::" }, { "first", "second" } },
|
||||
{ "Empty string many tokens", { "::::::", "::" }, { "", "", "", "" } },
|
||||
{ "Alphabet string many tokens", { "a::b::c::d", "::" }, { "a", "b", "c", "d" } },
|
||||
{ "Alphabet string many tokens", { "::a::b::c::d::", "::" }, { "", "a", "b", "c", "d", "" } },
|
||||
};
|
||||
};
|
||||
|
Reference in New Issue
Block a user