summaryrefslogtreecommitdiffstats
path: root/qtools/qstringlist.cpp
blob: ff4f33ef09199989983c33b8e5f5d9a9697e22bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/****************************************************************************
** 
**
** Implementation of QStringList
**
** Created : 990406
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of the tools module of the Qt GUI Toolkit.
**
** This file may be distributed under the terms of the Q Public License
** as defined by Trolltech AS of Norway and appearing in the file
** LICENSE.QPL included in the packaging of this file.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
** licenses may use this file in accordance with the Qt Commercial License
** Agreement provided with the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
**   information about Qt Commercial License Agreements.
** See http://www.trolltech.com/qpl/ for QPL licensing information.
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include "qstringlist.h"

#ifndef QT_NO_STRINGLIST
#include "qstrlist.h"
#include "qdatastream.h"
#include "qtl.h"

// NOT REVISED
/*!
  \class QStringList qstringlist.h
  \brief A list of strings.

  \ingroup qtl
  \ingroup tools
  \ingroup shared

  QStringList is basically a QValueList of QString objects. As opposed
  to QStrList, that stores pointers to characters, QStringList deals
  with real QString objects.  It is the class of choice whenever you
  work with unicode strings.

  Like QString itself, QStringList objects are implicit shared.
  Passing them around as value-parameters is both fast and safe.

  Example:
  \code
	QStringList list;

	// three different ways of appending values:
	list.append( "Torben");
	list += "Warwick";
	list << "Matthias" << "Arnt" << "Paul";

	// sort the list, Arnt's now first
	list.sort();

	// print it out
	for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) {
	    printf( "%s \n", (*it).latin1() );
	}
  \endcode

  Convenience methods such as sort(), split(), join() and grep() make
  working with QStringList easy.
*/

/*!
 \fn QStringList::QStringList()
  Creates an empty list.
*/

/*! \fn QStringList::QStringList( const QStringList& l )
  Creates a copy of the list. This function is very fast since
  QStringList is implicit shared. However, for the programmer this
  is the same as a deep copy. If this list or the original one or some
  other list referencing the same shared data is modified, then the
  modifying list makes a copy first.
*/

/*!
  \fn QStringList::QStringList (const QString & i)
  Constructs a string list consisting of the single string \a i.
  To make longer lists easily, use:
  \code
    QString s1,s2,s3;
    ...
    QStringList mylist = QStringList() << s1 << s2 << s3;
  \endcode
*/

/*!
  \fn QStringList::QStringList (const char* i)
  Constructs a string list consisting of the single latin-1 string \a i.
*/

/*! \fn QStringList::QStringList( const QValueList<QString>& l )

  Constructs a new string list that is a copy of \a l.
*/

/*!
  Sorts the list of strings in ascending order.

  Sorting is very fast. It uses the Qt Template Library's
  efficient HeapSort implementation that operates in O(n*log n).
*/
void QStringList::sort()
{
    qHeapSort(*this);
}

/*!
  Splits the string \a str using \a sep as separator. Returns the
  list of strings. If \a allowEmptyEntries is TRUE, also empty
  entries are inserted into the list, else not. So if you have
  a string 'abc..d.e.', a list which contains 'abc', 'd', and 'e'
  would be returned if \a allowEmptyEntries is FALSE, but
  a list containing 'abc', '', 'd', 'e' and '' would be returned if
  \a allowEmptyEntries is TRUE.
  If \a str doesn't contain \a sep, a stringlist
  with one item, which is the same as \a str, is returned.

  \sa join()
*/

QStringList QStringList::split( const QChar &sep, const QString &str, bool allowEmptyEntries )
{
    return split( QString( sep ), str, allowEmptyEntries );
}

/*!
  Splits the string \a str using \a sep as separator. Returns the
  list of strings. If \a allowEmptyEntries is TRUE, also empty
  entries are inserted into the list, else not. So if you have
  a string 'abc..d.e.', a list which contains 'abc', 'd', and 'e'
  would be returned if \a allowEmptyEntries is FALSE, but
  a list containing 'abc', '', 'd', 'e' and '' would be returned if
  \a allowEmptyEntries is TRUE.
  If \a str doesn't contain \a sep, a stringlist
  with one item, which is the same as \a str, is returned.

  \sa join()
*/

QStringList QStringList::split( const QString &sep, const QString &str, bool allowEmptyEntries )
{
    QStringList lst;

    int j = 0;
    int i = str.find( sep, j );

    while ( i != -1 ) {
	if ( str.mid( j, i - j ).length() > 0 )
	    lst << str.mid( j, i - j );
	else if ( allowEmptyEntries )
	    lst << QString::null;
	j = i + sep.length();
	i = str.find( sep, j );
    }

    int l = str.length() - 1;
    if ( str.mid( j, l - j + 1 ).length() > 0 )
	lst << str.mid( j, l - j + 1 );
    else if ( allowEmptyEntries )
	lst << QString::null;

    return lst;
}

/*!
  Splits the string \a str using the regular expression \a sep as separator. Returns the
  list of strings. If \a allowEmptyEntries is TRUE, also empty
  entries are inserted into the list, else not. So if you have
  a string 'abc..d.e.', a list which contains 'abc', 'd', and 'e'
  would be returned if \a allowEmptyEntries is FALSE, but
  a list containing 'abc', '', 'd', 'e' and '' would be returned if
  \a allowEmptyEntries is TRUE.
  If \a str doesn't contain \a sep, a stringlist
  with one item, which is the same as \a str, is returned.

  \sa join()
*/

QStringList QStringList::split( const QRegExp &sep, const QString &str, bool allowEmptyEntries )
{
    QStringList lst;

    int j = 0;
    int len = 0;
    int i = sep.match( str, j, &len );

    while ( i != -1 ) {
	if ( str.mid( j, i - j ).length() > 0 )
	    lst << str.mid( j, i - j );
	else if ( allowEmptyEntries )
	    lst << QString::null;
	j = i + len;
	i = sep.match( str, j, &len );
    }

    int l = str.length() - 1;
    if ( str.mid( j, l - j + 1 ).length() > 0 )
	lst << str.mid( j, l - j + 1 );
    else if ( allowEmptyEntries )
	lst << QString::null;

    return lst;
}

/*!
  Returns a list of all strings containing the substring \a str.

  If \a cs is TRUE, the grep is done case sensitively, else not.
*/

QStringList QStringList::grep( const QString &str, bool cs ) const
{
    QStringList res;
    for ( QStringList::ConstIterator it = begin(); it != end(); ++it )
	if ( (*it).contains( str, cs ) )
	    res << *it;

    return res;
}

/*!
  Returns a list of all strings containing a substring that matches
  the regular expression \a expr.
*/

QStringList QStringList::grep( const QRegExp &expr ) const
{
    QStringList res;
    for ( QStringList::ConstIterator it = begin(); it != end(); ++it )
	if ( (*it).contains( expr ) )
	    res << *it;

    return res;
}

/*!
  Joins the stringlist into a single string with each element
  separated by \a sep.

  \sa split()
*/
QString QStringList::join( const QString &sep ) const
{
    QString res;
    bool alredy = FALSE;
    for ( QStringList::ConstIterator it = begin(); it != end(); ++it ) {
	if ( alredy )
	    res += sep;
	alredy = TRUE;
	res += *it;
    }

    return res;
}

#ifndef QT_NO_DATASTREAM
Q_EXPORT QDataStream &operator>>( QDataStream & s, QStringList& l )
{
    return s >> (QValueList<QString>&)l;
}

Q_EXPORT QDataStream &operator<<( QDataStream & s, const QStringList& l )
{
    return s << (const QValueList<QString>&)l;
}
#endif

/*!
  Converts from a QStrList (ASCII) to a QStringList (Unicode).
*/
QStringList QStringList::fromStrList(const QStrList& ascii)
{
    QStringList res;
    const char * s;
    for ( QStrListIterator it(ascii); (s=it.current()); ++it )
	res << s;
    return res;
}

#endif //QT_NO_STRINGLIST