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 #include +#include #include namespace Kakoune @@ -99,6 +100,38 @@ bool option_add(std::unordered_set& opt, const std::unordered_set& set) return not set.empty(); } +template +String option_to_string(const std::unordered_map& 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 +void option_from_string(StringView str, std::unordered_map& opt) +{ + opt.clear(); + std::vector elems = split(str, list_separator, '\\'); + for (auto& elem: elems) + { + std::vector pair_str = split(elem, '=', '\\'); + if (pair_str.size() != 2) + throw runtime_error("map option expects key=value"); + std::pair 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