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