fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / khtml / misc / stringit.cpp
blob2fa08e8fa11fd028bee9aeb8441cc9dab52c226d
1 /*
2 This file is part of the KDE libraries
4 Copyright (C) 2004 Apple Computer
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.
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.
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.
22 #include "stringit.h"
24 namespace khtml {
26 uint TokenizerString::length() const
28 uint length = m_currentString.m_length;
29 if (!m_pushedChar1.isNull()) {
30 ++length;
31 if (!m_pushedChar2.isNull())
32 ++length;
34 if (m_composite) {
35 QList<TokenizerSubstring>::ConstIterator i = m_substrings.begin();
36 QList<TokenizerSubstring>::ConstIterator e = m_substrings.end();
37 for (; i != e; ++i)
38 length += (*i).m_length;
40 return length;
43 void TokenizerString::clear()
45 m_pushedChar1 = 0;
46 m_pushedChar2 = 0;
47 m_currentChar = 0;
48 m_currentString.clear();
49 m_substrings.clear();
50 m_lines = 0;
51 m_composite = false;
54 void TokenizerString::append(const TokenizerSubstring &s)
56 if (s.m_length) {
57 if (!m_currentString.m_length) {
58 m_currentString = s;
59 } else {
60 m_substrings.append(s);
61 m_composite = true;
66 void TokenizerString::prepend(const TokenizerSubstring &s)
68 assert(!escaped());
69 if (s.m_length) {
70 if (!m_currentString.m_length)
71 m_currentString = s;
72 else {
73 // Shift our m_currentString into our list.
74 m_substrings.prepend(m_currentString);
75 m_currentString = s;
76 m_composite = true;
81 void TokenizerString::append(const TokenizerString &s)
83 assert(!s.escaped());
84 append(s.m_currentString);
85 if (s.m_composite) {
86 QList<TokenizerSubstring>::ConstIterator i = s.m_substrings.begin();
87 QList<TokenizerSubstring>::ConstIterator e = s.m_substrings.end();
88 for (; i != e; ++i)
89 append(*i);
91 m_currentChar = m_pushedChar1.isNull() ? m_currentString.m_current : &m_pushedChar1;
94 void TokenizerString::prepend(const TokenizerString &s)
96 assert(!escaped());
97 assert(!s.escaped());
98 if (s.m_composite) {
99 QList<TokenizerSubstring>::ConstIterator e = s.m_substrings.end();
100 while (e != s.m_substrings.begin())
102 --e;
103 prepend(*e);
106 prepend(s.m_currentString);
107 m_currentChar = m_pushedChar1.isNull() ? m_currentString.m_current : &m_pushedChar1;
110 void TokenizerString::advanceSubstring()
112 if (m_composite) {
113 m_currentString = m_substrings.first();
114 m_substrings.removeFirst();
115 if (m_substrings.isEmpty())
116 m_composite = false;
117 } else {
118 m_currentString.clear();
122 QString TokenizerString::toString() const
124 QString result;
125 if (!m_pushedChar1.isNull()) {
126 result.append(m_pushedChar1);
127 if (!m_pushedChar2.isNull())
128 result.append(m_pushedChar2);
130 m_currentString.appendTo(result);
131 if (m_composite) {
132 QList<TokenizerSubstring>::ConstIterator i = m_substrings.begin();
133 QList<TokenizerSubstring>::ConstIterator e = m_substrings.end();
134 for (; i != e; ++i)
135 (*i).appendTo(result);
137 return result;