1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/**
* This file is part of the DOM implementation for KDE.
*
* (C) 1999 Lars Knoll (knoll@kde.org)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "dom/dom_string.h"
#include "xml/dom_stringimpl.h"
using namespace DOM;
DOMString::DOMString(const QChar *str, uint len)
{
impl = new DOMStringImpl( str, len );
impl->ref();
}
DOMString::DOMString(const QString &str)
{
if (str.isNull()) {
impl = 0;
return;
}
impl = new DOMStringImpl( str.unicode(), str.length() );
impl->ref();
}
DOMString::DOMString(const char *str)
{
if (!str) {
impl = 0;
return;
}
impl = new DOMStringImpl( str );
impl->ref();
}
DOMString::DOMString(DOMStringImpl *i)
{
impl = i;
if(impl) impl->ref();
}
DOMString::DOMString(const DOMString &other)
{
impl = other.impl;
if(impl) impl->ref();
}
DOMString::~DOMString()
{
if(impl) impl->deref();
}
DOMString &DOMString::operator =(const DOMString &other)
{
if ( impl != other.impl ) {
if(impl) impl->deref();
impl = other.impl;
if(impl) impl->ref();
}
return *this;
}
DOMString &DOMString::operator += (const DOMString &str)
{
if(!impl)
{
// ### FIXME!!!
impl = str.impl;
if (impl)
impl->ref();
return *this;
}
if(str.impl)
{
DOMStringImpl *i = impl->copy();
impl->deref();
impl = i;
impl->ref();
impl->append(str.impl);
}
return *this;
}
DOMString DOMString::operator + (const DOMString &str)
{
if(!impl) return str.copy();
if(str.impl)
{
DOMString s = copy();
s += str;
return s;
}
return copy();
}
void DOMString::insert(DOMString str, uint pos)
{
if(!impl)
{
impl = str.impl->copy();
impl->ref();
}
else
impl->insert(str.impl, pos);
}
const QChar &DOMString::operator [](unsigned int i) const
{
static const QChar nullChar = 0;
if(!impl || i >= impl->l ) return nullChar;
return *(impl->s+i);
}
int DOMString::find(const QChar c, int start) const
{
unsigned int l = start;
if(!impl || l >= impl->l ) return -1;
while( l < impl->l )
{
if( *(impl->s+l) == c ) return l;
l++;
}
return -1;
}
uint DOMString::length() const
{
if(!impl) return 0;
return impl->l;
}
void DOMString::truncate( unsigned int len )
{
if(impl) impl->truncate(len);
}
void DOMString::remove(unsigned int pos, int len)
{
if(impl) impl->remove(pos, len);
}
DOMString DOMString::split(unsigned int pos)
{
if(!impl) return DOMString();
return impl->split(pos);
}
DOMString DOMString::lower() const
{
if(!impl) return DOMString();
return impl->lower();
}
DOMString DOMString::upper() const
{
if(!impl) return DOMString();
return impl->upper();
}
bool DOMString::percentage(int &_percentage) const
{
if(!impl || !impl->l) return false;
if ( *(impl->s+impl->l-1) != QChar('%'))
return false;
_percentage = QConstString(impl->s, impl->l-1).string().toInt();
return true;
}
QChar *DOMString::unicode() const
{
if(!impl) return 0;
return impl->unicode();
}
QString DOMString::string() const
{
if(!impl) return QString::null;
return impl->string();
}
int DOMString::toInt() const
{
if(!impl) return 0;
return impl->toInt();
}
DOMString DOMString::copy() const
{
if(!impl) return DOMString();
return impl->copy();
}
// ------------------------------------------------------------------------
bool DOM::strcasecmp( const DOMString &as, const DOMString &bs )
{
if ( as.length() != bs.length() ) return true;
const QChar *a = as.unicode();
const QChar *b = bs.unicode();
if ( a == b ) return false;
if ( !( a && b ) ) return true;
int l = as.length();
while ( l-- ) {
if ( *a != *b && a->lower() != b->lower() ) return true;
a++,b++;
}
return false;
}
bool DOM::strcasecmp( const DOMString &as, const char* bs )
{
const QChar *a = as.unicode();
int l = as.length();
if ( !bs ) return ( l != 0 );
while ( l-- ) {
if ( a->latin1() != *bs ) {
char cc = ( ( *bs >= 'A' ) && ( *bs <= 'Z' ) ) ? ( ( *bs ) + 'a' - 'A' ) : ( *bs );
if ( a->lower().latin1() != cc ) return true;
}
a++, bs++;
}
return ( *bs != '\0' );
}
bool DOMString::isEmpty() const
{
return (!impl || impl->l == 0);
}
//-----------------------------------------------------------------------------
bool DOM::operator==( const DOMString &a, const DOMString &b )
{
unsigned int l = a.length();
if( l != b.length() ) return false;
if(!memcmp(a.unicode(), b.unicode(), l*sizeof(QChar)))
return true;
return false;
}
bool DOM::operator==( const DOMString &a, const QString &b )
{
unsigned int l = a.length();
if( l != b.length() ) return false;
if(!memcmp(a.unicode(), b.unicode(), l*sizeof(QChar)))
return true;
return false;
}
bool DOM::operator==( const DOMString &a, const char *b )
{
DOMStringImpl* aimpl = a.impl;
if ( !b ) return !aimpl;
if ( aimpl ) {
int alen = aimpl->l;
const QChar *aptr = aimpl->s;
while ( alen-- ) {
unsigned char c = *b++;
if ( !c || ( *aptr++ ).unicode() != c )
return false;
}
}
return !*b;
}
|