diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2014-11-10 23:24:02 +0000 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2014-11-10 23:24:02 +0000 |
| commit | d803333e737ba8b29de90dcaaa7279e91fb8f21a (patch) | |
| tree | 6ce2af953dadf4b0f8e830e639332099162e1c56 /src | |
| parent | 4235ab52492b931b90c7fbf06e800bf746ad037c (diff) | |
Add support for unordered_map options
Diffstat (limited to 'src')
| -rw-r--r-- | src/option_types.hh | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/option_types.hh b/src/option_types.hh index ea3bd49a..f7f372fb 100644 --- a/src/option_types.hh +++ b/src/option_types.hh @@ -8,6 +8,7 @@ #include <tuple> #include <vector> +#include <unordered_map> #include <unordered_set> namespace Kakoune @@ -99,6 +100,38 @@ bool option_add(std::unordered_set<T>& opt, const std::unordered_set<T>& set) return not set.empty(); } +template<typename Key, typename Value> +String option_to_string(const std::unordered_map<Key, Value>& opt) +{ + String res; + for (auto it = begin(opt); it != end(opt); ++it) + { + if (it != begin(opt)) + res += list_separator; + String elem = escape(option_to_string(it->first), '=', '\\') + "=" + + escape(option_to_string(it->second), '=', '\\'); + res += escape(elem, list_separator, '\\'); + } + return res; +} + +template<typename Key, typename Value> +void option_from_string(StringView str, std::unordered_map<Key, Value>& opt) +{ + opt.clear(); + std::vector<String> elems = split(str, list_separator, '\\'); + for (auto& elem: elems) + { + std::vector<String> pair_str = split(elem, '=', '\\'); + if (pair_str.size() != 2) + throw runtime_error("map option expects key=value"); + std::pair<Key, Value> pair; + option_from_string(pair_str[0], pair.first); + option_from_string(pair_str[1], pair.second); + opt.insert(std::move(pair)); + } +} + constexpr Codepoint tuple_separator = ','; template<size_t I, typename... Types> |
