KHtml

dom_string.h
1 /*
2  * This file is part of the DOM implementation for KDE.
3  *
4  * Copyright 1999 Lars Knoll ([email protected])
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 #ifndef _DOM_DOMString_h_
23 #define _DOM_DOMString_h_
24 
25 #include <khtml_export.h>
26 #include "khtml_debug.h"
27 #include <QString>
28 #include <limits.h>
29 
30 namespace DOM
31 {
32 
33 class DOMStringImpl;
34 
35 /**
36  * This class implements the basic string we use in the DOM. We do not use
37  * QString for 2 reasons: Memory overhead, and the missing explicit sharing
38  * of strings we need for the DOM.
39  *
40  * All DOMStrings are explicitly shared (they behave like pointers), meaning
41  * that modifications to one instance will also modify all others. If you
42  * wish to get a DOMString that is independent, use copy().
43  */
44 class KHTML_EXPORT DOMString
45 {
46  friend class CharacterDataImpl;
47  friend KHTML_EXPORT bool operator==(const DOMString &a, const char *b);
48 public:
49  /**
50  * default constructor. Gives an empty DOMString
51  */
52  DOMString() : impl(nullptr) {}
53 
54  DOMString(const QChar *str, uint len);
55  DOMString(const QString &);
56  DOMString(const char *str);
57  /**
58  * @since 4.2
59  */
60  DOMString(const char *str, uint len);
61  DOMString(DOMStringImpl *i);
62 
63  virtual ~DOMString();
64 
65  // assign and copy
66  DOMString(const DOMString &str);
67  DOMString &operator =(const DOMString &str);
68 
69  /**
70  * append str to this string
71  */
72  DOMString &operator += (const DOMString &str);
73  /**
74  * add two DOMString's
75  */
76  DOMString operator + (const DOMString &str);
77 
78  void insert(DOMString str, uint pos);
79 
80  /**
81  * The character at position i of the DOMString. If i >= length(), the
82  * character returned will be 0.
83  */
84  const QChar &operator [](unsigned int i) const;
85 
86  int find(const QChar c, int start = 0) const;
87  int reverseFind(const QChar c, int start = -1) const;
88 
89  DOMString substring(unsigned pos, unsigned len = UINT_MAX) const;
90 
91  uint length() const;
92  void truncate(unsigned int len);
93  void remove(unsigned int pos, int len = 1);
94  /**
95  * Splits the string into two. The original string gets truncated to pos, and the rest is returned.
96  */
97  DOMString split(unsigned int pos);
98 
99  /**
100  * Returns a lowercase version of the string
101  */
102  DOMString lower() const;
103  /**
104  * Returns an uppercase version of the string
105  */
106  DOMString upper() const;
107 
108  QChar *unicode() const;
109  // for WebCore API compatibility
110  inline QChar *characters() const
111  {
112  return unicode();
113  }
114  QString string() const;
115 
116  int toInt() const;
117  int toInt(bool *ok) const;
118  float toFloat(bool *ok = nullptr) const;
119  bool percentage(int &_percentage) const;
120 
121  static DOMString number(float f);
122 
123  DOMString copy() const;
124 
125  bool isNull() const
126  {
127  return (impl == nullptr);
128  }
129  bool isEmpty() const;
130 
131  bool endsWith(const DOMString &str) const;
132  bool startsWith(const DOMString &str) const;
133 
134  /**
135  * Returns a string with Space Characters removed from the start and the end.
136  * Space Characters as defined in
137  * https://dev.w3.org/html5/spec-LC/common-microsyntaxes.html#space-character
138  */
139  DOMString trimSpaces() const;
140 
141  /**
142  * @internal get a handle to the imlementation of the DOMString
143  * Use at own risk!!!
144  */
145  DOMStringImpl *implementation() const
146  {
147  return impl;
148  }
149 
150  static DOMString format(const char *format, ...)
151 #if defined(__GNUC__)
152  __attribute__((format(printf, 1, 2)))
153 #endif
154  ;
155 
156 protected:
157  DOMStringImpl *impl;
158 };
159 
160 inline QDebug operator<<(QDebug stream, const DOMString &string)
161 {
162  return (stream << (string.implementation() ? string.string() : QString::fromLatin1("null")));
163 }
164 
165 KHTML_EXPORT bool operator==(const DOMString &a, const DOMString &b);
166 KHTML_EXPORT bool operator==(const DOMString &a, const QString &b);
167 KHTML_EXPORT bool operator==(const DOMString &a, const char *b);
168 inline bool operator!=(const DOMString &a, const DOMString &b)
169 {
170  return !(a == b);
171 }
172 inline bool operator!=(const DOMString &a, const QString &b)
173 {
174  return !(a == b);
175 }
176 inline bool operator!=(const DOMString &a, const char *b)
177 {
178  return !(a == b);
179 }
180 inline bool strcmp(const DOMString &a, const DOMString &b)
181 {
182  return a != b;
183 }
184 
185 // returns false when equal, true otherwise (ignoring case)
186 KHTML_EXPORT bool strcasecmp(const DOMString &a, const DOMString &b);
187 KHTML_EXPORT bool strcasecmp(const DOMString &a, const char *b);
188 
189 }
190 #endif
DOMString()
default constructor.
Definition: dom_string.h:52
QDataStream & operator<<(QDataStream &out, const KDateTime &dateTime)
This library provides a full-featured HTML parser and widget.
Q_SCRIPTABLE Q_NOREPLY void start()
bool operator==(const Qt3DRender::QGraphicsApiFilter &reference, const Qt3DRender::QGraphicsApiFilter &sample)
bool operator!=(const Qt3DRender::QGraphicsApiFilter &reference, const Qt3DRender::QGraphicsApiFilter &sample)
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
QString fromLatin1(const char *str, int size)
DOMStringImpl * implementation() const
Definition: dom_string.h:145
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Fri Sep 29 2023 03:54:51 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.