Grantlee  5.0.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 
73 class GRANTLEE_TEMPLATES_EXPORT SafeString
74 {
75 public:
79  enum Safety {
81  IsNotSafe
82  };
83 
87  SafeString();
88 
92  SafeString( const SafeString &safeString );
93 
97  SafeString( const QString &str, bool safe );
98 
102  /* implicit */ SafeString( const QString &str, Safety safety = IsNotSafe ); // krazy:exclude=explicit
103 
107  ~SafeString();
108 
109 #ifndef Q_QDOC
110 
113  void setNeedsEscape( bool needsEscape );
114 #endif
115 
119  bool needsEscape() const;
120 
124  bool isSafe() const;
125 
126 #ifndef Q_QDOC
127 
130  void setSafety( Safety safety );
131 #endif
132 
138  class GRANTLEE_TEMPLATES_EXPORT NestedString : public QString
139  {
140 #ifndef Q_QDOC
141  friend class SafeString;
142  SafeString *m_safeString;
143  public:
144  NestedString( SafeString *safeString );
145  NestedString( const QString &content, SafeString *safeString );
146 
147  SafeString& append( const SafeString &str );
148  SafeString& append( const QString &str );
149  SafeString& append( const QStringRef &reference );
150  SafeString& append( const QLatin1String &str );
151 #ifndef QT_NO_CAST_FROM_ASCII
152  SafeString& append( const QByteArray &ba )
153  {
154  QString::append( ba );
155  m_safeString->m_safety = IsNotSafe;
156  return *m_safeString;
157  }
158 
159  SafeString& append( const char *str )
160  {
161  QString::append( str );
162  m_safeString->m_safety = IsNotSafe;
163  return *m_safeString;
164  }
165 #endif
166  SafeString& append( const QChar ch );
167 
168  SafeString& fill( QChar ch, int size = -1 );
169 
170  SafeString& insert( int position, const SafeString &str );
171  SafeString& insert( int position, const QString &str );
172  SafeString& insert( int position, const QLatin1String &str );
173  SafeString& insert( int position, const QChar *unicode, int size );
174  SafeString& insert( int position, QChar ch );
175 
176  SafeString left( int n ) const;
177  SafeString leftJustified( int width, QChar fill = QLatin1Char( ' ' ), bool truncate = false ) const;
178  SafeString mid( int position, int n = -1 ) const;
179 
180  SafeString normalized( NormalizationForm mode ) const;
181  SafeString normalized( NormalizationForm mode, QChar::UnicodeVersion version ) const;
182 
183  SafeString& prepend( const SafeString &str );
184  SafeString& prepend( const QString &str );
185  SafeString& prepend( const QLatin1String &str );
186 #ifndef QT_NO_CAST_FROM_ASCII
187  SafeString& prepend( const QByteArray &ba )
188  {
189  QString::prepend( ba );
190  m_safeString->m_safety = IsNotSafe;
191  return *m_safeString;
192  }
193 
194  SafeString& prepend( const char *str )
195  {
196  QString::prepend( str );
197  m_safeString->m_safety = IsNotSafe;
198  return *m_safeString;
199  }
200 #endif
201  SafeString& prepend( QChar ch );
202 
203  void push_back( const SafeString& other );
204  void push_front( const SafeString& other );
205 
206  SafeString& remove( int position, int n );
207  SafeString& remove( QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive );
208  SafeString& remove( const SafeString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive );
209  SafeString& remove( const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive );
210  SafeString& remove( const QRegExp &rx );
211  SafeString repeated( int times ) const;
212  SafeString& replace( int position, int n, const SafeString &after );
213  SafeString& replace( int position, int n, const QString &after );
214  SafeString& replace( int position, int n, const QChar *unicode, int size );
215  SafeString& replace( int position, int n, QChar after );
216  SafeString& replace( const SafeString &before, const SafeString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
217  SafeString& replace( const QString &before, const SafeString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
218  SafeString& replace( const SafeString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
219  SafeString& replace( const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
220  SafeString& replace( const QChar *before, int blen, const QChar *after, int alen, Qt::CaseSensitivity cs = Qt::CaseSensitive );
221  SafeString& replace( QChar ch, const SafeString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
222  SafeString& replace( QChar ch, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
223  SafeString& replace( QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
224  SafeString& replace( const QLatin1String &before, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
225  SafeString& replace( const QLatin1String &before, const SafeString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
226  SafeString& replace( const QLatin1String &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
227  SafeString& replace( const SafeString &before, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
228  SafeString& replace( const QString &before, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
229  SafeString& replace( QChar c, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
230  SafeString& replace( const QRegExp &rx, const SafeString &after );
231  SafeString& replace( const QRegExp &rx, const QString &after );
232 
233  SafeString right( int n ) const;
234  SafeString rightJustified( int width, QChar fill = QLatin1Char( ' ' ), bool truncate = false ) const;
235 
236  SafeString section( QChar sep, int start, int end = -1, SectionFlags flags = SectionDefault ) const;
237  SafeString section( const SafeString& sep, int start, int end = -1, SectionFlags flags = SectionDefault ) const;
238  SafeString section( const QString& sep, int start, int end = -1, SectionFlags flags = SectionDefault ) const;
239  SafeString section( const QRegExp& reg, int start, int end = -1, SectionFlags flags = SectionDefault ) const;
240  SafeString& setNum( int n, int base = 10 );
241  SafeString& setNum( uint n, int base = 10 );
242  SafeString& setNum( long n, int base = 10 );
243  SafeString& setNum( ulong n, int base = 10 );
244  SafeString& setNum( qlonglong n, int base = 10 );
245  SafeString& setNum( qulonglong n, int base = 10 );
246  SafeString& setNum( short n, int base = 10 );
247  SafeString& setNum( ushort n, int base = 10 );
248  SafeString& setNum( double n, char format = 'g', int precision = 6 );
249  SafeString& setNum( float n, char format = 'g', int precision = 6 );
250  SafeString& setUnicode( const QChar * unicode, int size );
251  SafeString& setUtf16( const ushort * unicode, int size );
252  SafeString simplified() const;
253 
254  QStringList split( const SafeString& sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const;
255  QStringList split( const QString& sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const;
256  QStringList split( const QChar & sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const;
257  QStringList split( const QRegExp & rx, SplitBehavior behavior = KeepEmptyParts ) const;
258 
259  SafeString toLower() const;
260  SafeString toUpper() const;
261  SafeString trimmed() const;
262 
263  void chop( int n );
264 #endif
265  };
266 
270  const NestedString& get() const {
271  return m_nestedString;
272  }
273 
277  NestedString& get() {
278  return m_nestedString;
279  }
280 
284  operator QString() const {
285  return m_nestedString;
286  }
287 
291  SafeString &operator=( const SafeString &str );
292 
298  SafeString operator+( const QString &str );
299 
305  SafeString operator+( const SafeString &str );
306 
312  SafeString &operator+=( const QString &str );
313 
319  SafeString &operator+=( const SafeString &str );
320 
326  bool operator==( const SafeString &other ) const;
327 
333  bool operator==( const QString &other ) const;
334 
338  operator QVariant() const {
339  return QVariant::fromValue( *this );
340  }
341 
342 private:
343 #ifndef Q_QDOC
344  NestedString m_nestedString;
345 #endif
346  Safety m_safety;
347  bool m_needsescape;
348 };
349 
350 }
351 
352 Q_DECLARE_METATYPE( Grantlee::SafeString )
353 
354 #endif
The string is safe and requires no further escaping.
Definition: safestring.h:80
The NestedString is a QString whose methods always return a SafeString.
Definition: safestring.h:138
A QString wrapper class for containing whether a string is safe or needs to be escaped.
Definition: safestring.h:73
The Grantlee namespace holds all public Grantlee API.
Definition: Mainpage.dox:7