Lexer: Added ::trim methods

This commit is contained in:
Paul Beckingham 2015-12-20 13:11:32 -05:00
parent a97f819423
commit 1ba3652c0e
3 changed files with 57 additions and 1 deletions

View file

@ -123,6 +123,32 @@ bool Lexer::isEOS () const
return _cursor >= _eos;
}
////////////////////////////////////////////////////////////////////////////////
std::string Lexer::trimLeft (const std::string& in, const std::string& t /*= " "*/)
{
std::string::size_type ws = in.find_first_not_of (t);
if (ws > 0)
{
std::string out {in};
return out.erase (0, ws);
}
return in;
}
////////////////////////////////////////////////////////////////////////////////
std::string Lexer::trimRight (const std::string& in, const std::string& t /*= " "*/)
{
std::string out {in};
return out.erase (in.find_last_not_of (t) + 1);
}
////////////////////////////////////////////////////////////////////////////////
std::string Lexer::trim (const std::string& in, const std::string& t /*= " "*/)
{
return trimLeft (trimRight (in, t), t);
}
////////////////////////////////////////////////////////////////////////////////
// Lexer::Type::word
// [^\s]+

View file

@ -43,6 +43,9 @@ public:
// Static helpers.
static bool isWhitespace (int);
static bool isSingleCharOperator (int);
static std::string trimLeft (const std::string& in, const std::string& t = " ");
static std::string trimRight (const std::string& in, const std::string& t = " ");
static std::string trim (const std::string& in, const std::string& t = " ");
// Stream Classifiers.
bool isEOS () const;

View file

@ -34,7 +34,7 @@
////////////////////////////////////////////////////////////////////////////////
int main (int, char**)
{
UnitTest t (29);
UnitTest t (50);
std::vector <std::pair <std::string, Lexer::Type>> tokens;
std::string token;
@ -77,6 +77,33 @@ int main (int, char**)
Lexer l1 (" \t ");
t.notok (l1.token (token, type), "' \\t ' --> no tokens");
// std::string Lexer::trimLeft (const std::string& in, const std::string&)
t.is (Lexer::trimLeft (""), "", "Lexer::trimLeft '' -> ''");
t.is (Lexer::trimLeft (" "), "", "Lexer::trimLeft ' ' -> ''");
t.is (Lexer::trimLeft ("", " \t"), "", "Lexer::trimLeft '' -> ''");
t.is (Lexer::trimLeft ("xxx"), "xxx", "Lexer::trimLeft 'xxx' -> 'xxx'");
t.is (Lexer::trimLeft ("xxx", " \t"), "xxx", "Lexer::trimLeft 'xxx' -> 'xxx'");
t.is (Lexer::trimLeft (" \t xxx \t "), "\t xxx \t ", "Lexer::trimLeft ' \\t xxx \\t ' -> '\\t xxx \\t '");
t.is (Lexer::trimLeft (" \t xxx \t ", " \t"), "xxx \t ", "Lexer::trimLeft ' \\t xxx \\t ' -> 'xxx \\t '");
// std::string Lexer::trimRight (const std::string& in, const std::string&)
t.is (Lexer::trimRight (""), "", "Lexer::trimRight '' -> ''");
t.is (Lexer::trimRight (" "), "", "Lexer::trimRight ' ' -> ''");
t.is (Lexer::trimRight ("", " \t"), "", "Lexer::trimRight '' -> ''");
t.is (Lexer::trimRight ("xxx"), "xxx", "Lexer::trimRight 'xxx' -> 'xxx'");
t.is (Lexer::trimRight ("xxx", " \t"), "xxx", "Lexer::trimRight 'xxx' -> 'xxx'");
t.is (Lexer::trimRight (" \t xxx \t "), " \t xxx \t", "Lexer::trimRight ' \\t xxx \\t ' -> ' \\t xxx \\t'");
t.is (Lexer::trimRight (" \t xxx \t ", " \t"), " \t xxx", "Lexer::trimRight ' \\t xxx \\t ' -> ' \\t xxx'");
// std::string Lexer::trim (const std::string& in, const std::string& t)
t.is (Lexer::trim (""), "", "Lexer::trim '' -> ''");
t.is (Lexer::trim (" "), "", "Lexer::trim ' ' -> ''");
t.is (Lexer::trim ("", " \t"), "", "Lexer::trim '' -> ''");
t.is (Lexer::trim ("xxx"), "xxx", "Lexer::trim 'xxx' -> 'xxx'");
t.is (Lexer::trim ("xxx", " \t"), "xxx", "Lexer::trim 'xxx' -> 'xxx'");
t.is (Lexer::trim (" \t xxx \t "), "\t xxx \t", "Lexer::trim ' \\t xxx \\t ' -> '\\t xxx \\t'");
t.is (Lexer::trim (" \t xxx \t ", " \t"), "xxx", "Lexer::trim ' \\t xxx \\t ' -> 'xxx'");
return 0;
}