File indexing completed on 2024-04-28 11:38:33

0001 /*
0002     This file is part of the KDE libraries
0003 
0004     Copyright (C) 2004 Apple Computer
0005 
0006     This library is free software; you can redistribute it and/or
0007     modify it under the terms of the GNU Library General Public
0008     License as published by the Free Software Foundation; either
0009     version 2 of the License, or (at your option) any later version.
0010 
0011     This library is distributed in the hope that it will be useful,
0012     but WITHOUT ANY WARRANTY; without even the implied warranty of
0013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014     Library General Public License for more details.
0015 
0016     You should have received a copy of the GNU Library General Public License
0017     along with this library; see the file COPYING.LIB.  If not, write to
0018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019     Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #include "stringit.h"
0023 
0024 namespace khtml
0025 {
0026 
0027 uint TokenizerString::length() const
0028 {
0029     uint length = m_currentString.m_length;
0030     if (!m_pushedChar1.isNull()) {
0031         ++length;
0032         if (!m_pushedChar2.isNull()) {
0033             ++length;
0034         }
0035     }
0036     if (m_composite) {
0037         QList<TokenizerSubstring>::ConstIterator i = m_substrings.begin();
0038         QList<TokenizerSubstring>::ConstIterator e = m_substrings.end();
0039         for (; i != e; ++i) {
0040             length += (*i).m_length;
0041         }
0042     }
0043     return length;
0044 }
0045 
0046 void TokenizerString::clear()
0047 {
0048     m_pushedChar1 = 0;
0049     m_pushedChar2 = 0;
0050     m_currentChar = nullptr;
0051     m_currentString.clear();
0052     m_substrings.clear();
0053     m_lines = 0;
0054     m_composite = false;
0055 }
0056 
0057 void TokenizerString::append(const TokenizerSubstring &s)
0058 {
0059     if (s.m_length) {
0060         if (!m_currentString.m_length) {
0061             m_currentString = s;
0062         } else {
0063             m_substrings.append(s);
0064             m_composite = true;
0065         }
0066     }
0067 }
0068 
0069 void TokenizerString::prepend(const TokenizerSubstring &s)
0070 {
0071     assert(!escaped());
0072     if (s.m_length) {
0073         if (!m_currentString.m_length) {
0074             m_currentString = s;
0075         } else {
0076             // Shift our m_currentString into our list.
0077             m_substrings.prepend(m_currentString);
0078             m_currentString = s;
0079             m_composite = true;
0080         }
0081     }
0082 }
0083 
0084 void TokenizerString::append(const TokenizerString &s)
0085 {
0086     assert(!s.escaped());
0087     append(s.m_currentString);
0088     if (s.m_composite) {
0089         QList<TokenizerSubstring>::ConstIterator i = s.m_substrings.begin();
0090         QList<TokenizerSubstring>::ConstIterator e = s.m_substrings.end();
0091         for (; i != e; ++i) {
0092             append(*i);
0093         }
0094     }
0095     m_currentChar = m_pushedChar1.isNull() ? m_currentString.m_current : &m_pushedChar1;
0096 }
0097 
0098 void TokenizerString::prepend(const TokenizerString &s)
0099 {
0100     assert(!escaped());
0101     assert(!s.escaped());
0102     if (s.m_composite) {
0103         QList<TokenizerSubstring>::ConstIterator e = s.m_substrings.end();
0104         while (e != s.m_substrings.begin()) {
0105             --e;
0106             prepend(*e);
0107         }
0108     }
0109     prepend(s.m_currentString);
0110     m_currentChar = m_pushedChar1.isNull() ? m_currentString.m_current : &m_pushedChar1;
0111 }
0112 
0113 void TokenizerString::advanceSubstring()
0114 {
0115     if (m_composite) {
0116         m_currentString = m_substrings.first();
0117         m_substrings.removeFirst();
0118         if (m_substrings.isEmpty()) {
0119             m_composite = false;
0120         }
0121     } else {
0122         m_currentString.clear();
0123     }
0124 }
0125 
0126 QString TokenizerString::toString() const
0127 {
0128     QString result;
0129     if (!m_pushedChar1.isNull()) {
0130         result.append(m_pushedChar1);
0131         if (!m_pushedChar2.isNull()) {
0132             result.append(m_pushedChar2);
0133         }
0134     }
0135     m_currentString.appendTo(result);
0136     if (m_composite) {
0137         QList<TokenizerSubstring>::ConstIterator i = m_substrings.begin();
0138         QList<TokenizerSubstring>::ConstIterator e = m_substrings.end();
0139         for (; i != e; ++i) {
0140             (*i).appendTo(result);
0141         }
0142     }
0143     return result;
0144 }
0145 
0146 }