Skip to content

Repeat string #162

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

Merged
merged 1 commit into from
Oct 9, 2019
Merged
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
28 changes: 28 additions & 0 deletions src/value_visitors.h
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,34 @@ struct BinaryMathOperation : BaseVisitor<>
return ProcessStrings(left, nonstd::basic_string_view<CharT1>(rightStr));
}

template<typename CharT>
InternalValue operator() (const std::basic_string<CharT> &left, int64_t right) const
{
return RepeatString(nonstd::basic_string_view<CharT>(left), right);
}

template<typename CharT>
InternalValue operator() (const nonstd::basic_string_view<CharT> &left, int64_t right) const
{
return RepeatString(left, right);
}

template<typename CharT>
InternalValue RepeatString(const nonstd::basic_string_view<CharT>& left, const int64_t right) const
{
using string = std::basic_string<CharT>;
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<typename CharT>
InternalValue ProcessStrings(const nonstd::basic_string_view<CharT>& left, const nonstd::basic_string_view<CharT>& right) const
{
Expand Down
10 changes: 10 additions & 0 deletions test/expressions_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"(
Expand All @@ -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 = {
Expand Down