Imported Upstream version 1.8.15
[platform/upstream/doxygen.git] / qtools / qcstringlist.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 1997-2018 by Dimitri van Heesch.
4 **
5 ** Permission to use, copy, modify, and distribute this software and its
6 ** documentation under the terms of the GNU General Public License is hereby
7 ** granted. No representations are made about the suitability of this software
8 ** for any purpose. It is provided "as is" without express or implied warranty.
9 ** See the GNU General Public License for more details.
10 **
11 ** Implementation of QCStringList
12 **
13 **********************************************************************/
14
15 #include "qcstringlist.h"
16
17 #include "qstrlist.h"
18 #include "qdatastream.h"
19 #include "qtl.h"
20
21 /*!
22   Splits the string \a str using \a sep as separator. Returns the
23   list of strings. If \a allowEmptyEntries is TRUE, also empty
24   entries are inserted into the list, else not. So if you have
25   a string 'abc..d.e.', a list which contains 'abc', 'd', and 'e'
26   would be returned if \a allowEmptyEntries is FALSE, but
27   a list containing 'abc', '', 'd', 'e' and '' would be returned if
28   \a allowEmptyEntries is TRUE.
29   If \a str doesn't contain \a sep, a stringlist
30   with one item, which is the same as \a str, is returned.
31
32   \sa join()
33 */
34
35 QCStringList QCStringList::split( char sep, const QCString &str, bool allowEmptyEntries )
36 {
37     char cs[2] = { sep, '\0' };
38     return split( cs, str, allowEmptyEntries );
39 }
40
41 /*!
42   Splits the string \a str using \a sep as separator. Returns the
43   list of strings. If \a allowEmptyEntries is TRUE, also empty
44   entries are inserted into the list, else not. So if you have
45   a string 'abc..d.e.', a list which contains 'abc', 'd', and 'e'
46   would be returned if \a allowEmptyEntries is FALSE, but
47   a list containing 'abc', '', 'd', 'e' and '' would be returned if
48   \a allowEmptyEntries is TRUE.
49   If \a str doesn't contain \a sep, a stringlist
50   with one item, which is the same as \a str, is returned.
51
52   \sa join()
53 */
54
55 QCStringList QCStringList::split( const QCString &sep, const QCString &str, bool allowEmptyEntries )
56 {
57     QCStringList lst;
58
59     int j = 0;
60     int i = str.find( sep, j );
61
62     while ( i != -1 ) {
63         if ( str.mid( j, i - j ).length() > 0 )
64             lst << str.mid( j, i - j );
65         else if ( allowEmptyEntries )
66             lst << QCString();
67         j = i + sep.length();
68         i = str.find( sep, j );
69     }
70
71     int l = str.length() - 1;
72     if ( str.mid( j, l - j + 1 ).length() > 0 )
73         lst << str.mid( j, l - j + 1 );
74     else if ( allowEmptyEntries )
75         lst << QCString();
76
77     return lst;
78 }
79
80 /*!
81   Splits the string \a str using the regular expression \a sep as separator. Returns the
82   list of strings. If \a allowEmptyEntries is TRUE, also empty
83   entries are inserted into the list, else not. So if you have
84   a string 'abc..d.e.', a list which contains 'abc', 'd', and 'e'
85   would be returned if \a allowEmptyEntries is FALSE, but
86   a list containing 'abc', '', 'd', 'e' and '' would be returned if
87   \a allowEmptyEntries is TRUE.
88   If \a str doesn't contain \a sep, a stringlist
89   with one item, which is the same as \a str, is returned.
90
91   \sa join()
92 */
93
94 QCStringList QCStringList::split( const QRegExp &sep, const QCString &str, bool allowEmptyEntries )
95 {
96     QCStringList lst;
97
98     int j = 0;
99     int len = 0;
100     int i = sep.match( str.data(), j, &len );
101
102     while ( i != -1 ) {
103         if ( str.mid( j, i - j ).length() > 0 )
104             lst << str.mid( j, i - j );
105         else if ( allowEmptyEntries )
106             lst << QCString();
107         j = i + len;
108         i = sep.match( str.data(), j, &len );
109     }
110
111     int l = str.length() - 1;
112     if ( str.mid( j, l - j + 1 ).length() > 0 )
113         lst << str.mid( j, l - j + 1 );
114     else if ( allowEmptyEntries )
115         lst << QCString();
116
117     return lst;
118 }
119
120 /*!
121   Returns a list of all strings containing the substring \a str.
122
123   If \a cs is TRUE, the grep is done case sensitively, else not.
124 */
125
126 QCStringList QCStringList::grep( const QCString &str, bool cs ) const
127 {
128     QCStringList res;
129     for ( QCStringList::ConstIterator it = begin(); it != end(); ++it )
130         if ( (*it).contains( str, cs ) )
131             res << *it;
132
133     return res;
134 }
135
136 /*!
137   Returns a list of all strings containing a substring that matches
138   the regular expression \a expr.
139 */
140
141 QCStringList QCStringList::grep( const QRegExp &expr ) const
142 {
143     QCStringList res;
144     for ( QCStringList::ConstIterator it = begin(); it != end(); ++it )
145         if ( (*it).contains( expr ) )
146             res << *it;
147
148     return res;
149 }
150
151 /*!
152   Joins the stringlist into a single string with each element
153   separated by \a sep.
154
155   \sa split()
156 */
157 QCString QCStringList::join( const QCString &sep ) const
158 {
159     QCString res;
160     bool alredy = FALSE;
161     for ( QCStringList::ConstIterator it = begin(); it != end(); ++it ) {
162         if ( alredy )
163             res += sep;
164         alredy = TRUE;
165         res += *it;
166     }
167
168     return res;
169 }
170
171 Q_EXPORT QDataStream &operator>>( QDataStream & s, QCStringList& l )
172 {
173     return s >> (QValueList<QCString>&)l;
174 }
175
176 Q_EXPORT QDataStream &operator<<( QDataStream & s, const QCStringList& l )
177 {
178     return s << (const QValueList<QCString>&)l;
179 }
180
181 /*!
182   Converts from a QStrList (ASCII) to a QCStringList (Unicode).
183 */
184 QCStringList QCStringList::fromStrList(const QStrList& ascii)
185 {
186     QCStringList res;
187     const char * s;
188     for ( QStrListIterator it(ascii); (s=it.current()); ++it )
189         res << s;
190     return res;
191 }
192