summaryrefslogtreecommitdiff
path: root/qmake/tools/qstringlist.cpp
Unidiff
Diffstat (limited to 'qmake/tools/qstringlist.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--qmake/tools/qstringlist.cpp376
1 files changed, 376 insertions, 0 deletions
diff --git a/qmake/tools/qstringlist.cpp b/qmake/tools/qstringlist.cpp
new file mode 100644
index 0000000..0720e7f
--- a/dev/null
+++ b/qmake/tools/qstringlist.cpp
@@ -0,0 +1,376 @@
1/****************************************************************************
2** $Id$
3**
4** Implementation of QStringList
5**
6** Created : 990406
7**
8** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
9**
10** This file is part of the tools module of the Qt GUI Toolkit.
11**
12** This file may be distributed under the terms of the Q Public License
13** as defined by Trolltech AS of Norway and appearing in the file
14** LICENSE.QPL included in the packaging of this file.
15**
16** This file may be distributed and/or modified under the terms of the
17** GNU General Public License version 2 as published by the Free Software
18** Foundation and appearing in the file LICENSE.GPL included in the
19** packaging of this file.
20**
21** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22** licenses may use this file in accordance with the Qt Commercial License
23** Agreement provided with the Software.
24**
25** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27**
28** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29** information about Qt Commercial License Agreements.
30** See http://www.trolltech.com/qpl/ for QPL licensing information.
31** See http://www.trolltech.com/gpl/ for GPL licensing information.
32**
33** Contact info@trolltech.com if any conditions of this licensing are
34** not clear to you.
35**
36**********************************************************************/
37
38#include "qstringlist.h"
39
40#ifndef QT_NO_STRINGLIST
41#include "qregexp.h"
42#include "qstrlist.h"
43#include "qdatastream.h"
44#include "qtl.h"
45
46/*!
47 \class QStringList qstringlist.h
48 \reentrant
49 \brief The QStringList class provides a list of strings.
50
51 \ingroup tools
52 \ingroup shared
53 \ingroup text
54 \mainclass
55
56 It is used to store and manipulate strings that logically belong
57 together. Essentially QStringList is a QValueList of QString
58 objects. Unlike QStrList, which stores pointers to characters,
59 QStringList holds real QString objects. It is the class of choice
60 whenever you work with Unicode strings. QStringList is part of the
61 \link qtl.html Qt Template Library\endlink.
62
63 Like QString itself, QStringList objects are implicitly shared.
64 Passing them around as value-parameters is both fast and safe.
65
66 Strings can be added to a list using append(), operator+=() or
67 operator<<(), e.g.
68 \code
69 QStringList fonts;
70 fonts.append( "Times" );
71 fonts += "Courier";
72 fonts += "Courier New";
73 fonts << "Helvetica [Cronyx]" << "Helvetica [Adobe]";
74 \endcode
75
76 String lists have an iterator, QStringList::Iterator(), e.g.
77 \code
78 for ( QStringList::Iterator it = fonts.begin(); it != fonts.end(); ++it ) {
79 cout << *it << ":";
80 }
81 cout << endl;
82 // Output:
83 //Times:Courier:Courier New:Helvetica [Cronyx]:Helvetica [Adobe]:
84 \endcode
85
86 Many Qt functions return const string lists; to iterate over these
87 you should make a copy and iterate over the copy.
88
89 You can concatenate all the strings in a string list into a single
90 string (with an optional separator) using join(), e.g.
91 \code
92 QString allFonts = fonts.join( ", " );
93 cout << allFonts << endl;
94 // Output:
95 //Times, Courier, Courier New, Helvetica [Cronyx], Helvetica [Adobe]
96 \endcode
97
98 You can sort the list with sort(), and extract a new list which
99 contains only those strings which contain a particular substring
100 (or match a particular regular expression) using the grep()
101 functions, e.g.
102 \code
103 fonts.sort();
104 cout << fonts.join( ", " ) << endl;
105 // Output:
106 //Courier, Courier New, Helvetica [Adobe], Helvetica [Cronyx], Times
107
108 QStringList helveticas = fonts.grep( "Helvetica" );
109 cout << helveticas.join( ", " ) << endl;
110 // Output:
111 //Helvetica [Adobe], Helvetica [Cronyx]
112 \endcode
113
114 Existing strings can be split into string lists with character,
115 string or regular expression separators, e.g.
116 \code
117 QString s = "Red\tGreen\tBlue";
118 QStringList colors = QStringList::split( "\t", s );
119 cout << colors.join( ", " ) << endl;
120 // Output:
121 //Red, Green, Blue
122 \endcode
123*/
124
125/*!
126 \fn QStringList::QStringList()
127
128 Creates an empty string list.
129*/
130
131/*!
132 \fn QStringList::QStringList( const QStringList& l )
133
134 Creates a copy of the list \a l. This function is very fast
135 because QStringList is implicitly shared. In most situations this
136 acts like a deep copy, for example, if this list or the original
137 one or some other list referencing the same shared data is
138 modified, the modifying list first makes a copy, i.e.
139 copy-on-write.
140 In a threaded environment you may require a real deep copy
141 \omit see \l QDeepCopy\endomit.
142*/
143
144/*!
145 \fn QStringList::QStringList (const QString & i)
146
147 Constructs a string list consisting of the single string \a i.
148 Longer lists are easily created as follows:
149
150 \code
151 QStringList items;
152 items << "Buy" << "Sell" << "Update" << "Value";
153 \endcode
154*/
155
156/*!
157 \fn QStringList::QStringList (const char* i)
158
159 Constructs a string list consisting of the single latin-1 string \a i.
160*/
161
162/*!
163 \fn QStringList::QStringList( const QValueList<QString>& l )
164
165 Constructs a new string list that is a copy of \a l.
166*/
167
168/*!
169 Sorts the list of strings in ascending case-sensitive order.
170
171 Sorting is very fast. It uses the \link qtl.html Qt Template
172 Library's\endlink efficient HeapSort implementation that has a
173 time complexity of O(n*log n).
174
175 If you want to sort your strings in an arbitrary order consider
176 using a QMap. For example you could use a QMap\<QString,QString\>
177 to create a case-insensitive ordering (e.g. mapping the lowercase
178 text to the text), or a QMap\<int,QString\> to sort the strings by
179 some integer index, etc.
180*/
181void QStringList::sort()
182{
183 qHeapSort( *this );
184}
185
186/*!
187 \overload
188
189 This version of the function uses a QChar as separator, rather
190 than a regular expression.
191
192 \sa join() QString::section()
193*/
194
195QStringList QStringList::split( const QChar &sep, const QString &str,
196 bool allowEmptyEntries )
197{
198 return split( QString(sep), str, allowEmptyEntries );
199}
200
201/*!
202 \overload
203
204 This version of the function uses a QString as separator, rather
205 than a regular expression.
206
207 If \a sep is an empty string, the return value is a list of
208 one-character strings: split( QString( "" ), "four" ) returns the
209 four-item list, "f", "o", "u", "r".
210
211 If \a allowEmptyEntries is TRUE, an empty string is inserted in
212 the list wherever the separator matches twice without intervening
213 text.
214
215 \sa join() QString::section()
216*/
217
218QStringList QStringList::split( const QString &sep, const QString &str,
219 bool allowEmptyEntries )
220{
221 QStringList lst;
222
223 int j = 0;
224 int i = str.find( sep, j );
225
226 while ( i != -1 ) {
227 if ( i > j && i <= (int)str.length() )
228 lst << str.mid( j, i - j );
229 else if ( allowEmptyEntries )
230 lst << QString::null;
231 j = i + sep.length();
232 i = str.find( sep, sep.length() > 0 ? j : j+1 );
233 }
234
235 int l = str.length() - 1;
236 if ( str.mid( j, l - j + 1 ).length() > 0 )
237 lst << str.mid( j, l - j + 1 );
238 else if ( allowEmptyEntries )
239 lst << QString::null;
240
241 return lst;
242}
243
244#ifndef QT_NO_REGEXP
245/*!
246 Splits the string \a str into strings wherever the regular
247 expression \a sep occurs, and returns the list of those strings.
248
249 If \a allowEmptyEntries is TRUE, an empty string is inserted in
250 the list wherever the separator matches twice without intervening
251 text.
252
253 For example, if you split the string "a,,b,c" on commas, split()
254 returns the three-item list "a", "b", "c" if \a allowEmptyEntries
255 is FALSE (the default), and the four-item list "a", "", "b", "c"
256 if \a allowEmptyEntries is TRUE.
257
258 If \a sep does not match anywhere in \a str, split() returns a
259 list consisting of the single string \a str.
260
261 \sa join() QString::section()
262*/
263
264QStringList QStringList::split( const QRegExp &sep, const QString &str,
265 bool allowEmptyEntries )
266{
267 QStringList lst;
268
269 QRegExp tep = sep;
270
271 int j = 0;
272 int i = tep.search( str, j );
273
274 while ( i != -1 ) {
275 if ( str.mid( j, i - j ).length() > 0 )
276 lst << str.mid( j, i - j );
277 else if ( allowEmptyEntries )
278 lst << QString::null;
279 if ( tep.matchedLength() == 0 )
280 j = i + 1;
281 else
282 j = i + tep.matchedLength();
283 i = tep.search( str, j );
284 }
285
286 int l = str.length() - 1;
287 if ( str.mid( j, l - j + 1 ).length() > 0 )
288 lst << str.mid( j, l - j + 1 );
289 else if ( allowEmptyEntries )
290 lst << QString::null;
291
292 return lst;
293}
294#endif
295
296/*!
297 Returns a list of all strings containing the substring \a str.
298
299 If \a cs is TRUE, the grep is done case-sensitively; otherwise
300 case is ignored.
301*/
302
303QStringList QStringList::grep( const QString &str, bool cs ) const
304{
305 QStringList res;
306 for ( QStringList::ConstIterator it = begin(); it != end(); ++it )
307 if ( (*it).contains(str, cs) )
308 res << *it;
309
310 return res;
311}
312
313#ifndef QT_NO_REGEXP
314/*!
315 \overload
316
317 Returns a list of all the strings that match the regular
318 expression \a expr.
319*/
320
321QStringList QStringList::grep( const QRegExp &expr ) const
322{
323 QStringList res;
324 for ( QStringList::ConstIterator it = begin(); it != end(); ++it )
325 if ( (*it).contains(expr) )
326 res << *it;
327
328 return res;
329}
330#endif
331
332/*!
333 Joins the string list into a single string with each element
334 separated by the string \a sep (which can be empty).
335
336 \sa split()
337*/
338QString QStringList::join( const QString &sep ) const
339{
340 QString res;
341 bool alredy = FALSE;
342 for ( QStringList::ConstIterator it = begin(); it != end(); ++it ) {
343 if ( alredy )
344 res += sep;
345 alredy = TRUE;
346 res += *it;
347 }
348
349 return res;
350}
351
352#ifndef QT_NO_DATASTREAM
353Q_EXPORT QDataStream &operator>>( QDataStream & s, QStringList& l )
354{
355 return s >> (QValueList<QString>&)l;
356}
357
358Q_EXPORT QDataStream &operator<<( QDataStream & s, const QStringList& l )
359{
360 return s << (const QValueList<QString>&)l;
361}
362#endif
363
364/*!
365 Converts from an ASCII-QStrList \a ascii to a QStringList (Unicode).
366*/
367QStringList QStringList::fromStrList(const QStrList& ascii)
368{
369 QStringList res;
370 const char * s;
371 for ( QStrListIterator it(ascii); (s=it.current()); ++it )
372 res << s;
373 return res;
374}
375
376#endif //QT_NO_STRINGLIST