~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to WebCore/platform/SegmentedString.h

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This file is part of the KDE libraries
 
3
 
 
4
    Copyright (C) 2004, 2005, 2006 Apple Computer
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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., 59 Temple Place - Suite 330,
 
19
    Boston, MA 02111-1307, USA.
 
20
*/
 
21
 
 
22
#ifndef SegmentedString_h
 
23
#define SegmentedString_h
 
24
 
 
25
#include "DeprecatedValueList.h"
 
26
#include "PlatformString.h"
 
27
 
 
28
namespace WebCore {
 
29
 
 
30
class SegmentedString;
 
31
 
 
32
class SegmentedSubstring {
 
33
private:
 
34
    friend class SegmentedString;
 
35
    
 
36
    SegmentedSubstring() : m_length(0), m_current(0), m_excludeLineNumbers(false) {}
 
37
    SegmentedSubstring(const String& str) : m_string(str), m_length(str.length()), m_excludeLineNumbers(false) {
 
38
        m_current = m_length == 0 ? 0 : m_string.characters();
 
39
    }
 
40
 
 
41
    SegmentedSubstring(const UChar* str, int length) : m_length(length), m_current(length == 0 ? 0 : str), m_excludeLineNumbers(false) {}
 
42
 
 
43
    void clear() { m_length = 0; m_current = 0; }
 
44
    
 
45
    bool excludeLineNumbers() const { return m_excludeLineNumbers; }
 
46
    void setExcludeLineNumbers() { m_excludeLineNumbers = true; }
 
47
 
 
48
    void appendTo(String& str) const {
 
49
        if (m_string.characters() == m_current) {
 
50
            if (str.isEmpty())
 
51
                str = m_string;
 
52
            else
 
53
                str.append(m_string);
 
54
        } else {
 
55
            str.append(String(m_current, m_length));
 
56
        }
 
57
    }
 
58
 
 
59
    String m_string;
 
60
    int m_length;
 
61
    const UChar* m_current;
 
62
    bool m_excludeLineNumbers;
 
63
};
 
64
 
 
65
class SegmentedString {
 
66
public:
 
67
    SegmentedString()
 
68
        : m_pushedChar1(0), m_pushedChar2(0), m_currentChar(0), m_composite(false) {}
 
69
    SegmentedString(const UChar* str, int length) : m_pushedChar1(0), m_pushedChar2(0)
 
70
        , m_currentString(str, length), m_currentChar(m_currentString.m_current), m_composite(false) {}
 
71
    SegmentedString(const String& str)
 
72
        : m_pushedChar1(0), m_pushedChar2(0), m_currentString(str)
 
73
        , m_currentChar(m_currentString.m_current), m_composite(false) {}
 
74
    SegmentedString(const SegmentedString&);
 
75
 
 
76
    const SegmentedString& operator=(const SegmentedString&);
 
77
 
 
78
    void clear();
 
79
 
 
80
    void append(const SegmentedString &);
 
81
    void prepend(const SegmentedString &);
 
82
    
 
83
    bool excludeLineNumbers() const { return m_currentString.excludeLineNumbers(); }
 
84
    void setExcludeLineNumbers();
 
85
 
 
86
    void push(UChar c) {
 
87
        if (!m_pushedChar1) {
 
88
            m_pushedChar1 = c;
 
89
            m_currentChar = m_pushedChar1 ? &m_pushedChar1 : m_currentString.m_current;
 
90
        } else {
 
91
            ASSERT(!m_pushedChar2);
 
92
            m_pushedChar2 = c;
 
93
        }
 
94
    }
 
95
    
 
96
    bool isEmpty() const { return !current(); }
 
97
    unsigned length() const;
 
98
 
 
99
    void advance(int* lineNumber = 0) {
 
100
        if (m_pushedChar1) {
 
101
            m_pushedChar1 = m_pushedChar2;
 
102
            m_pushedChar2 = 0;
 
103
        } else if (m_currentString.m_current) {
 
104
            if (*m_currentString.m_current++ == '\n' && lineNumber && !m_currentString.excludeLineNumbers())
 
105
                *lineNumber = *lineNumber + 1;
 
106
            if (--m_currentString.m_length == 0)
 
107
                advanceSubstring();
 
108
        }
 
109
        m_currentChar = m_pushedChar1 ? &m_pushedChar1 : m_currentString.m_current;
 
110
    }
 
111
    
 
112
    bool escaped() const { return m_pushedChar1; }
 
113
    
 
114
    String toString() const;
 
115
 
 
116
    const UChar& operator*() const { return *current(); }
 
117
    const UChar* operator->() const { return current(); }
 
118
    
 
119
private:
 
120
    void append(const SegmentedSubstring &);
 
121
    void prepend(const SegmentedSubstring &);
 
122
 
 
123
    void advanceSubstring();
 
124
    const UChar* current() const { return m_currentChar; }
 
125
 
 
126
    UChar m_pushedChar1;
 
127
    UChar m_pushedChar2;
 
128
    SegmentedSubstring m_currentString;
 
129
    const UChar* m_currentChar;
 
130
    DeprecatedValueList<SegmentedSubstring> m_substrings;
 
131
    bool m_composite;
 
132
};
 
133
 
 
134
}
 
135
 
 
136
#endif