Blame qtools/qstringlist.cpp

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