dom_string.cpp
00001
00022 #include "dom/dom_string.h"
00023 #include "xml/dom_stringimpl.h"
00024
00025
00026 using namespace DOM;
00027
00028
00029 DOMString::DOMString()
00030 {
00031 impl = 0;
00032 }
00033
00034 DOMString::DOMString(const QChar *str, uint len)
00035 {
00036 impl = new DOMStringImpl( str, len );
00037 impl->ref();
00038 }
00039
00040 DOMString::DOMString(const QString &str)
00041 {
00042 if (str.isNull()) {
00043 impl = 0;
00044 return;
00045 }
00046
00047 impl = new DOMStringImpl( str.unicode(), str.length() );
00048 impl->ref();
00049 }
00050
00051 DOMString::DOMString(const char *str)
00052 {
00053 if (!str) {
00054 impl = 0;
00055 return;
00056 }
00057
00058 impl = new DOMStringImpl( str );
00059 impl->ref();
00060 }
00061
00062 DOMString::DOMString(DOMStringImpl *i)
00063 {
00064 impl = i;
00065 if(impl) impl->ref();
00066 }
00067
00068 DOMString::DOMString(const DOMString &other)
00069 {
00070 impl = other.impl;
00071 if(impl) impl->ref();
00072 }
00073
00074 DOMString::~DOMString()
00075 {
00076 if(impl) impl->deref();
00077 }
00078
00079 DOMString &DOMString::operator =(const DOMString &other)
00080 {
00081 if ( impl != other.impl ) {
00082 if(impl) impl->deref();
00083 impl = other.impl;
00084 if(impl) impl->ref();
00085 }
00086 return *this;
00087 }
00088
00089 DOMString &DOMString::operator += (const DOMString &str)
00090 {
00091 if(!impl)
00092 {
00093
00094 impl = str.impl;
00095 impl->ref();
00096 return *this;
00097 }
00098 if(str.impl)
00099 {
00100 DOMStringImpl *i = impl->copy();
00101 impl->deref();
00102 impl = i;
00103 impl->ref();
00104 impl->append(str.impl);
00105 }
00106 return *this;
00107 }
00108
00109 DOMString DOMString::operator + (const DOMString &str)
00110 {
00111 if(!impl) return str.copy();
00112 if(str.impl)
00113 {
00114 DOMString s = copy();
00115 s += str;
00116 return s;
00117 }
00118
00119 return copy();
00120 }
00121
00122 void DOMString::insert(DOMString str, uint pos)
00123 {
00124 if(!impl)
00125 {
00126 impl = str.impl->copy();
00127 impl->ref();
00128 }
00129 else
00130 impl->insert(str.impl, pos);
00131 }
00132
00133
00134 const QChar &DOMString::operator [](unsigned int i) const
00135 {
00136 static const QChar nullChar = 0;
00137
00138 if(!impl || i >= impl->l ) return nullChar;
00139
00140 return *(impl->s+i);
00141 }
00142
00143 int DOMString::find(const QChar c, int start) const
00144 {
00145 unsigned int l = start;
00146 if(!impl || l >= impl->l ) return -1;
00147 while( l < impl->l )
00148 {
00149 if( *(impl->s+l) == c ) return l;
00150 l++;
00151 }
00152 return -1;
00153 }
00154
00155 uint DOMString::length() const
00156 {
00157 if(!impl) return 0;
00158 return impl->l;
00159 }
00160
00161 void DOMString::truncate( unsigned int len )
00162 {
00163 if(impl) impl->truncate(len);
00164 }
00165
00166 void DOMString::remove(unsigned int pos, int len)
00167 {
00168 if(impl) impl->remove(pos, len);
00169 }
00170
00171 DOMString DOMString::split(unsigned int pos)
00172 {
00173 if(!impl) return DOMString();
00174 return impl->split(pos);
00175 }
00176
00177 DOMString DOMString::lower() const
00178 {
00179 if(!impl) return DOMString();
00180 return impl->lower();
00181 }
00182
00183 DOMString DOMString::upper() const
00184 {
00185 if(!impl) return DOMString();
00186 return impl->upper();
00187 }
00188
00189 bool DOMString::percentage(int &_percentage) const
00190 {
00191 if(!impl || !impl->l) return false;
00192
00193 if ( *(impl->s+impl->l-1) != QChar('%'))
00194 return false;
00195
00196 _percentage = QConstString(impl->s, impl->l-1).string().toInt();
00197 return true;
00198 }
00199
00200 QChar *DOMString::unicode() const
00201 {
00202 if(!impl) return 0;
00203 return impl->s;
00204 }
00205
00206 QString DOMString::string() const
00207 {
00208 if(!impl) return QString::null;
00209
00210 return QString(impl->s, impl->l);
00211 }
00212
00213 int DOMString::toInt() const
00214 {
00215 if(!impl) return 0;
00216
00217 return impl->toInt();
00218 }
00219
00220 DOMString DOMString::copy() const
00221 {
00222 if(!impl) return DOMString();
00223 return impl->copy();
00224 }
00225
00226
00227
00228 bool DOM::strcasecmp( const DOMString &as, const DOMString &bs )
00229 {
00230 if ( as.length() != bs.length() ) return true;
00231
00232 const QChar *a = as.unicode();
00233 const QChar *b = bs.unicode();
00234 if ( a == b ) return false;
00235 if ( !( a && b ) ) return true;
00236 int l = as.length();
00237 while ( l-- ) {
00238 if ( *a != *b && a->lower() != b->lower() ) return true;
00239 a++,b++;
00240 }
00241 return false;
00242 }
00243
00244 bool DOM::strcasecmp( const DOMString &as, const char* bs )
00245 {
00246 const QChar *a = as.unicode();
00247 int l = as.length();
00248 if ( !bs ) return ( l != 0 );
00249 while ( l-- ) {
00250 if ( a->latin1() != *bs ) {
00251 char cc = ( ( *bs >= 'A' ) && ( *bs <= 'Z' ) ) ? ( ( *bs ) + 'a' - 'A' ) : ( *bs );
00252 if ( a->lower().latin1() != cc ) return true;
00253 }
00254 a++, bs++;
00255 }
00256 return ( *bs != '\0' );
00257 }
00258
00259 bool DOMString::isEmpty() const
00260 {
00261 return (!impl || impl->l == 0);
00262 }
00263
00264
00265
00266 bool DOM::operator==( const DOMString &a, const DOMString &b )
00267 {
00268 unsigned int l = a.length();
00269
00270 if( l != b.length() ) return false;
00271
00272 if(!memcmp(a.unicode(), b.unicode(), l*sizeof(QChar)))
00273 return true;
00274 return false;
00275 }
00276
00277 bool DOM::operator==( const DOMString &a, const QString &b )
00278 {
00279 unsigned int l = a.length();
00280
00281 if( l != b.length() ) return false;
00282
00283 if(!memcmp(a.unicode(), b.unicode(), l*sizeof(QChar)))
00284 return true;
00285 return false;
00286 }
00287
00288 bool DOM::operator==( const DOMString &a, const char *b )
00289 {
00290 if ( !b ) return a.isNull();
00291 unsigned int blen = strlen(b);
00292 if ( a.isNull() ) return (blen == 0);
00293 if(a.length() != blen) return false;
00294
00295 const QChar* aptr = a.impl->s;
00296 while( blen-- ) {
00297 if((*aptr++).latin1() != *b++)
00298 return false;
00299 }
00300
00301 return true;
00302 }
00303
00304
00305
This file is part of the documentation for kdelibs Version 3.1.0.