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
|
/*
Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
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.
*/
#ifndef SegmentedString_h
#define SegmentedString_h
#include "PlatformString.h"
#include <wtf/Deque.h>
namespace WebCore {
class SegmentedString;
class SegmentedSubstring {
public:
SegmentedSubstring() : m_length(0), m_current(0), m_doNotExcludeLineNumbers(true) {}
SegmentedSubstring(const String& str)
: m_length(str.length())
, m_current(str.isEmpty() ? 0 : str.characters())
, m_string(str)
, m_doNotExcludeLineNumbers(true)
{
}
SegmentedSubstring(const UChar* str, int length) : m_length(length), m_current(length == 0 ? 0 : str), m_doNotExcludeLineNumbers(true) {}
void clear() { m_length = 0; m_current = 0; }
bool excludeLineNumbers() const { return !m_doNotExcludeLineNumbers; }
bool doNotExcludeLineNumbers() const { return m_doNotExcludeLineNumbers; }
void setExcludeLineNumbers() { m_doNotExcludeLineNumbers = false; }
void appendTo(String& str) const
{
if (m_string.characters() == m_current) {
if (str.isEmpty())
str = m_string;
else
str.append(m_string);
} else {
str.append(String(m_current, m_length));
}
}
public:
int m_length;
const UChar* m_current;
private:
String m_string;
bool m_doNotExcludeLineNumbers;
};
class SegmentedString {
public:
SegmentedString()
: m_pushedChar1(0), m_pushedChar2(0), m_currentChar(0), m_composite(false) {}
SegmentedString(const UChar* str, int length) : m_pushedChar1(0), m_pushedChar2(0)
, m_currentString(str, length), m_currentChar(m_currentString.m_current), m_composite(false) {}
SegmentedString(const String& str)
: m_pushedChar1(0), m_pushedChar2(0), m_currentString(str)
, m_currentChar(m_currentString.m_current), m_composite(false) {}
SegmentedString(const SegmentedString&);
const SegmentedString& operator=(const SegmentedString&);
void clear();
void append(const SegmentedString&);
void prepend(const SegmentedString&);
bool excludeLineNumbers() const { return m_currentString.excludeLineNumbers(); }
void setExcludeLineNumbers();
void push(UChar c)
{
if (!m_pushedChar1) {
m_pushedChar1 = c;
m_currentChar = m_pushedChar1 ? &m_pushedChar1 : m_currentString.m_current;
} else {
ASSERT(!m_pushedChar2);
m_pushedChar2 = c;
}
}
bool isEmpty() const { return !current(); }
unsigned length() const;
void advance()
{
if (!m_pushedChar1 && m_currentString.m_length > 1) {
--m_currentString.m_length;
m_currentChar = ++m_currentString.m_current;
return;
}
advanceSlowCase();
}
void advancePastNewline(int& lineNumber)
{
ASSERT(*current() == '\n');
if (!m_pushedChar1 && m_currentString.m_length > 1) {
lineNumber += m_currentString.doNotExcludeLineNumbers();
--m_currentString.m_length;
m_currentChar = ++m_currentString.m_current;
return;
}
advanceSlowCase(lineNumber);
}
void advancePastNonNewline()
{
ASSERT(*current() != '\n');
if (!m_pushedChar1 && m_currentString.m_length > 1) {
--m_currentString.m_length;
m_currentChar = ++m_currentString.m_current;
return;
}
advanceSlowCase();
}
void advance(int& lineNumber)
{
if (!m_pushedChar1 && m_currentString.m_length > 1) {
lineNumber += (*m_currentString.m_current == '\n') & m_currentString.doNotExcludeLineNumbers();
--m_currentString.m_length;
m_currentChar = ++m_currentString.m_current;
return;
}
advanceSlowCase(lineNumber);
}
bool escaped() const { return m_pushedChar1; }
String toString() const;
const UChar& operator*() const { return *current(); }
const UChar* operator->() const { return current(); }
private:
void append(const SegmentedSubstring&);
void prepend(const SegmentedSubstring&);
void advanceSlowCase();
void advanceSlowCase(int& lineNumber);
void advanceSubstring();
const UChar* current() const { return m_currentChar; }
UChar m_pushedChar1;
UChar m_pushedChar2;
SegmentedSubstring m_currentString;
const UChar* m_currentChar;
Deque<SegmentedSubstring> m_substrings;
bool m_composite;
};
}
#endif
|