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<value_array>();
size_t pos = 0;
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<value_array>();
size_t pos = 0;
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<value_string>(str);
res->val_str.mark_input_based_on(args.get_pos(0)->val_str);
"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"}},