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