From fb1437ba5907a9d4609d2c6ebd558608bb3ed99e Mon Sep 17 00:00:00 2001 From: Sofia Martin Date: Tue, 18 Aug 2026 14:17:04 +0800 Subject: [PATCH] fix: return empty string from capitalize on empty input (#337) (#343) * fix: return empty string from capitalize on empty input (#337) Signed-off-by: sofiamartincraft * fix: return empty string from capitalize on empty input (#337) Signed-off-by: sofiamartincraft * fix: return empty string from capitalize on empty input (#337) Signed-off-by: sofiamartincraft --------- Signed-off-by: sofiamartincraft Co-authored-by: sofiamartincraft --- include/inja/renderer.hpp | 6 ++++-- single_include/inja/inja.hpp | 6 ++++-- test/test-functions.cpp | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/inja/renderer.hpp b/include/inja/renderer.hpp index e135158..61b6ff0 100644 --- a/include/inja/renderer.hpp +++ b/include/inja/renderer.hpp @@ -344,8 +344,10 @@ class Renderer : public NodeVisitor { } break; case Op::Capitalize: { auto result = get_arguments<1>(node)[0]->get(); - result[0] = static_cast(::toupper(result[0])); - std::transform(result.begin() + 1, result.end(), result.begin() + 1, [](char c) { return static_cast(::tolower(c)); }); + if (!result.empty()) { + result[0] = static_cast(::toupper(result[0])); + std::transform(result.begin() + 1, result.end(), result.begin() + 1, [](char c) { return static_cast(::tolower(c)); }); + } make_result(std::move(result)); } break; case Op::Default: { diff --git a/single_include/inja/inja.hpp b/single_include/inja/inja.hpp index c7cd104..5bc4a8f 100644 --- a/single_include/inja/inja.hpp +++ b/single_include/inja/inja.hpp @@ -2496,8 +2496,10 @@ class Renderer : public NodeVisitor { } break; case Op::Capitalize: { auto result = get_arguments<1>(node)[0]->get(); - result[0] = static_cast(::toupper(result[0])); - std::transform(result.begin() + 1, result.end(), result.begin() + 1, [](char c) { return static_cast(::tolower(c)); }); + if (!result.empty()) { + result[0] = static_cast(::toupper(result[0])); + std::transform(result.begin() + 1, result.end(), result.begin() + 1, [](char c) { return static_cast(::tolower(c)); }); + } make_result(std::move(result)); } break; case Op::Default: { diff --git a/test/test-functions.cpp b/test/test-functions.cpp index 6ed2df0..4208cfe 100644 --- a/test/test-functions.cpp +++ b/test/test-functions.cpp @@ -65,6 +65,7 @@ TEST_CASE("functions") { SUBCASE("capitalize") { CHECK(env.render("{{ capitalize(name) }}", data) == "Peter"); CHECK(env.render("{{ capitalize(city) }}", data) == "New york"); + CHECK(env.render("{{ capitalize(\"\") }}", data) == ""); } SUBCASE("range") {