2 * This file is part of the DOM implementation for KDE.
4 * (C) 1999 Lars Knoll (knoll@kde.org)
5 * Copyright (C) 2004 Apple Computer, Inc.
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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #include "dom/dom_string.h"
24 #include "xml/dom_stringimpl.h"
30 DOMString::DOMString(const QChar *str, uint len)
38 impl = DOMStringImpl::empty();
40 impl = new DOMStringImpl(str, len);
44 DOMString::DOMString(const QString &str)
52 impl = DOMStringImpl::empty();
54 impl = new DOMStringImpl(str.unicode(), str.length());
58 DOMString::DOMString(const char *str)
67 impl = DOMStringImpl::empty();
69 impl = new DOMStringImpl(str, l);
73 DOMString::DOMString(DOMStringImpl *i)
79 DOMString::DOMString(const DOMString &other)
85 DOMString &DOMString::operator =(const DOMString &other)
87 if ( impl != other.impl ) {
88 if(impl) impl->deref();
95 DOMString &DOMString::operator += (const DOMString &str)
106 DOMStringImpl *i = impl->copy();
110 impl->append(str.impl);
115 DOMString operator + (const DOMString &a, const DOMString &b)
121 DOMString c = a.copy();
126 void DOMString::insert(DOMString str, uint pos)
130 impl = str.impl->copy();
134 impl->insert(str.impl, pos);
138 const QChar &DOMString::operator [](unsigned int i) const
140 static const QChar nullChar = 0;
142 if(!impl || i >= impl->l ) return nullChar;
147 int DOMString::find(const QChar c, int start) const
149 unsigned int l = start;
150 if(!impl || l >= impl->l ) return -1;
153 if( *(impl->s+l) == c ) return l;
159 uint DOMString::length() const
165 void DOMString::truncate( unsigned int len )
167 if(impl) impl->truncate(len);
170 void DOMString::remove(unsigned int pos, int len)
172 if(impl) impl->remove(pos, len);
175 DOMString DOMString::substring(unsigned int pos, unsigned int len) const
179 return impl->substring(pos, len);
182 DOMString DOMString::split(unsigned int pos)
184 if(!impl) return DOMString();
185 return impl->split(pos);
188 DOMString DOMString::lower() const
190 if(!impl) return DOMString();
191 return impl->lower();
194 DOMString DOMString::upper() const
196 if(!impl) return DOMString();
197 return impl->upper();
200 bool DOMString::percentage(int &_percentage) const
202 if(!impl || !impl->l) return false;
204 if ( *(impl->s+impl->l-1) != QChar('%'))
207 _percentage = QConstString(impl->s, impl->l-1).string().toInt();
211 QChar *DOMString::unicode() const
217 QString DOMString::string() const
219 if(!impl) return QString::null;
221 return QString(impl->s, impl->l);
224 int DOMString::toInt() const
228 return impl->toInt();
231 DOMString DOMString::copy() const
233 if(!impl) return DOMString();
237 // ------------------------------------------------------------------------
239 bool DOM::strcasecmp( const DOMString &as, const DOMString &bs )
241 if ( as.length() != bs.length() ) return true;
243 const QChar *a = as.unicode();
244 const QChar *b = bs.unicode();
245 if ( a == b ) return false;
246 if ( !( a && b ) ) return true;
249 if ( *a != *b && a->lower() != b->lower() ) return true;
255 bool DOM::strcasecmp( const DOMString &as, const char* bs )
257 const QChar *a = as.unicode();
259 if ( !bs ) return ( l != 0 );
261 if ( a->latin1() != *bs ) {
262 char cc = ( ( *bs >= 'A' ) && ( *bs <= 'Z' ) ) ? ( ( *bs ) + 'a' - 'A' ) : ( *bs );
263 if ( a->lower().latin1() != cc ) return true;
267 return ( *bs != '\0' );
270 bool DOMString::isEmpty() const
272 return (!impl || impl->l == 0);
275 khtml::Length* DOMString::toLengthArray(int& len) const
277 return impl ? impl->toLengthArray(len) : 0;
281 const char *DOMString::ascii() const
283 return impl ? impl->ascii() : "(null impl)";
287 //-----------------------------------------------------------------------------
289 bool DOM::operator==( const DOMString &a, const DOMString &b )
291 if (a.impl == b.impl)
294 unsigned int l = a.length();
296 if( l != b.length() ) return false;
298 if(!memcmp(a.unicode(), b.unicode(), l*sizeof(QChar)))
303 bool DOM::operator==( const DOMString &a, const QString &b )
305 unsigned int l = a.length();
307 if( l != b.length() ) return false;
309 if(!memcmp(a.unicode(), b.unicode(), l*sizeof(QChar)))
314 bool DOM::operator==( const DOMString &a, const char *b )
316 DOMStringImpl *aimpl = a.impl;
323 const QChar *aptr = aimpl->s;
325 unsigned char c = *b++;
326 if (!c || (*aptr++).unicode() != c)