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

KHTML

  • 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
27using namespace DOM;
28
29
30DOMString::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
40DOMString::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
51DOMString::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
62DOMString::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
72DOMString::DOMString(DOMStringImpl *i)
73{
74 impl = i;
75 if(impl) impl->ref();
76}
77
78DOMString::DOMString(const DOMString &other)
79{
80 impl = other.impl;
81 if(impl) impl->ref();
82}
83
84DOMString::~DOMString()
85{
86 if(impl) impl->deref();
87}
88
89DOMString &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
99DOMString &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
120DOMString 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
133void 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
145const 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
154int 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
166int 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
180DOMString DOMString::substring(unsigned pos, unsigned len) const
181{
182 return (impl) ? impl->substring(pos, len) : DOMString();
183}
184
185uint DOMString::length() const
186{
187 if(!impl) return 0;
188 return impl->l;
189}
190
191void DOMString::truncate( unsigned int len )
192{
193 if(impl) impl->truncate(len);
194}
195
196void DOMString::remove(unsigned int pos, int len)
197{
198 if(impl) impl->remove(pos, len);
199}
200
201DOMString DOMString::split(unsigned int pos)
202{
203 if(!impl) return DOMString();
204 return impl->split(pos);
205}
206
207DOMString DOMString::lower() const
208{
209 if(!impl) return DOMString();
210 return impl->lower();
211}
212
213DOMString DOMString::upper() const
214{
215 if(!impl) return DOMString();
216 return impl->upper();
217}
218
219bool 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
230QChar *DOMString::unicode() const
231{
232 if(!impl) return 0;
233 return impl->unicode();
234}
235
236QString DOMString::string() const
237{
238 if(!impl) return QString();
239
240 return impl->string();
241}
242
243int DOMString::toInt() const
244{
245 if(!impl) return 0;
246
247 return impl->toInt();
248}
249
250int DOMString::toInt(bool* ok) const
251{
252 if (!impl) {
253 *ok = false;
254 return 0;
255 }
256
257 return impl->toInt(ok);
258}
259
260float 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
270DOMString DOMString::number(float f)
271{
272 return DOMString(QString::number(f));
273}
274
275DOMString DOMString::copy() const
276{
277 if(!impl) return DOMString();
278 return impl->copy();
279}
280
281bool DOMString::endsWith(const DOMString& str) const
282{
283 if (str.length() > length()) return false;
284 return impl->endsWith(str.implementation());
285}
286
287bool DOMString::startsWith(const DOMString& str) const
288{
289 if (str.length() > length()) return false;
290 return impl->startsWith(str.implementation());
291}
292
293static inline bool isSpaceCharacter(const ushort &c)
294{
295 // http://www.w3.org/TR/html/infrastructure.html#space-character
296 return ((c < 0x0021) &&
297 (c == 0x0020 || c == 0x0009 || c == 0x000A || c == 0x000C || c == 0x000D));
298}
299
300DOMString DOMString::parsedUrl() const
301{
302 if (!impl || !impl->l) {
303 return *this;
304 }
305
306 // 1. strip leading and trailing whitespace
307 const QChar *s = impl->s;
308 unsigned int start = 0;
309 unsigned int end = impl->l - 1;
310
311 while ((start <= end) && isSpaceCharacter(s[start].unicode())) {
312 ++start;
313 }
314
315 if (start > end) {
316 return DOMString("");
317 }
318
319 while (end && isSpaceCharacter(s[end].unicode())) {
320 --end;
321 }
322
323 const unsigned int len = end - start + 1;
324 DOMStringImpl *out = new DOMStringImpl(s + start, len);
325
326 // 2. remove garbage
327 unsigned int newLen = 0;
328 for (unsigned int k = 0; k < len; ++k) {
329 QChar ch = out->s[k];
330 if (ch.unicode() > '\r') {
331 out->s[newLen++] = ch;
332 }
333 }
334 out->l = newLen;
335
336 return out;
337}
338
339#ifndef KDE_NO_DEPRECATED
340DOMString DOMString::trimSpaces() const
341{
342 return parsedUrl();
343}
344#endif
345
346// ------------------------------------------------------------------------
347
348bool DOM::strcasecmp( const DOMString &as, const DOMString &bs )
349{
350 return strcasecmp(as.implementation(), bs.implementation());
351}
352
353bool DOM::strcasecmp( const DOMString &as, const char* bs )
354{
355 const QChar *a = as.unicode();
356 int l = as.length();
357 if ( !bs ) return ( l != 0 );
358 while ( l-- ) {
359 if ( a->toLatin1() != *bs ) {
360 char cc = ( ( *bs >= 'A' ) && ( *bs <= 'Z' ) ) ? ( ( *bs ) + 'a' - 'A' ) : ( *bs );
361 if ( a->toLower().toLatin1() != cc ) return true;
362 }
363 a++, bs++;
364 }
365 return ( *bs != '\0' );
366}
367
368bool DOMString::isEmpty() const
369{
370 return (!impl || impl->l == 0);
371}
372
373DOMString DOMString::format(const char *format, ...)
374{
375 va_list args;
376 va_start(args, format);
377
378 Vector<char, 256> buffer;
379
380 // Do the format once to get the length.
381#if COMPILER(MSVC)
382 int result = _vscprintf(format, args);
383#else
384 char ch;
385 int result = vsnprintf(&ch, 1, format, args);
386 // We need to call va_end() and then va_start() again here, as the
387 // contents of args is undefined after the call to vsnprintf
388 // according to http://man.cx/snprintf(3)
389 //
390 // Not calling va_end/va_start here happens to work on lots of
391 // systems, but fails e.g. on 64bit Linux.
392 va_end(args);
393 va_start(args, format);
394#endif
395
396 if (result == 0) {
397 va_end(args);
398 return DOMString("");
399 }
400 if (result < 0) {
401 va_end(args);
402 return DOMString();
403 }
404 unsigned len = result;
405 buffer.grow(len + 1);
406
407 // Now do the formatting again, guaranteed to fit.
408 vsnprintf(buffer.data(), buffer.size(), format, args);
409
410 va_end(args);
411
412 buffer[len] = 0; // we don't really need this I guess
413 return new DOMStringImpl(buffer.data()/*, len*/);
414}
415
416//-----------------------------------------------------------------------------
417
418bool DOM::operator==( const DOMString &a, const DOMString &b )
419{
420 return !strcmp(a.implementation(), b.implementation());
421}
422
423bool DOM::operator==( const DOMString &a, const QString &b )
424{
425 int l = a.length();
426
427 if( l != b.length() ) return false;
428
429 if(!memcmp(a.unicode(), b.unicode(), l*sizeof(QChar)))
430 return true;
431 return false;
432}
433
434bool DOM::operator==( const DOMString &a, const char *b )
435{
436 DOMStringImpl* aimpl = a.impl;
437 if ( !b ) return !aimpl;
438
439 if ( aimpl ) {
440 int alen = aimpl->l;
441 const QChar *aptr = aimpl->s;
442 while ( alen-- ) {
443 unsigned char c = *b++;
444 if ( !c || ( *aptr++ ).unicode() != c )
445 return false;
446 }
447 }
448
449 return !*b;
450}
DOM::DOMString
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
DOM::DOMString::DOMString
DOMString()
default constructor.
Definition: dom_string.h:51
DOM::DOMString::trimSpaces
DOMString trimSpaces() const
Definition: dom_string.cpp:340
DOM::DOMString::impl
DOMStringImpl * impl
Definition: dom_string.h:156
DOM::DOMString::operator+=
DOMString & operator+=(const DOMString &str)
append str to this string
Definition: dom_string.cpp:99
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
DOM::DOMString::length
uint length() const
Definition: dom_string.cpp:185
DOM::DOMString::parsedUrl
DOMString parsedUrl() const
Return a parsed url.
Definition: dom_string.cpp:300
DOM::DOMString::toFloat
float toFloat(bool *ok=0) const
Definition: dom_string.cpp:260
DOM::DOMString::endsWith
bool endsWith(const DOMString &str) const
Definition: dom_string.cpp:281
DOM::DOMString::startsWith
bool startsWith(const DOMString &str) const
Definition: dom_string.cpp:287
DOM::DOMString::~DOMString
virtual ~DOMString()
Definition: dom_string.cpp:84
DOM::DOMString::toInt
int toInt() const
Definition: dom_string.cpp:243
DOM::DOMString::truncate
void truncate(unsigned int len)
Definition: dom_string.cpp:191
DOM::DOMString::percentage
bool percentage(int &_percentage) const
Definition: dom_string.cpp:219
DOM::DOMString::number
static DOMString number(float f)
Definition: dom_string.cpp:270
DOM::DOMString::find
int find(const QChar c, int start=0) const
Definition: dom_string.cpp:154
DOM::DOMString::substring
DOMString substring(unsigned pos, unsigned len=UINT_MAX) const
Definition: dom_string.cpp:180
DOM::DOMString::operator=
DOMString & operator=(const DOMString &str)
Definition: dom_string.cpp:89
DOM::DOMString::split
DOMString split(unsigned int pos)
Splits the string into two.
Definition: dom_string.cpp:201
DOM::DOMString::format
static DOMString format(const char *format,...)
Definition: dom_string.cpp:373
DOM::DOMString::lower
DOMString lower() const
Returns a lowercase version of the string.
Definition: dom_string.cpp:207
DOM::DOMString::remove
void remove(unsigned int pos, int len=1)
Definition: dom_string.cpp:196
DOM::DOMString::implementation
DOMStringImpl * implementation() const
Definition: dom_string.h:147
DOM::DOMString::string
QString string() const
Definition: dom_string.cpp:236
DOM::DOMString::operator+
DOMString operator+(const DOMString &str)
add two DOMString's
Definition: dom_string.cpp:120
DOM::DOMString::copy
DOMString copy() const
Definition: dom_string.cpp:275
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::isEmpty
bool isEmpty() const
Definition: dom_string.cpp:368
DOM::DOMString::insert
void insert(DOMString str, uint pos)
Definition: dom_string.cpp:133
DOM::DOMString::reverseFind
int reverseFind(const QChar c, int start=-1) const
Definition: dom_string.cpp:166
isSpaceCharacter
static bool isSpaceCharacter(const ushort &c)
Definition: dom_string.cpp:293
dom_string.h
DOM
This library provides a full-featured HTML parser and widget.
Definition: design.h:55
DOM::operator==
bool operator==(const DOMString &a, const DOMString &b)
Definition: dom_string.cpp:418
DOM::strcasecmp
bool strcasecmp(const DOMString &a, const DOMString &b)
Definition: dom_string.cpp:348
DOM::strcmp
bool strcmp(const DOMString &a, const DOMString &b)
Definition: dom_string.h:169
ok
KGuiItem ok()
end
const KShortcut & end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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