summaryrefslogtreecommitdiffstats
path: root/libtdepim/kregexp3.cpp
blob: bffc491ea21730892058dd77dd77704f9e6da624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*  -*- c++ -*-
    kregexp3.cpp

    This file is part of libtdenetwork.
    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
    the TQt library by Trolltech AS, Norway (or with modified versions
    of TQt that use the same license as TQt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    TQt.  If you modify this file, you may extend this exception to
    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

TQString KRegExp3::replace( const TQString & str,
			   const TQString & replacementStr,
			   int start, bool global )
{
  int oldpos, pos;

  //-------- parsing the replacementStr into
  //-------- literal parts and backreferences:
  TQStringList     literalStrs;
  TQValueList<int> backRefs;

  // Due to LTS: The regexp in unquoted form and with spaces:
  // \\ (\d) | \$ (\d) | \$ \{ (\d+) \}
  TQRegExp rx( "\\\\(\\d)|\\$(\\d)|\\$\\{(\\d+)\\}" );
  TQRegExp bbrx("\\\\");
  TQRegExp brx("\\");

#ifdef DEBUG_KREGEXP3
  kdDebug() << "Analyzing replacementStr: \"" + replacementStr + "\"" << endl;
#endif

  oldpos = 0;
  pos = 0;
  while ( true ) {
    pos = rx.search( replacementStr, pos );
    
#ifdef DEBUG_KREGEXP3
    kdDebug() << TQString("  Found match at pos %1").tqarg(pos) << endl;
#endif

    if ( pos < 0 ) {
      literalStrs << replacementStr.mid( oldpos )
	.replace( bbrx, "\\" )
	.replace( brx, "" );
#ifdef DEBUG_KREGEXP3
      kdDebug() << "  No more matches. Last literal is \"" + literalStrs.last() + "\"" << endl;
#endif
      break;
    } else {
      literalStrs << replacementStr.mid( oldpos, pos-oldpos )
	.replace( bbrx, "\\" )
	.replace( brx, "" );
#ifdef DEBUG_KREGEXP3
      kdDebug() << TQString("  Inserting \"") + literalStrs.last() + "\" as literal." << endl;
      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
	  kdDebug() << TQString("      Found %1 at position %2 in the capturedTexts.")
            .tqarg(backRefs.last()).tqarg(i) << endl;
#endif
	  break;
	}
      pos += rx.matchedLength();
#ifdef DEBUG_KREGEXP3
      kdDebug() << TQString("  Setting new pos to %1.").tqarg(pos) << endl;
#endif
      oldpos = pos;
    }
  }

#ifdef DEBUG_KREGEXP3
  kdDebug() << "Finished the analysis of replacementStr!" << endl;
#endif
  Q_ASSERT( literalStrs.count() == backRefs.count() + 1 );

  //-------- actual construction of the
  //-------- resulting TQString
  TQString result = "";
  oldpos = 0;
  pos = start;

  TQStringList::Iterator sIt;
  TQValueList<int>::Iterator iIt;

  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
    kdDebug() << TQString("  Found match at pos %1").tqarg(pos) << endl;
#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
	kdDebug() << "   Adding literal replacement part:" << endl;
	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
      kdDebug() << "   Adding literal replacement part:" << endl;
      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
    kdDebug() << TQString("  Setting new pos to %1.").tqarg(pos) << endl;
#endif
    oldpos = pos;

    if ( !global ) {
      // only replace the first occurrence, so stop here:
      result += str.mid( oldpos );
      break;
    }
  }

  return result;
}