From 341babcf73f198c78aaa39e4b6ab1a84facb01e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sigbj=C3=B8rn=20Skj=C3=A6ret?= Date: Sat, 13 Jun 2026 16:56:59 +0200 Subject: [PATCH] jinja : fix split and replace with empty first arg (#24574) * fix split and replace with empty first arg * fix reserve size --- common/jinja/value.cpp | 27 +++++++++++++++++++++++---- tests/test-jinja.cpp | 6 ++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/common/jinja/value.cpp b/common/jinja/value.cpp index 0b79098cd1..72a12f665c 100644 --- a/common/jinja/value.cpp +++ b/common/jinja/value.cpp @@ -673,6 +673,9 @@ const func_builtins & value_string_t::get_builtins() const { std::string str = val_input->as_string().str(); // FIXME: Support non-specified delimiter (split on consecutive (no leading or trailing) whitespace) std::string delim = (args.count() > 1) ? args.get_pos(1)->as_string().str() : " "; + if (delim.empty()) { + throw raised_exception("empty separator"); + } int64_t maxsplit = (args.count() > 2) ? args.get_pos(2)->as_int() : -1; auto result = mk_val(); size_t pos = 0; @@ -697,6 +700,9 @@ const func_builtins & value_string_t::get_builtins() const { std::string str = val_input->as_string().str(); // FIXME: Support non-specified delimiter (split on consecutive (no leading or trailing) whitespace) std::string delim = (args.count() > 1) ? args.get_pos(1)->as_string().str() : " "; + if (delim.empty()) { + throw raised_exception("empty separator"); + } int64_t maxsplit = (args.count() > 2) ? args.get_pos(2)->as_int() : -1; auto result = mk_val(); size_t pos = 0; @@ -722,10 +728,23 @@ const func_builtins & value_string_t::get_builtins() const { if (count > 0) { throw not_implemented_exception("String replace with count argument not implemented"); } - size_t pos = 0; - while ((pos = str.find(old_str, pos)) != std::string::npos) { - str.replace(pos, old_str.length(), new_str); - pos += new_str.length(); + if (old_str != new_str) { + size_t pos = 0; + if (old_str.empty()) { + std::string new_res; + new_res.reserve(str.length() + new_str.length() * (str.length() + 1)); + new_res += new_str; + for (const char c : str) { + new_res.push_back(c); + new_res += new_str; + } + str = new_res; + } else { + while ((pos = str.find(old_str, pos)) != std::string::npos) { + str.replace(pos, old_str.length(), new_str); + pos += new_str.length(); + } + } } auto res = mk_val(str); res->val_str.mark_input_based_on(args.get_pos(0)->val_str); diff --git a/tests/test-jinja.cpp b/tests/test-jinja.cpp index b5ee53461e..21250d04c6 100644 --- a/tests/test-jinja.cpp +++ b/tests/test-jinja.cpp @@ -1320,6 +1320,12 @@ static void test_string_methods(testing & t) { "hello jinja" ); + test_template(t, "string.replace() empty", + "{{ s.replace('', '.') }}", + {{"s", "hello world"}}, + ".h.e.l.l.o. .w.o.r.l.d." + ); + test_template(t, "string.replace() with count", "{{ s.replace('a', 'X', 2) }}", {{"s", "banana"}},