Skip to content

add replace function #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ render("Hello {{ upper(neighbour) }}!", data); // "Hello PETER!"
render("Hello {{ lower(neighbour) }}!", data); // "Hello peter!"
render("Hello {{ capitalize(neighbour) }}!", data); // "Hello Peter!"

// Replace characters in a string
render("{{ replace(neighbour, \"e\", \"3\")}}", data); // "P3t3r"

// Range function, useful for loops
render("{% for i in range(4) %}{{ loop.index1 }}{% endfor %}", data); // "1234"
render("{% for i in range(3) %}{{ at(guests, i) }} {% endfor %}", data); // "Jeff Tom Patrick "
Expand Down
2 changes: 2 additions & 0 deletions include/inja/function_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class FunctionStorage {
Min,
Odd,
Range,
Replace,
Round,
Sort,
Upper,
Expand Down Expand Up @@ -106,6 +107,7 @@ class FunctionStorage {
{std::make_pair("min", 1), FunctionData {Operation::Min}},
{std::make_pair("odd", 1), FunctionData {Operation::Odd}},
{std::make_pair("range", 1), FunctionData {Operation::Range}},
{std::make_pair("replace", 3), FunctionData {Operation::Replace}},
{std::make_pair("round", 2), FunctionData {Operation::Round}},
{std::make_pair("sort", 1), FunctionData {Operation::Sort}},
{std::make_pair("upper", 1), FunctionData {Operation::Upper}},
Expand Down
6 changes: 6 additions & 0 deletions include/inja/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,12 @@ class Renderer : public NodeVisitor {
std::iota(result.begin(), result.end(), 0);
make_result(std::move(result));
} break;
case Op::Replace: {
const auto args = get_arguments<3>(node);
auto result = args[0]->get<std::string>();
replace_substring(result, args[1]->get<std::string>(), args[2]->get<std::string>());
make_result(std::move(result));
} break;
case Op::Round: {
const auto args = get_arguments<2>(node);
const auto precision = args[1]->get<const json::number_integer_t>();
Expand Down
8 changes: 8 additions & 0 deletions single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class FunctionStorage {
Min,
Odd,
Range,
Replace,
Round,
Sort,
Upper,
Expand Down Expand Up @@ -213,6 +214,7 @@ class FunctionStorage {
{std::make_pair("min", 1), FunctionData {Operation::Min}},
{std::make_pair("odd", 1), FunctionData {Operation::Odd}},
{std::make_pair("range", 1), FunctionData {Operation::Range}},
{std::make_pair("replace", 3), FunctionData {Operation::Replace}},
{std::make_pair("round", 2), FunctionData {Operation::Round}},
{std::make_pair("sort", 1), FunctionData {Operation::Sort}},
{std::make_pair("upper", 1), FunctionData {Operation::Upper}},
Expand Down Expand Up @@ -2521,6 +2523,12 @@ class Renderer : public NodeVisitor {
std::iota(result.begin(), result.end(), 0);
make_result(std::move(result));
} break;
case Op::Replace: {
const auto args = get_arguments<3>(node);
auto result = args[0]->get<std::string>();
replace_substring(result, args[1]->get<std::string>(), args[2]->get<std::string>());
make_result(std::move(result));
} break;
case Op::Round: {
const auto args = get_arguments<2>(node);
const auto precision = args[1]->get<const json::number_integer_t>();
Expand Down
5 changes: 5 additions & 0 deletions test/test-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ TEST_CASE("functions") {
// [json.exception.type_error.302] type must be array, but is number" );
}

SUBCASE("replace") {
CHECK(env.render("{{ replace(name, \"e\", \"3\") }}", data) == "P3t3r");
CHECK(env.render("{{ replace(city, \" \", \"_\") }}", data) == "New_York");
}

SUBCASE("round") {
CHECK(env.render("{{ round(4, 0) }}", data) == "4");
CHECK(env.render("{{ round(temperature, 2) }}", data) == "25.68");
Expand Down