Grantlee  0.4.0
templates/lib/safestring.h
00001 /*
00002   This file is part of the Grantlee template system.
00003 
00004   Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
00005 
00006   This library is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU Lesser General Public
00008   License as published by the Free Software Foundation; either version
00009   2.1 of the Licence, or (at your option) any later version.
00010 
00011   This library is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   Lesser General Public License for more details.
00015 
00016   You should have received a copy of the GNU Lesser General Public
00017   License along with this library.  If not, see <http://www.gnu.org/licenses/>.
00018 
00019 */
00020 
00021 #ifndef GRANTLEE_SAFESTRING_H
00022 #define GRANTLEE_SAFESTRING_H
00023 
00024 #include "grantlee_core_export.h"
00025 
00026 #include <QtCore/QString>
00027 #include <QtCore/QVariant>
00028 
00029 namespace Grantlee
00030 {
00031 
00033 
00073 class GRANTLEE_CORE_EXPORT SafeString
00074 {
00075 public:
00079   enum Safety {
00080     IsSafe,    
00081     IsNotSafe  
00082   };
00083 
00087   SafeString();
00088 
00092   SafeString( const SafeString &safeString );
00093 
00097   SafeString( const QString &str, bool safe );
00098 
00102   /* implicit */ SafeString( const QString &str, Safety safety = IsNotSafe ); // krazy:exclude=explicit
00103 
00107   ~SafeString();
00108 
00109 #ifndef Q_QDOC
00110 
00113   void setNeedsEscape( bool needsEscape );
00114 #endif
00115 
00119   bool needsEscape() const;
00120 
00124   bool isSafe() const;
00125 
00126 #ifndef Q_QDOC
00127 
00130   void setSafety( Safety safety );
00131 #endif
00132 
00138   class GRANTLEE_CORE_EXPORT NestedString : public QString
00139   {
00140 #ifndef Q_QDOC
00141     friend class SafeString;
00142     SafeString *m_safeString;
00143   public:
00144     NestedString( SafeString *safeString );
00145     NestedString( const QString &content, SafeString *safeString );
00146 
00147     SafeString& append( const SafeString &str );
00148     SafeString& append( const QString &str );
00149     SafeString& append( const QStringRef &reference );
00150     SafeString& append( const QLatin1String &str );
00151     // BIC: Use QT_NO_CAST_FROM_ASCII instead.
00152 #ifndef GRANTLEE_NO_CAST_FROM_ASCII
00153     SafeString& append( const QByteArray &ba );
00154     SafeString& append( const char *str );
00155 #endif
00156     SafeString& append( const QChar ch );
00157 
00158     SafeString& fill( QChar ch, int size = -1 );
00159 
00160     SafeString& insert( int position, const SafeString &str );
00161     SafeString& insert( int position, const QString &str );
00162     SafeString& insert( int position, const QLatin1String &str );
00163     SafeString& insert( int position, const QChar *unicode, int size );
00164     SafeString& insert( int position, QChar ch );
00165 
00166     SafeString left( int n ) const;
00167     SafeString leftJustified( int width, QChar fill = QLatin1Char( ' ' ), bool truncate = false ) const;
00168     SafeString mid( int position, int n = -1 ) const;
00169 
00170     SafeString normalized( NormalizationForm mode ) const;
00171     SafeString normalized( NormalizationForm mode, QChar::UnicodeVersion version ) const;
00172 
00173     SafeString& prepend( const SafeString &str );
00174     SafeString& prepend( const QString &str );
00175     SafeString& prepend( const QLatin1String &str );
00176 #ifndef GRANTLEE_NO_CAST_FROM_ASCII
00177     SafeString& prepend( const QByteArray &ba );
00178     SafeString& prepend( const char *str );
00179 #endif
00180     SafeString& prepend( QChar ch );
00181 
00182     void push_back( const SafeString& other );
00183     void push_front( const SafeString& other );
00184 
00185     SafeString& remove( int position, int n );
00186     SafeString& remove( QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00187     SafeString& remove( const SafeString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00188     SafeString& remove( const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00189     SafeString& remove( const QRegExp &rx );
00190     SafeString repeated( int times ) const;
00191     SafeString& replace( int position, int n, const SafeString &after );
00192     SafeString& replace( int position, int n, const QString &after );
00193     SafeString& replace( int position, int n, const QChar *unicode, int size );
00194     SafeString& replace( int position, int n, QChar after );
00195     SafeString& replace( const SafeString &before, const SafeString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00196     SafeString& replace( const QString &before, const SafeString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00197     SafeString& replace( const SafeString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00198     SafeString& replace( const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00199     SafeString& replace( const QChar *before, int blen, const QChar *after, int alen, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00200     SafeString& replace( QChar ch, const SafeString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00201     SafeString& replace( QChar ch, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00202     SafeString& replace( QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00203     SafeString& replace( const QLatin1String &before, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00204     SafeString& replace( const QLatin1String &before, const SafeString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00205     SafeString& replace( const QLatin1String &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00206     SafeString& replace( const SafeString &before, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00207     SafeString& replace( const QString &before, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00208     SafeString& replace( QChar c, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive );
00209     SafeString& replace( const QRegExp &rx, const SafeString &after );
00210     SafeString& replace( const QRegExp &rx, const QString &after );
00211 
00212     SafeString right( int n ) const;
00213     SafeString rightJustified( int width, QChar fill = QLatin1Char( ' ' ), bool truncate = false ) const;
00214 
00215     SafeString section( QChar sep, int start, int end = -1, SectionFlags flags = SectionDefault ) const;
00216     SafeString section( const SafeString& sep, int start, int end = -1, SectionFlags flags = SectionDefault ) const;
00217     SafeString section( const QString& sep, int start, int end = -1, SectionFlags flags = SectionDefault ) const;
00218     SafeString section( const QRegExp& reg, int start, int end = -1, SectionFlags flags = SectionDefault ) const;
00219     SafeString& setNum( int n, int base = 10 );
00220     SafeString& setNum( uint n, int base = 10 );
00221     SafeString& setNum( long n, int base = 10 );
00222     SafeString& setNum( ulong n, int base = 10 );
00223     SafeString& setNum( qlonglong n, int base = 10 );
00224     SafeString& setNum( qulonglong n, int base = 10 );
00225     SafeString& setNum( short n, int base = 10 );
00226     SafeString& setNum( ushort n, int base = 10 );
00227     SafeString& setNum( double n, char format = 'g', int precision = 6 );
00228     SafeString& setNum( float n, char format = 'g', int precision = 6 );
00229     SafeString& setUnicode( const QChar * unicode, int size );
00230     SafeString& setUtf16( const ushort * unicode, int size );
00231     SafeString simplified() const;
00232 
00233     QStringList split( const SafeString& sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const;
00234     QStringList split( const QString& sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const;
00235     QStringList split( const QChar & sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const;
00236     QStringList split( const QRegExp & rx, SplitBehavior behavior = KeepEmptyParts ) const;
00237 
00238     SafeString toLower() const;
00239     SafeString toUpper() const;
00240     SafeString trimmed() const;
00241 
00242     void chop( int n );
00243 #endif
00244   };
00245 
00249   const NestedString& get() const {
00250     return m_nestedString;
00251   }
00252 
00256   NestedString& get() {
00257     return m_nestedString;
00258   }
00259 
00263   operator QString() const {
00264     return m_nestedString;
00265   }
00266 
00270   SafeString &operator=( const SafeString &str );
00271 
00277   SafeString operator+( const QString &str );
00278 
00284   SafeString operator+( const SafeString &str );
00285 
00291   SafeString &operator+=( const QString &str );
00292 
00298   SafeString &operator+=( const SafeString &str );
00299 
00305   bool operator==( const SafeString &other ) const;
00306 
00312   bool operator==( const QString &other ) const;
00313 
00317   operator QVariant() const {
00318     return QVariant::fromValue( *this );
00319   }
00320 
00321 private:
00322 #ifndef Q_QDOC
00323   NestedString m_nestedString;
00324 #endif
00325   Safety m_safety;
00326   bool m_needsescape;
00327 };
00328 
00329 }
00330 
00331 Q_DECLARE_METATYPE( Grantlee::SafeString )
00332 
00333 #endif
00334