Grantlee  0.5.1
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_core_export.h"
25 
26 #include <QtCore/QString>
27 #include <QtCore/QVariant>
28 
29 namespace Grantlee
30 {
31 
33 
73 class GRANTLEE_CORE_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_CORE_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  // BIC: Use QT_NO_CAST_FROM_ASCII instead.
152 #ifndef GRANTLEE_NO_CAST_FROM_ASCII
153  SafeString& append( const QByteArray &ba );
154  SafeString& append( const char *str );
155 #endif
156  SafeString& append( const QChar ch );
157 
158  SafeString& fill( QChar ch, int size = -1 );
159 
160  SafeString& insert( int position, const SafeString &str );
161  SafeString& insert( int position, const QString &str );
162  SafeString& insert( int position, const QLatin1String &str );
163  SafeString& insert( int position, const QChar *unicode, int size );
164  SafeString& insert( int position, QChar ch );
165 
166  SafeString left( int n ) const;
167  SafeString leftJustified( int width, QChar fill = QLatin1Char( ' ' ), bool truncate = false ) const;
168  SafeString mid( int position, int n = -1 ) const;
169 
170  SafeString normalized( NormalizationForm mode ) const;
171  SafeString normalized( NormalizationForm mode, QChar::UnicodeVersion version ) const;
172 
173  SafeString& prepend( const SafeString &str );
174  SafeString& prepend( const QString &str );
175  SafeString& prepend( const QLatin1String &str );
176 #ifndef GRANTLEE_NO_CAST_FROM_ASCII
177  SafeString& prepend( const QByteArray &ba );
178  SafeString& prepend( const char *str );
179 #endif
180  SafeString& prepend( QChar ch );
181 
182  void push_back( const SafeString& other );
183  void push_front( const SafeString& other );
184 
185  SafeString& remove( int position, int n );
186  SafeString& remove( QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive );
187  SafeString& remove( const SafeString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive );
188  SafeString& remove( const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive );
189  SafeString& remove( const QRegExp &rx );
190  SafeString repeated( int times ) const;
191  SafeString& replace( int position, int n, const SafeString &after );
192  SafeString& replace( int position, int n, const QString &after );
193  SafeString& replace( int position, int n, const QChar *unicode, int size );
194  SafeString& replace( int position, int n, QChar after );
195  SafeString& replace( const SafeString &before, const SafeString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
196  SafeString& replace( const QString &before, const SafeString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
197  SafeString& replace( const SafeString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
198  SafeString& replace( const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
199  SafeString& replace( const QChar *before, int blen, const QChar *after, int alen, Qt::CaseSensitivity cs = Qt::CaseSensitive );
200  SafeString& replace( QChar ch, const SafeString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
201  SafeString& replace( QChar ch, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
202  SafeString& replace( QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
203  SafeString& replace( const QLatin1String &before, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
204  SafeString& replace( const QLatin1String &before, const SafeString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
205  SafeString& replace( const QLatin1String &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
206  SafeString& replace( const SafeString &before, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
207  SafeString& replace( const QString &before, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
208  SafeString& replace( QChar c, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
209  SafeString& replace( const QRegExp &rx, const SafeString &after );
210  SafeString& replace( const QRegExp &rx, const QString &after );
211 
212  SafeString right( int n ) const;
213  SafeString rightJustified( int width, QChar fill = QLatin1Char( ' ' ), bool truncate = false ) const;
214 
215  SafeString section( QChar sep, int start, int end = -1, SectionFlags flags = SectionDefault ) const;
216  SafeString section( const SafeString& sep, int start, int end = -1, SectionFlags flags = SectionDefault ) const;
217  SafeString section( const QString& sep, int start, int end = -1, SectionFlags flags = SectionDefault ) const;
218  SafeString section( const QRegExp& reg, int start, int end = -1, SectionFlags flags = SectionDefault ) const;
219  SafeString& setNum( int n, int base = 10 );
220  SafeString& setNum( uint n, int base = 10 );
221  SafeString& setNum( long n, int base = 10 );
222  SafeString& setNum( ulong n, int base = 10 );
223  SafeString& setNum( qlonglong n, int base = 10 );
224  SafeString& setNum( qulonglong n, int base = 10 );
225  SafeString& setNum( short n, int base = 10 );
226  SafeString& setNum( ushort n, int base = 10 );
227  SafeString& setNum( double n, char format = 'g', int precision = 6 );
228  SafeString& setNum( float n, char format = 'g', int precision = 6 );
229  SafeString& setUnicode( const QChar * unicode, int size );
230  SafeString& setUtf16( const ushort * unicode, int size );
231  SafeString simplified() const;
232 
233  QStringList split( const SafeString& sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const;
234  QStringList split( const QString& sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const;
235  QStringList split( const QChar & sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const;
236  QStringList split( const QRegExp & rx, SplitBehavior behavior = KeepEmptyParts ) const;
237 
238  SafeString toLower() const;
239  SafeString toUpper() const;
240  SafeString trimmed() const;
241 
242  void chop( int n );
243 #endif
244  };
245 
249  const NestedString& get() const {
250  return m_nestedString;
251  }
252 
256  NestedString& get() {
257  return m_nestedString;
258  }
259 
263  operator QString() const {
264  return m_nestedString;
265  }
266 
270  SafeString &operator=( const SafeString &str );
271 
277  SafeString operator+( const QString &str );
278 
284  SafeString operator+( const SafeString &str );
285 
291  SafeString &operator+=( const QString &str );
292 
298  SafeString &operator+=( const SafeString &str );
299 
305  bool operator==( const SafeString &other ) const;
306 
312  bool operator==( const QString &other ) const;
313 
317  operator QVariant() const {
318  return QVariant::fromValue( *this );
319  }
320 
321 private:
322 #ifndef Q_QDOC
323  NestedString m_nestedString;
324 #endif
325  Safety m_safety;
326  bool m_needsescape;
327 };
328 
329 }
330 
331 Q_DECLARE_METATYPE( Grantlee::SafeString )
332 
333 #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