From 70250fc1e344e50b4af96e0963e22f18a1978eb5 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 12 Dec 2015 11:26:34 +0000 Subject: [PATCH] Pass directly the StringView to option_add and let it do the option parsing --- src/option_manager.hh | 4 +--- src/option_types.hh | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/option_manager.hh b/src/option_manager.hh index d668fddac..8e21808cf 100644 --- a/src/option_manager.hh +++ b/src/option_manager.hh @@ -135,9 +135,7 @@ public: } void add_from_string(StringView str) override { - T val; - option_from_string(str, val); - if (option_add(m_value, val)) + if (option_add(m_value, str)) m_manager.on_option_changed(*this); } diff --git a/src/option_types.hh b/src/option_types.hh index 5f702f9d7..12f5e4fd3 100644 --- a/src/option_types.hh +++ b/src/option_types.hh @@ -18,11 +18,16 @@ namespace Kakoune inline String option_to_string(StringView opt) { return opt.str(); } inline void option_from_string(StringView str, String& opt) { opt = str.str(); } -inline bool option_add(String& opt, const String& val) { opt += val; return not val.empty(); } +inline bool option_add(String& opt, StringView val) { opt += val; return not val.empty(); } inline String option_to_string(int opt) { return to_string(opt); } inline void option_from_string(StringView str, int& opt) { opt = str_to_int(str); } -inline bool option_add(int& opt, int val) { opt += val; return val != 0; } +inline bool option_add(int& opt, StringView str) +{ + auto val = str_to_int(str); + opt += val; + return val != 0; +} inline String option_to_string(bool opt) { return opt ? "true" : "false"; } inline void option_from_string(StringView str, bool& opt) @@ -64,9 +69,13 @@ void option_from_string(StringView str, Vector& opt) } template -bool option_add(Vector& opt, const Vector& vec) +bool option_add(Vector& opt, StringView str) { - std::copy(vec.begin(), vec.end(), back_inserter(opt)); + Vector vec; + option_from_string(str, vec); + std::copy(std::make_move_iterator(vec.begin()), + std::make_move_iterator(vec.end()), + back_inserter(opt)); return not vec.empty(); } @@ -165,14 +174,15 @@ inline void option_from_string(StringView str, StronglyTypedNumber inline bool option_add(StronglyTypedNumber& opt, - StronglyTypedNumber val) + StringView str) { + int val = str_to_int(str); opt += val; return val != 0; } template -bool option_add(T&, const T&) +bool option_add(T&, StringView str) { throw runtime_error("no add operation supported for this option type"); }