char_separator<Char, Traits>

The char_separator class breaks a sequence of characters into
tokens based on character delimiters much in the same way that
strtok() does (but without all the evils of non-reentrancy and
destruction of the input sequence).

Definitions

The strtok() function does not include matches with the
character delimiters in the output sequence of tokens. However, sometimes
it is useful to have the delimiters show up in the output sequence,
therefore char_separator provides this as an option. We refer to
delimiters that show up as output tokens as kept delimiters
and delimiters that do now show up as output tokens as dropped
delimiters.

When two delimiters appear next to each other in the input sequence,
there is the question of whether to output an empty token or
to skip ahead. The behaviour of strtok() is to skip ahead. The
char_separator class provides both options.

Examples

This first examples shows how to use char_separator as a
replacement for the strtok() function. We've specified three
character delimiters, and they will not show up as output tokens. We have
not specified any kept delimiters, and by default any empty tokens will be
ignored.

The next example shows tokenizing with two dropped delimiters '-' and
';' and a single kept delimiter '|'. We also specify that empty tokens
should show up in the output when two delimiters are next to each
other.

Model of

Members

This creates a char_separator object, which can then be used to
create a token_iterator or
tokenizer to perform tokenizing. The
dropped_delims and kept_delims are strings of characters
where each character is used as delimiter during tokenizing. Whenever a
delimiter is seen in the input sequence, the current token is finished, and
a new token begins. The delimiters in dropped_delims do not show
up as tokens in the output whereas the delimiters in kept_delims
do show up as tokens. If empty_tokens is
drop_empty_tokens, then empty tokens will not show up in the
output. If empty_tokens is keep_empty_tokens then empty
tokens will show up in the output.

explicit char_separator()

The function std::isspace() is used to identify dropped
delimiters and std::ispunct() is used to identify kept delimiters.
In addition, empty tokens are dropped.