File: replace.cc

package info (click to toggle)
libpcre%2B%2B 0.9.5-6
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,032 kB
  • ctags: 194
  • sloc: sh: 6,870; cpp: 835; makefile: 41
file content (202 lines) | stat: -rw-r--r-- 5,975 bytes parent folder | download | duplicates (5)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 *
 *  This file  is part of the PCRE++ Class Library.
 *
 *  By  accessing  this software,  PCRE++, you  are  duly informed
 *  of and agree to be  bound  by the  conditions  described below
 *  in this notice:
 *
 *  This software product,  PCRE++,  is developed by Thomas Linden
 *  and copyrighted (C) 2002-2003 by Thomas Linden,with all rights 
 *  reserved.
 *
 *  There  is no charge for PCRE++ software.  You can redistribute
 *  it and/or modify it under the terms of the GNU  Lesser General
 *  Public License, which is incorporated by reference herein.
 *
 *  PCRE++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS,
 *  OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that
 *  the use of it will not infringe on any third party's intellec-
 *  tual property rights.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with PCRE++.  Copies can also be obtained from:
 *
 *    http://www.gnu.org/licenses/lgpl.txt
 *
 *  or by writing to:
 *
 *  Free Software Foundation, Inc.
 *  59 Temple Place, Suite 330
 *  Boston, MA 02111-1307
 *  USA
 *
 *  Or contact:
 *
 *   "Thomas Linden" <tom@daemon.de>
 *
 *
 */


#include "pcre++.h"

using namespace std;
using namespace pcrepp;

/*
 * replace method
 */

string Pcre::replace(const string& piece, const string& with) {
  /*
   * Pcre::replace version by "Marcus Kramer" <marcus.kramer@scherrer.de>
   */
  string Replaced(piece);

  bool bReplaced = false;
  int  iReplaced = -1;

  __pcredebug << "replace: " << piece << " with: " << with << endl;

  /*
   * certainly we need an anchor, we want to check if the whole arg is in brackets
   * //Pcre braces("^[^\\\\]\\(.*[^\\\\]\\)$"); // perlish: [^\\]\(.*[^\\]\)
   *
   * There's no reason, not to add brackets in general.
   * It's more comfortable, cause we wants to start with $1 at all, 
   * also if we set the whole arg in brackets!
   */
  
  /* recreate the p_pcre* objects to avoid memory leaks */
  pcre_free(p_pcre);
  pcre_free(p_pcre_extra);
  
  pcre       *_p = NULL;
  pcre_extra *_e = NULL;;
        
  p_pcre = _p;
  p_pcre_extra = _e;
  
  if (! _have_paren ) {
    string::size_type p_open, p_close;
    p_open  = _expression.find_first_of("(");
    p_close = _expression.find_first_of(")");
    if ( p_open == string::npos && p_close == string::npos ) {
      /*
       * Well, _expression completely lacks of parens, which are
       * required for search/replace operation. So add parens automatically.
       * Note, that we add 2 pairs of parens so that we finally have $0 matchin
       * the whole expression and $1 matching the inner side (which is in this
       * case the very same string.
       * We do this for perl compatibility. If the expression already contains
       * parens, the whole match will produce $0 for us, so in this case we
       * have no problem
       */
      _expression = "((" + _expression + "))";
    }
    else {
      /*
       * Add parens to the very beginning and end of the expression
       * so that we have $0. I don't care if the user already supplied
       * double-parentesed experssion (e.g. "((1))"), because PCRE seems
       * to eat redundant parenteses, e.g. "((((1))))" returns the same
       * result as "((1))".
       */
      _expression = "(" + _expression;
      _expression=_expression + ")"; 
    }

    _have_paren = true;
  }

  __pcredebug << "_expression: " << _expression << endl;

  Compile(_flags);
        
  if(search(piece)) {
    /* we found at least one match */
    
    // sure we must resolve $1 for ever piece we found especially for "g"
    // so let's just create that var, we resolve it when we needed!
    string use_with;


    if(!global_t) {
      // here we can resolve vars if option g is not set
      use_with = _replace_vars(with);

      if(matched() && matches() >= 1) {
	__pcredebug << "matches: " << matches() << endl;
	int len = get_match_end() - get_match_start() + 1;
	Replaced.replace(get_match_start(0), len, use_with);
	bReplaced  = true;
	iReplaced = 0;
      }
    }
    else {
      /* global replace */

      // in global replace we just need to remember our position
      // so let's initialize it first
      int match_pos = 0;
      while( search( Replaced, match_pos ) ) {
	int len = 0;
                                
	// here we need to resolve the vars certainly for every hit.
	// could be different content sometimes!
	use_with = _replace_vars(with);
                                
	len = get_match_end() - get_match_start() + 1;
	Replaced.replace(get_match_start(0), len, use_with);
                                
	//# Next run should begin after the last char of the stuff we put in the text
	match_pos = ( use_with.length() - len ) + get_match_end() + 1;

	bReplaced  = true;
	++iReplaced;
      }
    }
  }
  
  did_match   = bReplaced;
  num_matches = iReplaced;

  return Replaced;
}





string Pcre::_replace_vars(const string& piece) {
  /*
   * Pcre::_replace_vars version by "Marcus Kramer" <marcus.kramer@scherrer.de>
   */
  string with  = piece;
  Pcre dollar("\\$([0-9]+)");

  while ( dollar.search( with ) ) {
    // let's do some conversion first
    __pcredebug << "Pcre::dollar matched: " << piece << ". Match(0): " << dollar.get_match(0) << endl;
    int iBracketIndex = atoi( dollar.get_match(0).data() );
    string sBracketContent = get_match(iBracketIndex);
    
    // now we can splitt the stuff
    string sSubSplit = "\\$" + dollar.get_match(0);
    Pcre subsplit(sSubSplit);
                
    // normally 2 (or more) parts, the one in front of and the other one after "$1"
    vector<string> splitted = subsplit.split(with); 
    string Replaced;
                
    for(size_t pos=0; pos < splitted.size(); pos++) {
      if( pos == ( splitted.size() - 1 ) ) 
	Replaced += splitted[pos];
      else 
	Replaced += splitted[pos] + sBracketContent;
    }
    with = Replaced; // well, one part is done
  }
  return with;
}