diff --git a/src/CLI2.cpp b/src/CLI2.cpp index 6e37b11b2..b779f7198 100644 --- a/src/CLI2.cpp +++ b/src/CLI2.cpp @@ -25,11 +25,12 @@ //////////////////////////////////////////////////////////////////////////////// #include +#include #include #include #include #include -#include +#include #include #include #include @@ -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); diff --git a/src/Color.cpp b/src/Color.cpp index bac00a149..6ecf077bf 100644 --- a/src/Color.cpp +++ b/src/Color.cpp @@ -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; diff --git a/src/Config.cpp b/src/Config.cpp index 94581feaa..1bfa69cc6 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -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 ()) diff --git a/src/Lexer.cpp b/src/Lexer.cpp index 3ef152ec7..2e0f32b80 100644 --- a/src/Lexer.cpp +++ b/src/Lexer.cpp @@ -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 // '|" diff --git a/src/Lexer.h b/src/Lexer.h index f31236ca7..e57e5e941 100644 --- a/src/Lexer.h +++ b/src/Lexer.h @@ -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; diff --git a/src/Msg.cpp b/src/Msg.cpp index 2a65a1bf2..c48ea33dc 100644 --- a/src/Msg.cpp +++ b/src/Msg.cpp @@ -26,6 +26,7 @@ #include #include +#include #include //////////////////////////////////////////////////////////////////////////////// @@ -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. diff --git a/src/commands/CmdEdit.cpp b/src/commands/CmdEdit.cpp index 91ef87805..ee53cc39f 100644 --- a/src/commands/CmdEdit.cpp +++ b/src/commands/CmdEdit.cpp @@ -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 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); diff --git a/src/commands/CmdUrgency.cpp b/src/commands/CmdUrgency.cpp index d43e31c45..dd2485d57 100644 --- a/src/commands/CmdUrgency.cpp +++ b/src/commands/CmdUrgency.cpp @@ -25,14 +25,15 @@ //////////////////////////////////////////////////////////////////////////////// #include +#include #include #include #include #include +#include #include #include #include -#include 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"; } diff --git a/src/text.cpp b/src/text.cpp index 69489fa02..0240b5735 100644 --- a/src/text.cpp +++ b/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) diff --git a/src/text.h b/src/text.h index 5dcd78de9..34add5e9c 100644 --- a/src/text.h +++ b/src/text.h @@ -34,9 +34,6 @@ // text.cpp, Non-UTF-8 aware. void wrapText (std::vector &, 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&); diff --git a/src/util.cpp b/src/util.cpp index d47c4b7a0..c947c0a93 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -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); diff --git a/test/lexer.t.cpp b/test/lexer.t.cpp index 9d32857df..86da0ed48 100644 --- a/test/lexer.t.cpp +++ b/test/lexer.t.cpp @@ -37,7 +37,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int, char**) { - UnitTest t (1253); + UnitTest t (1274); std::vector > 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; } diff --git a/test/text.t.cpp b/test/text.t.cpp index 7116ae835..fe643eeed 100644 --- a/test/text.t.cpp +++ b/test/text.t.cpp @@ -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'");