From 8c1c18ed5496ae6c459820110b77147800ecb360 Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 27 Jan 2020 04:08:42 +0100 Subject: [PATCH 1/4] Add support for more robust message parsing to native-wrapper. Step 1 for #7. --- .../native-wrapper/remwharead_wrapper.cpp | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/browser-plugins/webextension/native-wrapper/remwharead_wrapper.cpp b/browser-plugins/webextension/native-wrapper/remwharead_wrapper.cpp index 31ddd99..0151c69 100644 --- a/browser-plugins/webextension/native-wrapper/remwharead_wrapper.cpp +++ b/browser-plugins/webextension/native-wrapper/remwharead_wrapper.cpp @@ -30,6 +30,8 @@ using std::system; string read_input(); void send_message(const string &message); int launch(const string &args); +string decode_args(const string &args); +void replace_in_field(string &field); string read_input() { @@ -78,11 +80,44 @@ int launch(const string &args) return ret; } +string decode_args(const string &args) +{ + constexpr char separator{'\u001F'}; // UNIT SEPARATOR. + if (args[0] != separator) // Extension uses old method. + { + return args; + } + + size_t pos{1}; + size_t endpos{0}; + string newargs; + while ((endpos = args.find(separator, pos)) != string::npos) + { + string field{args.substr(pos, endpos - pos)}; + replace_in_field(field); + newargs += " \"" + field + '"'; + pos = endpos + 1; + } + + return newargs; +} + +void replace_in_field(string &field) +{ + size_t pos{0}; + while ((pos = field.find('"', pos)) != string::npos) + { + field.replace(pos, 1, R"(\")"); // Replace " with \". + pos += 2; + } +} + int main() { const string args = read_input(); - const int ret = launch(args); + cout << decode_args(args) << std::endl; + const int ret = launch(decode_args(args)); if (ret == 0) { From bb5d9cecad1bee182aa4a001ec2ec7dd8e855ef4 Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 27 Jan 2020 04:50:33 +0100 Subject: [PATCH 2/4] Change namespace of version. --- src/cli/parse_options.cpp | 2 +- src/lib/export/adoc.cpp | 2 +- src/lib/export/rss.cpp | 2 +- src/lib/uri.cpp | 2 +- src/version.hpp.in | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/cli/parse_options.cpp b/src/cli/parse_options.cpp index dae2c5c..2a6690d 100644 --- a/src/cli/parse_options.cpp +++ b/src/cli/parse_options.cpp @@ -237,7 +237,7 @@ void App::print_help(const string &option) void App::print_version() { - cout << "remwharead " << global::version << endl << + cout << "remwharead " << remwharead::version << endl << "Copyright (C) 2019 tastytea \n" "License GPLv3: GNU GPL version 3 " ".\n" diff --git a/src/lib/export/adoc.cpp b/src/lib/export/adoc.cpp index 25d255c..9f7d49b 100644 --- a/src/lib/export/adoc.cpp +++ b/src/lib/export/adoc.cpp @@ -36,7 +36,7 @@ void Export::AsciiDoc::print() const try { _out << "= Visited things\n" - << ":Author: remwharead " << global::version << "\n" + << ":Author: remwharead " << version << "\n" << ":Date: " << timepoint_to_string(system_clock::now()) << "\n" << ":TOC: right\n" diff --git a/src/lib/export/rss.cpp b/src/lib/export/rss.cpp index 903fa59..6adda04 100644 --- a/src/lib/export/rss.cpp +++ b/src/lib/export/rss.cpp @@ -66,7 +66,7 @@ void Export::RSS::print() const writer.endElement("", "", "description"); writer.startElement("", "", "generator"); - writer.characters(string("remwharead ") + global::version); + writer.characters(string("remwharead ") + version); writer.endElement("", "", "generator"); const string now = DateTimeFormatter::format(DateTime(), diff --git a/src/lib/uri.cpp b/src/lib/uri.cpp index b8d5c46..5a1e0c0 100644 --- a/src/lib/uri.cpp +++ b/src/lib/uri.cpp @@ -187,7 +187,7 @@ string URI::make_request(const string &uri, bool archive) const } HTTPRequest request(method, path, HTTPMessage::HTTP_1_1); - request.set("User-Agent", string("remwharead/") + global::version); + request.set("User-Agent", string("remwharead/") + version); HTTPResponse response; diff --git a/src/version.hpp.in b/src/version.hpp.in index d2a5c45..b4bec2d 100644 --- a/src/version.hpp.in +++ b/src/version.hpp.in @@ -1,9 +1,9 @@ #ifndef VERSION_HPP #define VERSION_HPP -namespace global +namespace remwharead { static constexpr char version[] = "@PROJECT_VERSION@"; -} +} // namespace remwharead #endif // VERSION_HPP From 3a7f4ba8e2ad683b4857dd18fcf19eba741b880d Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 27 Jan 2020 06:18:58 +0100 Subject: [PATCH 3/4] Update .clang-tidy. --- .clang-tidy | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 447c4dd..97007d9 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -5,6 +5,7 @@ Checks: '*, -cppcoreguidelines-non-private-member-variables-in-classes, -fuchsia-default-arguments-calls, + -fuchsia-default-arguments-declarations, -fuchsia-default-arguments, -llvm-include-order, -llvm-header-guard, @@ -25,17 +26,18 @@ Checks: '*, -fuchsia-statically-constructed-objects, -google-readability-todo, -modernize-use-trailing-return-type' -CheckOptions: - { key: readability-identifier-naming.ClassCase, value: CamelCase } +CheckOptions: - { key: readability-identifier-naming.ClassCase, value: CamelCase } # Clashes with constant private member prefix. (const int _var;) - # - { key: readability-identifier-naming.ConstantCase, value: lower_case } - - { key: readability-identifier-naming.EnumCase, value: lower_case } - - { key: readability-identifier-naming.FunctionCase, value: lower_case } - - { key: readability-identifier-naming.MemberCase, value: lower_case } - - { key: readability-identifier-naming.NamespaceCase, value: lower_case } - - { key: readability-identifier-naming.ParameterCase, value: lower_case } - - { key: readability-identifier-naming.PrivateMemberCase, value: lower_case } - - { key: readability-identifier-naming.PrivateMemberPrefix, value: _ } - - { key: readability-identifier-naming.StructCase, value: lower_case } + # - { key: readability-identifier-naming.ConstantCase, value: lower_case } + - { key: readability-identifier-naming.EnumCase, value: lower_case } + - { key: readability-identifier-naming.FunctionCase, value: lower_case } + - { key: readability-identifier-naming.MemberCase, value: lower_case } + - { key: readability-identifier-naming.ParameterCase, value: lower_case } + - { key: readability-identifier-naming.PrivateMemberCase, value: lower_case } + - { key: readability-identifier-naming.PrivateMemberPrefix, value: _ } + - { key: readability-identifier-naming.ProtextedMemberCase, value: lower_case } + - { key: readability-identifier-naming.ProtectedMemberPrefix, value: _ } + - { key: readability-identifier-naming.StructCase, value: lower_case } # Clashes with static private member prefix. (static int _var;) - # - { key: readability-identifier-naming.VariableCase, value: lower_case } + # - { key: readability-identifier-naming.VariableCase, value: lower_case } ... From 8a4f518cb9a05356cd80d11176fc0347607ae388 Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 27 Jan 2020 06:32:28 +0100 Subject: [PATCH 4/4] Fix clang-tidy warnings. --- include/export/adoc.hpp | 12 ++++++------ include/export/csv.hpp | 2 +- include/export/export.hpp | 2 +- include/search.hpp | 4 ++-- include/sqlite.hpp | 2 +- include/uri.hpp | 11 ++++++----- src/cli/parse_options.cpp | 12 ++++++------ src/cli/remwharead_cli.hpp | 2 +- src/lib/export/adoc.cpp | 13 +++++++------ src/lib/export/csv.cpp | 2 +- src/lib/export/export.cpp | 2 +- src/lib/search.cpp | 4 ++-- src/lib/sqlite.cpp | 4 ++-- src/lib/uri.cpp | 8 ++++---- tests/test_search.cpp | 2 -- 15 files changed, 41 insertions(+), 41 deletions(-) diff --git a/include/export/adoc.hpp b/include/export/adoc.hpp index 7d8a984..e68978b 100644 --- a/include/export/adoc.hpp +++ b/include/export/adoc.hpp @@ -47,30 +47,30 @@ private: //! Replace strings in text. [[nodiscard]] - string replace(string text, const replacemap &replacements) const; + static string replace(string text, const replacemap &replacements); //! Replaces characters in tags that asciidoctor doesn't like. [[nodiscard]] - string replace_in_tag(const string &text) const; + static string replace_in_tag(const string &text); //! Replaces characters in title that asciidoctor doesn't like. [[nodiscard]] - string replace_in_title(const string &text) const; + static string replace_in_title(const string &text); //! Replaces characters in URI that asciidoctor doesn't like. [[nodiscard]] - string replace_in_uri(const string &text) const; + static string replace_in_uri(const string &text); //! Print things sorted by tag. void print_tags(const tagmap &tags) const; //! Get ISO-8601 day from Database::entry. [[nodiscard]] - string get_day(const Database::entry &entry) const; + static string get_day(const Database::entry &entry); //! Get ISO-8601 time from Database::entry. [[nodiscard]] - string get_time(const Database::entry &entry) const; + static string get_time(const Database::entry &entry); }; } // namespace remwharead::Export diff --git a/include/export/csv.hpp b/include/export/csv.hpp index 7810185..d65e8a0 100644 --- a/include/export/csv.hpp +++ b/include/export/csv.hpp @@ -41,7 +41,7 @@ namespace remwharead::Export private: //! replaces " with "". [[nodiscard]] - string quote(string field) const; + static string quote(string field); }; } // namespace remwharead::Export diff --git a/include/export/export.hpp b/include/export/export.hpp index b4528ae..3ac85ba 100644 --- a/include/export/export.hpp +++ b/include/export/export.hpp @@ -68,7 +68,7 @@ protected: * @return Sorted list of Database::entry. */ [[nodiscard]] - list sort_entries(list entries) const; + static list sort_entries(list entries); }; } // namespace remwharead::Export diff --git a/include/search.hpp b/include/search.hpp index 3a935b9..7c93d7b 100644 --- a/include/search.hpp +++ b/include/search.hpp @@ -110,7 +110,7 @@ private: * @since 0.7.0 */ [[nodiscard]] - vector> parse_expression(const string &expression) const; + static vector> parse_expression(const string &expression); /*! * @brief Convert str to lowercase. Works with unicode. @@ -118,7 +118,7 @@ private: * @since 0.7.0 */ [[nodiscard]] - inline string to_lowercase(const string &str) const; + static inline string to_lowercase(const string &str); }; } // namespace remwharead diff --git a/include/sqlite.hpp b/include/sqlite.hpp index 5636f73..4a0e909 100644 --- a/include/sqlite.hpp +++ b/include/sqlite.hpp @@ -132,7 +132,7 @@ private: bool _connected; [[nodiscard]] - fs::path get_data_home() const; + static fs::path get_data_home(); }; } // namespace remwharead diff --git a/include/uri.hpp b/include/uri.hpp index d88d128..18946c9 100644 --- a/include/uri.hpp +++ b/include/uri.hpp @@ -151,7 +151,7 @@ protected: * @since 0.6.0 */ [[nodiscard]] - string remove_html_tags(const string &html, const string &tag = "") const; + static string remove_html_tags(const string &html, const string &tag = ""); /*! * @brief Convert HTML entities to UTF-8. @@ -159,7 +159,7 @@ protected: * @since 0.6.0 */ [[nodiscard]] - string unescape_html(string html) const; + static string unescape_html(string html); /*! * @brief Replace newlines with spaces. @@ -167,14 +167,14 @@ protected: * @since 0.6.0 */ [[nodiscard]] - string remove_newlines(string text) const; + static string remove_newlines(string text); /*! * @brief Set proxy server. * * @since 0.8.5 */ - void set_proxy(); + static void set_proxy(); /*! * @brief Limits text to N characters, cuts at space. @@ -182,7 +182,7 @@ protected: * @since 0.8.5 */ [[nodiscard]] - string cut_text(const string &text, uint16_t n_chars) const; + static string cut_text(const string &text, uint16_t n_chars); /*! * @brief Converts string to UTF-8. @@ -204,6 +204,7 @@ protected: * * @since 0.9.2 */ + [[nodiscard]] bool is_html() const; }; } // namespace remwharead diff --git a/src/cli/parse_options.cpp b/src/cli/parse_options.cpp index 2a6690d..9b39c7b 100644 --- a/src/cli/parse_options.cpp +++ b/src/cli/parse_options.cpp @@ -32,12 +32,12 @@ using Poco::Util::OptionCallback; using Poco::Util::HelpFormatter; App::App() - : _exit_requested(false) - , _argument_error(false) - , _format(export_format::undefined) - , _timespan({ time_point(), system_clock::now() }) - , _archive(true) - , _regex(false) + : _exit_requested{false} + , _argument_error{false} + , _format{export_format::undefined} + , _timespan{{time_point(), system_clock::now()}} + , _archive{true} + , _regex{false} {} void App::defineOptions(OptionSet& options) diff --git a/src/cli/remwharead_cli.hpp b/src/cli/remwharead_cli.hpp index 30acecd..fc4024b 100644 --- a/src/cli/remwharead_cli.hpp +++ b/src/cli/remwharead_cli.hpp @@ -44,7 +44,7 @@ protected: void defineOptions(OptionSet& options) override; void handle_options(const string &name, const string &value); void print_help(const string &option); - void print_version(); + static void print_version(); int main(const std::vector &args) override; private: diff --git a/src/lib/export/adoc.cpp b/src/lib/export/adoc.cpp index 9f7d49b..da58425 100644 --- a/src/lib/export/adoc.cpp +++ b/src/lib/export/adoc.cpp @@ -124,7 +124,6 @@ void Export::AsciiDoc::print() const } string Export::AsciiDoc::replace(string text, const replacemap &replacements) - const { for (const std::pair &sr : replacements) { @@ -137,7 +136,7 @@ string Export::AsciiDoc::replace(string text, const replacemap &replacements) } return text; } -string Export::AsciiDoc::replace_in_tag(const string &text) const +string Export::AsciiDoc::replace_in_tag(const string &text) { // TODO(tastytea): Find a better solution. const replacemap replacements = @@ -165,13 +164,13 @@ string Export::AsciiDoc::replace_in_tag(const string &text) const return replace(text, replacements); } -string Export::AsciiDoc::replace_in_title(const string &text) const +string Export::AsciiDoc::replace_in_title(const string &text) { // [ is implicitly escaped if the corresponding ] is. return replace(text, {{ "]", "\\]" }}); } -string Export::AsciiDoc::replace_in_uri(const string &text) const +string Export::AsciiDoc::replace_in_uri(const string &text) { string out; Poco::URI::encode(text, "+", out); @@ -196,7 +195,9 @@ void Export::AsciiDoc::print_tags(const tagmap &tags) const const std::locale loc; const auto &coll = std::use_facet>(loc); return (coll.compare( + // NOLINTNEXTLINE – pointer arithmetic &a.first[0], &a.first[0] + a.first.size(), + // NOLINTNEXTLINE – pointer arithmetic &b.first[0], &b.first[0] + b.first.size()) == -1); }; std::sort(sortedtags.begin(), sortedtags.end(), compare_tags); @@ -235,13 +236,13 @@ void Export::AsciiDoc::print_tags(const tagmap &tags) const _out << endl; } -string Export::AsciiDoc::get_day(const Database::entry &entry) const +string Export::AsciiDoc::get_day(const Database::entry &entry) { const string datetime = timepoint_to_string(entry.datetime); return datetime.substr(0, datetime.find('T')); } -string Export::AsciiDoc::get_time(const Database::entry &entry) const +string Export::AsciiDoc::get_time(const Database::entry &entry) { const string datetime = timepoint_to_string(entry.datetime); return datetime.substr(datetime.find('T') + 1); diff --git a/src/lib/export/csv.cpp b/src/lib/export/csv.cpp index ac3a8d1..ec4c23b 100644 --- a/src/lib/export/csv.cpp +++ b/src/lib/export/csv.cpp @@ -45,7 +45,7 @@ void Export::CSV::print() const } } -string Export::CSV::quote(string field) const +string Export::CSV::quote(string field) { size_t pos = 0; while ((pos = field.find('"', pos)) != std::string::npos) diff --git a/src/lib/export/export.cpp b/src/lib/export/export.cpp index 745a4ef..ce28937 100644 --- a/src/lib/export/export.cpp +++ b/src/lib/export/export.cpp @@ -25,7 +25,7 @@ ExportBase::ExportBase(const list &entries, ostream &out) {} list -ExportBase::sort_entries(list entries) const +ExportBase::sort_entries(list entries) { entries.sort([](const auto &a, const auto &b) { diff --git a/src/lib/search.cpp b/src/lib/search.cpp index d013432..8ce7b96 100644 --- a/src/lib/search.cpp +++ b/src/lib/search.cpp @@ -37,7 +37,7 @@ Search::Search(list entries) :_entries(move(entries)) {} -vector> Search::parse_expression(const string &expression) const +vector> Search::parse_expression(const string &expression) { vector> searchlist; const RegEx re_or("(.+?) (OR|\\|\\|) "); @@ -78,7 +78,7 @@ vector> Search::parse_expression(const string &expression) const return searchlist; } -string Search::to_lowercase(const string &str) const +string Search::to_lowercase(const string &str) { return Poco::UTF8::toLower(str); } diff --git a/src/lib/sqlite.cpp b/src/lib/sqlite.cpp index e842869..408fa6a 100644 --- a/src/lib/sqlite.cpp +++ b/src/lib/sqlite.cpp @@ -33,7 +33,7 @@ using Poco::Data::Statement; using Poco::Environment; Database::Database() - : _connected(false) + : _connected{false} { try { @@ -182,7 +182,7 @@ string Database::tags_to_string(const vector &tags) return strtags; } -fs::path Database::get_data_home() const +fs::path Database::get_data_home() { fs::path path; diff --git a/src/lib/uri.cpp b/src/lib/uri.cpp index 5a1e0c0..517a34d 100644 --- a/src/lib/uri.cpp +++ b/src/lib/uri.cpp @@ -286,7 +286,7 @@ string URI::strip_html() const return unescape_html(out); } -string URI::remove_html_tags(const string &html, const string &tag) const +string URI::remove_html_tags(const string &html, const string &tag) { // NOTE: I did this with regex_replace before, but libstdc++ segfaulted. string out; @@ -324,7 +324,7 @@ string URI::remove_html_tags(const string &html, const string &tag) const return out; } -string URI::unescape_html(string html) const +string URI::unescape_html(string html) { // Used to convert int to utf-8 char. std::wstring_convert, char32_t> u8c; @@ -648,7 +648,7 @@ archive_answer URI::archive() const return { false, "Unknown error.", "" }; } -string URI::remove_newlines(string text) const +string URI::remove_newlines(string text) { size_t posn = 0; while ((posn = text.find('\n', posn)) != std::string::npos) @@ -666,7 +666,7 @@ string URI::remove_newlines(string text) const return text; } -string URI::cut_text(const string &text, const uint16_t n_chars) const +string URI::cut_text(const string &text, const uint16_t n_chars) { if (text.size() > n_chars) { diff --git a/tests/test_search.cpp b/tests/test_search.cpp index 1b39b5c..15dc65a 100644 --- a/tests/test_search.cpp +++ b/tests/test_search.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #include #include "sqlite.hpp" #include "search.hpp" @@ -25,7 +24,6 @@ using namespace remwharead; using std::string; using std::chrono::system_clock; -using std::vector; SCENARIO ("Searching works correctly") {