From 7b9f162e7d8bccee2eb47cecb4cf0430b9afef2d Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 22 Jul 2018 15:51:32 +1000 Subject: [PATCH] Opt-in types for quoting of option lists This avoid quoting ints in int-lists for example, as they do not risk containing whitespaces. Fixes #2223 --- src/option.hh | 5 +++++ src/option_types.hh | 24 ++++++++++++++++++++---- src/string_utils.hh | 1 + 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/option.hh b/src/option.hh index 19ca709f..fd2de5c5 100644 --- a/src/option.hh +++ b/src/option.hh @@ -42,6 +42,11 @@ option_add_from_strings(T& opt, ConstArrayView strs) return option_add(opt, strs[0]); } +template +constexpr bool option_needs_quoting(Meta::Type) +{ + return false; +} template struct PrefixedList diff --git a/src/option_types.hh b/src/option_types.hh index 8a782e15..d3fd9364 100644 --- a/src/option_types.hh +++ b/src/option_types.hh @@ -18,6 +18,20 @@ namespace Kakoune { +template +constexpr bool option_needs_quoting(Meta::Type type, Meta::Type... rest) +{ + return option_needs_quoting(type) or option_needs_quoting(rest...); +} + +template +String quote_ifn(String str) +{ + if (option_needs_quoting(Meta::Type{}...)) + return quote(std::move(str)); + return str; +} + template constexpr decltype(T::option_type_name) option_type_name(Meta::Type) { @@ -66,6 +80,7 @@ inline Codepoint option_from_string(Meta::Type, StringView str) return str[0_char]; } constexpr StringView option_type_name(Meta::Type) { return "codepoint"; } +constexpr bool option_needs_quoting(Meta::Type) { return true; } template Vector option_to_strings(const Vector& opt) @@ -76,7 +91,7 @@ Vector option_to_strings(const Vector& opt) template String option_to_string(const Vector& opt) { - return join(opt | transform([](const T& t) { return quote(option_to_string(t)); }), ' ', false); + return join(opt | transform([](const T& t) { return quote_ifn(option_to_string(t)); }), ' ', false); } template @@ -123,9 +138,10 @@ template String option_to_string(const HashMap& opt) { return join(opt | transform([](auto&& item) { - return quote(format("{}={}", - escape(option_to_string(item.key), '=', '\\'), - escape(option_to_string(item.value), '=', '\\'))); + return quote_ifn( + format("{}={}", + escape(option_to_string(item.key), '=', '\\'), + escape(option_to_string(item.value), '=', '\\'))); }), ' ', false); } diff --git a/src/string_utils.hh b/src/string_utils.hh index 558ba62f..0a6c2594 100644 --- a/src/string_utils.hh +++ b/src/string_utils.hh @@ -68,6 +68,7 @@ Optional str_to_int_ifp(StringView str); inline String option_to_string(StringView opt) { return opt.str(); } inline String option_from_string(Meta::Type, StringView str) { return str.str(); } inline bool option_add(String& opt, StringView val) { opt += val; return not val.empty(); } +constexpr bool option_needs_quoting(Meta::Type) { return true; } template struct InplaceString