KSS Utility
C++ general utilities
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
kss::util::strings::Tokenizer Class Reference

Container like access to a string separated by tokens. More...

#include <tokenizer.hpp>

Public Types

using value_type = std::string
 
using iterator = kss::util::iterators::ForwardIterator< Tokenizer, std::string >
 

Public Member Functions

 Tokenizer (const std::string &s, const std::string &delims=" \t\n\r", std::string::size_type start=0, std::string::size_type end=std::string::npos)
 
 Tokenizer (std::string &&s, const std::string &delims=" \t\n\r", std::string::size_type start=0, std::string::size_type end=std::string::npos)
 
 ~Tokenizer () noexcept=default
 
bool hasAnother () const noexcept
 
std::string & next (std::string &token)
 
iterator begin ()
 
iterator end ()
 

Detailed Description

Container like access to a string separated by tokens.

Provide a container-ish view of a string separated by tokens. This is loosly based on code published at http://stackoverflow.com/questions/236129/split-a-string-in-c.

This provides either a stream based or an input iterator based view of the tokens.

Definition at line 31 of file tokenizer.hpp.

Member Typedef Documentation

Container/iterator based type definitions.

Definition at line 36 of file tokenizer.hpp.

Constructor & Destructor Documentation

kss::util::strings::Tokenizer::Tokenizer ( const std::string &  s,
const std::string &  delims = " \t\n\r",
std::string::size_type  start = 0,
std::string::size_type  end = std::string::npos 
)
explicit

Create the tokenizer for a given string and delimiter set. Note that having two or more delimiters in a row will lead to empty tokens (i.e. empty strings).

Parameters
sis the string to be split into tokens
delimsis the set of delimiters
startis the position in s to start searching
endis the position in s one after the position to end searching
Exceptions
std::invalid_argumentif delim is an empty string
anyexceptions that may be thrown by the std::string class
kss::util::strings::Tokenizer::Tokenizer ( std::string &&  s,
const std::string &  delims = " \t\n\r",
std::string::size_type  start = 0,
std::string::size_type  end = std::string::npos 
)
explicit
kss::util::strings::Tokenizer::~Tokenizer ( )
defaultnoexcept

Member Function Documentation

iterator kss::util::strings::Tokenizer::begin ( )
inline

Iterator based access. Note that if you combine this and the stream based access you will find that some tokens are "skipped" as these iterators are written in terms of the stream based access.

Definition at line 83 of file tokenizer.hpp.

iterator kss::util::strings::Tokenizer::end ( )
inline

Definition at line 84 of file tokenizer.hpp.

bool kss::util::strings::Tokenizer::hasAnother ( ) const
inlinenoexcept

Returns true if there is another item available and false otherwise.

Definition at line 67 of file tokenizer.hpp.

std::string& kss::util::strings::Tokenizer::next ( std::string &  token)

Retrieves the next token and places it in token. It is invalid to call this if there are no more tokens.

Returns
a reference to token

The documentation for this class was generated from the following file: