Skip to content

Commit

Permalink
Implement '~' operator
Browse files Browse the repository at this point in the history
  • Loading branch information
flexferrum committed Jul 5, 2018
1 parent 3a593f5 commit faedda1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 60 deletions.
19 changes: 19 additions & 0 deletions src/expression_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ InternalValue BinaryExpression::Evaluate(RenderContext& context)
break;
}
case jinja2::BinaryExpression::StringConcat:
{
auto leftStr = context.GetRendererCallback()->GetAsTargetString(leftVal);
auto rightStr = context.GetRendererCallback()->GetAsTargetString(rightVal);
TargetString resultStr;
std::string* nleftStr = boost::get<std::string>(&leftStr);
if (nleftStr != nullptr)
{
auto* nrightStr = boost::get<std::string>(&rightStr);
resultStr = *nleftStr + *nrightStr;
}
else
{
auto* wleftStr = boost::get<std::wstring>(&leftStr);
auto* wrightStr = boost::get<std::wstring>(&rightStr);
resultStr = *wleftStr + *wrightStr;
}
result = InternalValue(std::move(resultStr));
break;
}
default:
break;
}
Expand Down
13 changes: 12 additions & 1 deletion src/render_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@

namespace jinja2
{
struct IRendererCallback
{
virtual TargetString GetAsTargetString(const InternalValue& val) = 0;
};

class RenderContext
{
public:
RenderContext(const InternalValueMap& extValues)
RenderContext(const InternalValueMap& extValues, IRendererCallback* rendererCallback)
: m_rendererCallback(rendererCallback)
{
m_externalScope = &extValues;
EnterScope();
Expand Down Expand Up @@ -63,10 +69,15 @@ class RenderContext
{
return *m_currentScope;
}
auto GetRendererCallback()
{
return m_rendererCallback;
}
private:
InternalValueMap* m_currentScope;
const InternalValueMap* m_externalScope;
std::list<InternalValueMap> m_scopes;
IRendererCallback* m_rendererCallback;

};
} // jinja2
Expand Down
15 changes: 14 additions & 1 deletion src/template_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class TemplateImpl : public ITemplateImpl
else
intParams[ip.first] = newParam.get();
}
RenderContext context(intParams);
RendererCallback<CharT> callback;
RenderContext context(intParams, &callback);
OutStream outStream(
[this, &os](size_t offset, size_t length) {
os.write(m_template.data() + offset, length);
Expand All @@ -68,6 +69,18 @@ class TemplateImpl : public ITemplateImpl
}
}

template<typename CharT>
class RendererCallback : public IRendererCallback
{
public:
TargetString GetAsTargetString(const InternalValue& val) override
{
std::basic_ostringstream<CharT> os;
Apply<visitors::ValueRenderer<CharT>>(val, os);
return TargetString(os.str());
}
};

private:

std::basic_string<CharT> m_template;
Expand Down
58 changes: 0 additions & 58 deletions src/value_visitors.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,64 +96,6 @@ struct BaseVisitor : public boost::static_visitor<R>
{
return R();
}
#if 0
template<typename U>
R operator() (const GenericMap&, U&&) const
{
assert(false);
return R();
}

template<typename U>
R operator() (const GenericList&, U&&) const
{
assert(false);
return R();
}

template<typename U>
R operator() (const ValueRef&, U&&) const
{
assert(false);
return R();
}

template<typename U>
R operator() (const TargetString&, U&&) const
{
assert(false);
return R();
}

template<typename T>
R operator() (T&&, const GenericMap&) const
{
assert(false);
return R();
}

template<typename T>
R operator() (T&&, const GenericList&) const
{
assert(false);
return R();
}

template<typename T>
R operator() (T&&, const ValueRef&) const
{
assert(false);
return R();
}

template<typename T>
R operator() (T&&, const TargetString&) const
{
assert(false);
return R();
}
#endif

};


Expand Down
2 changes: 2 additions & 0 deletions test/expressions_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ TEST(ExpressionsTest, BinaryMathOperations)
{{ 10 ** -2 }}
{{ 10/10 + 2*5 }}
{{ 'Hello' + " " + 'World ' + stringValue }}
{{ 'Hello' ~ " " ~ 123 ~ ' ' ~ 1.234 ~ " " ~ true ~ " " ~ intValue ~ " " ~ false ~ ' ' ~ 'World ' ~ stringValue }}
)";

Template tpl;
Expand Down Expand Up @@ -51,6 +52,7 @@ TEST(ExpressionsTest, BinaryMathOperations)
0.01
11
Hello World rain
Hello 123 1.234 true 3 false World rain
)";

EXPECT_STREQ(expectedResult.c_str(), result.c_str());
Expand Down

0 comments on commit faedda1

Please sign in to comment.