don't discard iframe children.
[kdelibs.git] / khtml / misc / stringit.h
blob0a0610df579ccae10a0122a4313e4f1db8e17da0
1 /*
2 This file is part of the KDE libraries
4 Copyright (C) 1999 Lars Knoll (knoll@mpi-hd.mpg.de)
5 Copyright (C) 2004 Apple Computer, Inc.
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
22 //----------------------------------------------------------------------------
24 // KDE HTML Widget -- String class
26 #ifndef KHTMLSTRING_H
27 #define KHTMLSTRING_H
29 #include "dom/dom_string.h"
31 #include <QtCore/QList>
32 #include <QtCore/QString>
34 #include <assert.h>
36 using namespace DOM;
38 namespace khtml
41 class DOMStringIt
43 public:
44 DOMStringIt()
45 { s = 0, l = 0; lines = 0; }
46 DOMStringIt(QChar *str, uint len)
47 { s = str, l = len; lines = 0; }
48 DOMStringIt(const QString &str)
49 { s = str.unicode(); l = str.length(); lines = 0; }
51 DOMStringIt *operator++()
53 if(!pushedChar.isNull())
54 pushedChar=0;
55 else if(l > 0 ) {
56 if (*s == QLatin1Char('\n'))
57 lines++;
58 s++, l--;
60 return this;
62 public:
63 void push(const QChar& c) { /* assert(pushedChar.isNull());*/ pushedChar = c; }
65 const QChar& operator*() const { return pushedChar.isNull() ? *s : pushedChar; }
66 const QChar* operator->() const { return pushedChar.isNull() ? s : &pushedChar; }
68 bool escaped() const { return !pushedChar.isNull(); }
69 uint length() const { return l+(!pushedChar.isNull()); }
71 const QChar *current() const { return pushedChar.isNull() ? s : &pushedChar; }
72 int lineCount() const { return lines; }
74 protected:
75 QChar pushedChar;
76 const QChar *s;
77 int l;
78 int lines;
81 class TokenizerString;
83 class TokenizerSubstring
85 friend class TokenizerString;
86 public:
87 TokenizerSubstring() : m_length(0), m_current(0) {}
88 TokenizerSubstring(const QString &str) : m_string(str), m_length(str.length()), m_current(m_length == 0 ? 0 : str.unicode()) {}
89 TokenizerSubstring(const QChar *str, int length) : m_length(length), m_current(length == 0 ? 0 : str) {}
91 void clear() { m_length = 0; m_current = 0; }
93 void appendTo(QString &str) const {
94 if (m_string.unicode() == m_current) {
95 if (str.isEmpty())
96 str = m_string;
97 else
98 str.append(m_string);
99 } else {
100 str.insert(str.length(), m_current, m_length);
103 private:
104 QString m_string;
105 int m_length;
106 const QChar *m_current;
109 class TokenizerString
112 public:
113 TokenizerString() : m_currentChar(0), m_lines(0), m_composite(false) {}
114 TokenizerString(const QChar *str, int length) : m_currentString(str, length), m_currentChar(m_currentString.m_current), m_lines(0), m_composite(false) {}
115 TokenizerString(const QString &str) : m_currentString(str), m_currentChar(m_currentString.m_current), m_lines(0), m_composite(false) {}
116 TokenizerString(const TokenizerString &o) : m_pushedChar1(o.m_pushedChar1), m_pushedChar2(o.m_pushedChar2),
117 m_currentString(o.m_currentString), m_substrings(o.m_substrings),
118 m_lines(o.m_lines), m_composite(o.m_composite) {
119 m_currentChar = m_pushedChar1.isNull() ? m_currentString.m_current : &m_pushedChar1;
122 void clear();
124 void append(const TokenizerString &);
125 void prepend(const TokenizerString &);
127 void push(QChar c) {
128 if (m_pushedChar1.isNull()) {
129 m_pushedChar1 = c;
130 m_currentChar = m_pushedChar1.isNull() ? m_currentString.m_current : &m_pushedChar1;
131 } else {
132 assert(m_pushedChar2.isNull());
133 m_pushedChar2 = c;
137 bool isEmpty() const { return !current(); }
138 uint length() const;
140 void advance() {
141 if (!m_pushedChar1.isNull()) {
142 m_pushedChar1 = m_pushedChar2;
143 m_pushedChar2 = 0;
144 } else if (m_currentString.m_current) {
145 m_lines += *m_currentString.m_current++ == QLatin1Char('\n');
146 if (--m_currentString.m_length == 0)
147 advanceSubstring();
149 m_currentChar = m_pushedChar1.isNull() ? m_currentString.m_current: &m_pushedChar1;
151 uint count() const { return m_substrings.count(); }
153 bool escaped() const { return !m_pushedChar1.isNull(); }
155 int lineCount() const { return m_lines; }
156 void resetLineCount() { m_lines = 0; }
158 QString toString() const;
160 void operator++() { advance(); }
161 const QChar &operator*() const { return *current(); }
162 const QChar *operator->() const { return current(); }
164 private:
165 void append(const TokenizerSubstring &);
166 void prepend(const TokenizerSubstring &);
168 void advanceSubstring();
169 const QChar *current() const { return m_currentChar; }
171 QChar m_pushedChar1;
172 QChar m_pushedChar2;
173 TokenizerSubstring m_currentString;
174 const QChar *m_currentChar;
175 QList<TokenizerSubstring> m_substrings;
176 int m_lines;
177 bool m_composite;
182 class TokenizerQueue : public QList<TokenizerString>
185 public:
186 TokenizerQueue() {}
187 ~TokenizerQueue() {}
188 void push( const TokenizerString &t ) { prepend(t); }
189 TokenizerString pop() {
190 if (isEmpty())
191 return TokenizerString();
192 TokenizerString t(first());
193 erase( begin() );
194 return t;
196 TokenizerString& top() { return first(); }
197 TokenizerString& bottom() { return last(); }
202 #endif