2 * This file is part of the DOM implementation for KDE.
4 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
5 * Copyright (C) 2003 Dirk Mueller (mueller@kde.org)
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.
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.
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.
23 #ifndef _DOM_DOMStringImpl_h_
24 #define _DOM_DOMStringImpl_h_
26 #include <QtCore/QString>
30 #include "dom/dom_string.h"
31 #include "dom/dom_misc.h"
32 #include "misc/khtmllayout.h"
33 #include "misc/shared.h"
35 #define QT_ALLOC_QCHAR_VEC( N ) (QChar*) new char[ sizeof(QChar)*( N ) ]
36 #define QT_DELETE_QCHAR_VEC( P ) delete[] ((char*)( P ))
40 class DOMStringImpl
: public khtml::Shared
<DOMStringImpl
>
43 DOMStringImpl(const DOMStringImpl
&);
44 //DOMStringImpl& operator=(const DOMStringImpl&);
46 DOMStringImpl() { s
= 0, l
= 0; m_hash
= 0; m_inTable
= 0; }
48 DOMStringImpl(const QChar
*str
, unsigned int len
) : m_hash(0), m_inTable(0) {
49 bool havestr
= str
&& len
;
50 s
= QT_ALLOC_QCHAR_VEC( havestr
? len
: 1 );
52 memcpy( s
, str
, len
* sizeof(QChar
) );
61 explicit DOMStringImpl(const char *str
);
62 explicit DOMStringImpl(const char *str
, uint len
);
63 explicit DOMStringImpl(const QChar
&ch
) : m_hash(0), m_inTable(0) {
64 s
= QT_ALLOC_QCHAR_VEC( 1 );
69 DOMStringImpl(const QChar
* str
, unsigned length
, unsigned hash
) : m_hash(hash
), m_inTable(true) {
70 bool havestr
= str
&& length
;
71 s
= QT_ALLOC_QCHAR_VEC( havestr
? length
: 1 );
73 memcpy( s
, str
, length
* sizeof(QChar
) );
82 DOMStringImpl(const char* str
, unsigned length
, unsigned hash
);
86 void append(DOMStringImpl
*str
);
87 void insert(DOMStringImpl
*str
, unsigned int pos
);
88 void truncate(int len
);
89 void remove(unsigned int pos
, int len
=1);
90 DOMStringImpl
*split(unsigned int pos
);
91 DOMStringImpl
*copy() const {
92 return new DOMStringImpl(s
,l
);
96 DOMStringImpl
*substring(unsigned int pos
, unsigned int len
);
97 DOMStringImpl
*collapseWhiteSpace(bool preserveLF
, bool preserveWS
);
99 const QChar
&operator [] (int pos
) { return s
[pos
]; }
100 bool containsOnlyWhitespace() const;
102 // ignores trailing garbage, unlike QString
103 int toInt(bool* ok
= 0) const;
104 float toFloat(bool* ok
= 0) const;
106 khtml::Length
* toLengthArray(int& len
) const;
107 khtml::Length
* toCoordsArray(int& len
) const;
108 bool isLower() const;
109 DOMStringImpl
*lower() const;
110 DOMStringImpl
*upper() const;
111 DOMStringImpl
*capitalize(bool noFirstCap
=false) const;
112 DOMStringImpl
*escapeHTML();
114 QChar
*unicode() const { return s
; }
115 uint
length() const { return l
; }
116 QString
string() const;
118 bool endsWith(DOMStringImpl
* str
) const;
119 bool startsWith(DOMStringImpl
* str
) const;
121 DOMStringImpl
* substring(unsigned pos
, unsigned len
= UINT_MAX
) const;
123 // Note: this actually computes the hash, so shouldn't be used lightly
124 unsigned hash() const;
126 // for WebCore API compatibility;
127 static unsigned computeHash(const QChar
* str
, unsigned int length
) { return DOMStringImpl(str
, length
).hash(); }
128 static unsigned computeHash(const char* str
) { return DOMStringImpl(str
).hash(); }
130 static DOMStringImpl
* empty();
134 mutable unsigned m_hash
;
138 inline unsigned int qHash(const DOMString
& key
) {
139 // 82610334 - hash value for empty string ""
140 return key
.implementation() ? key
.implementation()->hash() : 82610334;
143 inline bool strcmp(const DOMStringImpl
* a
, const DOMStringImpl
* b
)
146 return (a
&& a
->l
) || (b
&& b
->l
);
147 return a
->l
!= b
->l
|| memcmp(a
->s
, b
->s
, a
->l
* sizeof(QChar
));
150 bool strcasecmp(const DOMStringImpl
* a
, const DOMStringImpl
* b
);
161 // WebCore::StringHash is the default hash for StringImpl* and RefPtr<StringImpl>
162 template<typename T
> struct DefaultHash
;
163 template<> struct DefaultHash
<DOM::DOMStringImpl
*> {
164 typedef khtml::StringHash Hash
;
166 /*template<> struct DefaultHash<RefPtr<DOM::DOMStringImpl> > {
167 typedef khtml::StringHash Hash;