dom_string.cpp
#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;
}
DOM::DOMString::lower
DOMString lower() const
Returns a lowercase version of the string.
Definition: dom_string.cpp:173
DOM::DOMString::split
DOMString split(unsigned int pos)
Splits the string into two.
Definition: dom_string.cpp:167
DOM::DOMString::operator+
DOMString operator+(const DOMString &str)
add two DOMString's
Definition: dom_string.cpp:105
DOM::DOMString
This class implements the basic string we use in the DOM.
Definition: dom_string.h:43
DOM::DOMString::DOMString
DOMString()
default constructor.
Definition: dom_string.h:51
DOM::DOMString::operator+=
DOMString & operator+=(const DOMString &str)
append str to this string
Definition: dom_string.cpp:84
DOM::DOMString::operator[]
const QChar & operator[](unsigned int i) const
The character at position i of the DOMString.
Definition: dom_string.cpp:130
DOM::DOMString::upper
DOMString upper() const
Returns an uppercase version of the string.
Definition: dom_string.cpp:179

KDE Home | KDE Accessibility Home | Description of Access Keys