Commit 89bfabaf8 for llama.cpp
commit 89bfabaf81f51561b1e351b87dce553cadce96be
Author: Nakul Lagwal <NLagwal@proton.me>
Date: Mon Nov 30 15:51:03 2026 +0530
jinja : fix replace filter with count=0 (#33319)
[no ci]
diff --git a/common/jinja/value.cpp b/common/jinja/value.cpp
index 9692d7cab..330059726 100644
--- a/common/jinja/value.cpp
+++ b/common/jinja/value.cpp
@@ -883,7 +883,7 @@ const func_builtins & value_string_t::get_builtins() const {
if (count > 0) {
throw not_implemented_exception("String replace with count argument not implemented");
}
- if (old_str != new_str) {
+ if (count != 0 && old_str != new_str) {
size_t pos = 0;
if (old_str.empty()) {
std::string new_res;
diff --git a/tests/test-jinja.cpp b/tests/test-jinja.cpp
index 624542838..e86966c41 100644
--- a/tests/test-jinja.cpp
+++ b/tests/test-jinja.cpp
@@ -931,6 +931,24 @@ static void test_filters(testing & t) {
"hello jinja"
);
+ test_template(t, "replace count 0 is a no-op",
+ "{{ 'hello world'|replace('world', 'llama', 0) }}",
+ json::object(),
+ "hello world"
+ );
+
+ test_template(t, "replace count 0 with empty old",
+ "{{ 'abc'|replace('', '-', 0) }}",
+ json::object(),
+ "abc"
+ );
+
+ test_template(t, "replace count negative replaces all",
+ "{{ 'banana'|replace('a', 'X', -1) }}",
+ json::object(),
+ "bXnXnX"
+ );
+
test_template(t, "length (count alias) list",
"{{ items|count }}",
{{"items", json::array({1, 2, 3})}},
@@ -1954,6 +1972,12 @@ static void test_string_methods(testing & t) {
"bXnXna"
);
+ test_template(t, "string.replace() count 0 is a no-op",
+ "{{ 'hello world'.replace('world', 'llama', 0) }}",
+ json::object(),
+ "hello world"
+ );
+
test_template(t, "string.format() auto numbering",
"{{ '<{}|{}>'.format(s, 42) }}",
{{"s", "hello"}},