Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions include/minja/minja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1446,24 +1446,42 @@ struct ArgumentsExpression {
static std::string strip(const std::string & s, const std::string & chars = "", bool left = true, bool right = true) {
auto charset = chars.empty() ? " \t\n\r" : chars;
auto start = left ? s.find_first_not_of(charset) : 0;
if (start == std::string::npos) return "";
if (start == std::string::npos) return "";
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (start == std::string::npos) return "";
if (start == std::string::npos) return "";

auto end = right ? s.find_last_not_of(charset) : s.size() - 1;
return s.substr(start, end - start + 1);
}

static std::vector<std::string> split(const std::string & s, const std::string & sep) {
static std::vector<std::string> split(const std::string & s, const std::string & sep, int maxsplit = -1) {
std::vector<std::string> result;
size_t start = 0;
size_t end = s.find(sep);
while (end != std::string::npos) {
int splits = 0;
while (end != std::string::npos && (maxsplit < 0 || splits < maxsplit)) {
result.push_back(s.substr(start, end - start));
start = end + sep.length();
end = s.find(sep, start);
splits++;
Comment on lines +1458 to +1463
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int splits = 0;
while (end != std::string::npos && (maxsplit < 0 || splits < maxsplit)) {
result.push_back(s.substr(start, end - start));
start = end + sep.length();
end = s.find(sep, start);
splits++;
while (end != std::string::npos && maxsplit != 0) {
result.push_back(s.substr(start, end - start));
start = end + sep.length();
end = s.find(sep, start);
--maxsplit;

}
result.push_back(s.substr(start));
return result;
}

static std::vector<std::string> rsplit(const std::string & s, const std::string & sep, int maxsplit = -1) {
std::vector<std::string> result;
size_t end = s.length();
size_t pos = s.rfind(sep);
int splits = 0;
while (pos != std::string::npos && (maxsplit < 0 || splits < maxsplit)) {
result.insert(result.begin(), s.substr(pos + sep.length(), end - pos - sep.length()));
end = pos;
splits++;
if (pos == 0) break;
pos = s.rfind(sep, pos - 1);
}
result.insert(result.begin(), s.substr(0, end));
Comment on lines +1473 to +1481
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int splits = 0;
while (pos != std::string::npos && (maxsplit < 0 || splits < maxsplit)) {
result.insert(result.begin(), s.substr(pos + sep.length(), end - pos - sep.length()));
end = pos;
splits++;
if (pos == 0) break;
pos = s.rfind(sep, pos - 1);
}
result.insert(result.begin(), s.substr(0, end));
while (pos != std::string::npos && maxsplit != 0) {
result.push_back(s.substr(pos + sep.length(), end - pos - sep.length()));
end = pos;
--maxsplit;
if (pos == 0) break;
pos = s.rfind(sep, pos - 1);
}
result.push_back(s.substr(0, end));
std::reverse(result.begin(), result.end());

return result;
}

static std::string capitalize(const std::string & s) {
if (s.empty()) return s;
auto result = s;
Expand Down Expand Up @@ -1566,9 +1584,20 @@ class MethodCallExpr : public Expression {
auto chars = vargs.args.empty() ? "" : vargs.args[0].get<std::string>();
return Value(strip(str, chars, /* left= */ false, /* right= */ true));
} else if (method->get_name() == "split") {
vargs.expectArgs("split method", {1, 1}, {0, 0});
vargs.expectArgs("split method", {1, 2}, {0, 0});
auto sep = vargs.args[0].get<std::string>();
int maxsplit = vargs.args.size() > 1 ? vargs.args[1].to_int() : -1;
auto parts = split(str, sep, maxsplit);
Value result = Value::array();
for (const auto& part : parts) {
result.push_back(Value(part));
}
return result;
} else if (method->get_name() == "rsplit") {
vargs.expectArgs("rsplit method", {1, 2}, {0, 0});
auto sep = vargs.args[0].get<std::string>();
auto parts = split(str, sep);
int maxsplit = vargs.args.size() > 1 ? vargs.args[1].to_int() : -1;
auto parts = rsplit(str, sep, maxsplit);
Value result = Value::array();
for (const auto& part : parts) {
result.push_back(Value(part));
Expand Down
12 changes: 12 additions & 0 deletions tests/test-syntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ TEST(SyntaxTest, SimpleCases) {
EXPECT_EQ("bcXYZab", render("{{ 'abcXYZabc'.strip('ac') }}", {}, {}));

EXPECT_EQ(R"(["a", "b"])", render("{{ 'a b'.split(' ') | tojson }}", {}, {}));

// Test rsplit (reverse split) with maxsplit parameter
// rsplit splits from right to left, which is crucial for extracting content after the last delimiter
// Used in chat templates like DeepSeek-R1: content.rsplit('</think>', 1)[-1]
EXPECT_EQ(R"(["a-b", "c"])", render("{{ 'a-b-c'.rsplit('-', 1) | tojson }}", {}, {}));
EXPECT_EQ(R"(["a", "b-c"])", render("{{ 'a-b-c'.split('-', 1) | tojson }}", {}, {}));
EXPECT_EQ(R"(["prefix</think>middle", "suffix"])", render("{{ 'prefix</think>middle</think>suffix'.rsplit('</think>', 1) | tojson }}", {}, {}));

// Test rsplit with indexing - extract content after the last delimiter
EXPECT_EQ(" suffix", render("{{ 'prefix</think>middle</think> suffix'.rsplit('</think>', 1)[-1] }}", {}, {}));
EXPECT_EQ(R"(["a", "b", "c"])", render("{{ 'a-b-c'.rsplit('-') | tojson }}", {}, {}));


EXPECT_EQ(
"Ok",
Expand Down