File: wrapregex.cpp

package info (click to toggle)
xmlcopyeditor 1.3.1.0-1.1
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 24,544 kB
  • sloc: xml: 140,240; cpp: 29,372; sh: 4,925; makefile: 301; sed: 16
file content (270 lines) | stat: -rw-r--r-- 6,714 bytes parent folder | download | duplicates (2)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * Copyright 2005-2007 Gerald Schmidt.
 *
 * This file is part of Xml Copy Editor.
 *
 * Xml Copy Editor is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Xml Copy Editor 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 Xml Copy Editor; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <iostream>
#include <string>
#include <ctype.h>
#include <stdexcept>
#include <cstring>
#include "wrapregex.h"
#include "contexthandler.h"

using namespace std;

WrapRegex::WrapRegex (
    const string& pattern,
    bool matchCase,
    const string& replaceParameter ) :
		replace ( replaceParameter ),
		returnValue ( 0 )
{
	if ( pattern.empty() || pattern == ".*" )
	{
		disabled = true;
		patternCode = NULL;
		patternMatchData = NULL;
		patternMatchContext = NULL;
		return;
	}
	disabled = false;

	// compile
	uint32_t optionsFlag = ( matchCase ? 0 : PCRE2_CASELESS ) | PCRE2_UTF | PCRE2_NO_UTF_CHECK;
	int errorCode;
	PCRE2_SIZE errorOffset;

	if ( ( patternCode = pcre2_compile (
		(PCRE2_SPTR)pattern.c_str(), // pattern
		PCRE2_ZERO_TERMINATED, // pattern is zero-terminated
		optionsFlag, // options
		&errorCode, // error number
		&errorOffset, // error offset
		NULL ) ) == NULL ) // default compile context
	{
		char buf[256];
		pcre2_get_error_message ( errorCode, (PCRE2_UCHAR *)buf, sizeof(buf) );
		throw runtime_error ( string(buf) );
	}
	patternMatchData = pcre2_match_data_create_from_pattern ( patternCode, NULL );
	patternMatchContext = pcre2_match_context_create ( NULL );
}

WrapRegex::~WrapRegex()
{
	if ( disabled )
		return;

	pcre2_match_data_free ( patternMatchData );
	pcre2_code_free ( patternCode );
	pcre2_match_context_free ( patternMatchContext );
}

int WrapRegex::matchPatternGlobal (
    string &buffer,
    vector<ContextMatch> &matchVector,
    unsigned elementCount,
    int context )
{
	if ( disabled )
		return 0;

	return matchPatternGlobal_ (
	           buffer.c_str(),
	           buffer.size(),
	           matchVector,
	           elementCount,
	           context );
}

string WrapRegex::replaceGlobal (
    const string& buffer,
    int *matchCount )
{
	*matchCount = 0;

	if ( disabled )
		return buffer;

	const char *s = buffer.c_str();

	string output, match;

	output.reserve ( buffer.size() );
	while ( ( returnValue = pcre2_match (
		patternCode, // compiled pattern
		(PCRE2_SPTR)s, // subject string
		strlen ( s ), // length of the subject
		0, // start at offset 0 in the subject
		0, // default options
		patternMatchData, // block where results will be stored
		patternMatchContext ) ) >= 0 ) // match context
	{
		++ ( *matchCount );

		PCRE2_SIZE *matchArray = pcre2_get_ovector_pointer ( patternMatchData );
		output.append ( s, matchArray[0] );

		match.clear();
		match.append ( s + matchArray[0], matchArray[1] - matchArray[0] );
		output.append ( getInterpolatedString_ ( s, ( char * ) replace.c_str() ) );
		s += matchArray[1];
	}
	output.append ( s );
	return output;
}

int WrapRegex::matchPatternGlobal_ (
    const char *buffer,
    size_t buflen,
    vector<ContextMatch> &matchVector,
    unsigned elementCount,
    int context )
{
	if ( disabled )
		return 0;

	const char *s, *origin;
	int matchcount;
	size_t offset;
	ContextMatch match;

	s = origin = buffer;
	matchcount = 0;
	offset = 0;

	while ( ( returnValue = pcre2_match (
		patternCode, // compiled pattern
		(PCRE2_SPTR)s, // subject string
		buflen, // length of the subject
		offset, // start at this offset in the subject
		0, // default options
		patternMatchData, // block where results will be stored
		patternMatchContext ) ) >= 0 ) // match context
	{
		++matchcount;

		PCRE2_SIZE *matchArray = pcre2_get_ovector_pointer ( patternMatchData );
		if ( context )
		{
			match = ContextHandler::getContext (
			            s + matchArray[0],
			            matchArray[1] - matchArray[0],
			            origin,
			            context );
		}
		else
		{
			match.prelog = match.postlog = "";
			match.match.assign ( s + matchArray[0], matchArray[1] - matchArray[0] );
		}

		// record element and offset information
		match.elementCount = elementCount;
		match.offset = matchArray[0];

		if ( replace != "" )
			match.replace = getInterpolatedString_ ( s, ( char * ) replace.c_str() );

		matchVector.push_back ( match );

		if ( ( offset = matchArray[1] ) >= buflen )
			break;
	}
	return matchcount;
}

string WrapRegex::getInterpolatedString_ ( const char *buffer, const char *source )
{
	if ( disabled )
		return "";

	const char *s = source;

	string interpol_string;

	int escapeState = false;
	for ( ; *s; ++s )
	{
		if ( *s == '\\' )
		{
			escapeState = ( escapeState ) ? false : true;
			if ( escapeState )
			{
				if ( isdigit ( * ( s + 1 ) ) )
				{
					const char *number, *it;
					number = s + 1;
					for ( it = number; *it && isdigit ( * ( it + 1 ) ); ++it )
						;
					size_t len = it - s;
					char *tmp = new char[len + 1];
					memcpy ( tmp, number, sizeof ( char ) * len );
					* ( tmp + len ) = '\0';
					int i = atoi ( tmp );
					delete[] tmp;

					interpol_string += getSubpattern_ ( buffer, i );

					s += len;
					escapeState = false;
				}
				else if ( * ( s + 1 ) == 't' )
				{
					interpol_string += '\t';
					++s;
					escapeState = false;
				}
				else if ( * ( s + 1 ) == 'n' )
				{
					interpol_string += '\n';
					++s;
					escapeState = false;
				}
				else
					interpol_string += *s;
			}
			else
				interpol_string += *s;
		}
		else
			interpol_string += *s;
	}
	return interpol_string;
}

string WrapRegex::getSubpattern_ ( const char *s, unsigned subpattern )
{
	if ( disabled )
		return "";

	char *sub = NULL;
	size_t sublen;
	int ret = pcre2_substring_get_bynumber (
		patternMatchData,
		subpattern,
		(PCRE2_UCHAR **)sub,
		&sublen
	);
	if ( ret == PCRE2_ERROR_NOMATCH || ret == PCRE2_ERROR_BADDATA )
		return "";
	string subString ( sub );
	pcre2_substring_free ( (PCRE2_UCHAR *)sub );
	return subString;
}