diff options
author | Alexander Batischev <eual.jp@gmail.com> | 2021-04-23 02:43:40 +0300 |
---|---|---|
committer | Alexander Batischev <eual.jp@gmail.com> | 2021-05-17 15:23:12 +0300 |
commit | 682d5ccd3e7e746220282738a17160dcfd61dbcb (patch) | |
tree | 69e5c8a7df378ad6635d47996ad5524919bc841d | |
parent | e480326ee160f581de8be1339caff2869f40650b (diff) |
Switch KeyMap's storage to Utf8String
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | include/keymap.h | 23 | ||||
-rw-r--r-- | include/utf8string.h | 12 | ||||
-rw-r--r-- | mk/mk.deps | 33 | ||||
-rw-r--r-- | src/keymap.cpp | 69 | ||||
-rw-r--r-- | src/utf8string.cpp | 7 |
6 files changed, 106 insertions, 40 deletions
@@ -50,7 +50,7 @@ CXX_FOR_BUILD?=$(CXX) DEFINES=-DLOCALEDIR='"$(localedir)"' WARNFLAGS=-Werror -Wall -Wextra -Wunreachable-code -INCLUDES=-Iinclude -Istfl -Ifilter -I. -Irss -I$(relative_cargo_target_dir)/cxxbridge/libnewsboat-ffi/src/ +INCLUDES=-Iinclude -Istfl -Ifilter -I. -Irss -I$(relative_cargo_target_dir)/cxxbridge/libnewsboat-ffi/src/ -I$(relative_cargo_target_dir)/cxxbridge/rust/ BARE_CXXFLAGS=-std=c++11 -O2 -ggdb $(INCLUDES) LDFLAGS+=-L. diff --git a/include/keymap.h b/include/keymap.h index 59b3a029..3208a9ff 100644 --- a/include/keymap.h +++ b/include/keymap.h @@ -7,6 +7,7 @@ #include <vector> #include "configactionhandler.h" +#include "utf8string.h" // in configuration: bind-key <key> <operation> @@ -171,14 +172,24 @@ struct MacroCmd { std::vector<std::string> args; }; +struct InternalMacroCmd { + Operation op; + std::vector<Utf8String> args; +}; + struct MacroBinding { std::vector<MacroCmd> cmds; std::string description; }; +struct InternalMacroBinding { + std::vector<InternalMacroCmd> cmds; + Utf8String description; +}; + struct ParsedOperations { - std::vector<MacroCmd> operations; - std::string description; + std::vector<InternalMacroCmd> operations; + Utf8String description; }; class KeyMap : public ConfigActionHandler { @@ -200,8 +211,8 @@ public: const std::string& params) override; void dump_config(std::vector<std::string>& config_output) const override; std::vector<KeyMapDesc> get_keymap_descriptions(std::string context); - const std::map<std::string, MacroBinding>& get_macro_descriptions(); - + // FIXME(utf8): change this back to const std::map<std::string, MacroBinding>& + std::map<std::string, MacroBinding> get_macro_descriptions(); std::vector<MacroCmd> get_startup_operation_sequence(); private: @@ -213,8 +224,8 @@ private: const std::string& command_name, bool allow_description = true); std::map<std::string, std::map<std::string, Operation>> keymap_; - std::map<std::string, MacroBinding> macros_; - std::vector<MacroCmd> startup_operations_sequence; + std::map<Utf8String, InternalMacroBinding> macros_; + std::vector<InternalMacroCmd> startup_operations_sequence; }; } // namespace newsboat diff --git a/include/utf8string.h b/include/utf8string.h index 56a5144c..1ac57b71 100644 --- a/include/utf8string.h +++ b/include/utf8string.h @@ -4,6 +4,12 @@ #include <functional> #include <string> +namespace rust { +inline namespace cxxbridge1 { +class String; +} +} + namespace newsboat { /// A string that's guaranteed to contain valid UTF-8. @@ -11,6 +17,12 @@ class Utf8String { public: Utf8String() = default; + /// Construct an object from a String received from Rust. + /// + /// This performs no validations because Rust strings are known to be + /// represented in UTF-8. + Utf8String(const rust::String&); + /// Construct an object from a string literal, which is assumed to be in /// UTF-8 since Newsboat's source code is in UTF-8. /// @@ -215,7 +215,7 @@ src/controller.o: src/controller.cpp include/controller.h include/cache.h \ src/dialogsformaction.o: src/dialogsformaction.cpp \ include/dialogsformaction.h include/formaction.h include/history.h \ target/cxxbridge/libnewsboat-ffi/src/history.rs.h include/keymap.h \ - include/configactionhandler.h include/stflpp.h include/utf8string.h \ + include/configactionhandler.h include/utf8string.h include/stflpp.h \ include/listwidget.h include/listformatter.h include/regexmanager.h \ include/matcher.h filter/FilterParser.h include/regexowner.h config.h \ include/fmtstrformatter.h include/listformatter.h include/strprintf.h \ @@ -268,7 +268,7 @@ src/downloadthread.o: src/downloadthread.cpp include/downloadthread.h \ src/emptyformaction.o: src/emptyformaction.cpp include/emptyformaction.h \ include/formaction.h include/history.h \ target/cxxbridge/libnewsboat-ffi/src/history.rs.h include/keymap.h \ - include/configactionhandler.h include/stflpp.h include/utf8string.h + include/configactionhandler.h include/utf8string.h include/stflpp.h src/exception.o: src/exception.cpp include/exception.h config.h src/feedcontainer.o: src/feedcontainer.cpp include/feedcontainer.h \ include/configcontainer.h include/configactionhandler.h \ @@ -362,8 +362,8 @@ src/fmtstrformatter.o: src/fmtstrformatter.cpp include/fmtstrformatter.h \ target/cxxbridge/libnewsboat-ffi/src/logger.rs.h include/ruststring.h src/formaction.o: src/formaction.cpp include/formaction.h \ include/history.h target/cxxbridge/libnewsboat-ffi/src/history.rs.h \ - include/keymap.h include/configactionhandler.h include/stflpp.h \ - include/utf8string.h config.h include/configexception.h include/logger.h \ + include/keymap.h include/configactionhandler.h include/utf8string.h \ + include/stflpp.h config.h include/configexception.h include/logger.h \ include/strprintf.h target/cxxbridge/libnewsboat-ffi/src/logger.rs.h \ include/matcherexception.h include/strprintf.h include/utils.h \ 3rd-party/expected.hpp 3rd-party/optional.hpp include/configcontainer.h \ @@ -404,7 +404,7 @@ src/fslock.o: src/fslock.cpp include/fslock.h \ src/helpformaction.o: src/helpformaction.cpp include/helpformaction.h \ include/formaction.h include/history.h \ target/cxxbridge/libnewsboat-ffi/src/history.rs.h include/keymap.h \ - include/configactionhandler.h include/stflpp.h include/utf8string.h \ + include/configactionhandler.h include/utf8string.h include/stflpp.h \ include/textviewwidget.h config.h include/fmtstrformatter.h \ include/keymap.h include/listformatter.h include/regexmanager.h \ include/matcher.h filter/FilterParser.h include/regexowner.h \ @@ -451,7 +451,7 @@ src/itemlistformaction.o: src/itemlistformaction.cpp \ include/itemlistformaction.h 3rd-party/optional.hpp include/history.h \ target/cxxbridge/libnewsboat-ffi/src/history.rs.h \ include/listformaction.h include/formaction.h include/keymap.h \ - include/configactionhandler.h include/stflpp.h include/utf8string.h \ + include/configactionhandler.h include/utf8string.h include/stflpp.h \ include/listwidget.h include/listformatter.h include/regexmanager.h \ include/matcher.h filter/FilterParser.h include/regexowner.h \ include/view.h include/colormanager.h include/configcontainer.h \ @@ -484,7 +484,7 @@ src/itemrenderer.o: src/itemrenderer.cpp include/itemrenderer.h \ src/itemviewformaction.o: src/itemviewformaction.cpp \ include/itemviewformaction.h include/formaction.h include/history.h \ target/cxxbridge/libnewsboat-ffi/src/history.rs.h include/keymap.h \ - include/configactionhandler.h include/stflpp.h include/utf8string.h \ + include/configactionhandler.h include/utf8string.h include/stflpp.h \ include/htmlrenderer.h include/textformatter.h include/regexmanager.h \ include/matcher.h filter/FilterParser.h include/regexowner.h \ include/textviewwidget.h config.h include/confighandlerexception.h \ @@ -509,8 +509,8 @@ src/itemviewformaction.o: src/itemviewformaction.cpp \ include/strprintf.h include/textformatter.h include/utils.h \ include/view.h src/keymap.o: src/keymap.cpp include/keymap.h \ - include/configactionhandler.h config.h include/confighandlerexception.h \ - include/utf8string.h include/configparser.h include/logger.h \ + include/configactionhandler.h include/utf8string.h config.h \ + include/confighandlerexception.h include/configparser.h include/logger.h \ include/strprintf.h target/cxxbridge/libnewsboat-ffi/src/logger.rs.h \ include/strprintf.h include/utils.h 3rd-party/expected.hpp \ 3rd-party/optional.hpp include/configcontainer.h include/logger.h \ @@ -519,7 +519,7 @@ src/keymap.o: src/keymap.cpp include/keymap.h \ src/listformaction.o: src/listformaction.cpp include/listformaction.h \ 3rd-party/optional.hpp include/formaction.h include/history.h \ target/cxxbridge/libnewsboat-ffi/src/history.rs.h include/keymap.h \ - include/configactionhandler.h include/stflpp.h include/utf8string.h \ + include/configactionhandler.h include/utf8string.h include/stflpp.h \ include/listwidget.h include/listformatter.h include/regexmanager.h \ include/matcher.h filter/FilterParser.h include/regexowner.h \ include/rssfeed.h include/matchable.h include/rssitem.h include/utils.h \ @@ -865,7 +865,7 @@ src/urlreader.o: src/urlreader.cpp include/urlreader.h \ src/urlviewformaction.o: src/urlviewformaction.cpp \ include/urlviewformaction.h include/formaction.h include/history.h \ target/cxxbridge/libnewsboat-ffi/src/history.rs.h include/keymap.h \ - include/configactionhandler.h include/stflpp.h include/utf8string.h \ + include/configactionhandler.h include/utf8string.h include/stflpp.h \ include/htmlrenderer.h include/textformatter.h include/listwidget.h \ include/listformatter.h include/regexmanager.h include/matcher.h \ filter/FilterParser.h include/regexowner.h config.h \ @@ -884,8 +884,9 @@ src/urlviewformaction.o: src/urlviewformaction.cpp \ include/dirbrowserformaction.h include/filesystembrowser.h \ include/feedlistformaction.h include/listformaction.h include/view.h \ include/filebrowserformaction.h include/statusline.h -src/utf8string.o: src/utf8string.cpp include/utf8string.h include/utils.h \ - 3rd-party/expected.hpp 3rd-party/optional.hpp include/configcontainer.h \ +src/utf8string.o: src/utf8string.cpp include/utf8string.h \ + target/cxxbridge/rust/cxx.h include/utils.h 3rd-party/expected.hpp \ + 3rd-party/optional.hpp include/configcontainer.h \ include/configactionhandler.h include/utf8string.h include/logger.h \ config.h include/strprintf.h \ target/cxxbridge/libnewsboat-ffi/src/logger.rs.h \ @@ -1014,7 +1015,7 @@ test/itemlistformaction.o: test/itemlistformaction.cpp \ include/itemlistformaction.h 3rd-party/optional.hpp include/history.h \ target/cxxbridge/libnewsboat-ffi/src/history.rs.h \ include/listformaction.h include/formaction.h include/keymap.h \ - include/configactionhandler.h include/stflpp.h include/utf8string.h \ + include/configactionhandler.h include/utf8string.h include/stflpp.h \ include/listwidget.h include/listformatter.h include/regexmanager.h \ include/matcher.h filter/FilterParser.h include/regexowner.h \ include/view.h include/colormanager.h include/configcontainer.h \ @@ -1048,8 +1049,8 @@ test/itemrenderer.o: test/itemrenderer.cpp include/itemrenderer.h \ target/cxxbridge/libnewsboat-ffi/src/utils.rs.h \ test/test-helpers/envvar.h test/keymap.o: test/keymap.cpp include/keymap.h \ - include/configactionhandler.h 3rd-party/catch.hpp \ - include/confighandlerexception.h include/utf8string.h + include/configactionhandler.h include/utf8string.h 3rd-party/catch.hpp \ + include/confighandlerexception.h test/listformatter.o: test/listformatter.cpp include/listformatter.h \ include/regexmanager.h include/configactionhandler.h include/matcher.h \ filter/FilterParser.h include/regexowner.h 3rd-party/catch.hpp diff --git a/src/keymap.cpp b/src/keymap.cpp index af444a29..6f6722f3 100644 --- a/src/keymap.cpp +++ b/src/keymap.cpp @@ -561,9 +561,24 @@ std::vector<KeyMapDesc> KeyMap::get_keymap_descriptions(std::string context) return descs; } -const std::map<std::string, MacroBinding>& KeyMap::get_macro_descriptions() +std::map<std::string, MacroBinding> KeyMap::get_macro_descriptions() { - return macros_; + std::map<std::string, MacroBinding> result; + for (const auto& macro : macros_) { + const auto key = macro.first.to_utf8(); + + MacroBinding value; + value.description = macro.second.description.to_utf8(); + for (const auto& internal_cmd : macro.second.cmds) { + MacroCmd cmd; + cmd.op = internal_cmd.op; + for (const auto& arg : internal_cmd.args) { + cmd.args.push_back(arg.to_utf8()); + } + value.cmds.push_back(cmd); + } + } + return result; } KeyMap::~KeyMap() {} @@ -667,21 +682,21 @@ void KeyMap::dump_config(std::vector<std::string>& config_output) const } for (const auto& macro : macros_) { std::string configline = "macro "; - configline.append(macro.first); + configline.append(macro.first.to_utf8()); configline.append(" "); for (unsigned int i = 0; i < macro.second.cmds.size(); ++i) { const auto& cmd = macro.second.cmds[i]; configline.append(getopname(cmd.op)); for (const auto& arg : cmd.args) { configline.append(" "); - configline.append(utils::quote(arg)); + configline.append(utils::quote(arg.to_utf8())); } if (i < (macro.second.cmds.size() - 1)) { configline.append(" ; "); } } - if (macro.second.description.size() >= 1) { - const auto escaped_string = utils::replace_all(macro.second.description, { + if (!macro.second.description.empty()) { + const auto escaped_string = utils::replace_all(macro.second.description.to_utf8(), { {R"(\)", R"(\\)"}, {R"(")", R"(\")"}, }); @@ -748,14 +763,14 @@ void KeyMap::handle_action(const std::string& action, const std::string& params) std::string remaining_params = params; const auto token = utils::extract_token_quoted(remaining_params); const auto parsed = parse_operation_sequence(remaining_params, action); - const std::vector<MacroCmd> cmds = parsed.operations; - const std::string description = parsed.description; + const std::vector<InternalMacroCmd> cmds = parsed.operations; + const Utf8String description = parsed.description; if (!token.has_value() || cmds.empty()) { throw ConfigHandlerException(ActionHandlerStatus::TOO_FEW_PARAMS); } const std::string macrokey = token.value(); - macros_[macrokey] = {cmds, description}; + macros_[Utf8String::from_utf8(macrokey)] = {cmds, description}; } else if (action == "run-on-startup") { startup_operations_sequence = parse_operation_sequence(params, action, false).operations; } else { @@ -776,7 +791,7 @@ ParsedOperations KeyMap::parse_operation_sequence(const std::string& line, command_name)); } - std::vector<MacroCmd> cmds; + std::vector<InternalMacroCmd> cmds; for (const auto& operation : operations) { const auto& tokens = keymap::bridged::operation_tokens(operation); if (tokens.empty()) { @@ -784,10 +799,12 @@ ParsedOperations KeyMap::parse_operation_sequence(const std::string& line, } const auto command_name = std::string(tokens[0]); - const auto arguments = std::vector<std::string>(std::next(std::begin(tokens)), - std::end(tokens)); + std::vector<Utf8String> arguments; + for (auto it = std::next(std::begin(tokens)); it != std::end(tokens); ++it) { + arguments.push_back(*it); + } - MacroCmd cmd; + InternalMacroCmd cmd; cmd.op = get_opcode(command_name); if (cmd.op == OP_NIL) { throw ConfigHandlerException(strprintf::fmt(_("`%s' is not a valid operation"), @@ -800,13 +817,22 @@ ParsedOperations KeyMap::parse_operation_sequence(const std::string& line, return ParsedOperations{ .operations = cmds, - .description = std::string(description) + .description = Utf8String(description) }; } std::vector<MacroCmd> KeyMap::get_startup_operation_sequence() { - return startup_operations_sequence; + std::vector<MacroCmd> result; + for (const auto& from : startup_operations_sequence) { + MacroCmd to; + to.op = from.op; + for (const auto& from : from.args) { + to.args.push_back(from.to_utf8()); + } + result.push_back(to); + } + return result; } std::vector<std::string> KeyMap::get_keys(Operation op, @@ -823,8 +849,17 @@ std::vector<std::string> KeyMap::get_keys(Operation op, std::vector<MacroCmd> KeyMap::get_macro(const std::string& key) { - if (macros_.count(key) >= 1) { - return macros_.at(key).cmds; + if (macros_.count(Utf8String::from_utf8(key)) >= 1) { + std::vector<MacroCmd> result; + for (const auto& from : macros_.at(Utf8String::from_utf8(key)).cmds) { + MacroCmd to; + to.op = from.op; + for (const auto& from : from.args) { + to.args.push_back(from.to_utf8()); + } + result.push_back(to); + } + return result; } return {}; } diff --git a/src/utf8string.cpp b/src/utf8string.cpp index f363cdb0..0a77e991 100644 --- a/src/utf8string.cpp +++ b/src/utf8string.cpp @@ -3,10 +3,17 @@ #include <stfl.h> #include <wctype.h> +#include "cxx.h" + #include "utils.h" namespace newsboat { +Utf8String::Utf8String(const rust::String& input) +{ + inner = std::string(input); +} + Utf8String::Utf8String(std::string input) { inner = std::move(input); |