Cutelee 6.1.0
integers.cpp
1/*
2 This file is part of the Cutelee template system.
3
4 Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either version
9 2.1 of the Licence, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library. If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21#include "integers.h"
22
23#include "util.h"
24
25QVariant AddFilter::doFilter(const QVariant &input, const QVariant &argument,
26 bool autoescape) const
27{
28 Q_UNUSED(autoescape)
29
30 if (isSafeString(input)) {
31 if (isSafeString(argument))
32 return getSafeString(input) + getSafeString(argument);
33 return input;
34 }
35
36 if (input.userType() == qMetaTypeId<QVariantList>()) {
37 if (argument.userType() == qMetaTypeId<QVariantList>())
38 return input.value<QVariantList>() + argument.value<QVariantList>();
39 return input;
40 }
41
42 if (input.userType() == qMetaTypeId<QStringList>()) {
43 if (argument.userType() == QMetaType::QStringList)
44 return input.toStringList() + argument.toStringList();
45 return input;
46 }
47
48 if (input.userType() == qMetaTypeId<int>()) {
49 if (argument.userType() == qMetaTypeId<int>())
50 return input.value<int>() + argument.value<int>();
51 return input;
52 }
53
54 if (input.userType() == qMetaTypeId<uint>()) {
55 if (argument.userType() == qMetaTypeId<uint>())
56 return input.value<uint>() + argument.value<uint>();
57 return input;
58 }
59
60 if (input.canConvert<double>()) {
61 if (argument.canConvert<double>())
62 return input.value<double>() + argument.value<double>();
63 return input;
64 }
65
66 if (input.userType() == qMetaTypeId<long long>()) {
67 if (argument.userType() == qMetaTypeId<long long>())
68 return input.value<long long>() + argument.value<long long>();
69 return input;
70 }
71
72 if (input.userType() == qMetaTypeId<unsigned long long>()) {
73 if (input.userType() == qMetaTypeId<unsigned long long>())
74 return input.value<unsigned long long>()
75 + argument.value<unsigned long long>();
76 return input;
77 }
78 return input;
79}
80
82 const QVariant &argument,
83 bool autoescape) const
84{
85 Q_UNUSED(autoescape)
86 auto value = getSafeString(input);
87
88 bool ok;
89 (void)value.get().toInt(&ok);
90 if (!ok)
91 return QString();
92
93 if (value.get().size() < 1)
94 return value;
95
96 auto arg = getSafeString(argument).get().toInt();
97
98 if (value.get().size() < arg)
99 return value;
100
101 return SafeString(value.get().at(value.get().size() - arg));
102}
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
Definition integers.cpp:25
A QString wrapper class for containing whether a string is safe or needs to be escaped.
Definition safestring.h:92
const NestedString & get() const
Definition safestring.h:340
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
Definition integers.cpp:81
bool isSafeString(const QVariant &input)
Definition util.cpp:117
Cutelee::SafeString getSafeString(const QVariant &input)
Definition util.cpp:108
Utility functions used throughout Cutelee.