Commit c9dbfa577 for llama.cpp
commit c9dbfa5779d9c12d05e88bc7887003e0d168336d
Author: Nakul Lagwal <NLagwal@proton.me>
Date: Tue Dec 1 20:07:29 2026 +0530
jinja : iterate over strings in for loops (#33433)
* jinja : iterate over strings in for loops
* jinja : pre-reserve the string expansion
* jinja : mark byte-based string handling with FIXMEs
* jinja : drop redundant iteration comment
Assisted-by: Hermes Agent
diff --git a/common/jinja/runtime.cpp b/common/jinja/runtime.cpp
index 07d828043..efec2def0 100644
--- a/common/jinja/runtime.cpp
+++ b/common/jinja/runtime.cpp
@@ -556,10 +556,25 @@ value for_statement::execute_impl(context & ctx) const {
iterable_val = mk_val<value_array>();
}
- if (!is_val<value_array>(iterable_val) && !is_val<value_object>(iterable_val)) {
+ if (!is_val<value_array>(iterable_val) && !is_val<value_object>(iterable_val) && !is_val<value_string>(iterable_val)) {
throw std::runtime_error("Expected iterable or object type in for loop: got " + iterable_val->type());
}
+ if (is_val<value_string>(iterable_val)) {
+ // FIXME: byte-based iteration splits UTF-8 codepoints in multibyte strings
+ // iterable_val is rebound below, so hold an explicit reference to the source
+ const value str_owner = iterable_val;
+ const auto * str_val = cast_val<value_string>(str_owner);
+ std::vector<value> chars;
+ chars.reserve(str_val->val_str.length());
+ for (size_t i = 0; i < str_val->val_str.length(); ++i) {
+ if (auto byte = str_val->val_str.byte_at(i)) {
+ chars.push_back(mk_val<value_string>(std::string(1, *byte)));
+ }
+ }
+ iterable_val = mk_val<value_array>(std::move(chars));
+ }
+
std::vector<value> items;
if (is_val<value_object>(iterable_val)) {
JJ_DEBUG("%s", "For loop over object keys");
@@ -970,6 +985,7 @@ value member_expression::execute_impl(context & ctx) const {
val = arr->as_array().at(static_cast<size_t>(index));
}
} else { // value_string
+ // FIXME: byte-based indexing splits UTF-8 codepoints in multibyte strings
const auto * str = cast_val<value_string>(object);
const auto len = str->val_str.length();
if (index < 0) {
diff --git a/common/jinja/value.cpp b/common/jinja/value.cpp
index e7a45ab0b..79d79b2db 100644
--- a/common/jinja/value.cpp
+++ b/common/jinja/value.cpp
@@ -1016,6 +1016,7 @@ const func_builtins & value_string_t::get_builtins() const {
throw raised_exception("slice step cannot be zero");
}
auto input = args.get_pos(0);
+ // FIXME: byte-based slicing splits UTF-8 codepoints in multibyte strings
auto sliced = slice(input->as_string().str(), start, stop, step);
auto res = mk_val<value_string>(sliced);
res->val_str.mark_input_based_on(input->as_string());
diff --git a/tests/test-jinja.cpp b/tests/test-jinja.cpp
index c9220cc0d..b72b00b12 100644
--- a/tests/test-jinja.cpp
+++ b/tests/test-jinja.cpp
@@ -435,6 +435,62 @@ static void test_loops(testing & t) {
"22"
);
+ test_template(t, "for over string",
+ "{% for c in 'abc' %}{{ c }}{% endfor %}",
+ json::object(),
+ "abc"
+ );
+
+ test_template(t, "for over string with loop metadata",
+ "{% for c in 'abc' %}{{ loop.index }}{{ loop.length }}{% endfor %}",
+ json::object(),
+ "132333"
+ );
+
+ test_template(t, "for over empty string",
+ "{% for c in '' %}{{ c }}{% else %}empty{% endfor %}",
+ json::object(),
+ "empty"
+ );
+
+ test_template(t, "for over multipart string",
+ "{% for c in a + 'bc' %}{{ c }}{% endfor %}",
+ {{"a", "a"}},
+ "abc"
+ );
+
+ test_template(t, "for over string: loop.length and loop.last",
+ "{% for c in 'abcd' %}{{ loop.length }}{% if loop.last %}!{% endif %}{% endfor %}",
+ json::object(),
+ "4444!"
+ );
+
+ test_template(t, "for over string: loop.first and revindex",
+ "{% for c in 'abc' %}{{ loop.first }}{{ loop.revindex }}{% endfor %}",
+ json::object(),
+ "True3False2False1"
+ );
+
+ // string elements are bytes, not codepoints: 'é' is 2 bytes in UTF-8,
+ // while python iterates codepoints -- the divergence is expected here
+ test_template(t, "for over multibyte string iterates bytes",
+ "{% for c in 'é' %}{{ loop.length }}{% endfor %}",
+ json::object(),
+ g_python_mode ? "1" : "22"
+ );
+
+ test_template(t, "for over string: nested loops",
+ "{% for a in 'ab' %}{% for b in '12' %}{{ a }}{{ b }}{{ loop.index }}{% endfor %}{% endfor %}",
+ json::object(),
+ "a11a22b11b22"
+ );
+
+ test_template(t, "for over string with filter recomputes length",
+ "{% for c in 'abc' if c != 'b' %}{{ c }}{{ loop.length }}{% endfor %}",
+ json::object(),
+ "a2c2"
+ );
+
test_template(t, "for over dict items",
"{% for k, v in data.items() %}{{ k }}={{ v }} {% endfor %}",
{{"data", {{"x", 1}, {"y", 2}}}},