From 8bd3395d4db71a8d283c455c87143832da1a6ae3 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 13 Feb 2016 12:59:27 +0000 Subject: [PATCH] Do not allow / in highlighter names as it is used for hierachies / are replaced with in the highlighter names. Fixes #553 --- src/highlighter_group.cc | 2 ++ src/string.cc | 18 ++++++++++++++++++ src/string.hh | 2 ++ 3 files changed, 22 insertions(+) diff --git a/src/highlighter_group.cc b/src/highlighter_group.cc index 7e76264a..d3755734 100644 --- a/src/highlighter_group.cc +++ b/src/highlighter_group.cc @@ -14,6 +14,8 @@ void HighlighterGroup::highlight(const Context& context, HighlightFlags flags, void HighlighterGroup::add_child(HighlighterAndId&& hl) { + hl.first = replace(hl.first, "/", ""); + if (m_highlighters.contains(hl.first)) throw runtime_error(format("duplicate id: '{}'", hl.first)); diff --git a/src/string.cc b/src/string.cc index 2734042b..2e09be2e 100644 --- a/src/string.cc +++ b/src/string.cc @@ -241,6 +241,22 @@ String indent(StringView str, StringView indent) return res; } +String replace(StringView str, StringView substr, StringView replacement) +{ + String res; + for (auto it = str.begin(); it != str.end(); ) + { + auto match = std::search(it, str.end(), substr.begin(), substr.end()); + res += StringView{it, match}; + if (match == str.end()) + break; + + res += replacement; + it = match + (int)substr.length(); + } + return res; +} + Optional str_to_int_ifp(StringView str) { unsigned int res = 0; @@ -503,6 +519,8 @@ UnitTest test_string{[]() kak_assert(str_to_int(to_string(INT_MIN)) == INT_MIN); kak_assert(str_to_int("00") == 0); kak_assert(str_to_int("-0") == 0); + + kak_assert(replace("tchou/tcha/tchi", "/", "!!") == "tchou!!tcha!!tchi"); }}; } diff --git a/src/string.hh b/src/string.hh index ab83b56a..c4bd86a4 100644 --- a/src/string.hh +++ b/src/string.hh @@ -280,6 +280,8 @@ String unescape(StringView str, StringView characters, char escape); String indent(StringView str, StringView indent = " "); +String replace(StringView str, StringView substr, StringView replacement); + template String join(const Container& container, char joiner, bool esc_joiner = true) {