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
29 #include "dom/dom_string.h"
31 #include <QtCore/QList>
32 #include <QtCore/QString>
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())
56 if (*s
== QLatin1Char('\n'))
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
; }
81 class TokenizerString
;
83 class TokenizerSubstring
85 friend class TokenizerString
;
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
) {
100 str
.insert(str
.length(), m_current
, m_length
);
106 const QChar
*m_current
;
109 class TokenizerString
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
;
124 void append(const TokenizerString
&);
125 void prepend(const TokenizerString
&);
128 if (m_pushedChar1
.isNull()) {
130 m_currentChar
= m_pushedChar1
.isNull() ? m_currentString
.m_current
: &m_pushedChar1
;
132 assert(m_pushedChar2
.isNull());
137 bool isEmpty() const { return !current(); }
141 if (!m_pushedChar1
.isNull()) {
142 m_pushedChar1
= m_pushedChar2
;
144 } else if (m_currentString
.m_current
) {
145 m_lines
+= *m_currentString
.m_current
++ == QLatin1Char('\n');
146 if (--m_currentString
.m_length
== 0)
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(); }
165 void append(const TokenizerSubstring
&);
166 void prepend(const TokenizerSubstring
&);
168 void advanceSubstring();
169 const QChar
*current() const { return m_currentChar
; }
173 TokenizerSubstring m_currentString
;
174 const QChar
*m_currentChar
;
175 QList
<TokenizerSubstring
> m_substrings
;
182 class TokenizerQueue
: public QList
<TokenizerString
>
188 void push( const TokenizerString
&t
) { prepend(t
); }
189 TokenizerString
pop() {
191 return TokenizerString();
192 TokenizerString
t(first());
196 TokenizerString
& top() { return first(); }
197 TokenizerString
& bottom() { return last(); }