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 
29 namespace Cutelee
30 {
31 
33 
91 class CUTELEE_TEMPLATES_EXPORT SafeString
92 {
93 public:
97  enum Safety {
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 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)
176  SafeString &append(QStringView reference);
177 #endif
178  SafeString &append(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, 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(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(QLatin1String before, QLatin1String after,
265  Qt::CaseSensitivity cs = Qt::CaseSensitive);
266  SafeString &replace(QLatin1String before, const SafeString &after,
267  Qt::CaseSensitivity cs = Qt::CaseSensitive);
268  SafeString &replace(QLatin1String before, const QString &after,
269  Qt::CaseSensitivity cs = Qt::CaseSensitive);
270  SafeString &replace(const SafeString &before, QLatin1String after,
271  Qt::CaseSensitivity cs = Qt::CaseSensitive);
272  SafeString &replace(const QString &before, QLatin1String after,
273  Qt::CaseSensitivity cs = Qt::CaseSensitive);
274  SafeString &replace(QChar c, 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 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
306  QStringList split(const SafeString &sep,
307  QString::SplitBehavior behavior = QString::KeepEmptyParts,
308  Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
309  QStringList split(const QString &sep,
310  QString::SplitBehavior behavior = QString::KeepEmptyParts,
311  Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
312  QStringList split(const QChar &sep, QString::SplitBehavior behavior = QString::KeepEmptyParts,
313  Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
314  QStringList split(const QRegularExpression &rx,
315  QString::SplitBehavior behavior = QString::KeepEmptyParts) const;
316 #else
317  QStringList split(const SafeString &sep,
318  Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
319  Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
320  QStringList split(const QString &sep,
321  Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
322  Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
323  QStringList split(const QChar &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
324  Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
325  QStringList split(const QRegularExpression &rx,
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 
404 private:
405 #ifndef Q_QDOC
406  NestedString m_nestedString;
407 #endif
408  Safety m_safety;
409  bool m_needsescape;
410 };
411 }
412 
413 Q_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