Blame qtools/qstringlist.cpp

Packit Service 50c9f2
/****************************************************************************
Packit Service 50c9f2
** 
Packit Service 50c9f2
**
Packit Service 50c9f2
** Implementation of QStringList
Packit Service 50c9f2
**
Packit Service 50c9f2
** Created : 990406
Packit Service 50c9f2
**
Packit Service 50c9f2
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file is part of the tools module of the Qt GUI Toolkit.
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file may be distributed under the terms of the Q Public License
Packit Service 50c9f2
** as defined by Trolltech AS of Norway and appearing in the file
Packit Service 50c9f2
** LICENSE.QPL included in the packaging of this file.
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file may be distributed and/or modified under the terms of the
Packit Service 50c9f2
** GNU General Public License version 2 as published by the Free Software
Packit Service 50c9f2
** Foundation and appearing in the file LICENSE.GPL included in the
Packit Service 50c9f2
** packaging of this file.
Packit Service 50c9f2
**
Packit Service 50c9f2
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
Packit Service 50c9f2
** licenses may use this file in accordance with the Qt Commercial License
Packit Service 50c9f2
** Agreement provided with the Software.
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
Packit Service 50c9f2
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Packit Service 50c9f2
**
Packit Service 50c9f2
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
Packit Service 50c9f2
**   information about Qt Commercial License Agreements.
Packit Service 50c9f2
** See http://www.trolltech.com/qpl/ for QPL licensing information.
Packit Service 50c9f2
** See http://www.trolltech.com/gpl/ for GPL licensing information.
Packit Service 50c9f2
**
Packit Service 50c9f2
** Contact info@trolltech.com if any conditions of this licensing are
Packit Service 50c9f2
** not clear to you.
Packit Service 50c9f2
**
Packit Service 50c9f2
**********************************************************************/
Packit Service 50c9f2
Packit Service 50c9f2
#include "qstringlist.h"
Packit Service 50c9f2
Packit Service 50c9f2
#ifndef QT_NO_STRINGLIST
Packit Service 50c9f2
#include "qstrlist.h"
Packit Service 50c9f2
#include "qdatastream.h"
Packit Service 50c9f2
#include "qtl.h"
Packit Service 50c9f2
Packit Service 50c9f2
// NOT REVISED
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  \class QStringList qstringlist.h
Packit Service 50c9f2
  \brief A list of strings.
Packit Service 50c9f2
Packit Service 50c9f2
  \ingroup qtl
Packit Service 50c9f2
  \ingroup tools
Packit Service 50c9f2
  \ingroup shared
Packit Service 50c9f2
Packit Service 50c9f2
  QStringList is basically a QValueList of QString objects. As opposed
Packit Service 50c9f2
  to QStrList, that stores pointers to characters, QStringList deals
Packit Service 50c9f2
  with real QString objects.  It is the class of choice whenever you
Packit Service 50c9f2
  work with unicode strings.
Packit Service 50c9f2
Packit Service 50c9f2
  Like QString itself, QStringList objects are implicit shared.
Packit Service 50c9f2
  Passing them around as value-parameters is both fast and safe.
Packit Service 50c9f2
Packit Service 50c9f2
  Example:
Packit Service 50c9f2
  \code
Packit Service 50c9f2
	QStringList list;
Packit Service 50c9f2
Packit Service 50c9f2
	// three different ways of appending values:
Packit Service 50c9f2
	list.append( "Torben");
Packit Service 50c9f2
	list += "Warwick";
Packit Service 50c9f2
	list << "Matthias" << "Arnt" << "Paul";
Packit Service 50c9f2
Packit Service 50c9f2
	// sort the list, Arnt's now first
Packit Service 50c9f2
	list.sort();
Packit Service 50c9f2
Packit Service 50c9f2
	// print it out
Packit Service 50c9f2
	for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) {
Packit Service 50c9f2
	    printf( "%s \n", (*it).latin1() );
Packit Service 50c9f2
	}
Packit Service 50c9f2
  \endcode
Packit Service 50c9f2
Packit Service 50c9f2
  Convenience methods such as sort(), split(), join() and grep() make
Packit Service 50c9f2
  working with QStringList easy.
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
 \fn QStringList::QStringList()
Packit Service 50c9f2
  Creates an empty list.
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
/*! \fn QStringList::QStringList( const QStringList& l )
Packit Service 50c9f2
  Creates a copy of the list. This function is very fast since
Packit Service 50c9f2
  QStringList is implicit shared. However, for the programmer this
Packit Service 50c9f2
  is the same as a deep copy. If this list or the original one or some
Packit Service 50c9f2
  other list referencing the same shared data is modified, then the
Packit Service 50c9f2
  modifying list makes a copy first.
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  \fn QStringList::QStringList (const QString & i)
Packit Service 50c9f2
  Constructs a string list consisting of the single string \a i.
Packit Service 50c9f2
  To make longer lists easily, use:
Packit Service 50c9f2
  \code
Packit Service 50c9f2
    QString s1,s2,s3;
Packit Service 50c9f2
    ...
Packit Service 50c9f2
    QStringList mylist = QStringList() << s1 << s2 << s3;
Packit Service 50c9f2
  \endcode
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  \fn QStringList::QStringList (const char* i)
Packit Service 50c9f2
  Constructs a string list consisting of the single latin-1 string \a i.
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
/*! \fn QStringList::QStringList( const QValueList<QString>& l )
Packit Service 50c9f2
Packit Service 50c9f2
  Constructs a new string list that is a copy of \a l.
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  Sorts the list of strings in ascending order.
Packit Service 50c9f2
Packit Service 50c9f2
  Sorting is very fast. It uses the Qt Template Library's
Packit Service 50c9f2
  efficient HeapSort implementation that operates in O(n*log n).
Packit Service 50c9f2
*/
Packit Service 50c9f2
void QStringList::sort()
Packit Service 50c9f2
{
Packit Service 50c9f2
    qHeapSort(*this);
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  Splits the string \a str using \a sep as separator. Returns the
Packit Service 50c9f2
  list of strings. If \a allowEmptyEntries is TRUE, also empty
Packit Service 50c9f2
  entries are inserted into the list, else not. So if you have
Packit Service 50c9f2
  a string 'abc..d.e.', a list which contains 'abc', 'd', and 'e'
Packit Service 50c9f2
  would be returned if \a allowEmptyEntries is FALSE, but
Packit Service 50c9f2
  a list containing 'abc', '', 'd', 'e' and '' would be returned if
Packit Service 50c9f2
  \a allowEmptyEntries is TRUE.
Packit Service 50c9f2
  If \a str doesn't contain \a sep, a stringlist
Packit Service 50c9f2
  with one item, which is the same as \a str, is returned.
Packit Service 50c9f2
Packit Service 50c9f2
  \sa join()
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
QStringList QStringList::split( const QChar &sep, const QString &str, bool allowEmptyEntries )
Packit Service 50c9f2
{
Packit Service 50c9f2
    return split( QString( sep ), str, allowEmptyEntries );
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  Splits the string \a str using \a sep as separator. Returns the
Packit Service 50c9f2
  list of strings. If \a allowEmptyEntries is TRUE, also empty
Packit Service 50c9f2
  entries are inserted into the list, else not. So if you have
Packit Service 50c9f2
  a string 'abc..d.e.', a list which contains 'abc', 'd', and 'e'
Packit Service 50c9f2
  would be returned if \a allowEmptyEntries is FALSE, but
Packit Service 50c9f2
  a list containing 'abc', '', 'd', 'e' and '' would be returned if
Packit Service 50c9f2
  \a allowEmptyEntries is TRUE.
Packit Service 50c9f2
  If \a str doesn't contain \a sep, a stringlist
Packit Service 50c9f2
  with one item, which is the same as \a str, is returned.
Packit Service 50c9f2
Packit Service 50c9f2
  \sa join()
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
QStringList QStringList::split( const QString &sep, const QString &str, bool allowEmptyEntries )
Packit Service 50c9f2
{
Packit Service 50c9f2
    QStringList lst;
Packit Service 50c9f2
Packit Service 50c9f2
    int j = 0;
Packit Service 50c9f2
    int i = str.find( sep, j );
Packit Service 50c9f2
Packit Service 50c9f2
    while ( i != -1 ) {
Packit Service 50c9f2
	if ( str.mid( j, i - j ).length() > 0 )
Packit Service 50c9f2
	    lst << str.mid( j, i - j );
Packit Service 50c9f2
	else if ( allowEmptyEntries )
Packit Service 50c9f2
	    lst << QString::null;
Packit Service 50c9f2
	j = i + sep.length();
Packit Service 50c9f2
	i = str.find( sep, j );
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    int l = str.length() - 1;
Packit Service 50c9f2
    if ( str.mid( j, l - j + 1 ).length() > 0 )
Packit Service 50c9f2
	lst << str.mid( j, l - j + 1 );
Packit Service 50c9f2
    else if ( allowEmptyEntries )
Packit Service 50c9f2
	lst << QString::null;
Packit Service 50c9f2
Packit Service 50c9f2
    return lst;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
QStringList QStringList::split( const QCString &sep, const QCString &str, bool allowEmptyEntries )
Packit Service 50c9f2
{
Packit Service 50c9f2
  return split(QString(sep.data()),QString(str.data()),allowEmptyEntries);
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  Splits the string \a str using the regular expression \a sep as separator. Returns the
Packit Service 50c9f2
  list of strings. If \a allowEmptyEntries is TRUE, also empty
Packit Service 50c9f2
  entries are inserted into the list, else not. So if you have
Packit Service 50c9f2
  a string 'abc..d.e.', a list which contains 'abc', 'd', and 'e'
Packit Service 50c9f2
  would be returned if \a allowEmptyEntries is FALSE, but
Packit Service 50c9f2
  a list containing 'abc', '', 'd', 'e' and '' would be returned if
Packit Service 50c9f2
  \a allowEmptyEntries is TRUE.
Packit Service 50c9f2
  If \a str doesn't contain \a sep, a stringlist
Packit Service 50c9f2
  with one item, which is the same as \a str, is returned.
Packit Service 50c9f2
Packit Service 50c9f2
  \sa join()
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
QStringList QStringList::split( const QRegExp &sep, const QString &str, bool allowEmptyEntries )
Packit Service 50c9f2
{
Packit Service 50c9f2
    QStringList lst;
Packit Service 50c9f2
Packit Service 50c9f2
    int j = 0;
Packit Service 50c9f2
    int len = 0;
Packit Service 50c9f2
    int i = sep.match( str.data(), j, &len );
Packit Service 50c9f2
Packit Service 50c9f2
    while ( i != -1 ) {
Packit Service 50c9f2
	if ( str.mid( j, i - j ).length() > 0 )
Packit Service 50c9f2
	    lst << str.mid( j, i - j );
Packit Service 50c9f2
	else if ( allowEmptyEntries )
Packit Service 50c9f2
	    lst << QString::null;
Packit Service 50c9f2
	j = i + len;
Packit Service 50c9f2
	i = sep.match( str.data(), j, &len );
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    int l = str.length() - 1;
Packit Service 50c9f2
    if ( str.mid( j, l - j + 1 ).length() > 0 )
Packit Service 50c9f2
	lst << str.mid( j, l - j + 1 );
Packit Service 50c9f2
    else if ( allowEmptyEntries )
Packit Service 50c9f2
	lst << QString::null;
Packit Service 50c9f2
Packit Service 50c9f2
    return lst;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  Returns a list of all strings containing the substring \a str.
Packit Service 50c9f2
Packit Service 50c9f2
  If \a cs is TRUE, the grep is done case sensitively, else not.
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
QStringList QStringList::grep( const QString &str, bool cs ) const
Packit Service 50c9f2
{
Packit Service 50c9f2
    QStringList res;
Packit Service 50c9f2
    for ( QStringList::ConstIterator it = begin(); it != end(); ++it )
Packit Service 50c9f2
	if ( (*it).contains( str, cs ) )
Packit Service 50c9f2
	    res << *it;
Packit Service 50c9f2
Packit Service 50c9f2
    return res;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  Returns a list of all strings containing a substring that matches
Packit Service 50c9f2
  the regular expression \a expr.
Packit Service 50c9f2
*/
Packit Service 50c9f2
Packit Service 50c9f2
QStringList QStringList::grep( const QRegExp &expr ) const
Packit Service 50c9f2
{
Packit Service 50c9f2
    QStringList res;
Packit Service 50c9f2
    for ( QStringList::ConstIterator it = begin(); it != end(); ++it )
Packit Service 50c9f2
	if ( (*it).contains( expr ) )
Packit Service 50c9f2
	    res << *it;
Packit Service 50c9f2
Packit Service 50c9f2
    return res;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  Joins the stringlist into a single string with each element
Packit Service 50c9f2
  separated by \a sep.
Packit Service 50c9f2
Packit Service 50c9f2
  \sa split()
Packit Service 50c9f2
*/
Packit Service 50c9f2
QString QStringList::join( const QString &sep ) const
Packit Service 50c9f2
{
Packit Service 50c9f2
    QString res;
Packit Service 50c9f2
    bool alredy = FALSE;
Packit Service 50c9f2
    for ( QStringList::ConstIterator it = begin(); it != end(); ++it ) {
Packit Service 50c9f2
	if ( alredy )
Packit Service 50c9f2
	    res += sep;
Packit Service 50c9f2
	alredy = TRUE;
Packit Service 50c9f2
	res += *it;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    return res;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
#ifndef QT_NO_DATASTREAM
Packit Service 50c9f2
Q_EXPORT QDataStream &operator>>( QDataStream & s, QStringList& l )
Packit Service 50c9f2
{
Packit Service 50c9f2
    return s >> (QValueList<QString>&);;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
Q_EXPORT QDataStream &operator<<( QDataStream & s, const QStringList& l )
Packit Service 50c9f2
{
Packit Service 50c9f2
    return s << (const QValueList<QString>&);;
Packit Service 50c9f2
}
Packit Service 50c9f2
#endif
Packit Service 50c9f2
Packit Service 50c9f2
/*!
Packit Service 50c9f2
  Converts from a QStrList (ASCII) to a QStringList (Unicode).
Packit Service 50c9f2
*/
Packit Service 50c9f2
QStringList QStringList::fromStrList(const QStrList& ascii)
Packit Service 50c9f2
{
Packit Service 50c9f2
    QStringList res;
Packit Service 50c9f2
    const char * s;
Packit Service 50c9f2
    for ( QStrListIterator it(ascii); (s=it.current()); ++it )
Packit Service 50c9f2
	res << s;
Packit Service 50c9f2
    return res;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
#endif //QT_NO_STRINGLIST