Orxonox  0.0.5 Codename: Arcturus
Public Member Functions | Static Public Attributes | Private Types | Static Private Member Functions | Private Attributes | List of all members
orxonox::SubString Class Reference

A class that splits a string into multiple tokens using different options. More...

#include </home/jenkins/workspace/orxonox_doxygen_trunk/src/libraries/util/SubString.h>

Public Member Functions

 SubString ()
 Default constructor. More...
 
 SubString (const SubString &other)=default
 Copy constructor. More...
 
 SubString (SubString &&other)=default
 Move constructor. More...
 
 SubString (const std::string &line, const std::string &delimiters=SubString::WhiteSpaces, const std::string &delimiterNeighbours="", bool bAllowEmptyEntries=false, char escapeChar='\\', bool bRemoveEscapeChar=true, char safemodeChar= '"', bool bRemoveSafemodeChar = true, char openparenthesisChar = '{', char closeparenthesisChar = '}', bool bRemoveParenthesisChars = true, char commentChar = '\0')
 Splits a string into multiple tokens. More...
 
 SubString (size_t argc, const char **argv)
 Creates a SubString from a count and values set. More...
 
 SubString (const SubString &other, size_t begin, size_t length=std::string::npos)
 creates a new SubString based on a subset of an other SubString. More...
 
 ~SubString ()
 Destructor. More...
 
SubStringappend (const SubString &other)
 Appends the tokens of another SubString to this. More...
 
const std::string & back () const
 Returns the back of the list of tokens. More...
 
bool compare (const SubString &other, size_t length=std::string::npos) const
 Compares this SubString to another SubString and returns true if the first length values match. More...
 
void debug () const
 Some nice debug information about this SubString. More...
 
bool empty () const
 Returns true if the SubString is empty. More...
 
const std::string & front () const
 Returns the front of the list of tokens. More...
 
const std::vector< std::string > & getAllStrings () const
 Returns all tokens as std::vector. More...
 
const std::string & getString (size_t index) const
 Returns the i'th token from the subset of strings. More...
 
bool isInSafemode (size_t index) const
 Returns true if the token is in safemode. More...
 
std::string join (const std::string &delimiter=" ") const
 Joins the tokens of this SubString using the given delimiter and returns a string. More...
 
SubString operator+ (const SubString &other) const
 Concatenates the tokens of two SubStrings and returns the resulting new SubString. More...
 
SubStringoperator+= (const SubString &other)
 Appends the tokens of other to this SubString. More...
 
SubStringoperator= (const SubString &other)
 Stores the tokens of other in this SubString. More...
 
SubStringoperator= (SubString &&other)=default
 Move assignment. More...
 
bool operator== (const SubString &other) const
 Compares this SubString to another SubString and returns true if they contain the same values. More...
 
const std::string & operator[] (size_t index) const
 Returns the i'th token from the subset of strings. More...
 
void pop_back ()
 Removes the back of the list of tokens. More...
 
size_t size () const
 Returns the number of tokens stored in this SubString. More...
 
size_t split (const std::string &line, const std::string &delimiters=SubString::WhiteSpaces, const std::string &delimiterNeighbours="", bool bAllowEmptyEntries=false, char escapeChar='\\', bool bRemoveEscapeChar=true, char safemodeChar= '"', bool bRemoveSafemodeChar = true, char openparenthesisChar = '{', char closeparenthesisChar = '}', bool bRemoveParenthesisChars = true, char commentChar = '\0')
 Splits a string into multiple tokens. More...
 
SubString subSet (size_t begin, size_t length=std::string::npos) const
 Creates a subset of this SubString. More...
 

Static Public Attributes

static const SubString NullSubString = SubString()
 An empty SubString. More...
 
static const std::string WhiteSpaces = " \n\t"
 All whitespaces (usually used as delimiters or delimiterNeighbours. More...
 
static const std::string WhiteSpacesWithComma = " \n\t,"
 All whitespaces and the comma (usually used as delimiters) More...
 

Private Types

enum  SPLIT_LINE_STATE {
  SPLIT_LINE_STATE::NORMAL, SPLIT_LINE_STATE::ESCAPE, SPLIT_LINE_STATE::SAFEMODE, SPLIT_LINE_STATE::SAFEESCAPE,
  SPLIT_LINE_STATE::COMMENT, SPLIT_LINE_STATE::PARENTHESES, SPLIT_LINE_STATE::PARENTHESESESCAPE
}
 An enumerator for the internal state of the parser. More...
 

Static Private Member Functions

static SPLIT_LINE_STATE splitLine (std::vector< std::string > &tokens, std::vector< bool > &bTokenInSafemode, const std::string &line, const std::string &delimiters=SubString::WhiteSpaces, const std::string &delimiterNeighbours="", bool bAllowEmptyEntries=false, char escapeChar= '\\', bool bRemoveEscapeChar=true, char safemodeChar= '"', bool bRemoveSafemodeChar = true, char openparenthesisChar = '{', char closeparenthesisChar = '}', bool bRemoveParenthesisChars = true, char commentChar = '\0', SPLIT_LINE_STATE start_state = SPLIT_LINE_STATE::NORMAL)
 Splits a string into multiple tokens. More...
 

Private Attributes

std::vector< bool > bTokenInSafemode_
 Saves for each token if it was in safe mode (between quotation marks or parenthesis) More...
 
std::vector< std::string > tokens_
 The tokens after splitting the input line. More...
 

Detailed Description

A class that splits a string into multiple tokens using different options.

The string is split into multiple tokens using a delimiter. Different options like escape character, quotation marks, and more can be used to satisfy your needs.

See this description for an example.

Member Enumeration Documentation

An enumerator for the internal state of the parser.

Enumerator
NORMAL 

Normal state.

ESCAPE 

After an escape character.

SAFEMODE 

In safe mode (usually between quotation marks).

SAFEESCAPE 

In safe mode with the internal escape character, that escapes even the savemode character.

COMMENT 

In Comment mode.

PARENTHESES 

Between parentheses (usually '{' and '}')

PARENTHESESESCAPE 

Between parentheses with the internal escape character, that escapes even the closing parenthesis character.

Constructor & Destructor Documentation

orxonox::SubString::SubString ( )

Default constructor.

orxonox::SubString::SubString ( const SubString other)
default

Copy constructor.

orxonox::SubString::SubString ( SubString &&  other)
default

Move constructor.

orxonox::SubString::SubString ( const std::string &  line,
const std::string &  delimiters = SubString::WhiteSpaces,
const std::string &  delimiterNeighbours = "",
bool  bAllowEmptyEntries = false,
char  escapeChar = '\\',
bool  bRemoveEscapeChar = true,
char  safemodeChar = '"',
bool  bRemoveSafemodeChar = true,
char  openparenthesisChar = '{',
char  closeparenthesisChar = '}',
bool  bRemoveParenthesisChars = true,
char  commentChar = '\0' 
)

Splits a string into multiple tokens.

Parameters
lineThe line to split
delimitersMultiple characters at which to split the line
delimiterNeighboursNeighbours of the delimiters that will be erased as well (for example white-spaces)
bAllowEmptyEntriesIf true, empty tokens are also added to the SubString (if there are two delimiters without a char in between)
escapeCharThe escape character that is used to escape safemode chars (for example if you want to use a quotation mark between two other quotation marks).
bRemoveEscapeCharIf true, the escape char is removed from the tokens
safemodeCharWithin these characters splitting won't happen (usually the quotation marks)
bRemoveSafemodeCharRemoves the safemodeChar from the beginning and the ending of a token
openparenthesisCharThe beginning of a safemode is marked with this (usually an opening brace)
closeparenthesisCharThe ending of a safemode is marked with this (usually a closing brace)
bRemoveParenthesisCharsRemoves the parenthesis chars from the beginning and the ending of a token
commentCharThe comment character (used to ignore the part of the line after the comment char).
orxonox::SubString::SubString ( size_t  argc,
const char **  argv 
)

Creates a SubString from a count and values set.

Parameters
argcThe number of arguments
argvAn array of pointers to the arguments
orxonox::SubString::SubString ( const SubString other,
size_t  begin,
size_t  length = std::string::npos 
)

creates a new SubString based on a subset of an other SubString.

Parameters
otherThe other SubString
beginThe beginning of the subset
lengthThe length of the subset

The subset ranges from the token with index begin and contains length elements.

orxonox::SubString::~SubString ( )

Destructor.

Member Function Documentation

SubString& orxonox::SubString::append ( const SubString other)
inline

Appends the tokens of another SubString to this.

Returns
This SubString.
const std::string& orxonox::SubString::back ( ) const
inline

Returns the back of the list of tokens.

bool orxonox::SubString::compare ( const SubString other,
size_t  length = std::string::npos 
) const

Compares this SubString to another SubString and returns true if the first length values match.

Parameters
otherThe other SubString
lengthHow many tokens to compare
void orxonox::SubString::debug ( ) const

Some nice debug information about this SubString.

bool orxonox::SubString::empty ( ) const
inline

Returns true if the SubString is empty.

const std::string& orxonox::SubString::front ( ) const
inline

Returns the front of the list of tokens.

const std::vector<std::string>& orxonox::SubString::getAllStrings ( ) const
inline

Returns all tokens as std::vector.

const std::string& orxonox::SubString::getString ( size_t  index) const
inline

Returns the i'th token from the subset of strings.

Parameters
indexThe index of the requested token
bool orxonox::SubString::isInSafemode ( size_t  index) const
inline

Returns true if the token is in safemode.

Parameters
indexThe index of the token
std::string orxonox::SubString::join ( const std::string &  delimiter = " ") const

Joins the tokens of this SubString using the given delimiter and returns a string.

Parameters
delimiterThis delimiter will be placed between each two tokens
Returns
The joined string.
SubString orxonox::SubString::operator+ ( const SubString other) const

Concatenates the tokens of two SubStrings and returns the resulting new SubString.

Returns
A new SubString that contains the tokens of this and the other SubString
SubString & orxonox::SubString::operator+= ( const SubString other)

Appends the tokens of other to this SubString.

Returns
This SubString
SubString & orxonox::SubString::operator= ( const SubString other)

Stores the tokens of other in this SubString.

Returns
This SubString.
SubString& orxonox::SubString::operator= ( SubString &&  other)
default

Move assignment.

bool orxonox::SubString::operator== ( const SubString other) const

Compares this SubString to another SubString and returns true if they contain the same values.

const std::string& orxonox::SubString::operator[] ( size_t  index) const
inline

Returns the i'th token from the subset of strings.

Parameters
indexThe index of the requested token
void orxonox::SubString::pop_back ( )
inline

Removes the back of the list of tokens.

size_t orxonox::SubString::size ( ) const
inline

Returns the number of tokens stored in this SubString.

size_t orxonox::SubString::split ( const std::string &  line,
const std::string &  delimiters = SubString::WhiteSpaces,
const std::string &  delimiterNeighbours = "",
bool  bAllowEmptyEntries = false,
char  escapeChar = '\\',
bool  bRemoveEscapeChar = true,
char  safemodeChar = '"',
bool  bRemoveSafemodeChar = true,
char  openparenthesisChar = '{',
char  closeparenthesisChar = '}',
bool  bRemoveParenthesisChars = true,
char  commentChar = '\0' 
)

Splits a string into multiple tokens.

Parameters
lineThe line to split
delimitersMultiple characters at which to split the line
delimiterNeighboursNeighbours of the delimiters that will be erased as well (for example white-spaces)
bAllowEmptyEntriesIf true, empty tokens are also added to the SubString (if there are two delimiters without a char in between)
escapeCharThe escape character that is used to escape safemode chars (for example if you want to use a quotation mark between two other quotation marks).
bRemoveEscapeCharIf true, the escape char is removed from the tokens
safemodeCharWithin these characters splitting won't happen (usually the quotation marks)
bRemoveSafemodeCharRemoves the safemodeChar from the beginning and the ending of a token
openparenthesisCharThe beginning of a safemode is marked with this (usually an opening brace)
closeparenthesisCharThe ending of a safemode is marked with this (usually a closing brace)
bRemoveParenthesisCharsRemoves the parenthesis chars from the beginning and the ending of a token
commentCharThe comment character (used to ignore the part of the line after the comment char).
SubString::SPLIT_LINE_STATE orxonox::SubString::splitLine ( std::vector< std::string > &  tokens,
std::vector< bool > &  bTokenInSafemode,
const std::string &  line,
const std::string &  delimiters = SubString::WhiteSpaces,
const std::string &  delimiterNeighbours = "",
bool  bAllowEmptyEntries = false,
char  escapeChar = '\\',
bool  bRemoveEscapeChar = true,
char  safemodeChar = '"',
bool  bRemoveSafemodeChar = true,
char  openparenthesisChar = '{',
char  closeparenthesisChar = '}',
bool  bRemoveParenthesisChars = true,
char  commentChar = '\0',
SPLIT_LINE_STATE  start_state = SPLIT_LINE_STATE::NORMAL 
)
staticprivate

Splits a string into multiple tokens.

Parameters
lineThe line to split
delimitersMultiple characters at which to split the line
delimiterNeighboursNeighbours of the delimiters that will be erased as well (for example white-spaces)
bAllowEmptyEntriesIf true, empty tokens are also added to the SubString (if there are two delimiters without a char in between)
escapeCharThe escape character that is used to escape safemode chars (for example if you want to use a quotation mark between two other quotation marks).
bRemoveEscapeCharIf true, the escape char is removed from the tokens
safemodeCharWithin these characters splitting won't happen (usually the quotation marks)
bRemoveSafemodeCharRemoves the safemodeChar from the beginning and the ending of a token
openparenthesisCharThe beginning of a safemode is marked with this (usually an opening brace)
closeparenthesisCharThe ending of a safemode is marked with this (usually a closing brace)
bRemoveParenthesisCharsRemoves the parenthesis chars from the beginning and the ending of a token
commentCharThe comment character (used to ignore the part of the line after the comment char).
tokensThe array, where the splitted strings will be stored in
bTokenInSafemodeA vector wich stores for each character of the string if it is in safemode or not
start_stateThe internal state of the parser

This is the actual splitting algorithm from Clemens Wacha. Supports delimiters, escape characters, ignores special characters between safemodeChar and between commentChar and line end "\n".

Extended by Orxonox to support parenthesis as additional safe-mode.

SubString orxonox::SubString::subSet ( size_t  begin,
size_t  length = std::string::npos 
) const

Creates a subset of this SubString.

Parameters
beginThe beginning of the subset
lengthThe length of the subset
Returns
A new SubString containing the defined subset.

The subset ranges from the token with index begin and contains length elements.

This function is added for your convenience, and does the same as SubString::SubString(const SubString& other, size_t begin, size_t length)

Member Data Documentation

std::vector<bool> orxonox::SubString::bTokenInSafemode_
private

Saves for each token if it was in safe mode (between quotation marks or parenthesis)

const SubString orxonox::SubString::NullSubString = SubString()
static

An empty SubString.

std::vector<std::string> orxonox::SubString::tokens_
private

The tokens after splitting the input line.

const std::string orxonox::SubString::WhiteSpaces = " \n\t"
static

All whitespaces (usually used as delimiters or delimiterNeighbours.

const std::string orxonox::SubString::WhiteSpacesWithComma = " \n\t,"
static

All whitespaces and the comma (usually used as delimiters)


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