diff --git a/src/Config.cpp b/src/Config.cpp index 681a101ce..794dd5b16 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -40,7 +40,7 @@ #include #include #include -#include +#include #include #include #include @@ -437,8 +437,7 @@ void Config::parse (const std::string& input, int nest /* = 1 */) return; // Split the input into lines. - std::vector lines; - split (lines, input, "\n"); + auto lines = split (input, '\n'); // Parse each line. for (auto& line : lines) diff --git a/src/Msg.cpp b/src/Msg.cpp index bed357e11..86efa0930 100644 --- a/src/Msg.cpp +++ b/src/Msg.cpp @@ -27,7 +27,7 @@ #include #include #include -#include +#include //////////////////////////////////////////////////////////////////////////////// void Msg::set (const std::string& name, const std::string& value) @@ -89,9 +89,7 @@ bool Msg::parse (const std::string& input) throw std::string ("ERROR: Malformed message"); // Parse header. - std::vector lines; - split (lines, input.substr (0, separator), '\n'); - for (auto& i : lines) + for (auto& i : split (input.substr (0, separator), '\n')) { auto delimiter = i.find (':'); if (delimiter == std::string::npos) diff --git a/src/commands/CmdCustom.cpp b/src/commands/CmdCustom.cpp index cc45fff3d..27a8eeb11 100644 --- a/src/commands/CmdCustom.cpp +++ b/src/commands/CmdCustom.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -72,18 +73,15 @@ int CmdCustom::execute (std::string& output) std::string reportSort = context.config.get ("report." + _keyword + ".sort"); std::string reportFilter = context.config.get ("report." + _keyword + ".filter"); - std::vector columns; - split (columns, reportColumns, ','); + auto columns = split (reportColumns, ','); validateReportColumns (columns); - std::vector labels; - split (labels, reportLabels, ','); + auto labels = split (reportLabels, ','); if (columns.size () != labels.size () && labels.size () != 0) throw format (STRING_CMD_CUSTOM_MISMATCH, _keyword); - std::vector sortOrder; - split (sortOrder, reportSort, ','); + auto sortOrder = split (reportSort, ','); if (sortOrder.size () != 0 && sortOrder[0] != "none") validateSortColumns (sortOrder); diff --git a/src/commands/CmdExec.cpp b/src/commands/CmdExec.cpp index 2bfefebf7..d06f8b03e 100644 --- a/src/commands/CmdExec.cpp +++ b/src/commands/CmdExec.cpp @@ -29,7 +29,7 @@ #include #include #include -#include +#include extern Context context; @@ -52,9 +52,7 @@ CmdExec::CmdExec () //////////////////////////////////////////////////////////////////////////////// int CmdExec::execute (std::string&) { - std::string command_line; - join (command_line, " ", context.cli2.getWords ()); - return system (command_line.c_str ()); + return system (join (" ", context.cli2.getWords ()).c_str ()); } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/CmdImport.cpp b/src/commands/CmdImport.cpp index 4ede977c0..657a438ff 100644 --- a/src/commands/CmdImport.cpp +++ b/src/commands/CmdImport.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -147,10 +148,7 @@ int CmdImport::import (const std::string& input) // { ... } catch (std::string& e) { - std::vector lines; - split (lines, input, '\n'); - - for (auto& line : lines) + for (auto& line : split (input, '\n')) { if (line.length ()) { diff --git a/src/commands/CmdInfo.cpp b/src/commands/CmdInfo.cpp index 0f6273fb1..4134413ae 100644 --- a/src/commands/CmdInfo.cpp +++ b/src/commands/CmdInfo.cpp @@ -299,8 +299,7 @@ int CmdInfo::execute (std::string& output) task.getTags (tags); if (tags.size ()) { - std::string allTags; - join (allTags, " ", tags); + auto allTags = join (" ", tags); row = view.addRow (); view.set (row, 0, STRING_COLUMN_LABEL_TAGS); diff --git a/src/commands/CmdUDAs.cpp b/src/commands/CmdUDAs.cpp index 00091d904..685adeefd 100644 --- a/src/commands/CmdUDAs.cpp +++ b/src/commands/CmdUDAs.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -215,8 +216,7 @@ int CmdCompletionUDAs::execute (std::string& output) if (udas.size ()) { std::sort (udas.begin (), udas.end ()); - join (output, "\n", udas); - output += '\n'; + output = join ("\n", udas) + '\n'; } return 0; diff --git a/src/dependency.cpp b/src/dependency.cpp index b8e912579..de4f6ee94 100644 --- a/src/dependency.cpp +++ b/src/dependency.cpp @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/rules.cpp b/src/rules.cpp index 0bb86a161..c9715030e 100644 --- a/src/rules.cpp +++ b/src/rules.cpp @@ -67,8 +67,7 @@ void initializeColorRules () // Load the rule.precedence.color list, split it, then autocomplete against // the 'rules' vector loaded above. std::vector results; - std::vector precedence; - split (precedence, context.config.get ("rule.precedence.color"), ','); + auto precedence = split (context.config.get ("rule.precedence.color"), ','); for (const auto& p : precedence) { diff --git a/src/text.cpp b/src/text.cpp index ccd151410..1709e6a84 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -45,246 +46,6 @@ extern Context context; static const char* newline = "\n"; static const char* noline = ""; -/////////////////////////////////////////////////////////////////////////////// -void wrapText ( - std::vector & lines, - const std::string& text, - const int width, - bool hyphenate) -{ - std::string line; - unsigned int offset = 0; - while (extractLine (line, text, width, hyphenate, offset)) - lines.push_back (line); -} - -//////////////////////////////////////////////////////////////////////////////// -void split ( - std::set& results, - const std::string& input, - const char delimiter) -{ - results.clear (); - std::string::size_type start = 0; - std::string::size_type i; - while ((i = input.find (delimiter, start)) != std::string::npos) - { - results.insert (input.substr (start, i - start)); - start = i + 1; - } - - if (input.length ()) - results.insert (input.substr (start)); -} - -//////////////////////////////////////////////////////////////////////////////// -void split ( - std::vector& results, - const std::string& input, - const char delimiter) -{ - results.clear (); - std::string::size_type start = 0; - std::string::size_type i; - while ((i = input.find (delimiter, start)) != std::string::npos) - { - results.push_back (input.substr (start, i - start)); - start = i + 1; - } - - if (input.length ()) - results.push_back (input.substr (start)); -} - -//////////////////////////////////////////////////////////////////////////////// -void split ( - std::vector& results, - const std::string& input, - const std::string& delimiter) -{ - results.clear (); - std::string::size_type length = delimiter.length (); - - std::string::size_type start = 0; - std::string::size_type i; - while ((i = input.find (delimiter, start)) != std::string::npos) - { - results.push_back (input.substr (start, i - start)); - start = i + length; - } - - if (input.length ()) - results.push_back (input.substr (start)); -} - -//////////////////////////////////////////////////////////////////////////////// -void join ( - std::string& result, - const std::string& separator, - const std::vector& items) -{ - std::stringstream s; - unsigned int size = items.size (); - for (unsigned int i = 0; i < size; ++i) - { - s << items[i]; - if (i < size - 1) - s << separator; - } - - result = s.str (); -} - -//////////////////////////////////////////////////////////////////////////////// -void join ( - std::string& result, - const std::string& separator, - const std::vector& items) -{ - std::stringstream s; - unsigned int size = items.size (); - for (unsigned int i = 0; i < size; ++i) - { - s << items[i]; - if (i < size - 1) - s << separator; - } - - result = s.str (); -} - -//////////////////////////////////////////////////////////////////////////////// -// Remove enclosing balanced quotes. Assumes trimmed text. -std::string unquoteText (const std::string& input) -{ - std::string output = input; - - if (output.length () > 1) - { - char quote = output[0]; - if ((quote == '\'' || quote == '"') && - output[output.length () - 1] == quote) - return output.substr (1, output.length () - 2); - } - - return output; -} - -//////////////////////////////////////////////////////////////////////////////// -int longestLine (const std::string& input) -{ - int longest = 0; - int length = 0; - std::string::size_type i = 0; - int character; - - while ((character = utf8_next_char (input, i))) - { - if (character == '\n') - { - if (length > longest) - longest = length; - - length = 0; - } - else - length += mk_wcwidth (character); - } - - if (length > longest) - longest = length; - - return longest; -} - -//////////////////////////////////////////////////////////////////////////////// -// Break UTF8 text into chunks no more than width characters. -bool extractLine ( - std::string& line, - const std::string& text, - int width, - bool hyphenate, - unsigned int& offset) -{ - // Terminate processing. - if (offset >= text.length ()) - return false; - - int line_length {0}; - int character {0}; - std::string::size_type lastWordEnd {std::string::npos}; - bool something {false}; - std::string::size_type cursor {offset}; - std::string::size_type prior_cursor {offset}; - while ((character = utf8_next_char (text, cursor))) - { - // Premature EOL. - if (character == '\n') - { - line = text.substr (offset, prior_cursor - offset); - offset = cursor; - return true; - } - - if (! Lexer::isWhitespace (character)) - { - something = true; - if (! text[cursor] || Lexer::isWhitespace (text[cursor])) - lastWordEnd = prior_cursor; - } - - line_length += mk_wcwidth (character); - - if (line_length >= width) - { - // Backtrack to previous word end. - if (lastWordEnd != std::string::npos) - { - // Eat one WS after lastWordEnd. - std::string::size_type lastBreak = lastWordEnd; - utf8_next_char (text, lastBreak); - - // Position offset at following char. - std::string::size_type nextStart = lastBreak; - utf8_next_char (text, nextStart); - - line = text.substr (offset, lastBreak - offset); - offset = nextStart; - return true; - } - - // No backtrack, possible hyphenation. - else if (hyphenate) - { - line = text.substr (offset, prior_cursor - offset) + '-'; - offset = prior_cursor; - return true; - } - - // No hyphenation, just truncation. - else - { - line = text.substr (offset, cursor - offset); - offset = cursor; - return true; - } - } - - // Hindsight. - prior_cursor = cursor; - } - - // Residual text. - if (something) - { - line = text.substr (offset, cursor - offset); - offset = cursor; - return true; - } - - return false; -} - //////////////////////////////////////////////////////////////////////////////// const char* optionalBlankLine () { diff --git a/src/text.h b/src/text.h index b1dfef035..de92b5263 100644 --- a/src/text.h +++ b/src/text.h @@ -33,15 +33,6 @@ #include // text.cpp, Non-UTF-8 aware. -void wrapText (std::vector &, const std::string&, const int, bool); -std::string unquoteText (const std::string&); -int longestLine (const std::string&); -bool extractLine (std::string&, const std::string&, int, bool, unsigned int&); -void split (std::set&, const std::string&, const char); -void split (std::vector&, const std::string&, const char); -void split (std::vector&, const std::string&, const std::string&); -void join (std::string&, const std::string&, const std::vector&); -void join (std::string&, const std::string&, const std::vector&); const char* optionalBlankLine (); bool nontrivial (const std::string&); int strippedLength (const std::string&);