qstringlist.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
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 "qstrlist.h"
42 #include "qdatastream.h"
43 #include "qtl.h"
44 
45 // NOT REVISED
46 /*!
47  \class QStringList qstringlist.h
48  \brief A list of strings.
49 
50  \ingroup qtl
51  \ingroup tools
52  \ingroup shared
53 
54  QStringList is basically a QValueList of QString objects. As opposed
55  to QStrList, that stores pointers to characters, QStringList deals
56  with real QString objects. It is the class of choice whenever you
57  work with unicode strings.
58 
59  Like QString itself, QStringList objects are implicit shared.
60  Passing them around as value-parameters is both fast and safe.
61 
62  Example:
63  \code
64  QStringList list;
65 
66  // three different ways of appending values:
67  list.append( "Torben");
68  list += "Warwick";
69  list << "Matthias" << "Arnt" << "Paul";
70 
71  // sort the list, Arnt's now first
72  list.sort();
73 
74  // print it out
75  for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) {
76  printf( "%s \n", (*it).latin1() );
77  }
78  \endcode
79 
80  Convenience methods such as sort(), split(), join() and grep() make
81  working with QStringList easy.
82 */
83 
84 /*!
85  \fn QStringList::QStringList()
86  Creates an empty list.
87 */
88 
89 /*! \fn QStringList::QStringList( const QStringList& l )
90  Creates a copy of the list. This function is very fast since
91  QStringList is implicit shared. However, for the programmer this
92  is the same as a deep copy. If this list or the original one or some
93  other list referencing the same shared data is modified, then the
94  modifying list makes a copy first.
95 */
96 
97 /*!
98  \fn QStringList::QStringList (const QString & i)
99  Constructs a string list consisting of the single string \a i.
100  To make longer lists easily, use:
101  \code
102  QString s1,s2,s3;
103  ...
104  QStringList mylist = QStringList() << s1 << s2 << s3;
105  \endcode
106 */
107 
108 /*!
109  \fn QStringList::QStringList (const char* i)
110  Constructs a string list consisting of the single latin-1 string \a i.
111 */
112 
113 /*! \fn QStringList::QStringList( const QValueList<QString>& l )
114 
115  Constructs a new string list that is a copy of \a l.
116 */
117 
118 /*!
119  Sorts the list of strings in ascending order.
120 
121  Sorting is very fast. It uses the Qt Template Library's
122  efficient HeapSort implementation that operates in O(n*log n).
123 */
125 {
126  qHeapSort(*this);
127 }
128 
129 /*!
130  Splits the string \a str using \a sep as separator. Returns the
131  list of strings. If \a allowEmptyEntries is TRUE, also empty
132  entries are inserted into the list, else not. So if you have
133  a string 'abc..d.e.', a list which contains 'abc', 'd', and 'e'
134  would be returned if \a allowEmptyEntries is FALSE, but
135  a list containing 'abc', '', 'd', 'e' and '' would be returned if
136  \a allowEmptyEntries is TRUE.
137  If \a str doesn't contain \a sep, a stringlist
138  with one item, which is the same as \a str, is returned.
139 
140  \sa join()
141 */
142 
143 QStringList QStringList::split( const QChar &sep, const QString &str, bool allowEmptyEntries )
144 {
145  return split( QString( sep ), str, allowEmptyEntries );
146 }
147 
148 /*!
149  Splits the string \a str using \a sep as separator. Returns the
150  list of strings. If \a allowEmptyEntries is TRUE, also empty
151  entries are inserted into the list, else not. So if you have
152  a string 'abc..d.e.', a list which contains 'abc', 'd', and 'e'
153  would be returned if \a allowEmptyEntries is FALSE, but
154  a list containing 'abc', '', 'd', 'e' and '' would be returned if
155  \a allowEmptyEntries is TRUE.
156  If \a str doesn't contain \a sep, a stringlist
157  with one item, which is the same as \a str, is returned.
158 
159  \sa join()
160 */
161 
162 QStringList QStringList::split( const QString &sep, const QString &str, bool allowEmptyEntries )
163 {
165 
166  int j = 0;
167  int i = str.find( sep, j );
168 
169  while ( i != -1 ) {
170  if ( str.mid( j, i - j ).length() > 0 )
171  lst << str.mid( j, i - j );
172  else if ( allowEmptyEntries )
173  lst << QString::null;
174  j = i + sep.length();
175  i = str.find( sep, j );
176  }
177 
178  int l = str.length() - 1;
179  if ( str.mid( j, l - j + 1 ).length() > 0 )
180  lst << str.mid( j, l - j + 1 );
181  else if ( allowEmptyEntries )
182  lst << QString::null;
183 
184  return lst;
185 }
186 
187 QStringList QStringList::split( const QCString &sep, const QCString &str, bool allowEmptyEntries )
188 {
189  return split(QString(sep.data()),QString(str.data()),allowEmptyEntries);
190 }
191 
192 /*!
193  Splits the string \a str using the regular expression \a sep as separator. Returns the
194  list of strings. If \a allowEmptyEntries is TRUE, also empty
195  entries are inserted into the list, else not. So if you have
196  a string 'abc..d.e.', a list which contains 'abc', 'd', and 'e'
197  would be returned if \a allowEmptyEntries is FALSE, but
198  a list containing 'abc', '', 'd', 'e' and '' would be returned if
199  \a allowEmptyEntries is TRUE.
200  If \a str doesn't contain \a sep, a stringlist
201  with one item, which is the same as \a str, is returned.
202 
203  \sa join()
204 */
205 
206 QStringList QStringList::split( const QRegExp &sep, const QString &str, bool allowEmptyEntries )
207 {
209 
210  int j = 0;
211  int len = 0;
212  int i = sep.match( str.data(), j, &len );
213 
214  while ( i != -1 ) {
215  if ( str.mid( j, i - j ).length() > 0 )
216  lst << str.mid( j, i - j );
217  else if ( allowEmptyEntries )
218  lst << QString::null;
219  j = i + len;
220  i = sep.match( str.data(), j, &len );
221  }
222 
223  int l = str.length() - 1;
224  if ( str.mid( j, l - j + 1 ).length() > 0 )
225  lst << str.mid( j, l - j + 1 );
226  else if ( allowEmptyEntries )
227  lst << QString::null;
228 
229  return lst;
230 }
231 
232 /*!
233  Returns a list of all strings containing the substring \a str.
234 
235  If \a cs is TRUE, the grep is done case sensitively, else not.
236 */
237 
239 {
240  QStringList res;
241  for ( QStringList::ConstIterator it = begin(); it != end(); ++it )
242  if ( (*it).contains( str, cs ) )
243  res << *it;
244 
245  return res;
246 }
247 
248 /*!
249  Returns a list of all strings containing a substring that matches
250  the regular expression \a expr.
251 */
252 
254 {
255  QStringList res;
256  for ( QStringList::ConstIterator it = begin(); it != end(); ++it )
257  if ( (*it).contains( expr ) )
258  res << *it;
259 
260  return res;
261 }
262 
263 /*!
264  Joins the stringlist into a single string with each element
265  separated by \a sep.
266 
267  \sa split()
268 */
269 QString QStringList::join( const QString &sep ) const
270 {
271  QString res;
272  bool alredy = FALSE;
273  for ( QStringList::ConstIterator it = begin(); it != end(); ++it ) {
274  if ( alredy )
275  res += sep;
276  alredy = TRUE;
277  res += *it;
278  }
279 
280  return res;
281 }
282 
283 #ifndef QT_NO_DATASTREAM
285 {
286  return s >> (QValueList<QString>&)l;
287 }
288 
290 {
291  return s << (const QValueList<QString>&)l;
292 }
293 #endif
294 
295 /*!
296  Converts from a QStrList (ASCII) to a QStringList (Unicode).
297 */
299 {
300  QStringList res;
301  const char * s;
302  for ( QStrListIterator it(ascii); (s=it.current()); ++it )
303  res << s;
304  return res;
305 }
306 
307 #endif //QT_NO_STRINGLIST
The QRegExp class provides pattern matching using regular expressions or wildcards.
Definition: qregexp.h:46
QStringList grep(const QString &str, bool cs=TRUE) const
QString mid(uint index, uint len=0xffffffff) const
Definition: qstring.cpp:13265
static QStringList fromStrList(const QStrList &)
const bool FALSE
Definition: qglobal.h:370
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
The QChar class provides a light-weight Unicode character.
Definition: qstring.h:56
static QStrList * l
Definition: config.cpp:1044
const char * data() const
Definition: qstring.h:542
A list of strings.
Definition: qstringlist.h:51
void qHeapSort(InputIterator b, InputIterator e)
Definition: qtl.h:192
uint length() const
Definition: qstring.h:679
QValueList< QString > & operator<<(const QString &x)
Definition: qvaluelist.h:404
const char * data() const
Definition: qcstring.h:207
int match(const QCString &str, int index=0, int *len=0, bool indexIsStart=TRUE) const
Definition: qregexp.cpp:649
int find(QChar c, int index=0, bool cs=TRUE) const
Definition: qstring.cpp:12902
QString join(const QString &sep) const
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:47
static const Null null
Definition: qstring.h:376
const char * cs
static QStringList split(const QString &sep, const QString &str, bool allowEmptyEntries=FALSE)
static QCString * s
Definition: config.cpp:1042
#define Q_EXPORT
Definition: qglobal.h:468
const bool TRUE
Definition: qglobal.h:371
static QCString str
Q_EXPORT QDataStream & operator>>(QDataStream &s, QStringList &l)