File indexing completed on 2024-04-28 11:39:37
0001 /* 0002 * This file is part of the DOM implementation for KDE. 0003 * 0004 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 0005 * Copyright (C) 2003 Dirk Mueller (mueller@kde.org) 0006 * 0007 * This library is free software; you can redistribute it and/or 0008 * modify it under the terms of the GNU Library General Public 0009 * License as published by the Free Software Foundation; either 0010 * version 2 of the License, or (at your option) any later version. 0011 * 0012 * This library is distributed in the hope that it will be useful, 0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0015 * Library General Public License for more details. 0016 * 0017 * You should have received a copy of the GNU Library General Public License 0018 * along with this library; see the file COPYING.LIB. If not, write to 0019 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0020 * Boston, MA 02110-1301, USA. 0021 * 0022 */ 0023 #ifndef _DOM_DOMStringImpl_h_ 0024 #define _DOM_DOMStringImpl_h_ 0025 0026 #include <QString> 0027 0028 #include <limits.h> 0029 0030 #include "dom/dom_string.h" 0031 #include "dom/dom_misc.h" 0032 #include "misc/khtmllayout.h" 0033 #include "misc/shared.h" 0034 0035 #define QT_ALLOC_QCHAR_VEC( N ) reinterpret_cast<QChar*>(new char[ sizeof(QChar)*( N ) ]) 0036 #define QT_DELETE_QCHAR_VEC( P ) delete[] (reinterpret_cast<char*>( P )) 0037 0038 namespace DOM 0039 { 0040 0041 enum CaseSensitivity { CaseSensitive, CaseInsensitive }; 0042 0043 class DOMStringImpl : public khtml::Shared<DOMStringImpl> 0044 { 0045 private: 0046 DOMStringImpl(const DOMStringImpl &); 0047 //DOMStringImpl& operator=(const DOMStringImpl&); 0048 protected: 0049 DOMStringImpl() 0050 { 0051 s = nullptr, l = 0; 0052 m_hash = 0; 0053 m_inTable = 0; 0054 m_shallowCopy = 0; 0055 } 0056 public: 0057 DOMStringImpl(const QChar *str, unsigned int len) : m_hash(0), m_inTable(0), m_shallowCopy(0) 0058 { 0059 bool havestr = str && len; 0060 s = QT_ALLOC_QCHAR_VEC(havestr ? len : 1); 0061 if (str && len) { 0062 memcpy(s, str, len * sizeof(QChar)); 0063 l = len; 0064 } else { 0065 // crash protection 0066 s[0] = 0x0; 0067 l = 0; 0068 } 0069 } 0070 0071 explicit DOMStringImpl(const char *str); 0072 explicit DOMStringImpl(const char *str, uint len); 0073 explicit DOMStringImpl(const QChar &ch) : m_hash(0), m_inTable(0), m_shallowCopy(0) 0074 { 0075 s = QT_ALLOC_QCHAR_VEC(1); 0076 s[0] = ch; 0077 l = 1; 0078 } 0079 0080 DOMStringImpl(const QChar *str, unsigned length, unsigned hash) : m_hash(hash), m_inTable(true), m_shallowCopy(0) 0081 { 0082 bool havestr = str && length; 0083 s = QT_ALLOC_QCHAR_VEC(havestr ? length : 1); 0084 if (str && length) { 0085 memcpy(s, str, length * sizeof(QChar)); 0086 l = length; 0087 } else { 0088 // crash protection 0089 s[0] = 0x0; 0090 l = 0; 0091 } 0092 } 0093 0094 DOMStringImpl(const char *str, unsigned length, unsigned hash); 0095 0096 enum ShallowCopyTag { ShallowCopy }; 0097 // create DOMStringImpl without copying the data, it's really dangerous! 0098 // think a lot, before using it 0099 DOMStringImpl(ShallowCopyTag, QChar *str, unsigned length) : m_hash(0), m_inTable(0), m_shallowCopy(1) 0100 { 0101 s = str; 0102 l = length; 0103 ref(); // guard from automatic deletion 0104 } 0105 0106 ~DOMStringImpl(); 0107 0108 void append(DOMStringImpl *str); 0109 void insert(DOMStringImpl *str, unsigned int pos); 0110 void truncate(int len); 0111 void remove(unsigned int pos, int len = 1); 0112 DOMStringImpl *split(unsigned int pos); 0113 DOMStringImpl *copy() const 0114 { 0115 return new DOMStringImpl(s, l); 0116 } 0117 0118 DOMStringImpl *substring(unsigned int pos, unsigned int len); 0119 DOMStringImpl *collapseWhiteSpace(bool preserveLF, bool preserveWS); 0120 0121 const QChar &operator [](int pos) 0122 { 0123 return s[pos]; 0124 } 0125 bool containsOnlyWhitespace() const; 0126 0127 // ignores trailing garbage, unlike QString 0128 int toInt(bool *ok = nullptr) const; 0129 float toFloat(bool *ok = nullptr) const; 0130 0131 khtml::Length *toLengthArray(int &len) const; 0132 khtml::Length *toCoordsArray(int &len) const; 0133 bool isLower() const; 0134 DOMStringImpl *lower() const; 0135 DOMStringImpl *upper() const; 0136 DOMStringImpl *capitalize(bool noFirstCap = false) const; 0137 DOMStringImpl *escapeHTML(); 0138 0139 QChar *unicode() const 0140 { 0141 return s; 0142 } 0143 uint length() const 0144 { 0145 return l; 0146 } 0147 QString string() const; 0148 0149 bool endsWith(DOMStringImpl *str, CaseSensitivity cs = CaseSensitive) const; 0150 bool startsWith(DOMStringImpl *str, CaseSensitivity cs = CaseSensitive) const; 0151 0152 DOMStringImpl *substring(unsigned pos, unsigned len = UINT_MAX) const; 0153 0154 // Note: this actually computes the hash, so shouldn't be used lightly 0155 unsigned hash() const; 0156 0157 // Unlike the above, these don't even cache the hash; 0158 // they return hashes for lowercase, and upper-case versions 0159 unsigned lowerHash() const; 0160 unsigned upperHash() const; 0161 0162 // for WebCore API compatibility; 0163 static unsigned computeHash(const QChar *str, unsigned int length); 0164 static unsigned computeHash(const char *str) 0165 { 0166 return DOMStringImpl(str).hash(); 0167 } 0168 0169 static DOMStringImpl *empty(); 0170 0171 QChar *s; 0172 unsigned int l; 0173 mutable unsigned m_hash; 0174 bool m_inTable : 1; 0175 bool m_shallowCopy : 1; 0176 }; 0177 0178 inline unsigned int qHash(const DOMString &key) 0179 { 0180 // 82610334 - hash value for empty string "" 0181 return key.implementation() ? key.implementation()->hash() : 82610334; 0182 } 0183 0184 inline bool strcmp(const DOMStringImpl *a, const DOMStringImpl *b) 0185 { 0186 if (!(a && b)) { 0187 return (a && a->l) || (b && b->l); 0188 } 0189 return a->l != b->l || memcmp(a->s, b->s, a->l * sizeof(QChar)); 0190 } 0191 0192 bool strcasecmp(const DOMStringImpl *a, const DOMStringImpl *b); 0193 0194 } 0195 0196 namespace khtml 0197 { 0198 struct StringHash; 0199 } 0200 0201 namespace WTF 0202 { 0203 // WebCore::StringHash is the default hash for StringImpl* and RefPtr<StringImpl> 0204 template<typename T> struct DefaultHash; 0205 template<> struct DefaultHash<DOM::DOMStringImpl *> { 0206 typedef khtml::StringHash Hash; 0207 }; 0208 /*template<> struct DefaultHash<RefPtr<DOM::DOMStringImpl> > { 0209 typedef khtml::StringHash Hash; 0210 };*/ 0211 0212 } 0213 0214 #endif