File indexing completed on 2024-04-28 15:22:58

0001 /*
0002  * This file is part of the DOM implementation for KDE.
0003  *
0004  * Copyright 1999 Lars Knoll (knoll@kde.org)
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Library General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Library General Public License
0017  * along with this library; see the file COPYING.LIB.  If not, write to
0018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020  *
0021  */
0022 #ifndef _DOM_DOMString_h_
0023 #define _DOM_DOMString_h_
0024 
0025 #include <khtml_export.h>
0026 #include "khtml_debug.h"
0027 #include <QString>
0028 #include <limits.h>
0029 
0030 namespace DOM
0031 {
0032 
0033 class DOMStringImpl;
0034 
0035 /**
0036  * This class implements the basic string we use in the DOM. We do not use
0037  * QString for 2 reasons: Memory overhead, and the missing explicit sharing
0038  * of strings we need for the DOM.
0039  *
0040  * All DOMStrings are explicitly shared (they behave like pointers), meaning
0041  * that modifications to one instance will also modify all others. If you
0042  * wish to get a DOMString that is independent, use copy().
0043  */
0044 class KHTML_EXPORT DOMString
0045 {
0046     friend class CharacterDataImpl;
0047     friend KHTML_EXPORT bool operator==(const DOMString &a, const char *b);
0048 public:
0049     /**
0050      * default constructor. Gives an empty DOMString
0051      */
0052     DOMString() : impl(nullptr) {}
0053 
0054     DOMString(const QChar *str, uint len);
0055     DOMString(const QString &);
0056     DOMString(const char *str);
0057     /**
0058      * @since 4.2
0059      */
0060     DOMString(const char *str, uint len);
0061     DOMString(DOMStringImpl *i);
0062 
0063     virtual ~DOMString();
0064 
0065     // assign and copy
0066     DOMString(const DOMString &str);
0067     DOMString &operator =(const DOMString &str);
0068 
0069     /**
0070      * append str to this string
0071      */
0072     DOMString &operator += (const DOMString &str);
0073     /**
0074      * add two DOMString's
0075      */
0076     DOMString operator + (const DOMString &str);
0077 
0078     void insert(DOMString str, uint pos);
0079 
0080     /**
0081      * The character at position i of the DOMString. If i >= length(), the
0082      * character returned will be 0.
0083      */
0084     const QChar &operator [](unsigned int i) const;
0085 
0086     int find(const QChar c, int start = 0) const;
0087     int reverseFind(const QChar c, int start = -1) const;
0088 
0089     DOMString substring(unsigned pos, unsigned len = UINT_MAX) const;
0090 
0091     uint length() const;
0092     void truncate(unsigned int len);
0093     void remove(unsigned int pos, int len = 1);
0094     /**
0095      * Splits the string into two. The original string gets truncated to pos, and the rest is returned.
0096      */
0097     DOMString split(unsigned int pos);
0098 
0099     /**
0100      * Returns a lowercase version of the string
0101      */
0102     DOMString lower() const;
0103     /**
0104      * Returns an uppercase version of the string
0105      */
0106     DOMString upper() const;
0107 
0108     QChar *unicode() const;
0109     // for WebCore API compatibility
0110     inline QChar *characters() const
0111     {
0112         return unicode();
0113     }
0114     QString string() const;
0115 
0116     int toInt() const;
0117     int toInt(bool *ok) const;
0118     float toFloat(bool *ok = nullptr) const;
0119     bool percentage(int &_percentage) const;
0120 
0121     static DOMString number(float f);
0122 
0123     DOMString copy() const;
0124 
0125     bool isNull()  const
0126     {
0127         return (impl == nullptr);
0128     }
0129     bool isEmpty()  const;
0130 
0131     bool endsWith(const DOMString &str) const;
0132     bool startsWith(const DOMString &str) const;
0133 
0134     /**
0135      * Returns a string with Space Characters removed from the start and the end.
0136      * Space Characters as defined in
0137      * https://dev.w3.org/html5/spec-LC/common-microsyntaxes.html#space-character
0138      */
0139     DOMString trimSpaces() const;
0140 
0141     /**
0142      * @internal get a handle to the imlementation of the DOMString
0143      * Use at own risk!!!
0144      */
0145     DOMStringImpl *implementation() const
0146     {
0147         return impl;
0148     }
0149 
0150     static DOMString format(const char *format, ...)
0151 #if defined(__GNUC__)
0152     __attribute__((format(printf, 1, 2)))
0153 #endif
0154     ;
0155 
0156 protected:
0157     DOMStringImpl *impl;
0158 };
0159 
0160 inline QDebug operator<<(QDebug stream, const DOMString &string)
0161 {
0162     return (stream << (string.implementation() ? string.string() : QString::fromLatin1("null")));
0163 }
0164 
0165 KHTML_EXPORT bool operator==(const DOMString &a, const DOMString &b);
0166 KHTML_EXPORT bool operator==(const DOMString &a, const QString &b);
0167 KHTML_EXPORT bool operator==(const DOMString &a, const char *b);
0168 inline bool operator!=(const DOMString &a, const DOMString &b)
0169 {
0170     return !(a == b);
0171 }
0172 inline bool operator!=(const DOMString &a, const QString &b)
0173 {
0174     return !(a == b);
0175 }
0176 inline bool operator!=(const DOMString &a, const char *b)
0177 {
0178     return !(a == b);
0179 }
0180 inline bool strcmp(const DOMString &a, const DOMString &b)
0181 {
0182     return a != b;
0183 }
0184 
0185 // returns false when equal, true otherwise (ignoring case)
0186 KHTML_EXPORT bool strcasecmp(const DOMString &a, const DOMString &b);
0187 KHTML_EXPORT bool strcasecmp(const DOMString &a, const char *b);
0188 
0189 }
0190 #endif