Files CCore/inc/StrParse.h CCore/src/StrParse.cpp
This is a collection of tools for a simple string parsing. It is used, for example, in the parsing of format strings.
DecValue is a Class-function, it calculates a decimal value of a character.
struct DecValue
{
bool nok;
unsigned value;
explicit DecValue(char ch);
bool operator ! () const { return nok; }
unsigned operator * () const { return value; }
};
nok is true, if the character is not a decimal digit.
value is the decimal value of the character or 0.
operator ! is true, if the character is not a decimal digit.
operator * is the decimal value of the character or 0.
StrParse is a string-based "character stream" class. Its interface matches parser functions requirements, so it can be used with them.
class StrParse
{
....
public:
StrParse(const char *ptr,const char *lim);
explicit StrParse(StrLen str);
bool operator ! () const { return nok; }
struct Peek
{
bool nok;
char ch;
explicit Peek(StrParse &dev);
bool operator ! () const { return nok; }
char operator * () const { return ch; }
};
void operator ++ () { ptr++; }
void fail() { nok=true; }
NegBool finish() { if( ptr<lim ) nok=true; return nok; }
};
The first constructor creates a stream from the pair ptr, lim.
The second constructor creates a stream from a StrLen.
operator ! return true, if the stream is failed.
Peek is used to see the current stream character, if any. Failed stream is empty.
operator ++ moves to the next character.
fail() fails the stream.
finish() fails the stream, if it is not empty, and returns the fail flag as a NegBool.
Parse functions work with some "character stream device":
class Dev
{
....
public:
struct Peek
{
....
explicit Peek(Dev &dev);
bool operator ! () const { return nok; }
char operator * () const { return ch; }
};
void operator ++ ();
void fail();
};
Peek is used to see the current stream character, if any. Failed stream is empty. operator ! is true, if the stream is empty. Otherwise, operator * is the current character.
operator ++ moves to the next character.
fail() fails the stream.
template <class Dev> void ParseChar(Dev &dev,char ch); template <class Dev> bool ParseChar_try(Dev &dev,char ch); template <class Dev> void ParseChar_empty(Dev &dev,char &ret,char defval);
ParseChar() extracts the given character from the stream, or fails it.
ParseChar_try() tries to extract the given character from the stream, return value is the success of the operation.
ParseChar_empty() extracts the next character from the stream and puts it to the ret, if available. Otherwise, the defval is put.
template <class Dev,class UInt,class UInt1,class UInt2> void ParseUInt(Dev &dev,UInt &ret,UInt1 minval,UInt2 maxval); template <class Dev,class UInt,class UInt1> void ParseUInt(Dev &dev,UInt &ret,UInt1 maxval); template <class Dev,class UInt> void ParseUInt(Dev &dev,UInt &ret);
ParseUInt() extracts a decimal unsigned number from the stream, converts it to the UInt type and puts it to the ret. Stream is failed in case of error. The number must be in the given value range, which is a subset of the UInt value range. Types UInt1 and UInt2 are not necessary unsigned integral types, but values minval and maxval are implicitly converted to the type UInt.
template <class Dev,class UInt,class UInt1,class UInt2,class UInt3> void ParseUInt_empty(Dev &dev,UInt &ret,UInt1 defval,UInt2 minval,UInt3 maxval); template <class Dev,class UInt,class UInt1,class UInt2> void ParseUInt_empty(Dev &dev,UInt &ret,UInt1 defval,UInt2 maxval); template <class Dev,class UInt,class UInt1> void ParseUInt_empty(Dev &dev,UInt &ret,UInt1 defval);
ParseUInt_empty() works similar to the ParseUInt().The difference is if the stream does not begin with a digit, it returns the defval.
Files CCore/inc/CharProp.h CCore/src/CharProp.cpp
template <class Dev>
void ParseSpace(Dev &dev);
This function extracts space characters up to the end of the stream or the first non-space character.