Lexer: Migrated trim(), trimLeft() and trimRight() from text to Lexer
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user