source: trunk/src/tools/qstringlist.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 12.7 KB
Line 
1/****************************************************************************
2** $Id: qstringlist.cpp 2 2005-11-16 15:49:26Z dmik $
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, so
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 string lists by value; to iterate over
87 these 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 single element list with the element containing the single string
260 \a str.
261
262 \sa join() QString::section()
263*/
264
265QStringList QStringList::split( const QRegExp &sep, const QString &str,
266 bool allowEmptyEntries )
267{
268 QStringList lst;
269
270 QRegExp tep = sep;
271
272 int j = 0;
273 int i = tep.search( str, j );
274
275 while ( i != -1 ) {
276 if ( str.mid( j, i - j ).length() > 0 )
277 lst << str.mid( j, i - j );
278 else if ( allowEmptyEntries )
279 lst << QString::null;
280 if ( tep.matchedLength() == 0 )
281 j = i + 1;
282 else
283 j = i + tep.matchedLength();
284 i = tep.search( str, j );
285 }
286
287 int l = str.length() - 1;
288 if ( str.mid( j, l - j + 1 ).length() > 0 )
289 lst << str.mid( j, l - j + 1 );
290 else if ( allowEmptyEntries )
291 lst << QString::null;
292
293 return lst;
294}
295#endif
296
297/*!
298 Returns a list of all the strings containing the substring \a str.
299
300 If \a cs is TRUE, the grep is done case-sensitively; otherwise
301 case is ignored.
302
303 \code
304 QStringList list;
305 list << "Bill Gates" << "John Doe" << "Bill Clinton";
306 list = list.grep( "Bill" );
307 // list == ["Bill Gates", "Bill Clinton"]
308 \endcode
309
310 \sa QString::find()
311*/
312
313QStringList QStringList::grep( const QString &str, bool cs ) const
314{
315 QStringList res;
316 for ( QStringList::ConstIterator it = begin(); it != end(); ++it )
317 if ( (*it).contains(str, cs) )
318 res << *it;
319
320 return res;
321}
322
323#ifndef QT_NO_REGEXP
324/*!
325 \overload
326
327 Returns a list of all the strings that match the regular
328 expression \a rx.
329
330 \sa QString::find()
331*/
332
333QStringList QStringList::grep( const QRegExp &rx ) const
334{
335 QStringList res;
336 for ( QStringList::ConstIterator it = begin(); it != end(); ++it )
337 if ( (*it).find(rx) != -1 )
338 res << *it;
339
340 return res;
341}
342#endif
343
344/*!
345 Replaces every occurrence of the string \a before in the strings
346 that constitute the string list with the string \a after. Returns
347 a reference to the string list.
348
349 If \a cs is TRUE, the search is case sensitive; otherwise the
350 search is case insensitive.
351
352 Example:
353 \code
354 QStringList list;
355 list << "alpha" << "beta" << "gamma" << "epsilon";
356 list.gres( "a", "o" );
357 // list == ["olpho", "beto", "gommo", "epsilon"]
358 \endcode
359
360 \sa QString::replace()
361*/
362QStringList& QStringList::gres( const QString &before, const QString &after,
363 bool cs )
364{
365 QStringList::Iterator it = begin();
366 while ( it != end() ) {
367 (*it).replace( before, after, cs );
368 ++it;
369 }
370 return *this;
371}
372
373#ifndef QT_NO_REGEXP_CAPTURE
374/*!
375 \overload
376
377 Replaces every occurrence of the regexp \a rx in the string
378 with \a after. Returns a reference to the string list.
379
380 Example:
381 \code
382 QStringList list;
383 list << "alpha" << "beta" << "gamma" << "epsilon";
384 list.gres( QRegExp("^a"), "o" );
385 // list == ["olpha", "beta", "gamma", "epsilon"]
386 \endcode
387
388 For regexps containing \link qregexp.html#capturing-text
389 capturing parentheses \endlink, occurrences of <b>\\1</b>,
390 <b>\\2</b>, ..., in \a after are replaced with \a{rx}.cap(1),
391 cap(2), ...
392
393 Example:
394 \code
395 QStringList list;
396 list << "Bill Clinton" << "Gates, Bill";
397 list.gres( QRegExp("^(.*), (.*)$"), "\\2 \\1" );
398 // list == ["Bill Clinton", "Bill Gates"]
399 \endcode
400
401 \sa QString::replace()
402*/
403QStringList& QStringList::gres( const QRegExp &rx, const QString &after )
404{
405 QStringList::Iterator it = begin();
406 while ( it != end() ) {
407 (*it).replace( rx, after );
408 ++it;
409 }
410 return *this;
411}
412
413#endif
414
415/*!
416 Joins the string list into a single string with each element
417 separated by the string \a sep (which can be empty).
418
419 \sa split()
420*/
421QString QStringList::join( const QString &sep ) const
422{
423 QString res;
424 bool alredy = FALSE;
425 for ( QStringList::ConstIterator it = begin(); it != end(); ++it ) {
426 if ( alredy )
427 res += sep;
428 alredy = TRUE;
429 res += *it;
430 }
431
432 return res;
433}
434
435#ifndef QT_NO_DATASTREAM
436Q_EXPORT QDataStream &operator>>( QDataStream & s, QStringList& l )
437{
438 return s >> (QValueList<QString>&)l;
439}
440
441Q_EXPORT QDataStream &operator<<( QDataStream & s, const QStringList& l )
442{
443 return s << (const QValueList<QString>&)l;
444}
445#endif
446
447/*!
448 Converts from an ASCII-QStrList \a ascii to a QStringList (Unicode).
449*/
450QStringList QStringList::fromStrList(const QStrList& ascii)
451{
452 QStringList res;
453 const char * s;
454 for ( QStrListIterator it(ascii); (s=it.current()); ++it )
455 res << s;
456 return res;
457}
458
459/*! \fn void QStringList::detach()
460 \reimp
461*/
462
463
464#endif //QT_NO_STRINGLIST
Note: See TracBrowser for help on using the repository browser.