• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KHTML

  • sources
  • kde-4.12
  • kdelibs
  • khtml
  • dom
dom_string.cpp
Go to the documentation of this file.
1 
22 #include "dom/dom_string.h"
23 #include "xml/dom_stringimpl.h"
24 
25 #include <wtf/Vector.h>
26 
27 using namespace DOM;
28 
29 
30 DOMString::DOMString(const QChar *str, uint len)
31 {
32  if (!str) {
33  impl = 0;
34  return;
35  }
36  impl = new DOMStringImpl( str, len );
37  impl->ref();
38 }
39 
40 DOMString::DOMString(const QString &str)
41 {
42  if (str.isNull()) {
43  impl = 0;
44  return;
45  }
46 
47  impl = new DOMStringImpl( str.unicode(), str.length() );
48  impl->ref();
49 }
50 
51 DOMString::DOMString(const char *str)
52 {
53  if (!str) {
54  impl = 0;
55  return;
56  }
57 
58  impl = new DOMStringImpl( str );
59  impl->ref();
60 }
61 
62 DOMString::DOMString(const char *str, uint len)
63 {
64  if (!str) {
65  impl = 0;
66  return;
67  }
68  impl = new DOMStringImpl(str, len);
69  impl->ref();
70 }
71 
72 DOMString::DOMString(DOMStringImpl *i)
73 {
74  impl = i;
75  if(impl) impl->ref();
76 }
77 
78 DOMString::DOMString(const DOMString &other)
79 {
80  impl = other.impl;
81  if(impl) impl->ref();
82 }
83 
84 DOMString::~DOMString()
85 {
86  if(impl) impl->deref();
87 }
88 
89 DOMString &DOMString::operator =(const DOMString &other)
90 {
91  if ( impl != other.impl ) {
92  if(impl) impl->deref();
93  impl = other.impl;
94  if(impl) impl->ref();
95  }
96  return *this;
97 }
98 
99 DOMString &DOMString::operator += (const DOMString &str)
100 {
101  if(!impl)
102  {
103  // ### FIXME!!!
104  impl = str.impl;
105  if (impl)
106  impl->ref();
107  return *this;
108  }
109  if(str.impl)
110  {
111  DOMStringImpl *i = impl->copy();
112  impl->deref();
113  impl = i;
114  impl->ref();
115  impl->append(str.impl);
116  }
117  return *this;
118 }
119 
120 DOMString DOMString::operator + (const DOMString &str)
121 {
122  if(!impl) return str.copy();
123  if(str.impl)
124  {
125  DOMString s = copy();
126  s += str;
127  return s;
128  }
129 
130  return copy();
131 }
132 
133 void DOMString::insert(DOMString str, uint pos)
134 {
135  if(!impl)
136  {
137  impl = str.impl->copy();
138  impl->ref();
139  }
140  else
141  impl->insert(str.impl, pos);
142 }
143 
144 
145 const QChar &DOMString::operator [](unsigned int i) const
146 {
147  static const QChar nullChar = 0;
148 
149  if(!impl || i >= impl->l ) return nullChar;
150 
151  return *(impl->s+i);
152 }
153 
154 int DOMString::find(const QChar c, int start) const
155 {
156  unsigned int l = start;
157  if(!impl || l >= impl->l ) return -1;
158  while( l < impl->l )
159  {
160  if( *(impl->s+l) == c ) return l;
161  l++;
162  }
163  return -1;
164 }
165 
166 int DOMString::reverseFind(const QChar c, int start) const
167 {
168  unsigned int l = start;
169  if (!impl || l < -impl->l) return -1;
170  l += impl->l;
171  while (1) {
172  if (*(impl->s + l) == c) return l;
173  l--;
174  if (l == 0)
175  return -1;
176  }
177  return -1;
178 }
179 
180 DOMString DOMString::substring(unsigned pos, unsigned len) const
181 {
182  return (impl) ? impl->substring(pos, len) : DOMString();
183 }
184 
185 uint DOMString::length() const
186 {
187  if(!impl) return 0;
188  return impl->l;
189 }
190 
191 void DOMString::truncate( unsigned int len )
192 {
193  if(impl) impl->truncate(len);
194 }
195 
196 void DOMString::remove(unsigned int pos, int len)
197 {
198  if(impl) impl->remove(pos, len);
199 }
200 
201 DOMString DOMString::split(unsigned int pos)
202 {
203  if(!impl) return DOMString();
204  return impl->split(pos);
205 }
206 
207 DOMString DOMString::lower() const
208 {
209  if(!impl) return DOMString();
210  return impl->lower();
211 }
212 
213 DOMString DOMString::upper() const
214 {
215  if(!impl) return DOMString();
216  return impl->upper();
217 }
218 
219 bool DOMString::percentage(int &_percentage) const
220 {
221  if(!impl || !impl->l) return false;
222 
223  if ( *(impl->s+impl->l-1) != QChar('%'))
224  return false;
225 
226  _percentage = QString::fromRawData(impl->s, impl->l-1).toInt();
227  return true;
228 }
229 
230 QChar *DOMString::unicode() const
231 {
232  if(!impl) return 0;
233  return impl->unicode();
234 }
235 
236 QString DOMString::string() const
237 {
238  if(!impl) return QString();
239 
240  return impl->string();
241 }
242 
243 int DOMString::toInt() const
244 {
245  if(!impl) return 0;
246 
247  return impl->toInt();
248 }
249 
250 int DOMString::toInt(bool* ok) const
251 {
252  if (!impl) {
253  *ok = false;
254  return 0;
255  }
256 
257  return impl->toInt(ok);
258 }
259 
260 float DOMString::toFloat(bool* ok) const
261 {
262  if (!impl) {
263  if (ok)
264  *ok = false;
265  return 0;
266  }
267  return impl->toFloat(ok);
268 }
269 
270 DOMString DOMString::number(float f)
271 {
272  return DOMString(QString::number(f));
273 }
274 
275 DOMString DOMString::copy() const
276 {
277  if(!impl) return DOMString();
278  return impl->copy();
279 }
280 
281 bool DOMString::endsWith(const DOMString& str) const
282 {
283  if (str.length() > length()) return false;
284  return impl->endsWith(str.implementation());
285 }
286 
287 bool DOMString::startsWith(const DOMString& str) const
288 {
289  if (str.length() > length()) return false;
290  return impl->startsWith(str.implementation());
291 }
292 
293 // ------------------------------------------------------------------------
294 
295 bool DOM::strcasecmp( const DOMString &as, const DOMString &bs )
296 {
297  return strcasecmp(as.implementation(), bs.implementation());
298 }
299 
300 bool DOM::strcasecmp( const DOMString &as, const char* bs )
301 {
302  const QChar *a = as.unicode();
303  int l = as.length();
304  if ( !bs ) return ( l != 0 );
305  while ( l-- ) {
306  if ( a->toLatin1() != *bs ) {
307  char cc = ( ( *bs >= 'A' ) && ( *bs <= 'Z' ) ) ? ( ( *bs ) + 'a' - 'A' ) : ( *bs );
308  if ( a->toLower().toLatin1() != cc ) return true;
309  }
310  a++, bs++;
311  }
312  return ( *bs != '\0' );
313 }
314 
315 bool DOMString::isEmpty() const
316 {
317  return (!impl || impl->l == 0);
318 }
319 
320 DOMString DOMString::format(const char *format, ...)
321 {
322  va_list args;
323  va_start(args, format);
324 
325  Vector<char, 256> buffer;
326 
327  // Do the format once to get the length.
328 #if COMPILER(MSVC)
329  int result = _vscprintf(format, args);
330 #else
331  char ch;
332  int result = vsnprintf(&ch, 1, format, args);
333  // We need to call va_end() and then va_start() again here, as the
334  // contents of args is undefined after the call to vsnprintf
335  // according to http://man.cx/snprintf(3)
336  //
337  // Not calling va_end/va_start here happens to work on lots of
338  // systems, but fails e.g. on 64bit Linux.
339  va_end(args);
340  va_start(args, format);
341 #endif
342 
343  if (result == 0) {
344  va_end(args);
345  return DOMString("");
346  }
347  if (result < 0) {
348  va_end(args);
349  return DOMString();
350  }
351  unsigned len = result;
352  buffer.grow(len + 1);
353 
354  // Now do the formatting again, guaranteed to fit.
355  vsnprintf(buffer.data(), buffer.size(), format, args);
356 
357  va_end(args);
358 
359  buffer[len] = 0; // we don't really need this I guess
360  return new DOMStringImpl(buffer.data()/*, len*/);
361 }
362 
363 //-----------------------------------------------------------------------------
364 
365 bool DOM::operator==( const DOMString &a, const DOMString &b )
366 {
367  return !strcmp(a.implementation(), b.implementation());
368 }
369 
370 bool DOM::operator==( const DOMString &a, const QString &b )
371 {
372  int l = a.length();
373 
374  if( l != b.length() ) return false;
375 
376  if(!memcmp(a.unicode(), b.unicode(), l*sizeof(QChar)))
377  return true;
378  return false;
379 }
380 
381 bool DOM::operator==( const DOMString &a, const char *b )
382 {
383  DOMStringImpl* aimpl = a.impl;
384  if ( !b ) return !aimpl;
385 
386  if ( aimpl ) {
387  int alen = aimpl->l;
388  const QChar *aptr = aimpl->s;
389  while ( alen-- ) {
390  unsigned char c = *b++;
391  if ( !c || ( *aptr++ ).unicode() != c )
392  return false;
393  }
394  }
395 
396  return !*b;
397 }
dom_string.h
DOM::DOMString::lower
DOMString lower() const
Returns a lowercase version of the string.
Definition: dom_string.cpp:207
DOM::DOMString::length
uint length() const
Definition: dom_string.cpp:185
DOM::DOMString::split
DOMString split(unsigned int pos)
Splits the string into two.
Definition: dom_string.cpp:201
DOM::DOMString::reverseFind
int reverseFind(const QChar c, int start=-1) const
Definition: dom_string.cpp:166
DOM::DOMString::format
static DOMString format(const char *format,...)
Definition: dom_string.cpp:320
DOM::strcasecmp
bool strcasecmp(const DOMString &a, const DOMString &b)
Definition: dom_string.cpp:295
DOM::DOMString::operator+
DOMString operator+(const DOMString &str)
add two DOMString's
Definition: dom_string.cpp:120
DOM::DOMString::remove
void remove(unsigned int pos, int len=1)
Definition: dom_string.cpp:196
DOM::DOMString::string
QString string() const
Definition: dom_string.cpp:236
DOM::DOMString::operator=
DOMString & operator=(const DOMString &str)
Definition: dom_string.cpp:89
QString
DOM::DOMString::toFloat
float toFloat(bool *ok=0) const
Definition: dom_string.cpp:260
DOM::DOMString::copy
DOMString copy() const
Definition: dom_string.cpp:275
DOM::DOMString::impl
DOMStringImpl * impl
Definition: dom_string.h:140
DOM::strcmp
bool strcmp(const DOMString &a, const DOMString &b)
Definition: dom_string.h:153
DOM::DOMString::isEmpty
bool isEmpty() const
Definition: dom_string.cpp:315
DOM::DOMString::percentage
bool percentage(int &_percentage) const
Definition: dom_string.cpp:219
DOM::DOMString
This class implements the basic string we use in the DOM.
Definition: dom_string.h:43
DOM::operator==
bool operator==(const DOMString &a, const DOMString &b)
Definition: dom_string.cpp:365
DOM::DOMString::DOMString
DOMString()
default constructor.
Definition: dom_string.h:51
DOM::DOMString::toInt
int toInt() const
Definition: dom_string.cpp:243
DOM::DOMString::operator+=
DOMString & operator+=(const DOMString &str)
append str to this string
Definition: dom_string.cpp:99
DOM::DOMString::startsWith
bool startsWith(const DOMString &str) const
Definition: dom_string.cpp:287
DOM::DOMString::find
int find(const QChar c, int start=0) const
Definition: dom_string.cpp:154
DOM::DOMString::truncate
void truncate(unsigned int len)
Definition: dom_string.cpp:191
DOM::DOMString::endsWith
bool endsWith(const DOMString &str) const
Definition: dom_string.cpp:281
DOM::DOMString::insert
void insert(DOMString str, uint pos)
Definition: dom_string.cpp:133
DOM::DOMString::substring
DOMString substring(unsigned pos, unsigned len=UINT_MAX) const
Definition: dom_string.cpp:180
DOM::DOMString::implementation
DOMStringImpl * implementation() const
Definition: dom_string.h:131
DOM::DOMString::number
static DOMString number(float f)
Definition: dom_string.cpp:270
DOM::DOMString::~DOMString
virtual ~DOMString()
Definition: dom_string.cpp:84
DOM::DOMString::operator[]
const QChar & operator[](unsigned int i) const
The character at position i of the DOMString.
Definition: dom_string.cpp:145
DOM::DOMString::unicode
QChar * unicode() const
Definition: dom_string.cpp:230
DOM::DOMString::upper
DOMString upper() const
Returns an uppercase version of the string.
Definition: dom_string.cpp:213
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:51:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KHTML

Skip menu "KHTML"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal