KTextTemplate

typeaccessors.cpp
1/*
2 This file is part of the KTextTemplate library
3
4 SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7
8*/
9
10#include "typeaccessor.h"
11
12#include "metaenumvariable_p.h"
13#include "safestring.h"
14
15#include <QRegularExpression>
16#include <QStringList>
17#include <QVariant>
18
19namespace KTextTemplate
20{
21
22static QRegularExpression getIsTitleRE()
23{
24 QRegularExpression titleRe(QStringLiteral("\\b[a-z]"), QRegularExpression::InvertedGreedinessOption);
25 return titleRe;
26}
27
28static QRegularExpression getTitleRE()
29{
31 return titleRe;
32}
33
34template<>
35QVariant TypeAccessor<KTextTemplate::SafeString &>::lookUp(const KTextTemplate::SafeString &object, const QString &property)
36{
37 if (property == QStringLiteral("capitalize")) {
38 const QString &s = object.get();
39 return {s.at(0).toUpper() + s.right(s.length() - 1)};
40 }
41
42 static const QLatin1String falseString("False");
43 static const QLatin1String trueString("True");
44
45 if (property == QStringLiteral("isalnum")) {
46 const QString &s = object.get();
47 auto it = s.constBegin();
48 while (it != s.constEnd()) {
49 if (!it->isLetterOrNumber())
50 return falseString;
51 ++it;
52 }
53 return trueString;
54 }
55 if (property == QStringLiteral("isalpha")) {
56 const QString &s = object.get();
57 auto it = s.constBegin();
58 if (it == s.constEnd())
59 return falseString;
60 while (it != s.constEnd()) {
61 if (!it->isLetter())
62 return falseString;
63 ++it;
64 }
65 return trueString;
66 }
67 if (property == QStringLiteral("isdigit")) {
68 const QString &s = object.get();
69 auto it = s.constBegin();
70 while (it != s.constEnd()) {
71 if (!it->isNumber())
72 return falseString;
73 ++it;
74 }
75 return trueString;
76 }
77 if (property == QStringLiteral("islower")) {
78 const QString s = object.get().toLower();
79 return (s == object.get()) ? trueString : falseString;
80 }
81 if (property == QStringLiteral("isspace")) {
82 const QString s = object.get().trimmed();
83 return (s.isEmpty()) ? trueString : falseString;
84 }
85 if (property == QStringLiteral("istitle")) {
86 const QString &s = object.get();
87
88 static const auto titleRe = getIsTitleRE();
89 return (titleRe.match(s).hasMatch()) ? falseString : trueString;
90 }
91 if (property == QStringLiteral("isupper")) {
92 const QString s = object.get().toUpper();
93 return (s == object) ? trueString : falseString;
94 }
95 if (property == QStringLiteral("lower")) {
96 return object.get().toLower();
97 }
98 if (property == QStringLiteral("splitlines")) {
99 const auto strings = object.get().split(QLatin1Char('\n'));
100 QVariantList list;
101 auto it = strings.constBegin();
102 const auto end = strings.constEnd();
103 for (; it != end; ++it)
104 list << *it;
105 return list;
106 }
107 if (property == QStringLiteral("strip")) {
108 return object.get().trimmed();
109 }
110 if (property == QStringLiteral("swapcase")) {
111 const QString &inputString = object.get();
112 QString s;
113 s.reserve(inputString.size());
114 auto it = inputString.constBegin();
115 while (it != inputString.constEnd()) {
116 if (it->isUpper())
117 s += it->toLower();
118 else if (it->isLower())
119 s += it->toUpper();
120 else
121 s += *it;
122 ++it;
123 }
124 return s;
125 }
126 if (property == QStringLiteral("title")) {
127 static const auto titleRe = getTitleRE();
128
129 const QString &s = object.get();
130 QString output;
131 output.reserve(s.size());
132 auto pos = 0;
133 auto nextPos = 0;
134 int matchedLength;
135
136 auto it = titleRe.globalMatch(s);
137 while (it.hasNext()) {
138 auto match = it.next();
139 pos = match.capturedStart();
140 output += match.captured().toUpper();
141 matchedLength = match.capturedLength();
142 if (it.hasNext()) {
143 match = it.peekNext();
144 nextPos = match.capturedStart();
145 output += s.mid(pos + matchedLength, nextPos - pos - 1);
146 } else {
147 output += s.right(s.length() - (pos + matchedLength));
148 }
149 }
150
151 return output;
152 }
153 if (property == QStringLiteral("upper")) {
154 return object.get().toUpper();
155 }
156 return {};
157}
158
159template<>
160QVariant TypeAccessor<MetaEnumVariable &>::lookUp(const MetaEnumVariable &object, const QString &property)
161{
162 if (property == QStringLiteral("name"))
163 return QLatin1String(object.enumerator.name());
164 if (property == QStringLiteral("value"))
165 return object.value;
166 if (property == QStringLiteral("key"))
167 return QLatin1String(object.enumerator.valueToKey(object.value));
168 if (property == QStringLiteral("scope"))
169 return QLatin1String(object.enumerator.scope());
170 if (property == QStringLiteral("keyCount"))
171 return object.enumerator.keyCount();
172
173 auto ok = false;
174 const auto listIndex = property.toInt(&ok);
175 if (ok) {
176 if (listIndex >= object.enumerator.keyCount())
177 return {};
178
179 const MetaEnumVariable mev(object.enumerator, object.enumerator.value(listIndex));
180 return QVariant::fromValue(mev);
181 }
182
183 return {};
184}
185}
A QString wrapper class for containing whether a string is safe or needs to be escaped.
Definition safestring.h:81
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
KIOCORE_EXPORT TransferJob * get(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
const QList< QKeySequence > & end()
The KTextTemplate namespace holds all public KTextTemplate API.
Definition Mainpage.dox:8
char32_t toUpper(char32_t ucs4)
const_iterator constBegin() const const
const QChar at(qsizetype position) const const
const_iterator constBegin() const const
const_iterator constEnd() const const
bool isEmpty() const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const
void reserve(qsizetype size)
QString right(qsizetype n) const const
qsizetype size() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString toLower() const const
QString toUpper() const const
QString trimmed() const const
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Apr 27 2024 22:14:09 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.