Grantlee  5.1.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 
92 class GRANTLEE_TEMPLATES_EXPORT SafeString
93 {
94 public:
98  enum Safety {
100  IsNotSafe
101  };
104 
108  SafeString();
109 
113  SafeString(const SafeString &safeString);
114 
120  SafeString(const QString &str, bool safe);
121 
127  /* implicit */ SafeString(const QString &str,
128  Safety safety
129  = IsNotSafe); // krazy:exclude=explicit
130 
134  ~SafeString();
135 
136 #ifndef Q_QDOC
137 
140  void setNeedsEscape(bool needsEscape);
141 #endif
142 
146  bool needsEscape() const;
147 
151  bool isSafe() const;
152 
153 #ifndef Q_QDOC
154 
157  void setSafety(Safety safety);
158 #endif
159 
167  class GRANTLEE_TEMPLATES_EXPORT NestedString : public QString
168  {
169 #ifndef Q_QDOC
170  friend class SafeString;
171  SafeString *m_safeString;
172 
173  public:
174  NestedString(SafeString *safeString);
175  NestedString(const QString &content, SafeString *safeString);
176 
177  SafeString &append(const SafeString &str);
178  SafeString &append(const QString &str);
179  SafeString &append(const QStringRef &reference);
180  SafeString &append(const QLatin1String &str);
181 #ifndef QT_NO_CAST_FROM_ASCII
182  SafeString &append(const QByteArray &ba)
183  {
184  QString::append(ba);
185  m_safeString->m_safety = IsNotSafe;
186  return *m_safeString;
187  }
188 
189  SafeString &append(const char *str)
190  {
191  QString::append(str);
192  m_safeString->m_safety = IsNotSafe;
193  return *m_safeString;
194  }
195 #endif
196  SafeString &append(const QChar ch);
197 
198  SafeString &fill(QChar ch, int size = -1);
199 
200  SafeString &insert(int position, const SafeString &str);
201  SafeString &insert(int position, const QString &str);
202  SafeString &insert(int position, const QLatin1String &str);
203  SafeString &insert(int position, const QChar *unicode, int size);
204  SafeString &insert(int position, QChar ch);
205 
206  SafeString left(int n) const;
207  SafeString leftJustified(int width, QChar fill = QLatin1Char(' '),
208  bool truncate = false) const;
209  SafeString mid(int position, int n = -1) const;
210 
211  SafeString normalized(NormalizationForm mode) const;
212  SafeString normalized(NormalizationForm mode,
213  QChar::UnicodeVersion version) const;
214 
215  SafeString &prepend(const SafeString &str);
216  SafeString &prepend(const QString &str);
217  SafeString &prepend(const QLatin1String &str);
218 #ifndef QT_NO_CAST_FROM_ASCII
219  SafeString &prepend(const QByteArray &ba)
220  {
221  QString::prepend(ba);
222  m_safeString->m_safety = IsNotSafe;
223  return *m_safeString;
224  }
225 
226  SafeString &prepend(const char *str)
227  {
228  QString::prepend(str);
229  m_safeString->m_safety = IsNotSafe;
230  return *m_safeString;
231  }
232 #endif
233  SafeString &prepend(QChar ch);
234 
235  void push_back(const SafeString &other);
236  void push_front(const SafeString &other);
237 
238  SafeString &remove(int position, int n);
239  SafeString &remove(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive);
240  SafeString &remove(const SafeString &str,
241  Qt::CaseSensitivity cs = Qt::CaseSensitive);
242  SafeString &remove(const QString &str,
243  Qt::CaseSensitivity cs = Qt::CaseSensitive);
244  SafeString &remove(const QRegularExpression &rx);
245  SafeString repeated(int times) const;
246  SafeString &replace(int position, int n, const SafeString &after);
247  SafeString &replace(int position, int n, const QString &after);
248  SafeString &replace(int position, int n, const QChar *unicode, int size);
249  SafeString &replace(int position, int n, QChar after);
250  SafeString &replace(const SafeString &before, const SafeString &after,
251  Qt::CaseSensitivity cs = Qt::CaseSensitive);
252  SafeString &replace(const QString &before, const SafeString &after,
253  Qt::CaseSensitivity cs = Qt::CaseSensitive);
254  SafeString &replace(const SafeString &before, const QString &after,
255  Qt::CaseSensitivity cs = Qt::CaseSensitive);
256  SafeString &replace(const QString &before, const QString &after,
257  Qt::CaseSensitivity cs = Qt::CaseSensitive);
258  SafeString &replace(const QChar *before, int blen, const QChar *after,
259  int alen, Qt::CaseSensitivity cs = Qt::CaseSensitive);
260  SafeString &replace(QChar ch, const SafeString &after,
261  Qt::CaseSensitivity cs = Qt::CaseSensitive);
262  SafeString &replace(QChar ch, const QString &after,
263  Qt::CaseSensitivity cs = Qt::CaseSensitive);
264  SafeString &replace(QChar before, QChar after,
265  Qt::CaseSensitivity cs = Qt::CaseSensitive);
266  SafeString &replace(const QLatin1String &before, const QLatin1String &after,
267  Qt::CaseSensitivity cs = Qt::CaseSensitive);
268  SafeString &replace(const QLatin1String &before, const SafeString &after,
269  Qt::CaseSensitivity cs = Qt::CaseSensitive);
270  SafeString &replace(const QLatin1String &before, const QString &after,
271  Qt::CaseSensitivity cs = Qt::CaseSensitive);
272  SafeString &replace(const SafeString &before, const QLatin1String &after,
273  Qt::CaseSensitivity cs = Qt::CaseSensitive);
274  SafeString &replace(const QString &before, const QLatin1String &after,
275  Qt::CaseSensitivity cs = Qt::CaseSensitive);
276  SafeString &replace(QChar c, const QLatin1String &after,
277  Qt::CaseSensitivity cs = Qt::CaseSensitive);
278  SafeString &replace(const QRegularExpression &rx, const SafeString &after);
279  SafeString &replace(const QRegularExpression &rx, const QString &after);
280 
281  SafeString right(int n) const;
282  SafeString rightJustified(int width, QChar fill = QLatin1Char(' '),
283  bool truncate = false) const;
284 
285  SafeString section(QChar sep, int start, int end = -1,
286  SectionFlags flags = SectionDefault) const;
287  SafeString section(const SafeString &sep, int start, int end = -1,
288  SectionFlags flags = SectionDefault) const;
289  SafeString section(const QString &sep, int start, int end = -1,
290  SectionFlags flags = SectionDefault) const;
291  SafeString section(const QRegularExpression &reg, int start, int end = -1,
292  SectionFlags flags = SectionDefault) const;
293  SafeString &setNum(int n, int base = 10);
294  SafeString &setNum(uint n, int base = 10);
295  SafeString &setNum(long n, int base = 10);
296  SafeString &setNum(ulong n, int base = 10);
297  SafeString &setNum(qlonglong n, int base = 10);
298  SafeString &setNum(qulonglong n, int base = 10);
299  SafeString &setNum(short n, int base = 10);
300  SafeString &setNum(ushort n, int base = 10);
301  SafeString &setNum(double n, char format = 'g', int precision = 6);
302  SafeString &setNum(float n, char format = 'g', int precision = 6);
303  SafeString &setUnicode(const QChar *unicode, int size);
304  SafeString &setUtf16(const ushort *unicode, int size);
305  SafeString simplified() const;
306 
307  QStringList split(const SafeString &sep,
308  SplitBehavior behavior = KeepEmptyParts,
309  Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
310  QStringList split(const QString &sep,
311  SplitBehavior behavior = KeepEmptyParts,
312  Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
313  QStringList split(const QChar &sep, SplitBehavior behavior = KeepEmptyParts,
314  Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
315  QStringList split(const QRegularExpression &rx,
316  SplitBehavior behavior = KeepEmptyParts) const;
317 
318  SafeString toLower() const;
319  SafeString toUpper() const;
320  SafeString trimmed() const;
321 
322  void chop(int n);
323 #endif
324  };
325 
329  const NestedString &get() const { return m_nestedString; }
330 
334  NestedString &get() { return m_nestedString; }
335 
339  operator QString() const { return m_nestedString; }
340 
344  SafeString &operator=(const SafeString &str);
345 
351  SafeString operator+(const QString &str);
352 
358  SafeString operator+(const SafeString &str);
359 
365  SafeString &operator+=(const QString &str);
366 
372  SafeString &operator+=(const SafeString &str);
373 
379  bool operator==(const SafeString &other) const;
380 
386  bool operator==(const QString &other) const;
387 
391  operator QVariant() const { return QVariant::fromValue(*this); }
392 
393 private:
394 #ifndef Q_QDOC
395  NestedString m_nestedString;
396 #endif
397  Safety m_safety;
398  bool m_needsescape;
399 };
400 }
401 
402 Q_DECLARE_METATYPE(Grantlee::SafeString)
403 
404 #endif
The string is safe and requires no further escaping.
Definition: safestring.h:99
The NestedString is a QString whose methods always return a SafeString.
Definition: safestring.h:167
A QString wrapper class for containing whether a string is safe or needs to be escaped.
Definition: safestring.h:92
The Grantlee namespace holds all public Grantlee API.
Definition: Mainpage.dox:7