10000 Implement 'random', 'trim', 'title', 'upper', 'lower' and 'wordcount' filters by flexferrum · Pull Request #18 · jinja2cpp/Jinja2Cpp · GitHub
[go: up one dir, main page]

Skip to content

Implement 'random', 'trim', 'title', 'upper', 'lower' and 'wordcount' filters #18

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 7 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Implement 'title', 'upper', 'lower' and 'wordcount' filters
  • Loading branch information
flexferrum committed May 30, 2018
commit 7c12a62dd0253ed54350f91300995669816fd390
1 change: 1 addition & 0 deletions src/filters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ std::unordered_map<std::string, ExpressionFilter::FilterFactoryFn> s_filters = {
{"last", FilterFactory<filters::SequenceAccessor>::MakeCreator(filters::SequenceAccessor::LastItemMode)},
{"length", FilterFactory<filters::SequenceAccessor>::MakeCreator(filters::SequenceAccessor::LengthMode)},
{"list", FilterFactory<filters::ValueConverter>::MakeCreator(filters::ValueConverter::ToListMode)},
{"lower", FilterFactory<filters::StringConverter>::MakeCreator(filters::StringConverter::LowerMode)},
{"map", &FilterFactory<filters::Map>::Create},
{"max", FilterFactory<filters::SequenceAccessor>::MakeCreator(filters::SequenceAccessor::MaxItemMode)},
{"min", FilterFactory<filters::SequenceAccessor>::MakeCreator(filters::SequenceAccessor::MinItemMode)},
Expand Down
1 change: 1 addition & 0 deletions src/filters.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class StringConverter : public FilterBase
CamelMode,
EscapeCppMode,
EscapeHtmlMode,
LowerMode,
ReplaceMode,
TitleMode,
TrimMode,
Expand Down
69 changes: 65 additions & 4 deletions src/string_converter_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct StringEncoder : public visitors::BaseVisitor<>

for (auto& ch : str)
{
D::EncodeChar(ch, [&result](auto ... chs) {AppendChar(result, chs...);});
static_cast<const D*>(this)->EncodeChar(ch, [&result](auto ... chs) {AppendChar(result, chs...);});
}

return result;
Expand All @@ -46,13 +46,26 @@ struct StringEncoder : public visitors::BaseVisitor<>
str.push_back(static_cast<typename Str::value_type>(ch));
AppendChar(str, chs...);
}
};

template<typename Fn>
struct GenericStringEncoder : public StringEncoder<GenericStringEncoder<Fn>>
{
GenericStringEncoder(Fn fn) : m_fn(std::move(fn)) {}

template<typename CharT, typename AppendFn>
void EncodeChar(CharT ch, AppendFn&& fn) const
{
m_fn(ch, std::forward<AppendFn>(fn));
}

mutable Fn m_fn;
};

struct UrlStringEncoder : public StringEncoder<UrlStringEncoder>
{
template<typename CharT, typename Fn>
static void EncodeChar(CharT ch, Fn&& fn)
void EncodeChar(CharT ch, Fn&& fn) const
{
switch (ch)
{
Expand Down Expand Up @@ -145,12 +158,13 @@ struct StringConverterImpl : public visitors::BaseVisitor<>
const Fn& m_fn;
};

template<typename Fn>
template<template<typename> class Cvt = StringConverterImpl, typename Fn>
auto ApplyConverter(const InternalValue& str, Fn&& fn)
{
return Apply<StringConverterImpl<Fn>>(str, std::forward<Fn>(fn));
return Apply<Cvt<Fn>>(str, std::forward<Fn>(fn));
}


StringConverter::StringConverter(FilterParams params, StringConverter::Mode mode)
: m_mode(mode)
{
Expand All @@ -165,6 +179,9 @@ StringConverter::StringConverter(FilterParams params, StringConverter::Mode mode
InternalValue StringConverter::Filter(const InternalValue& baseVal, RenderContext& context)
{
InternalValue result;

auto isAlpha = ba::is_alpha();
auto isAlNum = ba::is_alnum();

switch (m_mode)
{
Expand All @@ -174,6 +191,50 @@ InternalValue StringConverter::Filter(const InternalValue& baseVal, RenderContex
return str;
});
break;
case TitleMode:
result = ApplyConverter<GenericStringEncoder>(baseVal, [isDelim = true, &isAlpha, &isAlNum](auto ch, auto&& fn) mutable {
if (isDelim && isAlpha(ch))
{
isDelim = false;
fn(std::toupper(ch, std::locale()));
return;
}

isDelim = !isAlNum(ch);
fn(ch);
});
break;
case WordCountMode:
{
int64_t wc = 0;
ApplyConverter<GenericStringEncoder>(baseVal, [isDelim = true, &wc, &isAlpha, &isAlNum](auto ch, auto&& fn) mutable {
if (isDelim && isAlNum(ch))
{
isDelim = false;
wc ++;
return;
}
isDelim = !isAlNum(ch);
});
result = wc;
break;
}
case UpperMode:
result = ApplyConverter<GenericStringEncoder>(baseVal, [&isAlpha](auto ch, auto&& fn) mutable {
if (isAlpha(ch))
fn(std::toupper(ch, std::locale()));
else
fn(ch);
});
break;
case LowerMode:
result = ApplyConverter<GenericStringEncoder>(baseVal, [&isAlpha](auto ch, auto&& fn) mutable {
if (isAlpha(ch))
fn(std::tolower(ch, std::locale()));
else
fn(ch);
});
break;
case ReplaceMode:
break;
case UrlEncodeMode:
Expand Down
34 changes: 34 additions & 0 deletions test/filters_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,37 @@ INSTANTIATE_TEST_CASE_P(Trim, FilterGenericTest, ::testing::Values(
InputOutputPair{"' string ' | trim | pprint", "'string'"}/*,
InputOutputPair{"wstringValue | trim", "'hello world'"}*/
));

INSTANTIATE_TEST_CASE_P(Title, FilterGenericTest, ::testing::Values(
InputOutputPair{"'string' | title | pprint", "'String'"},
InputOutputPair{"'1234string' | title | pprint", "'1234string'"},
InputOutputPair{"'hello world' | title | pprint", "'Hello World'"},
InputOutputPair{"'hello123ooo, world!' | title | pprint", "'Hello123ooo, World!'"}/*,
InputOutputPair{"wstringValue | trim", "'hello world'"}*/
));

INSTANTIATE_TEST_CASE_P(Upper, FilterGenericTest, ::testing::Values(
InputOutputPair{"'string' | upper | pprint", "'STRING'"},
InputOutputPair{"'1234string' | upper | pprint", "'1234STRING'"},
InputOutputPair{"'hello world' | upper | pprint", "'HELLO WORLD'"},
InputOutputPair{"'hello123ooo, world!' | upper | pprint", "'HELLO123OOO, WORLD!'"}/*,
InputOutputPair{"wstringValue | trim", "'hello world'"}*/
));


INSTANTIATE_TEST_CASE_P(Lower, FilterGenericTest, ::testing::Values(
InputOutputPair{"'String' | lower | pprint", "'string'"},
InputOutputPair{"'1234String' | lower | pprint", "'1234string'"},
InputOutputPair{"'Hello World' | lower | pprint", "'hello world'"},
InputOutputPair{"'Hello123OOO, World!' | lower | pprint", "'hello123ooo, world!'"}/*,
InputOutputPair{"wstringValue | trim", "'hello world'"}*/
));


INSTANTIATE_TEST_CASE_P(WordCount, FilterGenericTest, ::testing::Values(
InputOutputPair{"'string' | wordcount", "1"},
InputOutputPair{"'1234string' | wordcount", "1"},
InputOutputPair{"'hello world' | wordcount", "2"},
InputOutputPair{"'hello123ooo, world!' | wordcount", "2"}/*,
InputOutputPair{"wstringValue | trim", "'hello world'"}*/
));
0