From f0f2b1c38336090746a7a2085a8aa8a1bd592273 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 16 Jun 2017 10:48:14 +0100 Subject: [PATCH] Trim whitespaces surrounding docstrings Closes #1439 --- src/commands.cc | 6 +++--- src/string.cc | 11 +++++++++++ src/string.hh | 2 ++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index 2740d1310..ec615515b 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -1015,7 +1015,7 @@ void define_command(const ParametersParser& parser, Context& context, const Shel }; } - auto docstring = parser.get_switch("docstring").value_or(StringView{}); + auto docstring = trim_whitespaces(parser.get_switch("docstring").value_or(StringView{})); cm.register_command(cmd_name, cmd, docstring.str(), desc, flags, CommandHelper{}, completer); } @@ -1351,7 +1351,7 @@ const CommandDesc declare_option_cmd = { if (parser.get_switch("hidden")) flags = OptionFlags::Hidden; - auto docstring = parser.get_switch("docstring").value_or(StringView{}).str(); + auto docstring = trim_whitespaces(parser.get_switch("docstring").value_or(StringView{})).str(); OptionsRegistry& reg = GlobalScope::instance().option_registry(); @@ -1443,7 +1443,7 @@ const CommandDesc map_key_cmd = { KeyList mapping = parse_keys(parser[3]); keymaps.map_key(key[0], keymap_mode, std::move(mapping), - parser.get_switch("docstring").value_or("").str()); + trim_whitespaces(parser.get_switch("docstring").value_or("")).str()); } }; diff --git a/src/string.cc b/src/string.cc index 18ae5d806..6fb640985 100644 --- a/src/string.cc +++ b/src/string.cc @@ -207,6 +207,17 @@ Vector split(StringView str, char separator) return res; } +StringView trim_whitespaces(StringView str) +{ + auto beg = str.begin(), end = str.end(); + while (beg != end and is_blank(*beg)) + ++beg; + while (beg != end and is_blank(*(end-1))) + --end; + return {beg, end}; +} + + String escape(StringView str, StringView characters, char escape) { String res; diff --git a/src/string.hh b/src/string.hh index 056db814a..bfafa482e 100644 --- a/src/string.hh +++ b/src/string.hh @@ -333,6 +333,8 @@ inline StringView operator"" _sv(const char* str, size_t) Vector split(StringView str, char separator, char escape); Vector split(StringView str, char separator); +StringView trim_whitespaces(StringView str); + String escape(StringView str, StringView characters, char escape); String unescape(StringView str, StringView characters, char escape);