Grantlee  5.1.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_templates_export.h"
00025 
00026 #include <QtCore/QString>
00027 #include <QtCore/QVariant>
00028 
00029 namespace Grantlee
00030 {
00031 
00033 
00092 class GRANTLEE_TEMPLATES_EXPORT SafeString
00093 {
00094 public:
00098   enum Safety {
00099     IsSafe,   
00100     IsNotSafe 
00101 
00102 
00103   };
00104 
00108   SafeString();
00109 
00113   SafeString(const SafeString &safeString);
00114 
00120   SafeString(const QString &str, bool safe);
00121 
00127   /* implicit */ SafeString(const QString &str,
00128                             Safety safety
00129                             = IsNotSafe); // krazy:exclude=explicit
00130 
00134   ~SafeString();
00135 
00136 #ifndef Q_QDOC
00137 
00140   void setNeedsEscape(bool needsEscape);
00141 #endif
00142 
00146   bool needsEscape() const;
00147 
00151   bool isSafe() const;
00152 
00153 #ifndef Q_QDOC
00154 
00157   void setSafety(Safety safety);
00158 #endif
00159 
00167   class GRANTLEE_TEMPLATES_EXPORT NestedString : public QString
00168   {
00169 #ifndef Q_QDOC
00170     friend class SafeString;
00171     SafeString *m_safeString;
00172 
00173   public:
00174     NestedString(SafeString *safeString);
00175     NestedString(const QString &content, SafeString *safeString);
00176 
00177     SafeString &append(const SafeString &str);
00178     SafeString &append(const QString &str);
00179     SafeString &append(const QStringRef &reference);
00180     SafeString &append(const QLatin1String &str);
00181 #ifndef QT_NO_CAST_FROM_ASCII
00182     SafeString &append(const QByteArray &ba)
00183     {
00184       QString::append(ba);
00185       m_safeString->m_safety = IsNotSafe;
00186       return *m_safeString;
00187     }
00188 
00189     SafeString &append(const char *str)
00190     {
00191       QString::append(str);
00192       m_safeString->m_safety = IsNotSafe;
00193       return *m_safeString;
00194     }
00195 #endif
00196     SafeString &append(const QChar ch);
00197 
00198     SafeString &fill(QChar ch, int size = -1);
00199 
00200     SafeString &insert(int position, const SafeString &str);
00201     SafeString &insert(int position, const QString &str);
00202     SafeString &insert(int position, const QLatin1String &str);
00203     SafeString &insert(int position, const QChar *unicode, int size);
00204     SafeString &insert(int position, QChar ch);
00205 
00206     SafeString left(int n) const;
00207     SafeString leftJustified(int width, QChar fill = QLatin1Char(' '),
00208                              bool truncate = false) const;
00209     SafeString mid(int position, int n = -1) const;
00210 
00211     SafeString normalized(NormalizationForm mode) const;
00212     SafeString normalized(NormalizationForm mode,
00213                           QChar::UnicodeVersion version) const;
00214 
00215     SafeString &prepend(const SafeString &str);
00216     SafeString &prepend(const QString &str);
00217     SafeString &prepend(const QLatin1String &str);
00218 #ifndef QT_NO_CAST_FROM_ASCII
00219     SafeString &prepend(const QByteArray &ba)
00220     {
00221       QString::prepend(ba);
00222       m_safeString->m_safety = IsNotSafe;
00223       return *m_safeString;
00224     }
00225 
00226     SafeString &prepend(const char *str)
00227     {
00228       QString::prepend(str);
00229       m_safeString->m_safety = IsNotSafe;
00230       return *m_safeString;
00231     }
00232 #endif
00233     SafeString &prepend(QChar ch);
00234 
00235     void push_back(const SafeString &other);
00236     void push_front(const SafeString &other);
00237 
00238     SafeString &remove(int position, int n);
00239     SafeString &remove(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive);
00240     SafeString &remove(const SafeString &str,
00241                        Qt::CaseSensitivity cs = Qt::CaseSensitive);
00242     SafeString &remove(const QString &str,
00243                        Qt::CaseSensitivity cs = Qt::CaseSensitive);
00244     SafeString &remove(const QRegularExpression &rx);
00245     SafeString repeated(int times) const;
00246     SafeString &replace(int position, int n, const SafeString &after);
00247     SafeString &replace(int position, int n, const QString &after);
00248     SafeString &replace(int position, int n, const QChar *unicode, int size);
00249     SafeString &replace(int position, int n, QChar after);
00250     SafeString &replace(const SafeString &before, const SafeString &after,
00251                         Qt::CaseSensitivity cs = Qt::CaseSensitive);
00252     SafeString &replace(const QString &before, const SafeString &after,
00253                         Qt::CaseSensitivity cs = Qt::CaseSensitive);
00254     SafeString &replace(const SafeString &before, const QString &after,
00255                         Qt::CaseSensitivity cs = Qt::CaseSensitive);
00256     SafeString &replace(const QString &before, const QString &after,
00257                         Qt::CaseSensitivity cs = Qt::CaseSensitive);
00258     SafeString &replace(const QChar *before, int blen, const QChar *after,
00259                         int alen, Qt::CaseSensitivity cs = Qt::CaseSensitive);
00260     SafeString &replace(QChar ch, const SafeString &after,
00261                         Qt::CaseSensitivity cs = Qt::CaseSensitive);
00262     SafeString &replace(QChar ch, const QString &after,
00263                         Qt::CaseSensitivity cs = Qt::CaseSensitive);
00264     SafeString &replace(QChar before, QChar after,
00265                         Qt::CaseSensitivity cs = Qt::CaseSensitive);
00266     SafeString &replace(const QLatin1String &before, const QLatin1String &after,
00267                         Qt::CaseSensitivity cs = Qt::CaseSensitive);
00268     SafeString &replace(const QLatin1String &before, const SafeString &after,
00269                         Qt::CaseSensitivity cs = Qt::CaseSensitive);
00270     SafeString &replace(const QLatin1String &before, const QString &after,
00271                         Qt::CaseSensitivity cs = Qt::CaseSensitive);
00272     SafeString &replace(const SafeString &before, const QLatin1String &after,
00273                         Qt::CaseSensitivity cs = Qt::CaseSensitive);
00274     SafeString &replace(const QString &before, const QLatin1String &after,
00275                         Qt::CaseSensitivity cs = Qt::CaseSensitive);
00276     SafeString &replace(QChar c, const QLatin1String &after,
00277                         Qt::CaseSensitivity cs = Qt::CaseSensitive);
00278     SafeString &replace(const QRegularExpression &rx, const SafeString &after);
00279     SafeString &replace(const QRegularExpression &rx, const QString &after);
00280 
00281     SafeString right(int n) const;
00282     SafeString rightJustified(int width, QChar fill = QLatin1Char(' '),
00283                               bool truncate = false) const;
00284 
00285     SafeString section(QChar sep, int start, int end = -1,
00286                        SectionFlags flags = SectionDefault) const;
00287     SafeString section(const SafeString &sep, int start, int end = -1,
00288                        SectionFlags flags = SectionDefault) const;
00289     SafeString section(const QString &sep, int start, int end = -1,
00290                        SectionFlags flags = SectionDefault) const;
00291     SafeString section(const QRegularExpression &reg, int start, int end = -1,
00292                        SectionFlags flags = SectionDefault) const;
00293     SafeString &setNum(int n, int base = 10);
00294     SafeString &setNum(uint n, int base = 10);
00295     SafeString &setNum(long n, int base = 10);
00296     SafeString &setNum(ulong n, int base = 10);
00297     SafeString &setNum(qlonglong n, int base = 10);
00298     SafeString &setNum(qulonglong n, int base = 10);
00299     SafeString &setNum(short n, int base = 10);
00300     SafeString &setNum(ushort n, int base = 10);
00301     SafeString &setNum(double n, char format = 'g', int precision = 6);
00302     SafeString &setNum(float n, char format = 'g', int precision = 6);
00303     SafeString &setUnicode(const QChar *unicode, int size);
00304     SafeString &setUtf16(const ushort *unicode, int size);
00305     SafeString simplified() const;
00306 
00307     QStringList split(const SafeString &sep,
00308                       SplitBehavior behavior = KeepEmptyParts,
00309                       Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
00310     QStringList split(const QString &sep,
00311                       SplitBehavior behavior = KeepEmptyParts,
00312                       Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
00313     QStringList split(const QChar &sep, SplitBehavior behavior = KeepEmptyParts,
00314                       Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
00315     QStringList split(const QRegularExpression &rx,
00316                       SplitBehavior behavior = KeepEmptyParts) const;
00317 
00318     SafeString toLower() const;
00319     SafeString toUpper() const;
00320     SafeString trimmed() const;
00321 
00322     void chop(int n);
00323 #endif
00324   };
00325 
00329   const NestedString &get() const { return m_nestedString; }
00330 
00334   NestedString &get() { return m_nestedString; }
00335 
00339   operator QString() const { return m_nestedString; }
00340 
00344   SafeString &operator=(const SafeString &str);
00345 
00351   SafeString operator+(const QString &str);
00352 
00358   SafeString operator+(const SafeString &str);
00359 
00365   SafeString &operator+=(const QString &str);
00366 
00372   SafeString &operator+=(const SafeString &str);
00373 
00379   bool operator==(const SafeString &other) const;
00380 
00386   bool operator==(const QString &other) const;
00387 
00391   operator QVariant() const { return QVariant::fromValue(*this); }
00392 
00393 private:
00394 #ifndef Q_QDOC
00395   NestedString m_nestedString;
00396 #endif
00397   Safety m_safety;
00398   bool m_needsescape;
00399 };
00400 }
00401 
00402 Q_DECLARE_METATYPE(Grantlee::SafeString)
00403 
00404 #endif