1 /****************************************************************************
3 ** Copyright (C) 1997-2018 by Dimitri van Heesch.
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.
11 ** Implementation of QCStringList
13 **********************************************************************/
15 #include "qcstringlist.h"
18 #include "qdatastream.h"
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.
35 QCStringList QCStringList::split( char sep, const QCString &str, bool allowEmptyEntries )
37 char cs[2] = { sep, '\0' };
38 return split( cs, str, allowEmptyEntries );
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.
55 QCStringList QCStringList::split( const QCString &sep, const QCString &str, bool allowEmptyEntries )
60 int i = str.find( sep, j );
63 if ( str.mid( j, i - j ).length() > 0 )
64 lst << str.mid( j, i - j );
65 else if ( allowEmptyEntries )
68 i = str.find( sep, j );
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 )
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.
94 QCStringList QCStringList::split( const QRegExp &sep, const QCString &str, bool allowEmptyEntries )
100 int i = sep.match( str.data(), j, &len );
103 if ( str.mid( j, i - j ).length() > 0 )
104 lst << str.mid( j, i - j );
105 else if ( allowEmptyEntries )
108 i = sep.match( str.data(), j, &len );
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 )
121 Returns a list of all strings containing the substring \a str.
123 If \a cs is TRUE, the grep is done case sensitively, else not.
126 QCStringList QCStringList::grep( const QCString &str, bool cs ) const
129 for ( QCStringList::ConstIterator it = begin(); it != end(); ++it )
130 if ( (*it).contains( str, cs ) )
137 Returns a list of all strings containing a substring that matches
138 the regular expression \a expr.
141 QCStringList QCStringList::grep( const QRegExp &expr ) const
144 for ( QCStringList::ConstIterator it = begin(); it != end(); ++it )
145 if ( (*it).contains( expr ) )
152 Joins the stringlist into a single string with each element
157 QCString QCStringList::join( const QCString &sep ) const
161 for ( QCStringList::ConstIterator it = begin(); it != end(); ++it ) {
171 Q_EXPORT QDataStream &operator>>( QDataStream & s, QCStringList& l )
173 return s >> (QValueList<QCString>&)l;
176 Q_EXPORT QDataStream &operator<<( QDataStream & s, const QCStringList& l )
178 return s << (const QValueList<QCString>&)l;
182 Converts from a QStrList (ASCII) to a QCStringList (Unicode).
184 QCStringList QCStringList::fromStrList(const QStrList& ascii)
188 for ( QStrListIterator it(ascii); (s=it.current()); ++it )