diff --git a/src/value_visitors.h b/src/value_visitors.h index 1d33542d..e60f73f6 100644 --- a/src/value_visitors.h +++ b/src/value_visitors.h @@ -688,6 +688,34 @@ struct BinaryMathOperation : BaseVisitor<> return ProcessStrings(left, nonstd::basic_string_view(rightStr)); } + template + InternalValue operator() (const std::basic_string &left, int64_t right) const + { + return RepeatString(nonstd::basic_string_view(left), right); + } + + template + InternalValue operator() (const nonstd::basic_string_view &left, int64_t right) const + { + return RepeatString(left, right); + } + + template + InternalValue RepeatString(const nonstd::basic_string_view& left, const int64_t right) const + { + using string = std::basic_string; + InternalValue result; + + if(m_oper == jinja2::BinaryExpression::Mul) + { + string str; + for (int i = 0; i < right; ++i) + str.append(left.begin(), left.end()); + result = std::move(str); + } + return result; + } + template InternalValue ProcessStrings(const nonstd::basic_string_view& left, const nonstd::basic_string_view& right) const { diff --git a/test/expressions_test.cpp b/test/expressions_test.cpp index 015f0f2e..23b92d82 100644 --- a/test/expressions_test.cpp +++ b/test/expressions_test.cpp @@ -30,6 +30,11 @@ R"( {{ wstringValue + ' ' + wstringValue }} {{ stringValue + ' ' + stringValue }} {{ 'Hello' ~ " " ~ 123 ~ ' ' ~ 1.234 ~ " " ~ true ~ " " ~ intValue ~ " " ~ false ~ ' ' ~ 'World ' ~ stringValue ~ ' ' ~ wstringValue}} +{{ 'abc' * 0 }} +{{ 'abc' * 1 }} +{{ '123' * intValue }} +{{ stringValue * intValue }} +{{ wstringValue * intValue }} )", //----------- R"( @@ -51,6 +56,11 @@ rain rain rain rain rain rain Hello 123 1.234 true 3 false World rain rain + +abc +123123123 +rainrainrain +rainrainrain )") { params = {