Skip to content

Implement 'even', 'odd', 'lower' and 'upper' testers #25

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 12 commits into from
Jul 3, 2018
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
54 changes: 7 additions & 47 deletions src/string_converter_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,46 +143,6 @@ struct UrlStringEncoder : public StringEncoder<UrlStringEncoder>
}
};

template<typename Fn>
struct StringConverterImpl : public visitors::BaseVisitor<>
{
using BaseVisitor::operator ();

StringConverterImpl(const Fn& fn) : m_fn(fn) {}

template<typename CharT>
InternalValue operator()(const std::basic_string<CharT>& str) const
{
return m_fn(str);
}

const Fn& m_fn;
};

template<typename CharT>
struct SameStringGetter : public visitors::BaseVisitor<std::basic_string<CharT>>
{
using ResultString = std::basic_string<CharT>;
using visitors::BaseVisitor<ResultString>::operator ();

ResultString operator()(const ResultString& str) const
{
return str;
}
};

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

template<typename CharT>
auto GetAsSameString(const std::basic_string<CharT>& s, const InternalValue& val)
{
return Apply<SameStringGetter<CharT>>(val);
}

StringConverter::StringConverter(FilterParams params, StringConverter::Mode mode)
: m_mode(mode)
{
Expand All @@ -207,13 +167,13 @@ InternalValue StringConverter::Filter(const InternalValue& baseVal, RenderContex
switch (m_mode)
{
case TrimMode:
result = ApplyConverter(baseVal, [](auto str) {
result = ApplyStringConverter(baseVal, [](auto str) -> InternalValue {
ba::trim_all(str);
return str;
});
break;
case TitleMode:
result = ApplyConverter<GenericStringEncoder>(baseVal, [isDelim = true, &isAlpha, &isAlNum](auto ch, auto&& fn) mutable {
result = ApplyStringConverter<GenericStringEncoder>(baseVal, [isDelim = true, &isAlpha, &isAlNum](auto ch, auto&& fn) mutable {
if (isDelim && isAlpha(ch))
{
isDelim = false;
Expand All @@ -228,7 +188,7 @@ InternalValue StringConverter::Filter(const InternalValue& baseVal, RenderContex
case WordCountMode:
{
int64_t wc = 0;
ApplyConverter<GenericStringEncoder>(baseVal, [isDelim = true, &wc, &isAlpha, &isAlNum](auto ch, auto&& fn) mutable {
ApplyStringConverter<GenericStringEncoder>(baseVal, [isDelim = true, &wc, &isAlpha, &isAlNum](auto ch, auto&& fn) mutable {
if (isDelim && isAlNum(ch))
{
isDelim = false;
Expand All @@ -241,23 +201,23 @@ InternalValue StringConverter::Filter(const InternalValue& baseVal, RenderContex
break;
}
case UpperMode:
result = ApplyConverter<GenericStringEncoder>(baseVal, [&isAlpha](auto ch, auto&& fn) mutable {
result = ApplyStringConverter<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 {
result = ApplyStringConverter<GenericStringEncoder>(baseVal, [&isAlpha](auto ch, auto&& fn) mutable {
if (isAlpha(ch))
fn(std::tolower(ch, std::locale()));
else
fn(ch);
});
break;
case ReplaceMode:
result = ApplyConverter(baseVal, [this, &context](auto str) {
result = ApplyStringConverter(baseVal, [this, &context](auto str) -> InternalValue {
auto oldStr = GetAsSameString(str, this->GetArgumentValue("old", context));
auto newStr = GetAsSameString(str, this->GetArgumentValue("new", context));
auto count = ConvertToInt(this->GetArgumentValue("count", context));
Expand All @@ -272,7 +232,7 @@ InternalValue StringConverter::Filter(const InternalValue& baseVal, RenderContex
});
break;
case TruncateMode:
result = ApplyConverter(baseVal, [this, &context, &isAlNum](auto str) {
result = ApplyStringConverter(baseVal, [this, &context, &isAlNum](auto str) -> InternalValue {
auto length = ConvertToInt(this->GetArgumentValue("length", context));
auto killWords = ConvertToBool(this->GetArgumentValue("killwords", context));
auto end = GetAsSameString(str, this->GetArgumentValue("end", context));
Expand Down
70 changes: 70 additions & 0 deletions src/testers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,30 @@ bool ValueTester::Test(const InternalValue& baseVal, RenderContext& context)
{
bool result = false;
auto valKind = Apply<ValueKindGetter>(baseVal);
enum
{
EvenTest,
OddTest
};

int testMode = EvenTest;
auto evenOddTest = [&testMode, valKind](const InternalValue& val) -> bool
{
bool result = false;
if (valKind == ValueKind::Integer)
{
auto intVal = ConvertToInt(val);
result = testMode == (intVal & 1) == (EvenTest ? 0 : 1);
}
else if (valKind == ValueKind::Double)
{
auto dblVal = ConvertToDouble(val);
int64_t intVal = dblVal;
if (dblVal == intVal)
result = testMode == (intVal & 1) == (EvenTest ? 0 : 1);
}
return result;
};

switch (m_mode)
{
Expand Down Expand Up @@ -235,12 +259,58 @@ bool ValueTester::Test(const InternalValue& baseVal, RenderContext& context)
break;
}
case IsEvenMode:
{
testMode = EvenTest;
result = evenOddTest(baseVal);
break;
}
case IsOddMode:
{
testMode = OddTest;
result = evenOddTest(baseVal);
break;
}
case IsLowerMode:
if (valKind != ValueKind::String)
{
result = false;
}
else
{
result = ApplyStringConverter(baseVal, [](const auto& str) {
bool result = true;
for (auto& ch : str)
{
if (std::isalpha(ch, std::locale()) && std::isupper(ch, std::locale()))
{
result = false;
break;
}
}
return result;
});
}
break;
case IsUpperMode:
if (valKind != ValueKind::String)
{
result = false;
}
else
{
result = ApplyStringConverter(baseVal, [](const auto& str) {
bool result = true;
for (auto& ch : str)
{
if (std::isalpha(ch, std::locale()) && std::islower(ch, std::locale()))
{
result = false;
break;
}
}
return result;
});
}
break;

}
Expand Down
Loading