mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-06-26 10:54:26 +02:00
Lexer: Migrated trim(), trimLeft() and trimRight() from text to Lexer
This commit is contained in:
parent
51def4b12b
commit
182b5427cd
13 changed files with 75 additions and 72 deletions
|
@ -25,11 +25,12 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <cmake.h>
|
||||
#include <CLI2.h>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <stdlib.h>
|
||||
#include <Context.h>
|
||||
#include <CLI2.h>
|
||||
#include <Lexer.h>
|
||||
#include <Color.h>
|
||||
#include <text.h>
|
||||
#include <util.h>
|
||||
|
@ -359,7 +360,7 @@ void CLI2::entity (const std::string& category, const std::string& name)
|
|||
// Capture a single argument.
|
||||
void CLI2::add (const std::string& argument)
|
||||
{
|
||||
A2 arg (trim (argument), Lexer::Type::word);
|
||||
A2 arg (Lexer::trim (argument), Lexer::Type::word);
|
||||
arg.tag ("ORIGINAL");
|
||||
_original_args.push_back (arg);
|
||||
|
||||
|
|
|
@ -113,7 +113,7 @@ Color::Color (const std::string& spec)
|
|||
std::string word;
|
||||
for (auto& it : words)
|
||||
{
|
||||
word = Lexer::lowerCase (trim (it));
|
||||
word = Lexer::lowerCase (Lexer::trim (it));
|
||||
|
||||
if (word == "bold") fg_value |= _COLOR_BOLD;
|
||||
else if (word == "bright") bg_value |= _COLOR_BRIGHT;
|
||||
|
|
|
@ -445,7 +445,7 @@ void Config::parse (const std::string& input, int nest /* = 1 */)
|
|||
if (pound != std::string::npos)
|
||||
line = line.substr (0, pound);
|
||||
|
||||
line = trim (line, " \t"); // no i18n
|
||||
line = Lexer::trim (line, " \t"); // no i18n
|
||||
|
||||
// Skip empty lines.
|
||||
if (line.length () > 0)
|
||||
|
@ -453,8 +453,8 @@ void Config::parse (const std::string& input, int nest /* = 1 */)
|
|||
auto equal = line.find ("="); // no i18n
|
||||
if (equal != std::string::npos)
|
||||
{
|
||||
std::string key = trim (line.substr (0, equal), " \t"); // no i18n
|
||||
std::string value = trim (line.substr (equal+1, line.length () - equal), " \t"); // no i18n
|
||||
std::string key = Lexer::trim (line.substr (0, equal), " \t"); // no i18n
|
||||
std::string value = Lexer::trim (line.substr (equal+1, line.length () - equal), " \t"); // no i18n
|
||||
|
||||
(*this)[key] = json::decode (value);
|
||||
}
|
||||
|
@ -463,7 +463,7 @@ void Config::parse (const std::string& input, int nest /* = 1 */)
|
|||
auto include = line.find ("include"); // no i18n.
|
||||
if (include != std::string::npos)
|
||||
{
|
||||
Path included (trim (line.substr (include + 7), " \t"));
|
||||
Path included (Lexer::trim (line.substr (include + 7), " \t"));
|
||||
if (included.is_absolute ())
|
||||
{
|
||||
if (included.readable ())
|
||||
|
|
|
@ -506,6 +506,27 @@ std::string Lexer::ucFirst (const std::string& input)
|
|||
return output;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string Lexer::trimLeft (const std::string& in, const std::string& t /*= " "*/)
|
||||
{
|
||||
std::string out = in;
|
||||
return out.erase (0, in.find_first_not_of (t));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string Lexer::trimRight (const std::string& in, const std::string& t /*= " "*/)
|
||||
{
|
||||
std::string out = in;
|
||||
return out.erase (out.find_last_not_of (t) + 1);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string Lexer::trim (const std::string& in, const std::string& t /*= " "*/)
|
||||
{
|
||||
std::string out = in;
|
||||
return trimLeft (trimRight (out, t), t);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Lexer::Type::string
|
||||
// '|"
|
||||
|
|
|
@ -90,6 +90,9 @@ public:
|
|||
static std::string commify (const std::string&);
|
||||
static std::string lowerCase (const std::string&);
|
||||
static std::string ucFirst (const std::string&);
|
||||
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;
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
#include <cmake.h>
|
||||
#include <Msg.h>
|
||||
#include <Lexer.h>
|
||||
#include <text.h>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -109,7 +110,7 @@ bool Msg::parse (const std::string& input)
|
|||
if (delimiter == std::string::npos)
|
||||
throw std::string ("ERROR: Malformed message header '") + i + "'";
|
||||
|
||||
_header[trim (i.substr (0, delimiter))] = trim (i.substr (delimiter + 1));
|
||||
_header[Lexer::trim (i.substr (0, delimiter))] = Lexer::trim (i.substr (delimiter + 1));
|
||||
}
|
||||
|
||||
// Parse payload.
|
||||
|
|
|
@ -102,7 +102,7 @@ std::string CmdEdit::findValue (
|
|||
found + name.length (),
|
||||
eol - (found + name.length ()));
|
||||
|
||||
return trim (value, "\t ");
|
||||
return Lexer::trim (value, "\t ");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -123,7 +123,7 @@ std::string CmdEdit::findMultilineValue (
|
|||
{
|
||||
std::string value = text.substr (start + startMarker.length (),
|
||||
end - (start + startMarker.length ()));
|
||||
return trim (value, "\t ");
|
||||
return Lexer::trim (value, "\t ");
|
||||
}
|
||||
}
|
||||
return "";
|
||||
|
@ -150,7 +150,7 @@ std::vector <std::string> CmdEdit::findValues (
|
|||
eol - (found + name.length ()));
|
||||
|
||||
found = eol - 1;
|
||||
results.push_back (trim (value, "\t "));
|
||||
results.push_back (Lexer::trim (value, "\t "));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -629,7 +629,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after, const std::string
|
|||
auto eol = after.find ("\n", found + 1);
|
||||
if (eol != std::string::npos)
|
||||
{
|
||||
std::string value = trim (after.substr (
|
||||
std::string value = Lexer::trim (after.substr (
|
||||
found,
|
||||
eol - found), "\t ");
|
||||
|
||||
|
@ -659,7 +659,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after, const std::string
|
|||
}
|
||||
while (annotations.find (name.str ()) != annotations.end ());
|
||||
|
||||
std::string text = trim (value.substr (gap + 4), "\t ");
|
||||
std::string text = Lexer::trim (value.substr (gap + 4), "\t ");
|
||||
annotations.insert (std::make_pair (name.str (), json::decode (text)));
|
||||
}
|
||||
}
|
||||
|
@ -736,8 +736,8 @@ void CmdEdit::parseTask (Task& task, const std::string& after, const std::string
|
|||
auto colon = orphan.find (':');
|
||||
if (colon != std::string::npos)
|
||||
{
|
||||
std::string name = trim (orphan.substr (0, colon), "\t ");
|
||||
std::string value = trim (orphan.substr (colon + 1), "\t ");
|
||||
std::string name = Lexer::trim (orphan.substr (0, colon), "\t ");
|
||||
std::string value = Lexer::trim (orphan.substr (colon + 1), "\t ");
|
||||
|
||||
if (value != "")
|
||||
task.set (name, value);
|
||||
|
|
|
@ -25,14 +25,15 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <cmake.h>
|
||||
#include <CmdUrgency.h>
|
||||
#include <sstream>
|
||||
#include <stdlib.h>
|
||||
#include <Context.h>
|
||||
#include <Filter.h>
|
||||
#include <Lexer.h>
|
||||
#include <main.h>
|
||||
#include <text.h>
|
||||
#include <i18n.h>
|
||||
#include <CmdUrgency.h>
|
||||
|
||||
extern Context context;
|
||||
|
||||
|
@ -72,7 +73,7 @@ int CmdUrgency::execute (std::string& output)
|
|||
{
|
||||
out << format (STRING_CMD_URGENCY_RESULT,
|
||||
task.identifier (),
|
||||
trim (format (task.urgency (), 6, 3)))
|
||||
Lexer::trim (format (task.urgency (), 6, 3)))
|
||||
<< "\n";
|
||||
}
|
||||
|
||||
|
|
21
src/text.cpp
21
src/text.cpp
|
@ -153,27 +153,6 @@ void join (
|
|||
result = s.str ();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string trimLeft (const std::string& in, const std::string& t /*= " "*/)
|
||||
{
|
||||
std::string out = in;
|
||||
return out.erase (0, in.find_first_not_of (t));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string trimRight (const std::string& in, const std::string& t /*= " "*/)
|
||||
{
|
||||
std::string out = in;
|
||||
return out.erase (out.find_last_not_of (t) + 1);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string trim (const std::string& in, const std::string& t /*= " "*/)
|
||||
{
|
||||
std::string out = in;
|
||||
return trimLeft (trimRight (out, t), t);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Remove enclosing balanced quotes. Assumes trimmed text.
|
||||
std::string unquoteText (const std::string& input)
|
||||
|
|
|
@ -34,9 +34,6 @@
|
|||
|
||||
// text.cpp, Non-UTF-8 aware.
|
||||
void wrapText (std::vector <std::string>&, const std::string&, const int, bool);
|
||||
std::string trimLeft (const std::string& in, const std::string& t = " ");
|
||||
std::string trimRight (const std::string& in, const std::string& t = " ");
|
||||
std::string trim (const std::string& in, const std::string& t = " ");
|
||||
std::string unquoteText (const std::string&);
|
||||
int longestWord (const std::string&);
|
||||
int longestLine (const std::string&);
|
||||
|
|
|
@ -90,7 +90,7 @@ bool confirm (const std::string& question)
|
|||
std::string answer {""};
|
||||
std::getline (std::cin, answer);
|
||||
context.debug ("STDIN '" + answer + "'");
|
||||
answer = std::cin.eof () ? STRING_UTIL_CONFIRM_NO : Lexer::lowerCase (trim (answer));
|
||||
answer = std::cin.eof () ? STRING_UTIL_CONFIRM_NO : Lexer::lowerCase (Lexer::trim (answer));
|
||||
|
||||
autoComplete (answer, options, matches, 1); // Hard-coded 1.
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ int confirm4 (const std::string& question)
|
|||
std::string answer {""};
|
||||
std::getline (std::cin, answer);
|
||||
context.debug ("STDIN '" + answer + "'");
|
||||
answer = std::cin.eof () ? STRING_UTIL_CONFIRM_QUIT : Lexer::lowerCase (trim (answer));
|
||||
answer = std::cin.eof () ? STRING_UTIL_CONFIRM_QUIT : Lexer::lowerCase (Lexer::trim (answer));
|
||||
autoComplete (answer, options, matches, 1); // Hard-coded 1.
|
||||
}
|
||||
while (! std::cin.eof () && matches.size () != 1);
|
||||
|
|
|
@ -37,7 +37,7 @@ Context context;
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
int main (int, char**)
|
||||
{
|
||||
UnitTest t (1253);
|
||||
UnitTest t (1274);
|
||||
|
||||
std::vector <std::pair <std::string, Lexer::Type>> tokens;
|
||||
std::string token;
|
||||
|
@ -582,6 +582,33 @@ int main (int, char**)
|
|||
t.is (Lexer::commify ("1234post"), "1,234post", "Lexer::commify '1234post' -> '1,234post'");
|
||||
t.is (Lexer::commify ("pre1234post"), "pre1,234post", "Lexer::commify 'pre1234post' -> 'pre1,234post'");
|
||||
|
||||
// std::string Lexer::trimLeft (const std::string& in, const std::string& t /*= " "*/)
|
||||
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 /*= " "*/)
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@ Context context;
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
int main (int, char**)
|
||||
{
|
||||
UnitTest t (198);
|
||||
UnitTest t (177);
|
||||
|
||||
// Ensure environment has no influence.
|
||||
unsetenv ("TASKDATA");
|
||||
|
@ -202,33 +202,6 @@ int main (int, char**)
|
|||
t.is (joined.length (), (size_t) 5, "join 0 1 2 -> length 5");
|
||||
t.is (joined, "0-1-2", "join 0 1 2 -> '0-1-2'");
|
||||
|
||||
// std::string trimLeft (const std::string& in, const std::string& t /*= " "*/)
|
||||
t.is (trimLeft (""), "", "trimLeft '' -> ''");
|
||||
t.is (trimLeft (" "), "", "trimLeft ' ' -> ''");
|
||||
t.is (trimLeft ("", " \t"), "", "trimLeft '' -> ''");
|
||||
t.is (trimLeft ("xxx"), "xxx", "trimLeft 'xxx' -> 'xxx'");
|
||||
t.is (trimLeft ("xxx", " \t"), "xxx", "trimLeft 'xxx' -> 'xxx'");
|
||||
t.is (trimLeft (" \t xxx \t "), "\t xxx \t ", "trimLeft ' \\t xxx \\t ' -> '\\t xxx \\t '");
|
||||
t.is (trimLeft (" \t xxx \t ", " \t"), "xxx \t ", "trimLeft ' \\t xxx \\t ' -> 'xxx \\t '");
|
||||
|
||||
// std::string trimRight (const std::string& in, const std::string& t /*= " "*/)
|
||||
t.is (trimRight (""), "", "trimRight '' -> ''");
|
||||
t.is (trimRight (" "), "", "trimRight ' ' -> ''");
|
||||
t.is (trimRight ("", " \t"), "", "trimRight '' -> ''");
|
||||
t.is (trimRight ("xxx"), "xxx", "trimRight 'xxx' -> 'xxx'");
|
||||
t.is (trimRight ("xxx", " \t"), "xxx", "trimRight 'xxx' -> 'xxx'");
|
||||
t.is (trimRight (" \t xxx \t "), " \t xxx \t", "trimRight ' \\t xxx \\t ' -> ' \\t xxx \\t'");
|
||||
t.is (trimRight (" \t xxx \t ", " \t"), " \t xxx", "trimRight ' \\t xxx \\t ' -> ' \\t xxx'");
|
||||
|
||||
// std::string trim (const std::string& in, const std::string& t /*= " "*/)
|
||||
t.is (trim (""), "", "trim '' -> ''");
|
||||
t.is (trim (" "), "", "trim ' ' -> ''");
|
||||
t.is (trim ("", " \t"), "", "trim '' -> ''");
|
||||
t.is (trim ("xxx"), "xxx", "trim 'xxx' -> 'xxx'");
|
||||
t.is (trim ("xxx", " \t"), "xxx", "trim 'xxx' -> 'xxx'");
|
||||
t.is (trim (" \t xxx \t "), "\t xxx \t", "trim ' \\t xxx \\t ' -> '\\t xxx \\t'");
|
||||
t.is (trim (" \t xxx \t ", " \t"), "xxx", "trim ' \\t xxx \\t ' -> 'xxx'");
|
||||
|
||||
// std::string unquoteText (const std::string& text)
|
||||
t.is (unquoteText (""), "", "unquoteText '' -> ''");
|
||||
t.is (unquoteText ("x"), "x", "unquoteText 'x' -> 'x'");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue