KHtml

dom_stringimpl.h
1 /*
2  * This file is part of the DOM implementation for KDE.
3  *
4  * Copyright (C) 1999 Lars Knoll ([email protected])
5  * Copyright (C) 2003 Dirk Mueller ([email protected])
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23 #ifndef _DOM_DOMStringImpl_h_
24 #define _DOM_DOMStringImpl_h_
25 
26 #include <QString>
27 
28 #include <limits.h>
29 
30 #include "dom/dom_string.h"
31 #include "dom/dom_misc.h"
32 #include "misc/khtmllayout.h"
33 #include "misc/shared.h"
34 
35 #define QT_ALLOC_QCHAR_VEC( N ) reinterpret_cast<QChar*>(new char[ sizeof(QChar)*( N ) ])
36 #define QT_DELETE_QCHAR_VEC( P ) delete[] (reinterpret_cast<char*>( P ))
37 
38 namespace DOM
39 {
40 
42 
43 class DOMStringImpl : public khtml::Shared<DOMStringImpl>
44 {
45 private:
46  DOMStringImpl(const DOMStringImpl &);
47  //DOMStringImpl& operator=(const DOMStringImpl&);
48 protected:
49  DOMStringImpl()
50  {
51  s = nullptr, l = 0;
52  m_hash = 0;
53  m_inTable = 0;
54  m_shallowCopy = 0;
55  }
56 public:
57  DOMStringImpl(const QChar *str, unsigned int len) : m_hash(0), m_inTable(0), m_shallowCopy(0)
58  {
59  bool havestr = str && len;
60  s = QT_ALLOC_QCHAR_VEC(havestr ? len : 1);
61  if (str && len) {
62  memcpy(s, str, len * sizeof(QChar));
63  l = len;
64  } else {
65  // crash protection
66  s[0] = 0x0;
67  l = 0;
68  }
69  }
70 
71  explicit DOMStringImpl(const char *str);
72  explicit DOMStringImpl(const char *str, uint len);
73  explicit DOMStringImpl(const QChar &ch) : m_hash(0), m_inTable(0), m_shallowCopy(0)
74  {
75  s = QT_ALLOC_QCHAR_VEC(1);
76  s[0] = ch;
77  l = 1;
78  }
79 
80  DOMStringImpl(const QChar *str, unsigned length, unsigned hash) : m_hash(hash), m_inTable(true), m_shallowCopy(0)
81  {
82  bool havestr = str && length;
83  s = QT_ALLOC_QCHAR_VEC(havestr ? length : 1);
84  if (str && length) {
85  memcpy(s, str, length * sizeof(QChar));
86  l = length;
87  } else {
88  // crash protection
89  s[0] = 0x0;
90  l = 0;
91  }
92  }
93 
94  DOMStringImpl(const char *str, unsigned length, unsigned hash);
95 
96  enum ShallowCopyTag { ShallowCopy };
97  // create DOMStringImpl without copying the data, it's really dangerous!
98  // think a lot, before using it
99  DOMStringImpl(ShallowCopyTag, QChar *str, unsigned length) : m_hash(0), m_inTable(0), m_shallowCopy(1)
100  {
101  s = str;
102  l = length;
103  ref(); // guard from automatic deletion
104  }
105 
106  ~DOMStringImpl();
107 
108  void append(DOMStringImpl *str);
109  void insert(DOMStringImpl *str, unsigned int pos);
110  void truncate(int len);
111  void remove(unsigned int pos, int len = 1);
112  DOMStringImpl *split(unsigned int pos);
113  DOMStringImpl *copy() const
114  {
115  return new DOMStringImpl(s, l);
116  }
117 
118  DOMStringImpl *substring(unsigned int pos, unsigned int len);
119  DOMStringImpl *collapseWhiteSpace(bool preserveLF, bool preserveWS);
120 
121  const QChar &operator [](int pos)
122  {
123  return s[pos];
124  }
125  bool containsOnlyWhitespace() const;
126 
127  // ignores trailing garbage, unlike QString
128  int toInt(bool *ok = nullptr) const;
129  float toFloat(bool *ok = nullptr) const;
130 
131  khtml::Length *toLengthArray(int &len) const;
132  khtml::Length *toCoordsArray(int &len) const;
133  bool isLower() const;
134  DOMStringImpl *lower() const;
135  DOMStringImpl *upper() const;
136  DOMStringImpl *capitalize(bool noFirstCap = false) const;
137  DOMStringImpl *escapeHTML();
138 
139  QChar *unicode() const
140  {
141  return s;
142  }
143  uint length() const
144  {
145  return l;
146  }
147  QString string() const;
148 
149  bool endsWith(DOMStringImpl *str, CaseSensitivity cs = CaseSensitive) const;
150  bool startsWith(DOMStringImpl *str, CaseSensitivity cs = CaseSensitive) const;
151 
152  DOMStringImpl *substring(unsigned pos, unsigned len = UINT_MAX) const;
153 
154  // Note: this actually computes the hash, so shouldn't be used lightly
155  unsigned hash() const;
156 
157  // Unlike the above, these don't even cache the hash;
158  // they return hashes for lowercase, and upper-case versions
159  unsigned lowerHash() const;
160  unsigned upperHash() const;
161 
162  // for WebCore API compatibility;
163  static unsigned computeHash(const QChar *str, unsigned int length);
164  static unsigned computeHash(const char *str)
165  {
166  return DOMStringImpl(str).hash();
167  }
168 
169  static DOMStringImpl *empty();
170 
171  QChar *s;
172  unsigned int l;
173  mutable unsigned m_hash;
174  bool m_inTable : 1;
175  bool m_shallowCopy : 1;
176 };
177 
178 inline unsigned int qHash(const DOMString &key)
179 {
180  // 82610334 - hash value for empty string ""
181  return key.implementation() ? key.implementation()->hash() : 82610334;
182 }
183 
184 inline bool strcmp(const DOMStringImpl *a, const DOMStringImpl *b)
185 {
186  if (!(a && b)) {
187  return (a && a->l) || (b && b->l);
188  }
189  return a->l != b->l || memcmp(a->s, b->s, a->l * sizeof(QChar));
190 }
191 
192 bool strcasecmp(const DOMStringImpl *a, const DOMStringImpl *b);
193 
194 }
195 
196 namespace khtml
197 {
198 struct StringHash;
199 }
200 
201 namespace WTF
202 {
203 // WebCore::StringHash is the default hash for StringImpl* and RefPtr<StringImpl>
204 template<typename T> struct DefaultHash;
205 template<> struct DefaultHash<DOM::DOMStringImpl *> {
206  typedef khtml::StringHash Hash;
207 };
208 /*template<> struct DefaultHash<RefPtr<DOM::DOMStringImpl> > {
209  typedef khtml::StringHash Hash;
210 };*/
211 
212 }
213 
214 #endif
This file is part of the HTML rendering engine for KDE.
void ref()
bool truncate(Part &part)
bool insert(Part *part, qint64 *insertId=nullptr)
KIOCORE_EXPORT CopyJob * copy(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
CaseSensitivity
KCALENDARCORE_EXPORT uint qHash(const KCalendarCore::Period &key)
This library provides a full-featured HTML parser and widget.
Definition: css_base.h:371
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 22:48:13 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.