summaryrefslogtreecommitdiffstats
path: root/khtml/misc/stringit.h
blob: 789f22af8fdf561331c565ea54004c1e01998487 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
    This file is part of the KDE libraries

    Copyright (C) 1999 Lars Knoll (knoll@mpi-hd.mpg.de)
    Copyright (C) 2004 Apple Computer, Inc.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/
//----------------------------------------------------------------------------
//
// KDE HTML Widget -- String class

#ifndef KHTMLSTRING_H
#define KHTMLSTRING_H

#include "dom/dom_string.h"

#include <tqstring.h>
#include <tqvaluelist.h>

#include <assert.h>

using namespace DOM;

namespace khtml
{

class DOMStringIt
{
public:
    DOMStringIt()
	{ s = 0, l = 0; lines = 0; }
    DOMStringIt(TQChar *str, uint len)
	{ s = str, l = len; lines = 0; }
    DOMStringIt(const TQString &str)
	{ s = str.unicode(); l = str.length(); lines = 0; }

    DOMStringIt *operator++()
    {
        if(!pushedChar.isNull())
            pushedChar=0;
        else if(l > 0 ) {
            if (*s == '\n')
                lines++;
	    s++, l--;
        }
	return this;
    }
public:
    void push(const TQChar& c) { /* assert(pushedChar.isNull());*/  pushedChar = c; }

    const TQChar& operator*() const  { return pushedChar.isNull() ? *s : pushedChar; }
    const TQChar* operator->() const { return pushedChar.isNull() ? s : &pushedChar; }

    bool escaped() const { return !pushedChar.isNull(); }
    uint length() const { return l+(!pushedChar.isNull()); }

    const TQChar *current() const { return pushedChar.isNull() ? s : &pushedChar; }
    int lineCount() const { return lines; }

protected:
    TQChar pushedChar;
    const TQChar *s;
    int l;
    int lines;
};

class TokenizerString;

class TokenizerSubstring
{
    friend class TokenizerString;
public:    
    TokenizerSubstring() : m_length(0), m_current(0) {}
    TokenizerSubstring(const TQString &str) : m_string(str), m_length(str.length()), m_current(m_length == 0 ? 0 : str.unicode()) {}
    TokenizerSubstring(const TQChar *str, int length) : m_length(length), m_current(length == 0 ? 0 : str) {}

    void clear() { m_length = 0; m_current = 0; }

    void appendTo(TQString &str) const {
        if (m_string.unicode() == m_current) {
            if (str.isEmpty())
                str = m_string;
            else
                str.append(m_string);
        } else {
            str.insert(str.length(), m_current, m_length);
        }
    }
private:
    TQString m_string;
    int m_length;
    const TQChar *m_current;
};

class TokenizerString
{

public:
    TokenizerString() : m_currentChar(0), m_lines(0), m_composite(false) {}
    TokenizerString(const TQChar *str, int length) : m_currentString(str, length), m_currentChar(m_currentString.m_current), m_lines(0), m_composite(false) {}
    TokenizerString(const TQString &str) : m_currentString(str), m_currentChar(m_currentString.m_current), m_lines(0), m_composite(false) {}
    TokenizerString(const TokenizerString &o) : m_pushedChar1(o.m_pushedChar1), m_pushedChar2(o.m_pushedChar2),
                                                m_currentString(o.m_currentString), m_substrings(o.m_substrings),
                                                m_lines(o.m_lines), m_composite(o.m_composite) { 
        m_currentChar = m_pushedChar1.isNull() ? m_currentString.m_current : &m_pushedChar1; 
    } 

    void clear();

    void append(const TokenizerString &);
    void prepend(const TokenizerString &);
    
    void push(TQChar c) {
        if (m_pushedChar1.isNull()) {
            m_pushedChar1 = c;
	    m_currentChar = m_pushedChar1.isNull() ? m_currentString.m_current : &m_pushedChar1;
	} else {
            assert(m_pushedChar2.isNull());
            m_pushedChar2 = c;
        }
    }
    
    bool isEmpty() const { return !current(); }
    uint length() const;

    void advance() {
        if (!m_pushedChar1.isNull()) {
            m_pushedChar1 = m_pushedChar2;
            m_pushedChar2 = 0;
        } else if (m_currentString.m_current) {
            m_lines += *m_currentString.m_current++ == '\n';
            if (--m_currentString.m_length == 0)
                advanceSubstring();
        }
	m_currentChar = m_pushedChar1.isNull() ? m_currentString.m_current: &m_pushedChar1;
    }
    uint count() const { return m_substrings.count(); }
    
    bool escaped() const { return !m_pushedChar1.isNull(); }

    int lineCount() const { return m_lines; }
    void resetLineCount() { m_lines = 0; }
    
    TQString toString() const;

    void operator++() { advance(); }
    const TQChar &operator*() const { return *current(); }
    const TQChar *operator->() const { return current(); }
    
private:
    void append(const TokenizerSubstring &);
    void prepend(const TokenizerSubstring &);

    void advanceSubstring();
    const TQChar *current() const { return m_currentChar; }

    TQChar m_pushedChar1;
    TQChar m_pushedChar2;
    TokenizerSubstring m_currentString;
    const TQChar *m_currentChar;
    TQValueList<TokenizerSubstring> m_substrings;
    int m_lines;
    bool m_composite;

};


class TokenizerQueue : public TQValueList<TokenizerString>
{

public:
    TokenizerQueue() {}
    ~TokenizerQueue() {}
    void  push( const TokenizerString &t ) { prepend(t); }
    TokenizerString pop() {
        if (isEmpty()) 
            return TokenizerString();
        TokenizerString t(first());
        remove( begin() );
        return t;
    }
    TokenizerString& top() { return first(); }
    TokenizerString& bottom() { return last(); }
};

}

#endif