Skip to content

add CapitalMode to StringConverter::Filter #139

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
Sep 26, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ More detailed examples and features describtion can be found in the documentatio
## Current Jinja2 support
Currently, Jinja2Cpp supports the limited number of Jinja2 features. By the way, Jinja2Cpp is planned to be full [jinja2 specification](http://jinja.pocoo.org/docs/2.10/templates/)-conformant. The current support is limited to:
- expressions. You can use almost every style of expressions: simple, filtered, conditional, and so on.
- big number of filters (**sort, default, first, last, length, max, min, reverse, unique, sum, attr, map, reject, rejectattr, select, selectattr, pprint, dictsort, abs, float, int, list, round, random, trim, title, upper, wordcount, replace, truncate, groupby, urlencode**)
- big number of filters (**sort, default, first, last, length, max, min, reverse, unique, sum, attr, map, reject, rejectattr, select, selectattr, pprint, dictsort, abs, float, int, list, round, random, trim, title, upper, wordcount, replace, truncate, groupby, urlencode, capitalize**)
- big number of testers (**eq, defined, ge, gt, iterable, le, lt, mapping, ne, number, sequence, string, undefined, in, even, odd, lower, upper**)
- limited number of functions (**range**, **loop.cycle**)
- 'if' statement (with 'elif' and 'else' branches)
Expand Down
15 changes: 15 additions & 0 deletions src/string_converter_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,21 @@ InternalValue StringConverter::Filter(const InternalValue& baseVal, RenderContex
case UrlEncodeMode:
result = Apply<UrlStringEncoder>(baseVal);
break;
case CapitalMode:
result = ApplyStringConverter<GenericStringEncoder>(baseVal, [isFirstChar = true, &isAlpha](auto ch, auto&& fn) mutable {
if (isAlpha(ch))
{
if (isFirstChar)
fn(std::toupper(ch, std::locale()));
else
fn(std::tolower(ch, std::locale()));
}
else
fn(ch);

isFirstChar = false;
});
break;
default:
break;
}
Expand Down
10 changes: 10 additions & 0 deletions test/filters_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,13 @@ INSTANTIATE_TEST_CASE_P(Truncate, FilterGenericTest, ::testing::Values(
InputOutputPair{"'VeryVeryVeryLongWord' | truncate(16) | pprint", "'VeryVeryVeryLongWord'"},
InputOutputPair{"'foo bar baz qux' | truncate(6, end=' >>', leeway=0) | pprint", "'foo >>'"}
));

INSTANTIATE_TEST_CASE_P(Capitalize, FilterGenericTest, ::testing::Values(
InputOutputPair{"'String' | capitalize | pprint", "'String'"},
InputOutputPair{"'string' | capitalize | pprint", "'String'"},
InputOutputPair{"'1234string' | capitalize | pprint", "'1234string'"},
InputOutputPair{"'1234String' | capitalize | pprint", "'1234string'"},
InputOutputPair{"'Hello World' | capitalize | pprint", "'Hello world'"},
InputOutputPair{"' Hello World' | capitalize | pprint", "' hello world'"},
InputOutputPair{"'Hello123OOO, World!' | capitalize | pprint", "'Hello123ooo, world!'"}
));