2 * This file is part of the DOM implementation for KDE.
4 * Copyright 1999 Lars Knoll (knoll@kde.org)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #include "dom/dom_string.h"
23 #include "xml/dom_stringimpl.h"
25 #include <wtf/Vector.h>
30 DOMString::DOMString(const QChar
*str
, uint len
)
32 impl
= new DOMStringImpl( str
, len
);
36 DOMString::DOMString(const QString
&str
)
43 impl
= new DOMStringImpl( str
.unicode(), str
.length() );
47 DOMString::DOMString(const char *str
)
54 impl
= new DOMStringImpl( str
);
58 DOMString::DOMString(const char *str
, uint len
)
64 impl
= new DOMStringImpl(str
, len
);
68 DOMString::DOMString(DOMStringImpl
*i
)
74 DOMString::DOMString(const DOMString
&other
)
80 DOMString::~DOMString()
82 if(impl
) impl
->deref();
85 DOMString
&DOMString::operator =(const DOMString
&other
)
87 if ( impl
!= other
.impl
) {
88 if(impl
) impl
->deref();
95 DOMString
&DOMString::operator += (const DOMString
&str
)
107 DOMStringImpl
*i
= impl
->copy();
111 impl
->append(str
.impl
);
116 DOMString
DOMString::operator + (const DOMString
&str
)
118 if(!impl
) return str
.copy();
121 DOMString s
= copy();
129 void DOMString::insert(DOMString str
, uint pos
)
133 impl
= str
.impl
->copy();
137 impl
->insert(str
.impl
, pos
);
141 const QChar
&DOMString::operator [](unsigned int i
) const
143 static const QChar nullChar
= 0;
145 if(!impl
|| i
>= impl
->l
) return nullChar
;
150 int DOMString::find(const QChar c
, int start
) const
152 unsigned int l
= start
;
153 if(!impl
|| l
>= impl
->l
) return -1;
156 if( *(impl
->s
+l
) == c
) return l
;
162 int DOMString::reverseFind(const QChar c
, int start
) const
164 unsigned int l
= start
;
165 if (!impl
|| l
< -impl
->l
) return -1;
168 if (*(impl
->s
+ l
) == c
) return l
;
176 DOMString
DOMString::substring(unsigned pos
, unsigned len
) const
178 return (impl
) ? impl
->substring(pos
, len
) : DOMString();
181 uint
DOMString::length() const
187 void DOMString::truncate( unsigned int len
)
189 if(impl
) impl
->truncate(len
);
192 void DOMString::remove(unsigned int pos
, int len
)
194 if(impl
) impl
->remove(pos
, len
);
197 DOMString
DOMString::split(unsigned int pos
)
199 if(!impl
) return DOMString();
200 return impl
->split(pos
);
203 DOMString
DOMString::lower() const
205 if(!impl
) return DOMString();
206 return impl
->lower();
209 DOMString
DOMString::upper() const
211 if(!impl
) return DOMString();
212 return impl
->upper();
215 bool DOMString::percentage(int &_percentage
) const
217 if(!impl
|| !impl
->l
) return false;
219 if ( *(impl
->s
+impl
->l
-1) != QChar('%'))
222 _percentage
= QString::fromRawData(impl
->s
, impl
->l
-1).toInt();
226 QChar
*DOMString::unicode() const
229 return impl
->unicode();
232 QString
DOMString::string() const
234 if(!impl
) return QString();
236 return impl
->string();
239 int DOMString::toInt() const
243 return impl
->toInt();
246 int DOMString::toInt(bool* ok
) const
253 return impl
->toInt(ok
);
256 float DOMString::toFloat(bool* ok
) const
263 return impl
->toInt(ok
);
266 DOMString
DOMString::number(float f
)
268 return DOMString(QString::number(f
));
271 DOMString
DOMString::copy() const
273 if(!impl
) return DOMString();
277 bool DOMString::endsWith(const DOMString
& str
) const
279 if (str
.length() > length()) return false;
280 return impl
->endsWith(str
.implementation());
283 bool DOMString::startsWith(const DOMString
& str
) const
285 if (str
.length() > length()) return false;
286 return impl
->startsWith(str
.implementation());
289 // ------------------------------------------------------------------------
291 bool DOM::strcasecmp( const DOMString
&as
, const DOMString
&bs
)
293 return strcasecmp(as
.implementation(), bs
.implementation());
296 bool DOM::strcasecmp( const DOMString
&as
, const char* bs
)
298 const QChar
*a
= as
.unicode();
300 if ( !bs
) return ( l
!= 0 );
302 if ( a
->toLatin1() != *bs
) {
303 char cc
= ( ( *bs
>= 'A' ) && ( *bs
<= 'Z' ) ) ? ( ( *bs
) + 'a' - 'A' ) : ( *bs
);
304 if ( a
->toLower().toLatin1() != cc
) return true;
308 return ( *bs
!= '\0' );
311 bool DOMString::isEmpty() const
313 return (!impl
|| impl
->l
== 0);
316 DOMString
DOMString::format(const char *format
, ...)
319 va_start(args
, format
);
321 Vector
<char, 256> buffer
;
323 // Do the format once to get the length.
325 int result
= _vscprintf(format
, args
);
328 int result
= vsnprintf(&ch
, 1, format
, args
);
329 // We need to call va_end() and then va_start() again here, as the
330 // contents of args is undefined after the call to vsnprintf
331 // according to http://man.cx/snprintf(3)
333 // Not calling va_end/va_start here happens to work on lots of
334 // systems, but fails e.g. on 64bit Linux.
336 va_start(args
, format
);
340 return DOMString("");
343 unsigned len
= result
;
344 buffer
.grow(len
+ 1);
346 // Now do the formatting again, guaranteed to fit.
347 vsnprintf(buffer
.data(), buffer
.size(), format
, args
);
351 buffer
[len
] = 0; // we don't really need this I guess
352 return new DOMStringImpl(buffer
.data()/*, len*/);
355 //-----------------------------------------------------------------------------
357 bool DOM::operator==( const DOMString
&a
, const DOMString
&b
)
359 return !strcmp(a
.implementation(), b
.implementation());
362 bool DOM::operator==( const DOMString
&a
, const QString
&b
)
366 if( l
!= b
.length() ) return false;
368 if(!memcmp(a
.unicode(), b
.unicode(), l
*sizeof(QChar
)))
373 bool DOM::operator==( const DOMString
&a
, const char *b
)
375 DOMStringImpl
* aimpl
= a
.impl
;
376 if ( !b
) return !aimpl
;
380 const QChar
*aptr
= aimpl
->s
;
382 unsigned char c
= *b
++;
383 if ( !c
|| ( *aptr
++ ).unicode() != c
)