Skip to content

Commit 3dc7857

Browse files
waybeforenowflexferrum
authored andcommitted
adding CapitalMode to StringConverter::Filter (#139)
1 parent 2b26969 commit 3dc7857

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ More detailed examples and features describtion can be found in the documentatio
9595
## Current Jinja2 support
9696
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:
9797
- expressions. You can use almost every style of expressions: simple, filtered, conditional, and so on.
98-
- 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**)
98+
- 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**)
9999
- big number of testers (**eq, defined, ge, gt, iterable, le, lt, mapping, ne, number, sequence, string, undefined, in, even, odd, lower, upper**)
100100
- limited number of functions (**range**, **loop.cycle**)
101101
- 'if' statement (with 'elif' and 'else' branches)

src/string_converter_filter.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,21 @@ InternalValue StringConverter::Filter(const InternalValue& baseVal, RenderContex
303303
case UrlEncodeMode:
304304
result = Apply<UrlStringEncoder>(baseVal);
305305
break;
306+
case CapitalMode:
307+
result = ApplyStringConverter<GenericStringEncoder>(baseVal, [isFirstChar = true, &isAlpha](auto ch, auto&& fn) mutable {
308+
if (isAlpha(ch))
309+
{
310+
if (isFirstChar)
311+
fn(std::toupper(ch, std::locale()));
312+
else
313+
fn(std::tolower(ch, std::locale()));
314+
}
315+
else
316+
fn(ch);
317+
318+
isFirstChar = false;
319+
});
320+
break;
306321
default:
307322
break;
308323
}

test/filters_test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,13 @@ INSTANTIATE_TEST_CASE_P(Truncate, FilterGenericTest, ::testing::Values(
466466
InputOutputPair{"'VeryVeryVeryLongWord' | truncate(16) | pprint", "'VeryVeryVeryLongWord'"},
467467
InputOutputPair{"'foo bar baz qux' | truncate(6, end=' >>', leeway=0) | pprint", "'foo >>'"}
468468
));
469+
470+
INSTANTIATE_TEST_CASE_P(Capitalize, FilterGenericTest, ::testing::Values(
471+
InputOutputPair{"'String' | capitalize | pprint", "'String'"},
472+
InputOutputPair{"'string' | capitalize | pprint", "'String'"},
473+
InputOutputPair{"'1234string' | capitalize | pprint", "'1234string'"},
474+
InputOutputPair{"'1234String' | capitalize | pprint", "'1234string'"},
475+
InputOutputPair{"'Hello World' | capitalize | pprint", "'Hello world'"},
476+
InputOutputPair{"' Hello World' | capitalize | pprint", "' hello world'"},
477+
InputOutputPair{"'Hello123OOO, World!' | capitalize | pprint", "'Hello123ooo, world!'"}
478+
));

0 commit comments

Comments
 (0)