Add support for unordered_map options

This commit is contained in:
Maxime Coste 2014-11-10 23:24:02 +00:00
parent 4235ab5249
commit d803333e73

View File

@ -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>