libtdepim

kregexp3.cpp
1 /*
2  kregexp3.cpp
3 
4  This file is part of libtdenetwork.
5  Copyright (c) 2001 Marc Mutz <mutz@kde.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License,
9  version 2, as published by the Free Software Foundation.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 
20  In addition, as a special exception, the copyright holders give
21  permission to link the code of this library with any edition of
22  the TQt library by Trolltech AS, Norway (or with modified versions
23  of TQt that use the same license as TQt), and distribute linked
24  combinations including the two. You must obey the GNU General
25  Public License in all respects for all of the code used other than
26  TQt. If you modify this file, you may extend this exception to
27  your version of the file, but you are not obligated to do so. If
28  you do not wish to do so, delete this exception statement from
29  your version.
30 */
31 
32 #include "kregexp3.h"
33 
34 // #define DEBUG_KREGEXP3
35 
36 #ifdef DEBUG_KREGEXP3
37 #include <kdebug.h>
38 #endif
39 
40 TQString KRegExp3::replace( const TQString & str,
41  const TQString & replacementStr,
42  int start, bool global )
43 {
44  int oldpos, pos;
45 
46  //-------- parsing the replacementStr into
47  //-------- literal parts and backreferences:
48  TQStringList literalStrs;
49  TQValueList<int> backRefs;
50 
51  // Due to LTS: The regexp in unquoted form and with spaces:
52  // \\ (\d) | \$ (\d) | \$ \{ (\d+) \}
53  TQRegExp rx( "\\\\(\\d)|\\$(\\d)|\\$\\{(\\d+)\\}" );
54  TQRegExp bbrx("\\\\");
55  TQRegExp brx("\\");
56 
57 #ifdef DEBUG_KREGEXP3
58  kdDebug() << "Analyzing replacementStr: \"" + replacementStr + "\"" << endl;
59 #endif
60 
61  oldpos = 0;
62  pos = 0;
63  while ( true ) {
64  pos = rx.search( replacementStr, pos );
65 
66 #ifdef DEBUG_KREGEXP3
67  kdDebug() << TQString(" Found match at pos %1").arg(pos) << endl;
68 #endif
69 
70  if ( pos < 0 ) {
71  literalStrs << replacementStr.mid( oldpos )
72  .replace( bbrx, "\\" )
73  .replace( brx, "" );
74 #ifdef DEBUG_KREGEXP3
75  kdDebug() << " No more matches. Last literal is \"" + literalStrs.last() + "\"" << endl;
76 #endif
77  break;
78  } else {
79  literalStrs << replacementStr.mid( oldpos, pos-oldpos )
80  .replace( bbrx, "\\" )
81  .replace( brx, "" );
82 #ifdef DEBUG_KREGEXP3
83  kdDebug() << TQString(" Inserting \"") + literalStrs.last() + "\" as literal." << endl;
84  kdDebug() << " Searching for corresponding digit(s):" << endl;
85 #endif
86  for ( int i = 1 ; i < 4 ; i++ )
87  if ( !rx.cap(i).isEmpty() ) {
88  backRefs << rx.cap(i).toInt();
89 #ifdef DEBUG_KREGEXP3
90  kdDebug() << TQString(" Found %1 at position %2 in the capturedTexts.")
91  .arg(backRefs.last()).arg(i) << endl;
92 #endif
93  break;
94  }
95  pos += rx.matchedLength();
96 #ifdef DEBUG_KREGEXP3
97  kdDebug() << TQString(" Setting new pos to %1.").arg(pos) << endl;
98 #endif
99  oldpos = pos;
100  }
101  }
102 
103 #ifdef DEBUG_KREGEXP3
104  kdDebug() << "Finished the analysis of replacementStr!" << endl;
105 #endif
106  Q_ASSERT( literalStrs.count() == backRefs.count() + 1 );
107 
108  //-------- actual construction of the
109  //-------- resulting TQString
110  TQString result = "";
111  oldpos = 0;
112  pos = start;
113 
114  TQStringList::Iterator sIt;
115  TQValueList<int>::Iterator iIt;
116 
117  if ( start < 0 )
118  start += str.length();
119 
120 #ifdef DEBUG_KREGEXP3
121  kdDebug() << "Constructing the resultant string starts now:" << endl;
122 #endif
123 
124  while ( pos < (int)str.length() ) {
125  pos = search( str, pos );
126 
127 #ifdef DEBUG_KREGEXP3
128  kdDebug() << TQString(" Found match at pos %1").arg(pos) << endl;
129 #endif
130 
131  if ( pos < 0 ) {
132  result += str.mid( oldpos );
133 #ifdef DEBUG_KREGEXP3
134  kdDebug() << " No more matches. Adding trailing part from str:" << endl;
135  kdDebug() << " result == \"" + result + "\"" << endl;
136 #endif
137  break;
138  } else {
139  result += str.mid( oldpos, pos-oldpos );
140 #ifdef DEBUG_KREGEXP3
141  kdDebug() << " Adding unchanged part from str:" << endl;
142  kdDebug() << " result == \"" + result + "\"" << endl;
143 #endif
144  for ( sIt = literalStrs.begin(), iIt = backRefs.begin() ;
145  iIt != backRefs.end() ; ++sIt, ++iIt ) {
146  result += (*sIt);
147 #ifdef DEBUG_KREGEXP3
148  kdDebug() << " Adding literal replacement part:" << endl;
149  kdDebug() << " result == \"" + result + "\"" << endl;
150 #endif
151  result += cap( (*iIt) );
152 #ifdef DEBUG_KREGEXP3
153  kdDebug() << " Adding captured string:" << endl;
154  kdDebug() << " result == \"" + result + "\"" << endl;
155 #endif
156  }
157  result += (*sIt);
158 #ifdef DEBUG_KREGEXP3
159  kdDebug() << " Adding literal replacement part:" << endl;
160  kdDebug() << " result == \"" + result + "\"" << endl;
161 #endif
162  }
163  if (matchedLength() == 0 && pos == 0) {
164  // if we matched the begin of the string, then better avoid endless
165  // recursion
166  result += str.mid( oldpos );
167  break;
168  }
169  pos += matchedLength();
170 #ifdef DEBUG_KREGEXP3
171  kdDebug() << TQString(" Setting new pos to %1.").arg(pos) << endl;
172 #endif
173  oldpos = pos;
174 
175  if ( !global ) {
176  // only replace the first occurrence, so stop here:
177  result += str.mid( oldpos );
178  break;
179  }
180  }
181 
182  return result;
183 }
TQString replace(const TQString &str, const TQString &replacementStr, int start=0, bool global=TRUE)
Replaces each matching subpattern in str with replacementStr, inserting captured substrings for \n,...
Definition: kregexp3.cpp:40