Subversion Repositories public

Compare Revisions

Ignore whitespace Rev 47 → Rev 55

/incron/trunk/strtok.h
26,6 → 26,8
 
#include <string>
 
typedef std::string::size_type SIZE;
 
/// Simple string tokenizer class.
/**
* This class implements a string tokenizer. It splits a string
32,12 → 34,22
* by a character to a number of elements (tokens) which are
* provided sequentially.
*
* All operations are made on the original string itself.
* The implementation is not ready to handle any changes of the
* string.
* All operations are made on a copy of the original string
* (which may be in fact a copy-on-write instance).
*
* The original string is left unchanged. All tokens are returned
* as newly created strings.
*
* There is possibility to specify a prefix character which
* causes the consecutive character is not considered as
* a delimiter. If you don't specify this character (or specify
* the NUL character, 0x00) this feature is disabled. The mostly
* used prefix is a backslash ('\').
*
* This class is not thread-safe.
*
* Performance note: This class is currently not intended
* to be very fast. Speed optimizations will be done later.
*/
class StringTokenizer
{
48,8 → 60,10
*
* \param[in] rStr string for tokenizing
* \param[in] cDelim delimiter (separator) character
* \param[in] cPrefix character which is prepended if a
* character must not separate tokens
*/
StringTokenizer(const std::string& rStr, char cDelim = ',');
StringTokenizer(const std::string& rStr, char cDelim = ',', char cPrefix = '\0');
/// Destructor.
~StringTokenizer() {}
65,15 → 79,50
/// Returns the next token.
/**
* If a prefix is defined it is stripped from the returned
* string (e.g. 'abc\ def' is transformed to 'abc def'
* while the prefix is '\').
*
* \param[in] fSkipEmpty skip empty strings (more consecutive delimiters)
* \return next token or "" if no more tokens available
*
* \sa GetNextTokenRaw()
*/
std::string GetNextToken();
std::string GetNextToken(bool fSkipEmpty = false);
/// Returns the next token.
/**
* This method always returns an unmodified string even
* if it contains prefix characters.
*
* \param[in] fSkipEmpty skip empty strings (more consecutive delimiters)
* \return next token or "" if no more tokens available
*
* \sa GetNextToken()
*/
std::string GetNextTokenRaw(bool fSkipEmpty = false);
/// Returns the remainder of the source string.
/**
* This method returns everything what has not been
* processed (tokenized) yet and moves the current
* position to the end of the string.
*
* If a prefix is defined it is stripped from
* the returned string.
*
* \return remainder string
*/
std::string GetRemainder();
/// Sets a delimiter (separator) character.
/**
* The new delimiter has effect only to tokens returned later;
* the position in the string is not affected.
*
* If you specify a NUL character (0x00) here the prefix
* will not be used.
*
* \param[in] cDelim delimiter character
*/
inline void SetDelimiter(char cDelim)
90,6 → 139,40
return m_cDelim;
}
/// Sets a prefix character.
/**
* The new prefix has effect only to tokens returned later;
* the position in the string is not affected.
*
* \param[in] cPrefix prefix character
*
* \sa SetNoPrefix()
*/
inline void SetPrefix(char cPrefix)
{
m_cPrefix = cPrefix;
}
/// Returns the prefix character.
/**
* \return prefix character
*/
inline char GetPrefix() const
{
return m_cPrefix;
}
/// Sets the prefix to 'no prefix'.
/**
* Calling this method is equivalent to SetPrefix((char) 0).
*
* \sa SetPrefix()
*/
inline void SetNoPrefix()
{
SetPrefix('\0');
}
/// Resets the tokenizer.
/**
* Re-initializes tokenizing to the start of the string.
102,8 → 185,46
private:
std::string m_str; ///< tokenized string
char m_cDelim; ///< delimiter character
char m_cPrefix; ///< prefix character
std::string::size_type m_pos; ///< current position
std::string::size_type m_len; ///< string length
/// Strips all prefix characters.
/**
* \param[in] s source string
* \param[in] cnt string length
* \return modified string
*/
std::string StripPrefix(const char* s, SIZE cnt);
/// Extracts the next token (internal method).
/**
* The extracted token may be empty.
*
* \param[out] rToken extracted token
* \param[in] fStripPrefix strip prefix characters yes/no
*/
void _GetNextToken(std::string& rToken, bool fStripPrefix);
/// Extracts the next token (internal method).
/**
* This method does no checking about the prefix character.
*
* The extracted token may be empty.
*
* \param[out] rToken extracted token
*/
void _GetNextTokenNoPrefix(std::string& rToken);
/// Extracts the next token (internal method).
/**
* This method does checking about the prefix character.
*
* The extracted token may be empty.
*
* \param[out] rToken extracted token
*/
void _GetNextTokenWithPrefix(std::string& rToken);
};