Grantlee 5.2.0

safestring.h
1 /*
2 This file is part of the Grantlee 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 #ifndef GRANTLEE_SAFESTRING_H
22 #define GRANTLEE_SAFESTRING_H
23
24 #include "grantlee_templates_export.h"
25
26 #include <QtCore/QString>
27 #include <QtCore/QVariant>
28
29 namespace Grantlee
30 {
31
33
91 class GRANTLEE_TEMPLATES_EXPORT SafeString
92 {
93 public:
97 enum Safety {
98 IsSafe,
99 IsNotSafe
100 };
102
106 SafeString();
107
111 SafeString(const SafeString &safeString);
112
117 SafeString(const QString &str, bool safe);
118
123 /* implicit */ SafeString(const QString &str,
124 Safety safety
125 = IsNotSafe); // krazy:exclude=explicit
126
130 ~SafeString();
131
132 #ifndef Q_QDOC
133
136 void setNeedsEscape(bool needsEscape);
137 #endif
138
142 bool needsEscape() const;
143
147 bool isSafe() const;
148
149 #ifndef Q_QDOC
150
153 void setSafety(Safety safety);
154 #endif
155
163 class GRANTLEE_TEMPLATES_EXPORT NestedString : public QString
164 {
165 #ifndef Q_QDOC
166 friend class SafeString;
167 SafeString *m_safeString;
168
169 public:
170 NestedString(SafeString *safeString);
171 NestedString(const QString &content, SafeString *safeString);
172
173 SafeString &append(const SafeString &str);
174 SafeString &append(const QString &str);
175 SafeString &append(const QStringRef &reference);
176 SafeString &append(const QLatin1String &str);
177 #ifndef QT_NO_CAST_FROM_ASCII
178 SafeString &append(const QByteArray &ba)
179 {
180 QString::append(ba);
181 m_safeString->m_safety = IsNotSafe;
182 return *m_safeString;
183 }
184
185 SafeString &append(const char *str)
186 {
187 QString::append(str);
188 m_safeString->m_safety = IsNotSafe;
189 return *m_safeString;
190 }
191 #endif
192 SafeString &append(const QChar ch);
193
194 SafeString &fill(QChar ch, int size = -1);
195
196 SafeString &insert(int position, const SafeString &str);
197 SafeString &insert(int position, const QString &str);
198 SafeString &insert(int position, const QLatin1String &str);
199 SafeString &insert(int position, const QChar *unicode, int size);
200 SafeString &insert(int position, QChar ch);
201
202 SafeString left(int n) const;
203 SafeString leftJustified(int width, QChar fill = QLatin1Char(' '),
204 bool truncate = {}) const;
205 SafeString mid(int position, int n = -1) const;
206
207 SafeString normalized(NormalizationForm mode) const;
208 SafeString normalized(NormalizationForm mode,
209 QChar::UnicodeVersion version) const;
210
211 SafeString &prepend(const SafeString &str);
212 SafeString &prepend(const QString &str);
213 SafeString &prepend(const QLatin1String &str);
214 #ifndef QT_NO_CAST_FROM_ASCII
215 SafeString &prepend(const QByteArray &ba)
216 {
217 QString::prepend(ba);
218 m_safeString->m_safety = IsNotSafe;
219 return *m_safeString;
220 }
221
222 SafeString &prepend(const char *str)
223 {
224 QString::prepend(str);
225 m_safeString->m_safety = IsNotSafe;
226 return *m_safeString;
227 }
228 #endif
229 SafeString &prepend(QChar ch);
230
231 void push_back(const SafeString &other);
232 void push_front(const SafeString &other);
233
234 SafeString &remove(int position, int n);
235 SafeString &remove(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive);
236 SafeString &remove(const SafeString &str,
237 Qt::CaseSensitivity cs = Qt::CaseSensitive);
238 SafeString &remove(const QString &str,
239 Qt::CaseSensitivity cs = Qt::CaseSensitive);
240 SafeString &remove(const QRegularExpression &rx);
241 SafeString repeated(int times) const;
242 SafeString &replace(int position, int n, const SafeString &after);
243 SafeString &replace(int position, int n, const QString &after);
244 SafeString &replace(int position, int n, const QChar *unicode, int size);
245 SafeString &replace(int position, int n, QChar after);
246 SafeString &replace(const SafeString &before, const SafeString &after,
247 Qt::CaseSensitivity cs = Qt::CaseSensitive);
248 SafeString &replace(const QString &before, const SafeString &after,
249 Qt::CaseSensitivity cs = Qt::CaseSensitive);
250 SafeString &replace(const SafeString &before, const QString &after,
251 Qt::CaseSensitivity cs = Qt::CaseSensitive);
252 SafeString &replace(const QString &before, const QString &after,
253 Qt::CaseSensitivity cs = Qt::CaseSensitive);
254 SafeString &replace(const QChar *before, int blen, const QChar *after,
255 int alen, Qt::CaseSensitivity cs = Qt::CaseSensitive);
256 SafeString &replace(QChar ch, const SafeString &after,
257 Qt::CaseSensitivity cs = Qt::CaseSensitive);
258 SafeString &replace(QChar ch, const QString &after,
259 Qt::CaseSensitivity cs = Qt::CaseSensitive);
260 SafeString &replace(QChar before, QChar after,
261 Qt::CaseSensitivity cs = Qt::CaseSensitive);
262 SafeString &replace(const QLatin1String &before, const QLatin1String &after,
263 Qt::CaseSensitivity cs = Qt::CaseSensitive);
264 SafeString &replace(const QLatin1String &before, const SafeString &after,
265 Qt::CaseSensitivity cs = Qt::CaseSensitive);
266 SafeString &replace(const QLatin1String &before, const QString &after,
267 Qt::CaseSensitivity cs = Qt::CaseSensitive);
268 SafeString &replace(const SafeString &before, const QLatin1String &after,
269 Qt::CaseSensitivity cs = Qt::CaseSensitive);
270 SafeString &replace(const QString &before, const QLatin1String &after,
271 Qt::CaseSensitivity cs = Qt::CaseSensitive);
272 SafeString &replace(QChar c, const QLatin1String &after,
273 Qt::CaseSensitivity cs = Qt::CaseSensitive);
274 SafeString &replace(const QRegularExpression &rx, const SafeString &after);
275 SafeString &replace(const QRegularExpression &rx, const QString &after);
276
277 SafeString right(int n) const;
278 SafeString rightJustified(int width, QChar fill = QLatin1Char(' '),
279 bool truncate = {}) const;
280
281 SafeString section(QChar sep, int start, int end = -1,
282 SectionFlags flags = SectionDefault) const;
283 SafeString section(const SafeString &sep, int start, int end = -1,
284 SectionFlags flags = SectionDefault) const;
285 SafeString section(const QString &sep, int start, int end = -1,
286 SectionFlags flags = SectionDefault) const;
287 SafeString section(const QRegularExpression &reg, int start, int end = -1,
288 SectionFlags flags = SectionDefault) const;
289 SafeString &setNum(int n, int base = 10);
290 SafeString &setNum(uint n, int base = 10);
291 SafeString &setNum(long n, int base = 10);
292 SafeString &setNum(ulong n, int base = 10);
293 SafeString &setNum(qlonglong n, int base = 10);
294 SafeString &setNum(qulonglong n, int base = 10);
295 SafeString &setNum(short n, int base = 10);
296 SafeString &setNum(ushort n, int base = 10);
297 SafeString &setNum(double n, char format = 'g', int precision = 6);
298 SafeString &setNum(float n, char format = 'g', int precision = 6);
299 SafeString &setUnicode(const QChar *unicode, int size);
300 SafeString &setUtf16(const ushort *unicode, int size);
301 SafeString simplified() const;
302
303 QStringList split(const SafeString &sep,
304 SplitBehavior behavior = KeepEmptyParts,
305 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
306 QStringList split(const QString &sep,
307 SplitBehavior behavior = KeepEmptyParts,
308 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
309 QStringList split(const QChar &sep, SplitBehavior behavior = KeepEmptyParts,
310 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
311 QStringList split(const QRegularExpression &rx,
312 SplitBehavior behavior = KeepEmptyParts) const;
313
314 SafeString toLower() const;
315 SafeString toUpper() const;
316 SafeString trimmed() const;
317
318 void chop(int n);
319 #endif
320 };
321
325 const NestedString &get() const { return m_nestedString; }
326
330 NestedString &get() { return m_nestedString; }
331
335 operator QString() const { return m_nestedString; }
336
340 SafeString &operator=(const SafeString &str);
341
347 SafeString operator+(const QString &str);
348
354 SafeString operator+(const SafeString &str);
355
361 SafeString &operator+=(const QString &str);
362
368 SafeString &operator+=(const SafeString &str);
369
375 bool operator==(const SafeString &other) const;
376
382 bool operator==(const QString &other) const;
383
387 operator QVariant() const { return QVariant::fromValue(*this); }
388
389 private:
390 #ifndef Q_QDOC
391 NestedString m_nestedString;
392 #endif
393 Safety m_safety;
394 bool m_needsescape;
395 };
396 }
397
398 Q_DECLARE_METATYPE(Grantlee::SafeString)
399
400 #endif
Grantlee::SafeString::get
NestedString & get()
Definition: safestring.h:330
Grantlee::SafeString::get
const NestedString & get() const
Definition: safestring.h:325
Grantlee::SafeString::IsSafe
The string is safe and requires no further escaping.
Definition: safestring.h:98
Grantlee::SafeString
A QString wrapper class for containing whether a string is safe or needs to be escaped.
Definition: safestring.h:91
Grantlee::SafeString::Safety
Safety
Definition: safestring.h:97
Grantlee
The Grantlee namespace holds all public Grantlee API.
Definition: Mainpage.dox:7
Grantlee::SafeString::NestedString
The NestedString is a QString whose methods always return a SafeString.
Definition: safestring.h:163