libkdepim
kregexp3.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "kregexp3.h"
00033
00034
00035
00036 #ifdef DEBUG_KREGEXP3
00037 #include <kdebug.h>
00038 #endif
00039
00040 QString KRegExp3::replace( const QString & str,
00041 const QString & replacementStr,
00042 int start, bool global )
00043 {
00044 int oldpos, pos;
00045
00046
00047
00048 QStringList literalStrs;
00049 QValueList<int> backRefs;
00050
00051
00052
00053 QRegExp rx( "\\\\(\\d)|\\$(\\d)|\\$\\{(\\d+)\\}" );
00054 QRegExp bbrx("\\\\");
00055 QRegExp brx("\\");
00056
00057 #ifdef DEBUG_KREGEXP3
00058 kdDebug() << "Analyzing replacementStr: \"" + replacementStr + "\"" << endl;
00059 #endif
00060
00061 oldpos = 0;
00062 pos = 0;
00063 while ( true ) {
00064 pos = rx.search( replacementStr, pos );
00065
00066 #ifdef DEBUG_KREGEXP3
00067 kdDebug() << QString(" Found match at pos %1").arg(pos) << endl;
00068 #endif
00069
00070 if ( pos < 0 ) {
00071 literalStrs << replacementStr.mid( oldpos )
00072 .replace( bbrx, "\\" )
00073 .replace( brx, "" );
00074 #ifdef DEBUG_KREGEXP3
00075 kdDebug() << " No more matches. Last literal is \"" + literalStrs.last() + "\"" << endl;
00076 #endif
00077 break;
00078 } else {
00079 literalStrs << replacementStr.mid( oldpos, pos-oldpos )
00080 .replace( bbrx, "\\" )
00081 .replace( brx, "" );
00082 #ifdef DEBUG_KREGEXP3
00083 kdDebug() << QString(" Inserting \"") + literalStrs.last() + "\" as literal." << endl;
00084 kdDebug() << " Searching for corresponding digit(s):" << endl;
00085 #endif
00086 for ( int i = 1 ; i < 4 ; i++ )
00087 if ( !rx.cap(i).isEmpty() ) {
00088 backRefs << rx.cap(i).toInt();
00089 #ifdef DEBUG_KREGEXP3
00090 kdDebug() << QString(" Found %1 at position %2 in the capturedTexts.")
00091 .arg(backRefs.last()).arg(i) << endl;
00092 #endif
00093 break;
00094 }
00095 pos += rx.matchedLength();
00096 #ifdef DEBUG_KREGEXP3
00097 kdDebug() << QString(" Setting new pos to %1.").arg(pos) << endl;
00098 #endif
00099 oldpos = pos;
00100 }
00101 }
00102
00103 #ifdef DEBUG_KREGEXP3
00104 kdDebug() << "Finished the analysis of replacementStr!" << endl;
00105 #endif
00106 Q_ASSERT( literalStrs.count() == backRefs.count() + 1 );
00107
00108
00109
00110 QString result = "";
00111 oldpos = 0;
00112 pos = start;
00113
00114 QStringList::Iterator sIt;
00115 QValueList<int>::Iterator iIt;
00116
00117 if ( start < 0 )
00118 start += str.length();
00119
00120 #ifdef DEBUG_KREGEXP3
00121 kdDebug() << "Constructing the resultant string starts now:" << endl;
00122 #endif
00123
00124 while ( pos < (int)str.length() ) {
00125 pos = search( str, pos );
00126
00127 #ifdef DEBUG_KREGEXP3
00128 kdDebug() << QString(" Found match at pos %1").arg(pos) << endl;
00129 #endif
00130
00131 if ( pos < 0 ) {
00132 result += str.mid( oldpos );
00133 #ifdef DEBUG_KREGEXP3
00134 kdDebug() << " No more matches. Adding trailing part from str:" << endl;
00135 kdDebug() << " result == \"" + result + "\"" << endl;
00136 #endif
00137 break;
00138 } else {
00139 result += str.mid( oldpos, pos-oldpos );
00140 #ifdef DEBUG_KREGEXP3
00141 kdDebug() << " Adding unchanged part from str:" << endl;
00142 kdDebug() << " result == \"" + result + "\"" << endl;
00143 #endif
00144 for ( sIt = literalStrs.begin(), iIt = backRefs.begin() ;
00145 iIt != backRefs.end() ; ++sIt, ++iIt ) {
00146 result += (*sIt);
00147 #ifdef DEBUG_KREGEXP3
00148 kdDebug() << " Adding literal replacement part:" << endl;
00149 kdDebug() << " result == \"" + result + "\"" << endl;
00150 #endif
00151 result += cap( (*iIt) );
00152 #ifdef DEBUG_KREGEXP3
00153 kdDebug() << " Adding captured string:" << endl;
00154 kdDebug() << " result == \"" + result + "\"" << endl;
00155 #endif
00156 }
00157 result += (*sIt);
00158 #ifdef DEBUG_KREGEXP3
00159 kdDebug() << " Adding literal replacement part:" << endl;
00160 kdDebug() << " result == \"" + result + "\"" << endl;
00161 #endif
00162 }
00163 if (matchedLength() == 0 && pos == 0) {
00164
00165
00166 result += str.mid( oldpos );
00167 break;
00168 }
00169 pos += matchedLength();
00170 #ifdef DEBUG_KREGEXP3
00171 kdDebug() << QString(" Setting new pos to %1.").arg(pos) << endl;
00172 #endif
00173 oldpos = pos;
00174
00175 if ( !global ) {
00176
00177 result += str.mid( oldpos );
00178 break;
00179 }
00180 }
00181
00182 return result;
00183 }
|