23 #include "metaenumvariable_p.h"
24 #include "safestring.h"
26 #include <QtCore/QRegularExpression>
27 #include <QtCore/QStringList>
28 #include <QtCore/QVariant>
33 static QRegularExpression getIsTitleRE()
35 QRegularExpression titleRe(QStringLiteral(
"\\b[a-z]"),
36 QRegularExpression::InvertedGreedinessOption);
40 static QRegularExpression getTitleRE()
42 QRegularExpression titleRe(QStringLiteral(
"\\b(.)"),
43 QRegularExpression::InvertedGreedinessOption);
52 if (property == QStringLiteral(
"capitalize")) {
54 return QVariant(s.at(0).toUpper() + s.right(s.length() - 1));
57 static const QLatin1String falseString(
"False");
58 static const QLatin1String trueString(
"True");
60 if (property == QStringLiteral(
"isalnum")) {
62 auto it = s.constBegin();
63 while (it != s.constEnd()) {
64 if (!it->isLetterOrNumber())
70 if (property == QStringLiteral(
"isalpha")) {
72 auto it = s.constBegin();
73 if (it == s.constEnd())
75 while (it != s.constEnd()) {
82 if (property == QStringLiteral(
"isdigit")) {
84 auto it = s.constBegin();
85 while (it != s.constEnd()) {
92 if (property == QStringLiteral(
"islower")) {
93 const QString s =
object.get().toLower();
94 return (s ==
object.get()) ? trueString : falseString;
96 if (property == QStringLiteral(
"isspace")) {
97 const QString s =
object.get().trimmed();
98 return (s.isEmpty()) ? trueString : falseString;
100 if (property == QStringLiteral(
"istitle")) {
101 const QString s =
object.get();
103 static const auto titleRe = getIsTitleRE();
104 return (titleRe.match(s).hasMatch()) ? falseString : trueString;
106 if (property == QStringLiteral(
"isupper")) {
107 const QString s =
object.get().toUpper();
108 return (s ==
object) ? trueString : falseString;
110 if (property == QStringLiteral(
"lower")) {
111 return object.get().toLower();
113 if (property == QStringLiteral(
"splitlines")) {
114 const auto strings =
object.get().split(QLatin1Char(
'\n'));
116 auto it = strings.constBegin();
117 const auto end = strings.constEnd();
118 for (; it != end; ++it)
122 if (property == QStringLiteral(
"strip")) {
123 return object.get().trimmed();
125 if (property == QStringLiteral(
"swapcase")) {
126 const QString inputString =
object.get();
128 s.reserve(inputString.size());
129 auto it = inputString.constBegin();
130 while (it != inputString.constEnd()) {
133 else if (it->isLower())
141 if (property == QStringLiteral(
"title")) {
142 static const auto titleRe = getTitleRE();
144 const QString s =
object.get();
146 output.reserve(s.size());
151 auto it = titleRe.globalMatch(s);
152 while (it.hasNext()) {
153 auto match = it.next();
154 pos = match.capturedStart();
155 output += match.captured().toUpper();
156 matchedLength = match.capturedLength();
158 match = it.peekNext();
159 nextPos = match.capturedStart();
160 output += s.mid(pos + matchedLength, nextPos - pos - 1);
162 output += s.right(s.length() - (pos + matchedLength));
168 if (property == QStringLiteral(
"upper")) {
169 return object.get().toUpper();
176 TypeAccessor<MetaEnumVariable &>::lookUp(
const MetaEnumVariable &
object,
179 if (property == QStringLiteral(
"name"))
180 return QLatin1String(
object.enumerator.name());
181 if (property == QStringLiteral(
"value"))
183 if (property == QStringLiteral(
"key"))
184 return QLatin1String(
object.enumerator.valueToKey(
object.value));
185 if (property == QStringLiteral(
"scope"))
186 return QLatin1String(
object.enumerator.scope());
187 if (property == QStringLiteral(
"keyCount"))
188 return object.enumerator.keyCount();
191 const auto listIndex =
property.toInt(&ok);
193 if (listIndex >=
object.enumerator.keyCount())
196 const MetaEnumVariable mev(
object.enumerator,
197 object.enumerator.value(listIndex));
198 return QVariant::fromValue(mev);
A QString wrapper class for containing whether a string is safe or needs to be escaped.
The Cutelee namespace holds all public Cutelee API.