Grantlee 5.3.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
101 };
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
136 void setNeedsEscape(bool needsEscape);
137#endif
138
142 bool needsEscape() const;
143
147 bool isSafe() const;
148
149#ifndef Q_QDOC
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#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
176 SafeString &append(const QStringRef &ref);
177#endif
178 SafeString &append(const QLatin1String &str);
179#ifndef QT_NO_CAST_FROM_ASCII
180 SafeString &append(const QByteArray &ba)
181 {
182 QString::append(ba);
183 m_safeString->m_safety = IsNotSafe;
184 return *m_safeString;
185 }
186
187 SafeString &append(const char *str)
188 {
189 QString::append(str);
190 m_safeString->m_safety = IsNotSafe;
191 return *m_safeString;
192 }
193#endif
194 SafeString &append(const QChar ch);
195
196 SafeString &fill(QChar ch, int size = -1);
197
198 SafeString &insert(int position, const SafeString &str);
199 SafeString &insert(int position, const QString &str);
200 SafeString &insert(int position, const QLatin1String &str);
201 SafeString &insert(int position, const QChar *unicode, int size);
202 SafeString &insert(int position, QChar ch);
203
204 SafeString left(int n) const;
205 SafeString leftJustified(int width, QChar fill = QLatin1Char(' '),
206 bool truncate = {}) const;
207 SafeString mid(int position, int n = -1) const;
208
209 SafeString normalized(NormalizationForm mode) const;
210 SafeString normalized(NormalizationForm mode,
211 QChar::UnicodeVersion version) const;
212
213 SafeString &prepend(const SafeString &str);
214 SafeString &prepend(const QString &str);
215 SafeString &prepend(const QLatin1String &str);
216#ifndef QT_NO_CAST_FROM_ASCII
217 SafeString &prepend(const QByteArray &ba)
218 {
219 QString::prepend(ba);
220 m_safeString->m_safety = IsNotSafe;
221 return *m_safeString;
222 }
223
224 SafeString &prepend(const char *str)
225 {
226 QString::prepend(str);
227 m_safeString->m_safety = IsNotSafe;
228 return *m_safeString;
229 }
230#endif
231 SafeString &prepend(QChar ch);
232
233 void push_back(const SafeString &other);
234 void push_front(const SafeString &other);
235
236 SafeString &remove(int position, int n);
237 SafeString &remove(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive);
238 SafeString &remove(const SafeString &str,
239 Qt::CaseSensitivity cs = Qt::CaseSensitive);
240 SafeString &remove(const QString &str,
241 Qt::CaseSensitivity cs = Qt::CaseSensitive);
242 SafeString &remove(const QRegularExpression &rx);
243 SafeString repeated(int times) const;
244 SafeString &replace(int position, int n, const SafeString &after);
245 SafeString &replace(int position, int n, const QString &after);
246 SafeString &replace(int position, int n, const QChar *unicode, int size);
247 SafeString &replace(int position, int n, QChar after);
248 SafeString &replace(const SafeString &before, const SafeString &after,
249 Qt::CaseSensitivity cs = Qt::CaseSensitive);
250 SafeString &replace(const QString &before, const SafeString &after,
251 Qt::CaseSensitivity cs = Qt::CaseSensitive);
252 SafeString &replace(const SafeString &before, const QString &after,
253 Qt::CaseSensitivity cs = Qt::CaseSensitive);
254 SafeString &replace(const QString &before, const QString &after,
255 Qt::CaseSensitivity cs = Qt::CaseSensitive);
256 SafeString &replace(const QChar *before, int blen, const QChar *after,
257 int alen, Qt::CaseSensitivity cs = Qt::CaseSensitive);
258 SafeString &replace(QChar ch, const SafeString &after,
259 Qt::CaseSensitivity cs = Qt::CaseSensitive);
260 SafeString &replace(QChar ch, const QString &after,
261 Qt::CaseSensitivity cs = Qt::CaseSensitive);
262 SafeString &replace(QChar before, QChar after,
263 Qt::CaseSensitivity cs = Qt::CaseSensitive);
264 SafeString &replace(const QLatin1String &before, const QLatin1String &after,
265 Qt::CaseSensitivity cs = Qt::CaseSensitive);
266 SafeString &replace(const QLatin1String &before, const SafeString &after,
267 Qt::CaseSensitivity cs = Qt::CaseSensitive);
268 SafeString &replace(const QLatin1String &before, const QString &after,
269 Qt::CaseSensitivity cs = Qt::CaseSensitive);
270 SafeString &replace(const SafeString &before, const QLatin1String &after,
271 Qt::CaseSensitivity cs = Qt::CaseSensitive);
272 SafeString &replace(const QString &before, const QLatin1String &after,
273 Qt::CaseSensitivity cs = Qt::CaseSensitive);
274 SafeString &replace(QChar c, const QLatin1String &after,
275 Qt::CaseSensitivity cs = Qt::CaseSensitive);
276 SafeString &replace(const QRegularExpression &rx, const SafeString &after);
277 SafeString &replace(const QRegularExpression &rx, const QString &after);
278
279 SafeString right(int n) const;
280 SafeString rightJustified(int width, QChar fill = QLatin1Char(' '),
281 bool truncate = {}) const;
282
283 SafeString section(QChar sep, int start, int end = -1,
284 SectionFlags flags = SectionDefault) const;
285 SafeString section(const SafeString &sep, int start, int end = -1,
286 SectionFlags flags = SectionDefault) const;
287 SafeString section(const QString &sep, int start, int end = -1,
288 SectionFlags flags = SectionDefault) const;
289 SafeString section(const QRegularExpression &reg, int start, int end = -1,
290 SectionFlags flags = SectionDefault) const;
291 SafeString &setNum(int n, int base = 10);
292 SafeString &setNum(uint n, int base = 10);
293 SafeString &setNum(long n, int base = 10);
294 SafeString &setNum(ulong n, int base = 10);
295 SafeString &setNum(qlonglong n, int base = 10);
296 SafeString &setNum(qulonglong n, int base = 10);
297 SafeString &setNum(short n, int base = 10);
298 SafeString &setNum(ushort n, int base = 10);
299 SafeString &setNum(double n, char format = 'g', int precision = 6);
300 SafeString &setNum(float n, char format = 'g', int precision = 6);
301 SafeString &setUnicode(const QChar *unicode, int size);
302 SafeString &setUtf16(const ushort *unicode, int size);
303 SafeString simplified() const;
304
305 // QT_DEPRECATED_SINCE(5, 15) to support consumers building with
306 // -DQT_DISABLE_DEPRECATED_BEFORE=0x050f00
307 // where QString enums will be invisible to the compiler
308#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && QT_DEPRECATED_SINCE(5, 15)
309 // Calls with default argument for "behavior" should pick the Qt >= 5.14
310 // version, if available
311#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
312 QStringList split(const SafeString &sep, QString::SplitBehavior behavior,
313 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
314 QStringList split(const QString &sep, QString::SplitBehavior behavior,
315 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
316 QStringList split(const QChar &sep, QString::SplitBehavior behavior,
317 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
318 QStringList split(const QRegularExpression &rx,
319 QString::SplitBehavior behavior) const;
320#else
321 QStringList split(const SafeString &sep,
322 QString::SplitBehavior behavior = QString::KeepEmptyParts,
323 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
324 QStringList split(const QString &sep,
325 QString::SplitBehavior behavior = QString::KeepEmptyParts,
326 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
327 QStringList split(const QChar &sep,
328 QString::SplitBehavior behavior = QString::KeepEmptyParts,
329 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
330 QStringList split(const QRegularExpression &rx,
331 QString::SplitBehavior behavior
332 = QString::KeepEmptyParts) const;
333#endif
334#endif
335
336#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
337 QStringList split(const SafeString &sep,
338 Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
339 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
340 QStringList split(const QString &sep,
341 Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
342 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
343 QStringList split(const QChar &sep,
344 Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
345 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
346 QStringList split(const QRegularExpression &rx,
347 Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const;
348#endif
349
350 SafeString toLower() const;
351 SafeString toUpper() const;
352 SafeString trimmed() const;
353
354 void chop(int n);
355#endif
356 };
357
361 const NestedString &get() const { return m_nestedString; }
362
366 NestedString &get() { return m_nestedString; }
367
371 operator QString() const { return m_nestedString; }
372
376 SafeString &operator=(const SafeString &str);
377
383 SafeString operator+(const QString &str);
384
390 SafeString operator+(const SafeString &str);
391
397 SafeString &operator+=(const QString &str);
398
404 SafeString &operator+=(const SafeString &str);
405
411 bool operator==(const SafeString &other) const;
412
418 bool operator==(const QString &other) const;
419
423 operator QVariant() const { return QVariant::fromValue(*this); }
424
425 private:
426#ifndef Q_QDOC
427 NestedString m_nestedString;
428#endif
429 Safety m_safety;
430 bool m_needsescape;
431};
432}
433
434Q_DECLARE_METATYPE(Grantlee::SafeString)
435
436#endif
Grantlee::SafeString::NestedString
The NestedString is a QString whose methods always return a SafeString.
Definition: safestring.h:164
Grantlee::SafeString
A QString wrapper class for containing whether a string is safe or needs to be escaped.
Definition: safestring.h:92
Grantlee::SafeString::operator+
SafeString operator+(const QString &str)
Grantlee::SafeString::get
NestedString & get()
Definition: safestring.h:366
Grantlee::SafeString::operator+=
SafeString & operator+=(const QString &str)
Grantlee::SafeString::operator=
SafeString & operator=(const SafeString &str)
Grantlee::SafeString::needsEscape
bool needsEscape() const
Grantlee::SafeString::SafeString
SafeString()
Grantlee::SafeString::operator+
SafeString operator+(const SafeString &str)
Grantlee::SafeString::Safety
Safety
Definition: safestring.h:97
Grantlee::SafeString::IsSafe
@ IsSafe
The string is safe and requires no further escaping.
Definition: safestring.h:98
Grantlee::SafeString::operator==
bool operator==(const QString &other) const
Grantlee::SafeString::get
const NestedString & get() const
Definition: safestring.h:361
Grantlee::SafeString::operator==
bool operator==(const SafeString &other) const
Grantlee::SafeString::isSafe
bool isSafe() const
Grantlee::SafeString::SafeString
SafeString(const SafeString &safeString)
Grantlee::SafeString::~SafeString
~SafeString()
Grantlee::SafeString::SafeString
SafeString(const QString &str, Safety safety=IsNotSafe)
Grantlee::SafeString::operator+=
SafeString & operator+=(const SafeString &str)
Grantlee::SafeString::SafeString
SafeString(const QString &str, bool safe)
Grantlee
The Grantlee namespace holds all public Grantlee API.
Definition: Mainpage.dox:8