Cutelee 6.1.0
safestring.h
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#ifndef CUTELEE_SAFESTRING_H
22#define CUTELEE_SAFESTRING_H
23
24#include "cutelee_templates_export.h"
25
26#include <QtCore/QString>
27#include <QtCore/QVariant>
28
29namespace Cutelee
30{
31
33
91class CUTELEE_TEMPLATES_EXPORT SafeString
92{
93public:
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 CUTELEE_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(5, 15, 2)
177#endif
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, 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;
206 bool truncate = {}) const;
207 SafeString mid(int position, int n = -1) const;
208
211 QChar::UnicodeVersion version) const;
212
213 SafeString &prepend(const SafeString &str);
214 SafeString &prepend(const QString &str);
216#ifndef QT_NO_CAST_FROM_ASCII
217 SafeString &prepend(const QByteArray &ba)
218 {
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
232
233 void push_back(const SafeString &other);
234 void push_front(const SafeString &other);
235
236 SafeString &remove(int position, int n);
238 SafeString &remove(const SafeString &str,
240 SafeString &remove(const QString &str,
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,
250 SafeString &replace(const QString &before, const SafeString &after,
252 SafeString &replace(const SafeString &before, const QString &after,
254 SafeString &replace(const QString &before, const QString &after,
256 SafeString &replace(const QChar *before, int blen, const QChar *after,
258 SafeString &replace(QChar ch, const SafeString &after,
260 SafeString &replace(QChar ch, const QString &after,
262 SafeString &replace(QChar before, QChar after,
266 SafeString &replace(QLatin1String before, const SafeString &after,
268 SafeString &replace(QLatin1String before, const QString &after,
270 SafeString &replace(const SafeString &before, QLatin1String after,
272 SafeString &replace(const QString &before, QLatin1String after,
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;
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#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
306 QStringList split(const SafeString &sep,
309 QStringList split(const QString &sep,
316#else
317 QStringList split(const SafeString &sep,
320 QStringList split(const QString &sep,
326 Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const;
327#endif
328
329 SafeString toLower() const;
330 SafeString toUpper() const;
331 SafeString trimmed() const;
332
333 void chop(int n);
334#endif
335 };
336
340 const NestedString &get() const { return m_nestedString; }
341
345 NestedString &get() { return m_nestedString; }
346
350 operator QString() const { return m_nestedString; }
351
355 SafeString &operator=(const SafeString &str);
356
362 SafeString operator+(const QString &str);
363
369 SafeString operator+(const SafeString &str);
370
376 SafeString &operator+=(const QString &str);
377
383 SafeString &operator+=(const SafeString &str);
384
390 bool operator==(const SafeString &other) const;
391
397 bool operator==(const QString &other) const;
398
402 operator QVariant() const { return QVariant::fromValue(*this); }
403
404private:
405#ifndef Q_QDOC
406 NestedString m_nestedString;
407#endif
408 Safety m_safety;
409 bool m_needsescape;
410};
411}
412
413Q_DECLARE_METATYPE(Cutelee::SafeString)
414
415#endif
The NestedString is a QString whose methods always return a SafeString.
Definition safestring.h:164
A QString wrapper class for containing whether a string is safe or needs to be escaped.
Definition safestring.h:92
NestedString & get()
Definition safestring.h:345
const NestedString & get() const
Definition safestring.h:340
@ IsSafe
The string is safe and requires no further escaping.
Definition safestring.h:98
The Cutelee namespace holds all public Cutelee API.
Definition Mainpage.dox:8
UnicodeVersion
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const const
typedef SectionFlags
QString & append(QChar ch)
void chop(int n)
iterator end()
QString & fill(QChar ch, int size)
QString & insert(int position, QChar ch)
QString left(int n) const const
QString leftJustified(int width, QChar fill, bool truncate) const const
QString mid(int position, int n) const const
QString normalized(NormalizationForm mode, QChar::UnicodeVersion version) const const
QString & prepend(QChar ch)
void push_back(QChar ch)
void push_front(QChar ch)
typedef reference
QString & remove(int position, int n)
QString repeated(int times) const const
QString & replace(int position, int n, QChar after)
QString right(int n) const const
QString rightJustified(int width, QChar fill, bool truncate) const const
QString section(QChar sep, int start, int end, SectionFlags flags) const const
QString & setNum(short n, int base)
QString & setUnicode(const QChar *unicode, int size)
QString & setUtf16(const ushort *unicode, int size)
QString simplified() const const
int size() const const
QString toLower() const const
QString toUpper() const const
QString trimmed() const const
void truncate(int position)
const QChar * unicode() const const
CaseSensitivity
typedef SplitBehavior
QVariant fromValue(const T &value)