Files CCore/inc/TextTools.h CCore/src/TextTools.cpp
This header contains several simple tools to work with text and characters.
inline bool CharIsEOL(char ch) { return ch=='\r' || ch=='\n' ; } inline bool CharIsBin(char ch); inline bool CharIsDec(char ch); inline bool CharIsHex(char ch); inline int CharBinValue(char ch);
CharIsEOL() — end-of-line character, one of '\n' or '\r'.
CharIsBin() — char is a binary digit.
CharIsDec() — char is a decimal digit.
CharIsHex() — char is a hexadecimal digit.
CharBinValue() — binary value of the character, or -1 if is not a binary digit.
This class represents a text position. It also counts a new text position after some text fragment. It is designed to calculate text positions during a text file processing. This class counts all cases "\r", "\n", "\r\n" as a line delimiters.
struct TextPos
{
ulen line;
ulen col;
TextPos() : line(1),col(1) {}
void nextPos() { col++; }
void nextLine() { col=1; line++; }
void update(ulen len) { col+=len; }
void update(StrLen text); // must not break "\r\n"
template <class P>
void print(P &out) const;
};
Default constructor sets the initial text position: line is 1 and col is 1.
nextPos() moves to the next position in the same line.
nextLine() moves to the begin of the next line.
update() moves on len positions at the same line.
update() moves by the text fragment. The fragment must not break a sequence "\r\n".
The class is printable in the form "(line=<line>,col=<col>)".
CharPropTable is a Partial class. It is a look-up table, which sets a value (character class) to each character. A derived class must initialize the table using protected methods.
template <class CharClass,CharClass DefaultClass>
class CharPropTable : NoCopy
{
CharClass table[256];
protected:
void set(char ch,CharClass cc);
void setSet(const char *zstr,CharClass cc);
template <class Func>
void setIf(Func cond,CharClass cc);
public:
CharPropTable();
CharClass operator [] (char ch) const;
};
CharClass is a value type, enum type is expected. DefaultClass is a default value, assigned to each character. Constructor initializes the table assigning the DefaultClass to each character.
operator [] returns a value, assigned to the particular character.
A derived class can use the set of protected members to initialize the table.
set() sets the given value to the given character.
setSet() sets the given value to the set of characters from the given zero-terminated string.
setIf() sets the given value to characters, satisfied the condition, given as a functor-predicate.