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 #include <wtf/Vector.h> 00026 00027 using namespace DOM; 00028 00029 00030 DOMString::DOMString(const QChar *str, uint len) 00031 { 00032 if (!str) { 00033 impl = 0; 00034 return; 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(const char *str, uint len) 00063 { 00064 if (!str) { 00065 impl = 0; 00066 return; 00067 } 00068 impl = new DOMStringImpl(str, len); 00069 impl->ref(); 00070 } 00071 00072 DOMString::DOMString(DOMStringImpl *i) 00073 { 00074 impl = i; 00075 if(impl) impl->ref(); 00076 } 00077 00078 DOMString::DOMString(const DOMString &other) 00079 { 00080 impl = other.impl; 00081 if(impl) impl->ref(); 00082 } 00083 00084 DOMString::~DOMString() 00085 { 00086 if(impl) impl->deref(); 00087 } 00088 00089 DOMString &DOMString::operator =(const DOMString &other) 00090 { 00091 if ( impl != other.impl ) { 00092 if(impl) impl->deref(); 00093 impl = other.impl; 00094 if(impl) impl->ref(); 00095 } 00096 return *this; 00097 } 00098 00099 DOMString &DOMString::operator += (const DOMString &str) 00100 { 00101 if(!impl) 00102 { 00103 // ### FIXME!!! 00104 impl = str.impl; 00105 if (impl) 00106 impl->ref(); 00107 return *this; 00108 } 00109 if(str.impl) 00110 { 00111 DOMStringImpl *i = impl->copy(); 00112 impl->deref(); 00113 impl = i; 00114 impl->ref(); 00115 impl->append(str.impl); 00116 } 00117 return *this; 00118 } 00119 00120 DOMString DOMString::operator + (const DOMString &str) 00121 { 00122 if(!impl) return str.copy(); 00123 if(str.impl) 00124 { 00125 DOMString s = copy(); 00126 s += str; 00127 return s; 00128 } 00129 00130 return copy(); 00131 } 00132 00133 void DOMString::insert(DOMString str, uint pos) 00134 { 00135 if(!impl) 00136 { 00137 impl = str.impl->copy(); 00138 impl->ref(); 00139 } 00140 else 00141 impl->insert(str.impl, pos); 00142 } 00143 00144 00145 const QChar &DOMString::operator [](unsigned int i) const 00146 { 00147 static const QChar nullChar = 0; 00148 00149 if(!impl || i >= impl->l ) return nullChar; 00150 00151 return *(impl->s+i); 00152 } 00153 00154 int DOMString::find(const QChar c, int start) const 00155 { 00156 unsigned int l = start; 00157 if(!impl || l >= impl->l ) return -1; 00158 while( l < impl->l ) 00159 { 00160 if( *(impl->s+l) == c ) return l; 00161 l++; 00162 } 00163 return -1; 00164 } 00165 00166 int DOMString::reverseFind(const QChar c, int start) const 00167 { 00168 unsigned int l = start; 00169 if (!impl || l < -impl->l) return -1; 00170 l += impl->l; 00171 while (1) { 00172 if (*(impl->s + l) == c) return l; 00173 l--; 00174 if (l == 0) 00175 return -1; 00176 } 00177 return -1; 00178 } 00179 00180 DOMString DOMString::substring(unsigned pos, unsigned len) const 00181 { 00182 return (impl) ? impl->substring(pos, len) : DOMString(); 00183 } 00184 00185 uint DOMString::length() const 00186 { 00187 if(!impl) return 0; 00188 return impl->l; 00189 } 00190 00191 void DOMString::truncate( unsigned int len ) 00192 { 00193 if(impl) impl->truncate(len); 00194 } 00195 00196 void DOMString::remove(unsigned int pos, int len) 00197 { 00198 if(impl) impl->remove(pos, len); 00199 } 00200 00201 DOMString DOMString::split(unsigned int pos) 00202 { 00203 if(!impl) return DOMString(); 00204 return impl->split(pos); 00205 } 00206 00207 DOMString DOMString::lower() const 00208 { 00209 if(!impl) return DOMString(); 00210 return impl->lower(); 00211 } 00212 00213 DOMString DOMString::upper() const 00214 { 00215 if(!impl) return DOMString(); 00216 return impl->upper(); 00217 } 00218 00219 bool DOMString::percentage(int &_percentage) const 00220 { 00221 if(!impl || !impl->l) return false; 00222 00223 if ( *(impl->s+impl->l-1) != QChar('%')) 00224 return false; 00225 00226 _percentage = QString::fromRawData(impl->s, impl->l-1).toInt(); 00227 return true; 00228 } 00229 00230 QChar *DOMString::unicode() const 00231 { 00232 if(!impl) return 0; 00233 return impl->unicode(); 00234 } 00235 00236 QString DOMString::string() const 00237 { 00238 if(!impl) return QString(); 00239 00240 return impl->string(); 00241 } 00242 00243 int DOMString::toInt() const 00244 { 00245 if(!impl) return 0; 00246 00247 return impl->toInt(); 00248 } 00249 00250 int DOMString::toInt(bool* ok) const 00251 { 00252 if (!impl) { 00253 *ok = false; 00254 return 0; 00255 } 00256 00257 return impl->toInt(ok); 00258 } 00259 00260 float DOMString::toFloat(bool* ok) const 00261 { 00262 if (!impl) { 00263 if (ok) 00264 *ok = false; 00265 return 0; 00266 } 00267 return impl->toFloat(ok); 00268 } 00269 00270 DOMString DOMString::number(float f) 00271 { 00272 return DOMString(QString::number(f)); 00273 } 00274 00275 DOMString DOMString::copy() const 00276 { 00277 if(!impl) return DOMString(); 00278 return impl->copy(); 00279 } 00280 00281 bool DOMString::endsWith(const DOMString& str) const 00282 { 00283 if (str.length() > length()) return false; 00284 return impl->endsWith(str.implementation()); 00285 } 00286 00287 bool DOMString::startsWith(const DOMString& str) const 00288 { 00289 if (str.length() > length()) return false; 00290 return impl->startsWith(str.implementation()); 00291 } 00292 00293 // ------------------------------------------------------------------------ 00294 00295 bool DOM::strcasecmp( const DOMString &as, const DOMString &bs ) 00296 { 00297 return strcasecmp(as.implementation(), bs.implementation()); 00298 } 00299 00300 bool DOM::strcasecmp( const DOMString &as, const char* bs ) 00301 { 00302 const QChar *a = as.unicode(); 00303 int l = as.length(); 00304 if ( !bs ) return ( l != 0 ); 00305 while ( l-- ) { 00306 if ( a->toLatin1() != *bs ) { 00307 char cc = ( ( *bs >= 'A' ) && ( *bs <= 'Z' ) ) ? ( ( *bs ) + 'a' - 'A' ) : ( *bs ); 00308 if ( a->toLower().toLatin1() != cc ) return true; 00309 } 00310 a++, bs++; 00311 } 00312 return ( *bs != '\0' ); 00313 } 00314 00315 bool DOMString::isEmpty() const 00316 { 00317 return (!impl || impl->l == 0); 00318 } 00319 00320 DOMString DOMString::format(const char *format, ...) 00321 { 00322 va_list args; 00323 va_start(args, format); 00324 00325 Vector<char, 256> buffer; 00326 00327 // Do the format once to get the length. 00328 #if COMPILER(MSVC) 00329 int result = _vscprintf(format, args); 00330 #else 00331 char ch; 00332 int result = vsnprintf(&ch, 1, format, args); 00333 // We need to call va_end() and then va_start() again here, as the 00334 // contents of args is undefined after the call to vsnprintf 00335 // according to http://man.cx/snprintf(3) 00336 // 00337 // Not calling va_end/va_start here happens to work on lots of 00338 // systems, but fails e.g. on 64bit Linux. 00339 va_end(args); 00340 va_start(args, format); 00341 #endif 00342 00343 if (result == 0) 00344 return DOMString(""); 00345 if (result < 0) 00346 return DOMString(); 00347 unsigned len = result; 00348 buffer.grow(len + 1); 00349 00350 // Now do the formatting again, guaranteed to fit. 00351 vsnprintf(buffer.data(), buffer.size(), format, args); 00352 00353 va_end(args); 00354 00355 buffer[len] = 0; // we don't really need this I guess 00356 return new DOMStringImpl(buffer.data()/*, len*/); 00357 } 00358 00359 //----------------------------------------------------------------------------- 00360 00361 bool DOM::operator==( const DOMString &a, const DOMString &b ) 00362 { 00363 return !strcmp(a.implementation(), b.implementation()); 00364 } 00365 00366 bool DOM::operator==( const DOMString &a, const QString &b ) 00367 { 00368 int l = a.length(); 00369 00370 if( l != b.length() ) return false; 00371 00372 if(!memcmp(a.unicode(), b.unicode(), l*sizeof(QChar))) 00373 return true; 00374 return false; 00375 } 00376 00377 bool DOM::operator==( const DOMString &a, const char *b ) 00378 { 00379 DOMStringImpl* aimpl = a.impl; 00380 if ( !b ) return !aimpl; 00381 00382 if ( aimpl ) { 00383 int alen = aimpl->l; 00384 const QChar *aptr = aimpl->s; 00385 while ( alen-- ) { 00386 unsigned char c = *b++; 00387 if ( !c || ( *aptr++ ).unicode() != c ) 00388 return false; 00389 } 00390 } 00391 00392 return !*b; 00393 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed May 2 2012 18:52:43 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed May 2 2012 18:52:43 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.