Warning: That file was not part of the compilation database. It may have many parsing errors.

1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*
43 text.cpp
44*/
45
46#include <qregexp.h>
47#include "text.h"
48#include <stdio.h>
49
50QT_BEGIN_NAMESPACE
51
52Text::Text()
53 : first(0), last(0)
54{
55}
56
57Text::Text(const QString &str)
58 : first(0), last(0)
59{
60 operator<<(str);
61}
62
63Text::Text(const Text& text)
64 : first(0), last(0)
65{
66 operator=(text);
67}
68
69Text::~Text()
70{
71 clear();
72}
73
74Text& Text::operator=(const Text& text)
75{
76 if (this != &text) {
77 clear();
78 operator<<(text);
79 }
80 return *this;
81}
82
83Text& Text::operator<<(Atom::Type atomType)
84{
85 return operator<<(Atom(atomType));
86}
87
88Text& Text::operator<<(const QString& string)
89{
90 return operator<<(Atom(Atom::String, string));
91}
92
93Text& Text::operator<<(const Atom& atom)
94{
95 if (first == 0) {
96 first = new Atom(atom.type(), atom.string());
97 last = first;
98 } else {
99 last = new Atom(last, atom.type(), atom.string());
100 }
101 return *this;
102}
103
104Text& Text::operator<<(const Text& text)
105{
106 const Atom* atom = text.firstAtom();
107 while (atom != 0) {
108 operator<<(*atom);
109 atom = atom->next();
110 }
111 return *this;
112}
113
114void Text::stripFirstAtom()
115{
116 if (first != 0) {
117 if (first == last)
118 last = 0;
119 Atom* oldFirst = first;
120 first = first->next();
121 delete oldFirst;
122 }
123}
124
125void Text::stripLastAtom()
126{
127 if (last != 0) {
128 Atom* oldLast = last;
129 if (first == last) {
130 first = 0;
131 last = 0;
132 } else {
133 last = first;
134 while (last->next() != oldLast)
135 last = last->next();
136 last->setNext(0);
137 }
138 delete oldLast;
139 }
140}
141
142QString Text::toString() const
143{
144 QString str;
145 const Atom* atom = firstAtom();
146 while (atom != 0) {
147 if (atom->type() == Atom::String ||
148 atom->type() == Atom::AutoLink ||
149 atom->type() == Atom::GuidLink)
150 str += atom->string();
151 atom = atom->next();
152 }
153 return str;
154}
155
156Text Text::subText(Atom::Type left, Atom::Type right, const Atom* from, bool inclusive) const
157{
158 const Atom* begin = from ? from : firstAtom();
159 const Atom* end;
160
161 while (begin != 0 && begin->type() != left)
162 begin = begin->next();
163 if (begin != 0) {
164 if (!inclusive)
165 begin = begin->next();
166 }
167
168 end = begin;
169 while (end != 0 && end->type() != right)
170 end = end->next();
171 if (end == 0)
172 begin = 0;
173 else if (inclusive)
174 end = end->next();
175 return subText(begin, end);
176}
177
178Text Text::sectionHeading(const Atom* sectionLeft)
179{
180 if (sectionLeft != 0) {
181 const Atom* begin = sectionLeft;
182 while (begin != 0 && begin->type() != Atom::SectionHeadingLeft)
183 begin = begin->next();
184 if (begin != 0)
185 begin = begin->next();
186
187 const Atom* end = begin;
188 while (end != 0 && end->type() != Atom::SectionHeadingRight)
189 end = end->next();
190
191 if (end != 0)
192 return subText(begin, end);
193 }
194 return Text();
195}
196
197const Atom* Text::sectionHeadingAtom(const Atom* sectionLeft)
198{
199 if (sectionLeft != 0) {
200 const Atom* begin = sectionLeft;
201 while (begin != 0 && begin->type() != Atom::SectionHeadingLeft)
202 begin = begin->next();
203 if (begin != 0)
204 begin = begin->next();
205
206 return begin;
207 }
208 return 0;
209}
210
211void Text::dump() const
212{
213 const Atom* atom = firstAtom();
214 while (atom != 0) {
215 QString str = atom->string();
216 str.replace("\\", "\\\\");
217 str.replace("\"", "\\\"");
218 str.replace("\n", "\\n");
219 str.replace(QRegExp("[^\x20-\x7e]"), "?");
220 if (!str.isEmpty())
221 str = " \"" + str + "\"";
222 fprintf(stderr, " %-15s%s\n", atom->typeString().toLatin1().data(), str.toLatin1().data());
223 atom = atom->next();
224 }
225}
226
227Text Text::subText(const Atom* begin, const Atom* end)
228{
229 Text text;
230 if (begin != 0) {
231 while (begin != end) {
232 text << *begin;
233 begin = begin->next();
234 }
235 }
236 return text;
237}
238
239void Text::clear()
240{
241 while (first != 0) {
242 Atom* atom = first;
243 first = first->next();
244 delete atom;
245 }
246 first = 0;
247 last = 0;
248}
249
250int Text::compare(const Text &text1, const Text &text2)
251{
252 if (text1.isEmpty())
253 return text2.isEmpty() ? 0 : -1;
254 if (text2.isEmpty())
255 return 1;
256
257 const Atom* atom1 = text1.firstAtom();
258 const Atom* atom2 = text2.firstAtom();
259
260 for (;;) {
261 if (atom1->type() != atom2->type())
262 return (int)atom1->type() - (int)atom2->type();
263 int cmp = QString::compare(atom1->string(), atom2->string());
264 if (cmp != 0)
265 return cmp;
266
267 if (atom1 == text1.lastAtom())
268 return atom2 == text2.lastAtom() ? 0 : -1;
269 if (atom2 == text2.lastAtom())
270 return 1;
271 atom1 = atom1->next();
272 atom2 = atom2->next();
273 }
274}
275
276QT_END_NAMESPACE
277

Warning: That file was not part of the compilation database. It may have many parsing errors.