Cutelee  6.2.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
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 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);
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, 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 
209  QChar::UnicodeVersion version) const;
210 
211  SafeString &prepend(const SafeString &str);
212  SafeString &prepend(const QString &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);
236  SafeString &remove(const SafeString &str,
238  SafeString &remove(const QString &str,
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,
248  SafeString &replace(const QString &before, const SafeString &after,
250  SafeString &replace(const SafeString &before, const QString &after,
252  SafeString &replace(const QString &before, const QString &after,
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,
258  SafeString &replace(QChar ch, const QString &after,
260  SafeString &replace(QChar before, QChar after,
264  SafeString &replace(QLatin1String before, const SafeString &after,
266  SafeString &replace(QLatin1String before, const QString &after,
268  SafeString &replace(const SafeString &before, QLatin1String after,
270  SafeString &replace(const QString &before, QLatin1String after,
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,
306  QStringList split(const QString &sep,
312  Qt::SplitBehavior behavior = Qt::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(Cutelee::SafeString)
399 
400 #endif
QString & append(QChar ch)
QString toUpper() const const
void truncate(qsizetype position)
QString & fill(QChar ch, qsizetype size)
iterator end()
The Cutelee namespace holds all public Cutelee API.
Definition: Mainpage.dox:7
QString & prepend(QChar ch)
QString operator+(QString &&s1, const QString &s2)
QVariant fromValue(T &&value)
qsizetype size() const const
QString simplified() const const
void chop(qsizetype n)
QString normalized(NormalizationForm mode, QChar::UnicodeVersion version) const const
QString & operator+=(QChar ch)
QString & insert(qsizetype position, QChar ch)
QString rightJustified(qsizetype width, QChar fill, bool truncate) const const
CaseSensitivity
QString trimmed() const const
typedef reference
A QString wrapper class for containing whether a string is safe or needs to be escaped.
Definition: safestring.h:91
QString right(qsizetype n) const const
void push_back(QChar ch)
QString toLower() const const
typedef SplitBehavior
The NestedString is a QString whose methods always return a SafeString.
Definition: safestring.h:163
bool operator==(QLatin1StringView s1, const QString &s2)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
const QChar * unicode() const const
QString mid(qsizetype position, qsizetype n) const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
typedef SectionFlags
QString & setNum(double n, char format, int precision)
void push_front(QChar ch)
QString leftJustified(qsizetype width, QChar fill, bool truncate) const const
QString section(QChar sep, qsizetype start, qsizetype end, SectionFlags flags) const const
QString left(qsizetype n) const const
UnicodeVersion
QString & setUtf16(const ushort *unicode, qsizetype size)
QString repeated(qsizetype times) const const
QString & setUnicode(const QChar *unicode, qsizetype size)
The string is safe and requires no further escaping.
Definition: safestring.h:98
QString & operator=(QChar ch)