~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/platform/text/SegmentedString.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
 
3
 
 
4
    This library is free software; you can redistribute it and/or
 
5
    modify it under the terms of the GNU Library General Public
 
6
    License as published by the Free Software Foundation; either
 
7
    version 2 of the License, or (at your option) any later version.
 
8
 
 
9
    This library is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
    Library General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU Library General Public License
 
15
    along with this library; see the file COPYING.LIB.  If not, write to
 
16
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
    Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "config.h"
 
21
#include "SegmentedString.h"
 
22
 
 
23
namespace WebCore {
 
24
 
 
25
SegmentedString::SegmentedString(const SegmentedString& other)
 
26
    : m_pushedChar1(other.m_pushedChar1)
 
27
    , m_pushedChar2(other.m_pushedChar2)
 
28
    , m_currentString(other.m_currentString)
 
29
    , m_substrings(other.m_substrings)
 
30
    , m_closed(other.m_closed)
 
31
    , m_empty(other.m_empty)
 
32
    , m_fastPathFlags(other.m_fastPathFlags)
 
33
    , m_advanceFunc(other.m_advanceFunc)
 
34
    , m_advanceAndUpdateLineNumberFunc(other.m_advanceAndUpdateLineNumberFunc)
 
35
{
 
36
    if (m_pushedChar2)
 
37
        m_currentChar = m_pushedChar2;
 
38
    else if (m_pushedChar1)
 
39
        m_currentChar = m_pushedChar1;
 
40
    else
 
41
        m_currentChar = m_currentString.m_length ? m_currentString.getCurrentChar() : 0;
 
42
}
 
43
 
 
44
const SegmentedString& SegmentedString::operator=(const SegmentedString& other)
 
45
{
 
46
    m_pushedChar1 = other.m_pushedChar1;
 
47
    m_pushedChar2 = other.m_pushedChar2;
 
48
    m_currentString = other.m_currentString;
 
49
    m_substrings = other.m_substrings;
 
50
    if (m_pushedChar2)
 
51
        m_currentChar = m_pushedChar2;
 
52
    else if (m_pushedChar1)
 
53
        m_currentChar = m_pushedChar1;
 
54
    else
 
55
        m_currentChar = m_currentString.m_length ? m_currentString.getCurrentChar() : 0;
 
56
 
 
57
    m_closed = other.m_closed;
 
58
    m_empty = other.m_empty;
 
59
    m_fastPathFlags = other.m_fastPathFlags;
 
60
    m_numberOfCharactersConsumedPriorToCurrentString = other.m_numberOfCharactersConsumedPriorToCurrentString;
 
61
    m_numberOfCharactersConsumedPriorToCurrentLine = other.m_numberOfCharactersConsumedPriorToCurrentLine;
 
62
    m_currentLine = other.m_currentLine;
 
63
 
 
64
    m_advanceFunc = other.m_advanceFunc;
 
65
    m_advanceAndUpdateLineNumberFunc = other.m_advanceAndUpdateLineNumberFunc;
 
66
 
 
67
    return *this;
 
68
}
 
69
 
 
70
unsigned SegmentedString::length() const
 
71
{
 
72
    unsigned length = m_currentString.m_length;
 
73
    if (m_pushedChar1) {
 
74
        ++length;
 
75
        if (m_pushedChar2)
 
76
            ++length;
 
77
    }
 
78
    if (isComposite()) {
 
79
        Deque<SegmentedSubstring>::const_iterator it = m_substrings.begin();
 
80
        Deque<SegmentedSubstring>::const_iterator e = m_substrings.end();
 
81
        for (; it != e; ++it)
 
82
            length += it->m_length;
 
83
    }
 
84
    return length;
 
85
}
 
86
 
 
87
void SegmentedString::setExcludeLineNumbers()
 
88
{
 
89
    m_currentString.setExcludeLineNumbers();
 
90
    if (isComposite()) {
 
91
        Deque<SegmentedSubstring>::iterator it = m_substrings.begin();
 
92
        Deque<SegmentedSubstring>::iterator e = m_substrings.end();
 
93
        for (; it != e; ++it)
 
94
            it->setExcludeLineNumbers();
 
95
    }
 
96
}
 
97
 
 
98
void SegmentedString::clear()
 
99
{
 
100
    m_pushedChar1 = 0;
 
101
    m_pushedChar2 = 0;
 
102
    m_currentChar = 0;
 
103
    m_currentString.clear();
 
104
    m_numberOfCharactersConsumedPriorToCurrentString = 0;
 
105
    m_numberOfCharactersConsumedPriorToCurrentLine = 0;
 
106
    m_currentLine = 0;
 
107
    m_substrings.clear();
 
108
    m_closed = false;
 
109
    m_empty = true;
 
110
    m_fastPathFlags = NoFastPath;
 
111
    m_advanceFunc = &SegmentedString::advanceEmpty;
 
112
    m_advanceAndUpdateLineNumberFunc = &SegmentedString::advanceEmpty;
 
113
}
 
114
 
 
115
void SegmentedString::append(const SegmentedSubstring& s)
 
116
{
 
117
    ASSERT(!m_closed);
 
118
    if (!s.m_length)
 
119
        return;
 
120
 
 
121
    if (!m_currentString.m_length) {
 
122
        m_numberOfCharactersConsumedPriorToCurrentString += m_currentString.numberOfCharactersConsumed();
 
123
        m_currentString = s;
 
124
        updateAdvanceFunctionPointers();
 
125
    } else
 
126
        m_substrings.append(s);
 
127
    m_empty = false;
 
128
}
 
129
 
 
130
void SegmentedString::prepend(const SegmentedSubstring& s)
 
131
{
 
132
    ASSERT(!escaped());
 
133
    ASSERT(!s.numberOfCharactersConsumed());
 
134
    if (!s.m_length)
 
135
        return;
 
136
 
 
137
    // FIXME: We're assuming that the prepend were originally consumed by
 
138
    //        this SegmentedString.  We're also ASSERTing that s is a fresh
 
139
    //        SegmentedSubstring.  These assumptions are sufficient for our
 
140
    //        current use, but we might need to handle the more elaborate
 
141
    //        cases in the future.
 
142
    m_numberOfCharactersConsumedPriorToCurrentString += m_currentString.numberOfCharactersConsumed();
 
143
    m_numberOfCharactersConsumedPriorToCurrentString -= s.m_length;
 
144
    if (!m_currentString.m_length) {
 
145
        m_currentString = s;
 
146
        updateAdvanceFunctionPointers();
 
147
    } else {
 
148
        // Shift our m_currentString into our list.
 
149
        m_substrings.prepend(m_currentString);
 
150
        m_currentString = s;
 
151
        updateAdvanceFunctionPointers();
 
152
    }
 
153
    m_empty = false;
 
154
}
 
155
 
 
156
void SegmentedString::close()
 
157
{
 
158
    // Closing a stream twice is likely a coding mistake.
 
159
    ASSERT(!m_closed);
 
160
    m_closed = true;
 
161
}
 
162
 
 
163
void SegmentedString::append(const SegmentedString& s)
 
164
{
 
165
    ASSERT(!m_closed);
 
166
    ASSERT(!s.escaped());
 
167
    append(s.m_currentString);
 
168
    if (s.isComposite()) {
 
169
        Deque<SegmentedSubstring>::const_iterator it = s.m_substrings.begin();
 
170
        Deque<SegmentedSubstring>::const_iterator e = s.m_substrings.end();
 
171
        for (; it != e; ++it)
 
172
            append(*it);
 
173
    }
 
174
    m_currentChar = m_pushedChar1 ? m_pushedChar1 : (m_currentString.m_length ? m_currentString.getCurrentChar() : 0);
 
175
}
 
176
 
 
177
void SegmentedString::prepend(const SegmentedString& s)
 
178
{
 
179
    ASSERT(!escaped());
 
180
    ASSERT(!s.escaped());
 
181
    if (s.isComposite()) {
 
182
        Deque<SegmentedSubstring>::const_reverse_iterator it = s.m_substrings.rbegin();
 
183
        Deque<SegmentedSubstring>::const_reverse_iterator e = s.m_substrings.rend();
 
184
        for (; it != e; ++it)
 
185
            prepend(*it);
 
186
    }
 
187
    prepend(s.m_currentString);
 
188
    m_currentChar = m_pushedChar1 ? m_pushedChar1 : (m_currentString.m_length ? m_currentString.getCurrentChar() : 0);
 
189
}
 
190
 
 
191
void SegmentedString::advanceSubstring()
 
192
{
 
193
    if (isComposite()) {
 
194
        m_numberOfCharactersConsumedPriorToCurrentString += m_currentString.numberOfCharactersConsumed();
 
195
        m_currentString = m_substrings.takeFirst();
 
196
        // If we've previously consumed some characters of the non-current
 
197
        // string, we now account for those characters as part of the current
 
198
        // string, not as part of "prior to current string."
 
199
        m_numberOfCharactersConsumedPriorToCurrentString -= m_currentString.numberOfCharactersConsumed();
 
200
        updateAdvanceFunctionPointers();
 
201
    } else {
 
202
        m_currentString.clear();
 
203
        m_empty = true;
 
204
        m_fastPathFlags = NoFastPath;
 
205
        m_advanceFunc = &SegmentedString::advanceEmpty;
 
206
        m_advanceAndUpdateLineNumberFunc = &SegmentedString::advanceEmpty;
 
207
    }
 
208
}
 
209
 
 
210
String SegmentedString::toString() const
 
211
{
 
212
    StringBuilder result;
 
213
    if (m_pushedChar1) {
 
214
        result.append(m_pushedChar1);
 
215
        if (m_pushedChar2)
 
216
            result.append(m_pushedChar2);
 
217
    }
 
218
    m_currentString.appendTo(result);
 
219
    if (isComposite()) {
 
220
        Deque<SegmentedSubstring>::const_iterator it = m_substrings.begin();
 
221
        Deque<SegmentedSubstring>::const_iterator e = m_substrings.end();
 
222
        for (; it != e; ++it)
 
223
            it->appendTo(result);
 
224
    }
 
225
    return result.toString();
 
226
}
 
227
 
 
228
void SegmentedString::advance(unsigned count, UChar* consumedCharacters)
 
229
{
 
230
    ASSERT(count <= length());
 
231
    for (unsigned i = 0; i < count; ++i) {
 
232
        consumedCharacters[i] = currentChar();
 
233
        advance();
 
234
    }
 
235
}
 
236
 
 
237
void SegmentedString::advance8()
 
238
{
 
239
    ASSERT(!m_pushedChar1);
 
240
    decrementAndCheckLength();
 
241
    m_currentChar = m_currentString.incrementAndGetCurrentChar8();
 
242
}
 
243
 
 
244
void SegmentedString::advance16()
 
245
{
 
246
    ASSERT(!m_pushedChar1);
 
247
    decrementAndCheckLength();
 
248
    m_currentChar = m_currentString.incrementAndGetCurrentChar16();
 
249
}
 
250
 
 
251
void SegmentedString::advanceAndUpdateLineNumber8()
 
252
{
 
253
    ASSERT(!m_pushedChar1);
 
254
    ASSERT(m_currentString.getCurrentChar() == m_currentChar);
 
255
    if (m_currentChar == '\n') {
 
256
        ++m_currentLine;
 
257
        m_numberOfCharactersConsumedPriorToCurrentLine = numberOfCharactersConsumed() + 1;
 
258
    }
 
259
    decrementAndCheckLength();
 
260
    m_currentChar = m_currentString.incrementAndGetCurrentChar8();
 
261
}
 
262
 
 
263
void SegmentedString::advanceAndUpdateLineNumber16()
 
264
{
 
265
    ASSERT(!m_pushedChar1);
 
266
    ASSERT(m_currentString.getCurrentChar() == m_currentChar);
 
267
    if (m_currentChar == '\n') {
 
268
        ++m_currentLine;
 
269
        m_numberOfCharactersConsumedPriorToCurrentLine = numberOfCharactersConsumed() + 1;
 
270
    }
 
271
    decrementAndCheckLength();
 
272
    m_currentChar = m_currentString.incrementAndGetCurrentChar16();
 
273
}
 
274
 
 
275
void SegmentedString::advanceSlowCase()
 
276
{
 
277
    if (m_pushedChar1) {
 
278
        m_pushedChar1 = m_pushedChar2;
 
279
        m_pushedChar2 = 0;
 
280
 
 
281
        if (m_pushedChar1) {
 
282
            m_currentChar = m_pushedChar1;
 
283
            return;
 
284
        }
 
285
 
 
286
        updateAdvanceFunctionPointers();
 
287
    } else if (m_currentString.m_length) {
 
288
        if (--m_currentString.m_length == 0)
 
289
            advanceSubstring();
 
290
    } else if (!isComposite()) {
 
291
        m_currentString.clear();
 
292
        m_empty = true;
 
293
        m_fastPathFlags = NoFastPath;
 
294
        m_advanceFunc = &SegmentedString::advanceEmpty;
 
295
        m_advanceAndUpdateLineNumberFunc = &SegmentedString::advanceEmpty;
 
296
    }
 
297
    m_currentChar = m_currentString.m_length ? m_currentString.getCurrentChar() : 0;
 
298
}
 
299
 
 
300
void SegmentedString::advanceAndUpdateLineNumberSlowCase()
 
301
{
 
302
    if (m_pushedChar1) {
 
303
        m_pushedChar1 = m_pushedChar2;
 
304
        m_pushedChar2 = 0;
 
305
 
 
306
        if (m_pushedChar1) {
 
307
            m_currentChar = m_pushedChar1;
 
308
            return;
 
309
        }
 
310
 
 
311
        updateAdvanceFunctionPointers();
 
312
    } else if (m_currentString.m_length) {
 
313
        if (m_currentString.getCurrentChar() == '\n' && m_currentString.doNotExcludeLineNumbers()) {
 
314
            ++m_currentLine;
 
315
            // Plus 1 because numberOfCharactersConsumed value hasn't incremented yet; it does with m_length decrement below.
 
316
            m_numberOfCharactersConsumedPriorToCurrentLine = numberOfCharactersConsumed() + 1;
 
317
        }
 
318
        if (--m_currentString.m_length == 0)
 
319
            advanceSubstring();
 
320
        else
 
321
            m_currentString.incrementAndGetCurrentChar(); // Only need the ++
 
322
    } else if (!isComposite()) {
 
323
        m_currentString.clear();
 
324
        m_empty = true;
 
325
        m_fastPathFlags = NoFastPath;
 
326
        m_advanceFunc = &SegmentedString::advanceEmpty;
 
327
        m_advanceAndUpdateLineNumberFunc = &SegmentedString::advanceEmpty;
 
328
    }
 
329
 
 
330
    m_currentChar = m_currentString.m_length ? m_currentString.getCurrentChar() : 0;
 
331
}
 
332
 
 
333
void SegmentedString::advanceEmpty()
 
334
{
 
335
    ASSERT(!m_currentString.m_length && !isComposite());
 
336
    m_currentChar = 0;
 
337
}
 
338
 
 
339
void SegmentedString::updateSlowCaseFunctionPointers()
 
340
{
 
341
    m_fastPathFlags = NoFastPath;
 
342
    m_advanceFunc = &SegmentedString::advanceSlowCase;
 
343
    m_advanceAndUpdateLineNumberFunc = &SegmentedString::advanceAndUpdateLineNumberSlowCase;
 
344
}
 
345
 
 
346
OrdinalNumber SegmentedString::currentLine() const
 
347
{
 
348
    return OrdinalNumber::fromZeroBasedInt(m_currentLine);
 
349
}
 
350
 
 
351
OrdinalNumber SegmentedString::currentColumn() const
 
352
{
 
353
    int zeroBasedColumn = numberOfCharactersConsumed() - m_numberOfCharactersConsumedPriorToCurrentLine;
 
354
    return OrdinalNumber::fromZeroBasedInt(zeroBasedColumn);
 
355
}
 
356
 
 
357
void SegmentedString::setCurrentPosition(OrdinalNumber line, OrdinalNumber columnAftreProlog, int prologLength)
 
358
{
 
359
    m_currentLine = line.zeroBasedInt();
 
360
    m_numberOfCharactersConsumedPriorToCurrentLine = numberOfCharactersConsumed() + prologLength - columnAftreProlog.zeroBasedInt();
 
361
}
 
362
 
 
363
}