You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
184 lines
5.5 KiB
184 lines
5.5 KiB
15 years ago
|
/* -*- c++ -*-
|
||
|
kregexp3.cpp
|
||
|
|
||
|
This file is part of libkdenetwork.
|
||
|
Copyright (c) 2001 Marc Mutz <mutz@kde.org>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU General Public License,
|
||
|
version 2, as published by the Free Software Foundation.
|
||
|
|
||
|
This library is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License
|
||
|
along with this library; if not, write to the Free Software
|
||
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||
|
|
||
|
In addition, as a special exception, the copyright holders give
|
||
|
permission to link the code of this library with any edition of
|
||
14 years ago
|
the TQt library by Trolltech AS, Norway (or with modified versions
|
||
|
of TQt that use the same license as TQt), and distribute linked
|
||
15 years ago
|
combinations including the two. You must obey the GNU General
|
||
|
Public License in all respects for all of the code used other than
|
||
14 years ago
|
TQt. If you modify this file, you may extend this exception to
|
||
15 years ago
|
your version of the file, but you are not obligated to do so. If
|
||
|
you do not wish to do so, delete this exception statement from
|
||
|
your version.
|
||
|
*/
|
||
|
|
||
|
#include "kregexp3.h"
|
||
|
|
||
|
// #define DEBUG_KREGEXP3
|
||
|
|
||
|
#ifdef DEBUG_KREGEXP3
|
||
|
#include <kdebug.h>
|
||
|
#endif
|
||
|
|
||
14 years ago
|
TQString KRegExp3::replace( const TQString & str,
|
||
14 years ago
|
const TQString & replacementStr,
|
||
15 years ago
|
int start, bool global )
|
||
|
{
|
||
|
int oldpos, pos;
|
||
|
|
||
14 years ago
|
//-------- parsing the replacementStr into
|
||
15 years ago
|
//-------- literal parts and backreferences:
|
||
15 years ago
|
TQStringList literalStrs;
|
||
|
TQValueList<int> backRefs;
|
||
15 years ago
|
|
||
|
// Due to LTS: The regexp in unquoted form and with spaces:
|
||
|
// \\ (\d) | \$ (\d) | \$ \{ (\d+) \}
|
||
15 years ago
|
TQRegExp rx( "\\\\(\\d)|\\$(\\d)|\\$\\{(\\d+)\\}" );
|
||
|
TQRegExp bbrx("\\\\");
|
||
|
TQRegExp brx("\\");
|
||
15 years ago
|
|
||
|
#ifdef DEBUG_KREGEXP3
|
||
14 years ago
|
kdDebug() << "Analyzing replacementStr: \"" + replacementStr + "\"" << endl;
|
||
15 years ago
|
#endif
|
||
|
|
||
|
oldpos = 0;
|
||
|
pos = 0;
|
||
|
while ( true ) {
|
||
14 years ago
|
pos = rx.search( replacementStr, pos );
|
||
15 years ago
|
|
||
|
#ifdef DEBUG_KREGEXP3
|
||
14 years ago
|
kdDebug() << TQString(" Found match at pos %1").tqarg(pos) << endl;
|
||
15 years ago
|
#endif
|
||
|
|
||
|
if ( pos < 0 ) {
|
||
14 years ago
|
literalStrs << replacementStr.mid( oldpos )
|
||
14 years ago
|
.replace( bbrx, "\\" )
|
||
|
.replace( brx, "" );
|
||
15 years ago
|
#ifdef DEBUG_KREGEXP3
|
||
|
kdDebug() << " No more matches. Last literal is \"" + literalStrs.last() + "\"" << endl;
|
||
|
#endif
|
||
|
break;
|
||
|
} else {
|
||
14 years ago
|
literalStrs << replacementStr.mid( oldpos, pos-oldpos )
|
||
14 years ago
|
.replace( bbrx, "\\" )
|
||
|
.replace( brx, "" );
|
||
15 years ago
|
#ifdef DEBUG_KREGEXP3
|
||
15 years ago
|
kdDebug() << TQString(" Inserting \"") + literalStrs.last() + "\" as literal." << endl;
|
||
15 years ago
|
kdDebug() << " Searching for corresponding digit(s):" << endl;
|
||
|
#endif
|
||
|
for ( int i = 1 ; i < 4 ; i++ )
|
||
|
if ( !rx.cap(i).isEmpty() ) {
|
||
|
backRefs << rx.cap(i).toInt();
|
||
|
#ifdef DEBUG_KREGEXP3
|
||
15 years ago
|
kdDebug() << TQString(" Found %1 at position %2 in the capturedTexts.")
|
||
14 years ago
|
.tqarg(backRefs.last()).tqarg(i) << endl;
|
||
15 years ago
|
#endif
|
||
|
break;
|
||
|
}
|
||
|
pos += rx.matchedLength();
|
||
|
#ifdef DEBUG_KREGEXP3
|
||
14 years ago
|
kdDebug() << TQString(" Setting new pos to %1.").tqarg(pos) << endl;
|
||
15 years ago
|
#endif
|
||
|
oldpos = pos;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#ifdef DEBUG_KREGEXP3
|
||
14 years ago
|
kdDebug() << "Finished the analysis of replacementStr!" << endl;
|
||
15 years ago
|
#endif
|
||
|
Q_ASSERT( literalStrs.count() == backRefs.count() + 1 );
|
||
|
|
||
|
//-------- actual construction of the
|
||
14 years ago
|
//-------- resulting TQString
|
||
15 years ago
|
TQString result = "";
|
||
15 years ago
|
oldpos = 0;
|
||
|
pos = start;
|
||
|
|
||
15 years ago
|
TQStringList::Iterator sIt;
|
||
|
TQValueList<int>::Iterator iIt;
|
||
15 years ago
|
|
||
|
if ( start < 0 )
|
||
|
start += str.length();
|
||
|
|
||
|
#ifdef DEBUG_KREGEXP3
|
||
|
kdDebug() << "Constructing the resultant string starts now:" << endl;
|
||
|
#endif
|
||
|
|
||
|
while ( pos < (int)str.length() ) {
|
||
|
pos = search( str, pos );
|
||
|
|
||
|
#ifdef DEBUG_KREGEXP3
|
||
14 years ago
|
kdDebug() << TQString(" Found match at pos %1").tqarg(pos) << endl;
|
||
15 years ago
|
#endif
|
||
|
|
||
|
if ( pos < 0 ) {
|
||
|
result += str.mid( oldpos );
|
||
|
#ifdef DEBUG_KREGEXP3
|
||
|
kdDebug() << " No more matches. Adding trailing part from str:" << endl;
|
||
|
kdDebug() << " result == \"" + result + "\"" << endl;
|
||
|
#endif
|
||
|
break;
|
||
|
} else {
|
||
|
result += str.mid( oldpos, pos-oldpos );
|
||
|
#ifdef DEBUG_KREGEXP3
|
||
|
kdDebug() << " Adding unchanged part from str:" << endl;
|
||
|
kdDebug() << " result == \"" + result + "\"" << endl;
|
||
|
#endif
|
||
|
for ( sIt = literalStrs.begin(), iIt = backRefs.begin() ;
|
||
|
iIt != backRefs.end() ; ++sIt, ++iIt ) {
|
||
|
result += (*sIt);
|
||
|
#ifdef DEBUG_KREGEXP3
|
||
14 years ago
|
kdDebug() << " Adding literal replacement part:" << endl;
|
||
15 years ago
|
kdDebug() << " result == \"" + result + "\"" << endl;
|
||
|
#endif
|
||
|
result += cap( (*iIt) );
|
||
|
#ifdef DEBUG_KREGEXP3
|
||
|
kdDebug() << " Adding captured string:" << endl;
|
||
|
kdDebug() << " result == \"" + result + "\"" << endl;
|
||
|
#endif
|
||
|
}
|
||
|
result += (*sIt);
|
||
|
#ifdef DEBUG_KREGEXP3
|
||
14 years ago
|
kdDebug() << " Adding literal replacement part:" << endl;
|
||
15 years ago
|
kdDebug() << " result == \"" + result + "\"" << endl;
|
||
|
#endif
|
||
|
}
|
||
|
if (matchedLength() == 0 && pos == 0) {
|
||
|
// if we matched the begin of the string, then better avoid endless
|
||
|
// recursion
|
||
|
result += str.mid( oldpos );
|
||
|
break;
|
||
|
}
|
||
|
pos += matchedLength();
|
||
|
#ifdef DEBUG_KREGEXP3
|
||
14 years ago
|
kdDebug() << TQString(" Setting new pos to %1.").tqarg(pos) << endl;
|
||
15 years ago
|
#endif
|
||
|
oldpos = pos;
|
||
|
|
||
|
if ( !global ) {
|
||
14 years ago
|
// only replace the first occurrence, so stop here:
|
||
15 years ago
|
result += str.mid( oldpos );
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|