summaryrefslogtreecommitdiffstats
path: root/qtools/qcstringlist.cpp
blob: 4bb231dd46e7fb71411b5d6fd0b11eb57b4f9b6e (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
/****************************************************************************
**
** Copyright (C) 1997-2018 by Dimitri van Heesch.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation under the terms of the GNU General Public License is hereby
** granted. No representations are made about the suitability of this software
** for any purpose. It is provided "as is" without express or implied warranty.
** See the GNU General Public License for more details.
**
** Implementation of QCStringList
**
**********************************************************************/

#include "qcstringlist.h"

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

/*!
  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()
*/

QCStringList QCStringList::split( char sep, const QCString &str, bool allowEmptyEntries )
{
    char cs[2] = { sep, '\0' };
    return split( cs, 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()
*/

QCStringList QCStringList::split( const QCString &sep, const QCString &str, bool allowEmptyEntries )
{
    QCStringList 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 << QCString();
	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 << QCString();

    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()
*/

QCStringList QCStringList::split( const QRegExp &sep, const QCString &str, bool allowEmptyEntries )
{
    QCStringList lst;

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

    while ( i != -1 ) {
	if ( str.mid( j, i - j ).length() > 0 )
	    lst << str.mid( j, i - j );
	else if ( allowEmptyEntries )
	    lst << QCString();
	j = i + len;
	i = sep.match( str.data(), 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 << QCString();

    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.
*/

QCStringList QCStringList::grep( const QCString &str, bool cs ) const
{
    QCStringList res;
    for ( QCStringList::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.
*/

QCStringList QCStringList::grep( const QRegExp &expr ) const
{
    QCStringList res;
    for ( QCStringList::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()
*/
QCString QCStringList::join( const QCString &sep ) const
{
    QCString res;
    bool already = FALSE;
    for ( QCStringList::ConstIterator it = begin(); it != end(); ++it ) {
	if ( already )
	    res += sep;
	already = TRUE;
	res += *it;
    }

    return res;
}

Q_EXPORT QDataStream &operator>>( QDataStream & s, QCStringList& l )
{
    return s >> (QValueList<QCString>&)l;
}

Q_EXPORT QDataStream &operator<<( QDataStream & s, const QCStringList& l )
{
    return s << (const QValueList<QCString>&)l;
}

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