1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the tools applications of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
57 Text::Text(const QString
&str
)
63 Text::Text( const Text
& text
)
74 Text
& Text::operator=( const Text
& text
)
76 if ( this != &text
) {
83 Text
& Text::operator<<( Atom::Type atomType
)
85 return operator<<( Atom(atomType
) );
88 Text
& Text::operator<<( const QString
& string
)
90 return operator<<( Atom(Atom::String
, string
) );
93 Text
& Text::operator<<( const Atom
& atom
)
96 first
= new Atom( atom
.type(), atom
.string() );
99 last
= new Atom( last
, atom
.type(), atom
.string() );
104 Text
& Text::operator<<( const Text
& text
)
106 const Atom
*atom
= text
.firstAtom();
107 while ( atom
!= 0 ) {
114 void Text::stripFirstAtom()
119 Atom
*oldFirst
= first
;
120 first
= first
->next();
125 void Text::stripLastAtom()
128 Atom
*oldLast
= last
;
129 if ( first
== last
) {
134 while ( last
->next() != oldLast
)
142 QString
Text::toString() const
145 const Atom
*atom
= firstAtom();
146 while ( atom
!= 0 ) {
147 if ( atom
->type() == Atom::String
|| atom
->type() == Atom::AutoLink
)
148 str
+= atom
->string();
154 Text
Text::subText( Atom::Type left
, Atom::Type right
, const Atom
*from
) const
156 const Atom
*begin
= from
? from
: firstAtom();
159 while ( begin
!= 0 && begin
->type() != left
)
160 begin
= begin
->next();
162 begin
= begin
->next();
165 while ( end
!= 0 && end
->type() != right
)
169 return subText( begin
, end
);
172 Text
Text::sectionHeading(const Atom
*sectionLeft
)
174 if ( sectionLeft
!= 0 ) {
175 const Atom
*begin
= sectionLeft
;
176 while ( begin
!= 0 && begin
->type() != Atom::SectionHeadingLeft
)
177 begin
= begin
->next();
179 begin
= begin
->next();
181 const Atom
*end
= begin
;
182 while ( end
!= 0 && end
->type() != Atom::SectionHeadingRight
)
186 return subText( begin
, end
);
191 const Atom
*Text::sectionHeadingAtom(const Atom
*sectionLeft
)
193 if ( sectionLeft
!= 0 ) {
194 const Atom
*begin
= sectionLeft
;
195 while ( begin
!= 0 && begin
->type() != Atom::SectionHeadingLeft
)
196 begin
= begin
->next();
198 begin
= begin
->next();
205 void Text::dump() const
207 const Atom
*atom
= firstAtom();
208 while ( atom
!= 0 ) {
209 QString str
= atom
->string();
210 str
.replace( "\\", "\\\\" );
211 str
.replace( "\"", "\\\"" );
212 str
.replace( "\n", "\\n" );
213 str
.replace( QRegExp("[^\x20-\x7e]"), "?" );
214 if ( !str
.isEmpty() )
215 str
= " \"" + str
+ "\"";
216 fprintf(stderr
, " %-15s%s\n", atom
->typeString().toLatin1().data(), str
.toLatin1().data() );
221 Text
Text::subText( const Atom
*begin
, const Atom
*end
)
225 while ( begin
!= end
) {
227 begin
= begin
->next();
235 while ( first
!= 0 ) {
237 first
= first
->next();
244 int Text::compare(const Text
&text1
, const Text
&text2
)
247 return text2
.isEmpty() ? 0 : -1;
251 const Atom
*atom1
= text1
.firstAtom();
252 const Atom
*atom2
= text2
.firstAtom();
255 if (atom1
->type() != atom2
->type())
256 return (int)atom1
->type() - (int)atom2
->type();
257 int cmp
= QString::compare(atom1
->string(), atom2
->string());
261 if (atom1
== text1
.lastAtom())
262 return atom2
== text2
.lastAtom() ? 0 : -1;
263 if (atom2
== text2
.lastAtom())
265 atom1
= atom1
->next();
266 atom2
= atom2
->next();