khtml Library API Documentation

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(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 // ### FIXME!!! 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 = QConstString(impl->s, impl->l-1).string().toInt(); 00193 return true; 00194 } 00195 00196 QChar *DOMString::unicode() const 00197 { 00198 if(!impl) return 0; 00199 return impl->s; 00200 } 00201 00202 QString DOMString::string() const 00203 { 00204 if(!impl) return QString::null; 00205 00206 return QString(impl->s, impl->l); 00207 } 00208 00209 int DOMString::toInt() const 00210 { 00211 if(!impl) return 0; 00212 00213 return impl->toInt(); 00214 } 00215 00216 DOMString DOMString::copy() const 00217 { 00218 if(!impl) return DOMString(); 00219 return impl->copy(); 00220 } 00221 00222 // ------------------------------------------------------------------------ 00223 00224 bool DOM::strcasecmp( const DOMString &as, const DOMString &bs ) 00225 { 00226 if ( as.length() != bs.length() ) return true; 00227 00228 const QChar *a = as.unicode(); 00229 const QChar *b = bs.unicode(); 00230 if ( a == b ) return false; 00231 if ( !( a && b ) ) return true; 00232 int l = as.length(); 00233 while ( l-- ) { 00234 if ( *a != *b && a->lower() != b->lower() ) return true; 00235 a++,b++; 00236 } 00237 return false; 00238 } 00239 00240 bool DOM::strcasecmp( const DOMString &as, const char* bs ) 00241 { 00242 const QChar *a = as.unicode(); 00243 int l = as.length(); 00244 if ( !bs ) return ( l != 0 ); 00245 while ( l-- ) { 00246 if ( a->latin1() != *bs ) { 00247 char cc = ( ( *bs >= 'A' ) && ( *bs <= 'Z' ) ) ? ( ( *bs ) + 'a' - 'A' ) : ( *bs ); 00248 if ( a->lower().latin1() != cc ) return true; 00249 } 00250 a++, bs++; 00251 } 00252 return ( *bs != '\0' ); 00253 } 00254 00255 bool DOMString::isEmpty() const 00256 { 00257 return (!impl || impl->l == 0); 00258 } 00259 00260 //----------------------------------------------------------------------------- 00261 00262 bool DOM::operator==( const DOMString &a, const DOMString &b ) 00263 { 00264 unsigned int l = a.length(); 00265 00266 if( l != b.length() ) return false; 00267 00268 if(!memcmp(a.unicode(), b.unicode(), l*sizeof(QChar))) 00269 return true; 00270 return false; 00271 } 00272 00273 bool DOM::operator==( const DOMString &a, const QString &b ) 00274 { 00275 unsigned int l = a.length(); 00276 00277 if( l != b.length() ) return false; 00278 00279 if(!memcmp(a.unicode(), b.unicode(), l*sizeof(QChar))) 00280 return true; 00281 return false; 00282 } 00283 00284 bool DOM::operator==( const DOMString &a, const char *b ) 00285 { 00286 DOMStringImpl* aimpl = a.impl; 00287 if ( !b ) return !aimpl; 00288 00289 if ( aimpl ) { 00290 int alen = aimpl->l; 00291 const QChar *aptr = aimpl->s; 00292 while ( alen-- ) { 00293 unsigned char c = *b++; 00294 if ( !c || ( *aptr++ ).unicode() != c ) 00295 return false; 00296 } 00297 } 00298 00299 return !*b; 00300 } 00301
KDE Logo
This file is part of the documentation for khtml Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Apr 12 23:31:23 2005 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003