This class operates on strings which contains data separated by a delimiter character. It identifies the sub-strings between the delimiters as tokens of type Substring . After an instance of this class is constructed, three methods are available:
false
.After a token was retrieved, it might be modified using the interface of class Substring as the tokenizer does not rely on the bounds of the current token when receiving the next. Furthermore, even field Rest is allowed to be changed using the interface of Substring if it seems appropriate. The effect is the same as if method Set was invoked to apply a different source string.
Objects of this class can be reused by freshly initializing them using method Set.
Sample code:
The following code sample shows how to tokenize a string:
The output will be:
test abc 1,2 , 3 1~2~3 xyz including;separator
TChar | The character type. Implementations for nchar and wchar are provided with type definitions alib::TokenizerN and alib::TokenizerW. |
Definition at line 66 of file tokenizer.hpp.
#include <tokenizer.hpp>
Public Field Index: | |
TSubstring< TChar > | Actual |
TSubstring< TChar > | Rest |
TLocalString< TChar, 8 > | TrimChars |
Public Method Index: | |
TTokenizer () | |
TTokenizer (const TString< TChar > &src, TChar delimiter, bool skipEmptyTokens=false) | |
TSubstring< TChar > & | GetRest (lang::Whitespaces trimming=lang::Whitespaces::Trim) |
bool | HasNext () |
ALIB_API TSubstring< TChar > & | Next (lang::Whitespaces trimming=lang::Whitespaces::Trim, TChar newDelim='\0') |
void | Set (const TString< TChar > &src, TChar delimiter, bool skipEmptyTokens=false) |
TSubstring<TChar> Actual |
The actual token, which is returned with every invocation of Next() or Rest(). It is allowed to manipulate this field any time.
Definition at line 84 of file tokenizer.hpp.
|
protected |
The most recently set delimiter used by default for the next token extraction.
Definition at line 98 of file tokenizer.hpp.
TSubstring<TChar> Rest |
A Substring that represents the part of the underlying data that has not been tokenized, yet. It is allowed to manipulate this public field, which has a similar effect as using method Set.
Definition at line 78 of file tokenizer.hpp.
|
protected |
If true
, empty tokens are omitted.
Definition at line 101 of file tokenizer.hpp.
TLocalString<TChar, 8> TrimChars |
The white spaces characters used to trim the tokens. Defaults to alib::DefaultWhitespaces
Definition at line 90 of file tokenizer.hpp.
|
inline |
Constructs an empty tokenizer. To initialize, method Set needs to be invoked.
Definition at line 111 of file tokenizer.hpp.
|
inline |
Constructs a tokenizer to work on a given string.
src | The string to be tokenized. |
delimiter | The delimiter that separates the tokens. Can be changed with every next token. |
skipEmptyTokens | If true , empty tokens are omitted. Optional and defaults to false . |
Definition at line 123 of file tokenizer.hpp.
|
inline |
Returns the currently remaining string (without searching for further delimiter characters). After this call HasNext will return false
and Next will return a nulled Substring.
trimming | Determines if the token is trimmed in respect to the white space characters defined in field TrimChars. Defaults to Whitespaces.Trim. |
Definition at line 184 of file tokenizer.hpp.
|
inline |
If this returns true
, a call to Next will be successful and will return a Substring which is not nulled.
true
if a next token is available. Definition at line 199 of file tokenizer.hpp.
template ALIB_API TSubstring< wchar > & Next | ( | lang::Whitespaces | trimming = lang::Whitespaces::Trim, |
TChar | newDelim = '\0' ) |
Returns the next token, which is afterwards also available through field Actual. If no further token was available, the returned Substring will be nulled. (see String::IsNull ). To prevent this, the availability of a next token should be checked using method HasNext().
For clarification, see the explanation and sample code in this classes documentation.
trimming | Determines if the token is trimmed in respect to the white space characters defined in field TrimChars. Defaults to Whitespaces.Trim. |
newDelim | The delimiter separates the tokens. Defaults to 0, which keeps the current delimiter intact. A new delimiter can be provided for every next token. |
true
if a next token was available, false
if not. Definition at line 18 of file tokenizer.cpp.
|
inline |
Resets a tokenizer to work on a given string.
src | The string to be tokenized |
delimiter | The delimiter that separates the tokens. Can be changed with every next token. |
skipEmptyTokens | If true , empty tokens are omitted. Optional and defaults to false . |
Definition at line 144 of file tokenizer.hpp.