forked from bruderstein/PythonScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplacer.h
More file actions
372 lines (290 loc) · 12.8 KB
/
Replacer.h
File metadata and controls
372 lines (290 loc) · 12.8 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#ifndef REPLACER_20140209_H
#define REPLACER_20140209_H
#include "ReplaceEntry.h"
#include "Match.h"
#include "UTF8Iterator.h"
#include "ANSIIterator.h"
#include "ConstString.h"
#include "UtfConversion.h"
namespace NppPythonScript
{
typedef ReplaceEntry* (*matchConverter)(const char *, Match *, void *state);
typedef bool (*searchResultHandler)(const char *, Match *, void *state);
using UtfConversion::toStringType;
typedef enum _python_re_flags {
python_re_flag_normal = 0,
python_re_flag_ignorecase = 2,
python_re_flag_locale = 4,
python_re_flag_multiline = 8,
python_re_flag_dotall = 16,
python_re_flag_wholedoc = 0x10000000, // This flag is the opposite of python_re_flag_multiline, which is the default
// Multiline is always true, except when this flag is specified
// Internal flags
python_re_flag_literal = 0x80000000
} python_re_flags;
template <class CharTraitsT>
class BoostRegexGroupDetail : public GroupDetail
{
public:
BoostRegexGroupDetail(const boost::sub_match<typename CharTraitsT::text_iterator_type>& subMatch)
: m_subMatch(subMatch)
{}
int start() const { return m_subMatch.first.pos(); }
int end() const { return m_subMatch.second.pos(); }
bool matched() const { return m_subMatch.matched; }
private:
boost::sub_match<typename CharTraitsT::text_iterator_type> m_subMatch;
};
template<class CharTraitsT>
class BoostRegexMatch : public Match
{
public:
BoostRegexMatch(const char *text, boost::match_results<typename CharTraitsT::text_iterator_type>* match)
: m_text(text),
m_match(match)
{}
BoostRegexMatch(const char *text)
: m_text(text),
m_match(NULL)
{}
virtual ~BoostRegexMatch();
BoostRegexMatch& operator= (BoostRegexMatch& rhs) {
m_text = rhs.m_text;
m_match = rhs.m_match;
/* We explicitely don't copy the list, as the allocatedGroupDetails will simply be destructed when this object gets destroyed.
* In theory, this would be bad, as we would delete the allocated GroupDetail objects when this object is deleted,
* even though the various groups may still be in use.
* In practice however, these GroupDetails don't actually live as long as this object, as we've created a ReplaceEntry
* by the time this object gets destroyed, and have no need for the allocated GroupDetails any more.
*/
}
void setMatchResults(boost::match_results<typename CharTraitsT::text_iterator_type>* match) { m_match = match; }
virtual size_t groupCount() { return m_match->size(); }
virtual GroupDetail* group(int groupNo);
virtual GroupDetail* groupName(const char *groupName);
virtual std::string getTextForGroup(GroupDetail* group);
virtual void expand(const char* format, char **result, size_t *resultLength);
virtual int groupIndexFromName(const char *groupName);
private:
const char *m_text;
boost::match_results<typename CharTraitsT::text_iterator_type>* m_match;
std::list<BoostRegexGroupDetail<CharTraitsT>* > m_allocatedGroupDetails;
static void deleteEntry(BoostRegexGroupDetail<CharTraitsT>*);
};
template <class CharTraitsT>
void BoostRegexMatch<CharTraitsT>::deleteEntry(BoostRegexGroupDetail<CharTraitsT>* entry)
{
delete entry;
}
template <class CharTraitsT>
BoostRegexMatch<CharTraitsT>::~BoostRegexMatch()
{
for_each(m_allocatedGroupDetails.begin(), m_allocatedGroupDetails.end(), deleteEntry);
}
template <class CharTraitsT>
GroupDetail* BoostRegexMatch<CharTraitsT>::group(int groupNo)
{
if (groupNo < 0 || groupNo >= static_cast<int>(m_match->size()))
{
return NULL;
}
BoostRegexGroupDetail<CharTraitsT>* groupDetail = new BoostRegexGroupDetail<CharTraitsT>((*m_match)[groupNo]);
m_allocatedGroupDetails.push_back(groupDetail);
return groupDetail;
}
template <class CharTraitsT>
GroupDetail* BoostRegexMatch<CharTraitsT>::groupName(const char *groupName)
{
CharTraitsT::string_type groupNameU32 = toStringType<CharTraitsT::string_type>(ConstString<char>(groupName));
int groupIndex = m_match->named_subexpression_index(groupNameU32.c_str(), groupNameU32.c_str() + groupNameU32.size());
return group(groupIndex);
}
template <class CharTraitsT>
int BoostRegexMatch<CharTraitsT>::groupIndexFromName(const char *groupName)
{
CharTraitsT::string_type groupNameU32 = toStringType<CharTraitsT::string_type>(ConstString<char>(groupName));
return m_match->named_subexpression_index(groupNameU32.c_str(), groupNameU32.c_str() + groupNameU32.size());
}
template <class CharTraitsT>
void BoostRegexMatch<CharTraitsT>::expand(const char *format, char **result, size_t *resultLength)
{
CharTraitsT::string_type formatString = CharTraitsT::fromChars(format);
CharTraitsT::string_type resultString = m_match->format(formatString, boost::regex_constants::format_all);
std::string charResult(CharTraitsT::toCharString(resultString));
*resultLength = charResult.size();
*result = new char[(*resultLength) + 1];
memcpy(*result, charResult.c_str(), *resultLength);
(*result)[*resultLength] = '\0';
}
template <class CharTraitsT>
typename std::string BoostRegexMatch<CharTraitsT>::getTextForGroup(GroupDetail* groupDetail)
{
return std::string(m_text + groupDetail->start(), m_text + groupDetail->end());
}
class Utf8CharTraits {
public:
typedef std::basic_string<U32> string_type;
typedef boost::basic_regex<U32, u32_regex_traits> regex_type;
typedef boost::regex_iterator<UTF8Iterator, U32, u32_regex_traits> regex_iterator_type;
typedef UTF8Iterator text_iterator_type;
static std::basic_string<char> toCharString(const string_type& source) {
// TODO: There's probably more copying, allocing and deleting going on here than there actually needs to be
// We just want a u32string to utf8 char*
return std::basic_string<char>(UtfConversion::toUtf8(ConstString<U32>(source)));
}
static string_type fromChars(const char *source) {
return string_type(UtfConversion::toUtf32(ConstString<U8>(source)));
}
};
class AnsiCharTraits {
public:
typedef std::string string_type;
typedef boost::regex regex_type;
typedef boost::regex_iterator<ANSIIterator, char> regex_iterator_type;
typedef ANSIIterator text_iterator_type;
static std::basic_string<char> toCharString(const string_type& source) {
return source;
}
static string_type fromChars(const char *source) {
return string_type(source);
}
};
template <class CharTraitsT>
class Replacer {
public:
Replacer()
{ }
bool startReplace(const char *text, const int textLength, int maxCount, const int startPosition, const char *search, matchConverter converter, void *converterState, python_re_flags flags, std::list<ReplaceEntry*>& replacements);
bool startReplace(const char *text, const int textLength, int maxCount, const int startPosition, const char *search, const char *replace, python_re_flags flags, std::list<ReplaceEntry*>& replacements);
void search(const char *text, const int textLength, const int startPosition, int maxCount, const char *search, searchResultHandler resultHandler, void *resultHandlerState, python_re_flags flags);
private:
static ReplaceEntry* matchToReplaceEntry(const char *text, Match *match, void *state);
boost::regex_constants::match_flag_type getMatchFlags(python_re_flags flags);
boost::regex_constants::syntax_option_type getSyntaxFlags(python_re_flags flags);
const char *m_replaceFormat;
};
template<class CharTraitsT>
ReplaceEntry* Replacer<CharTraitsT>::matchToReplaceEntry(const char * /* text */, Match *match, void *state)
{
// TODO: state is replacer instance, and contains the replacement string
// need to add format call in here,
Replacer *replacer = reinterpret_cast<Replacer*>(state);
char *replacement;
size_t replacementLength;
match->expand(replacer->m_replaceFormat, &replacement, &replacementLength);
GroupDetail *fullMatch = match->group(0);
ReplaceEntry* replaceEntry = new ReplaceEntry(fullMatch->start(), fullMatch->end(), replacement, replacementLength);
delete [] replacement;
return replaceEntry;
}
template<class CharTraitsT>
bool Replacer<CharTraitsT>::startReplace(const char *text, const int textLength, const int startPosition,
int maxCount,
const char *search,
const char *replace,
python_re_flags flags,
std::list<ReplaceEntry*>& replacements)
{
m_replaceFormat = replace;
return startReplace(text, textLength, startPosition, maxCount, search, matchToReplaceEntry, this, flags, replacements);
}
template<class CharTraitsT>
boost::regex_constants::match_flag_type Replacer<CharTraitsT>::getMatchFlags(python_re_flags flags)
{
boost::regex_constants::match_flag_type resultBoostFlags = boost::regex_constants::match_default;
// If we've not got the dotall flag, we want to add the match_not_dot_newline. I love negative based flags. grrr.
if (0 == (flags & python_re_flag_dotall))
{
resultBoostFlags |= boost::regex_constants::match_not_dot_newline;
}
if (flags & python_re_flag_wholedoc)
{
resultBoostFlags |= boost::regex_constants::match_single_line;
}
return resultBoostFlags;
}
template<class CharTraitsT>
boost::regex_constants::syntax_option_type Replacer<CharTraitsT>::getSyntaxFlags(python_re_flags flags)
{
boost::regex_constants::syntax_option_type resultBoostFlags;
if (flags & python_re_flag_literal)
{
resultBoostFlags = boost::regex_constants::literal;
}
else
{
resultBoostFlags = boost::regex_constants::normal;
}
if (flags & python_re_flag_ignorecase)
{
resultBoostFlags |= boost::regex_constants::icase;
}
return resultBoostFlags;
}
template<class CharTraitsT>
bool Replacer<CharTraitsT>::startReplace(const char *text, const int textLength,
const int startPosition,
int maxCount,
const char *search,
matchConverter converter,
void *converterState,
python_re_flags flags,
std::list<ReplaceEntry*> &replacements)
{
boost::regex_constants::syntax_option_type syntax_flags = getSyntaxFlags(flags);
CharTraitsT::regex_type r = CharTraitsT::regex_type(toStringType<CharTraitsT::string_type>(ConstString<char>(search)), syntax_flags);
CharTraitsT::text_iterator_type start(text, startPosition, textLength);
CharTraitsT::text_iterator_type end(text, textLength, textLength);
CharTraitsT::regex_iterator_type iteratorEnd;
BoostRegexMatch<CharTraitsT> match(text);
bool checkCountOfReplaces = false;
if (maxCount > 0)
{
checkCountOfReplaces = true;
}
for(CharTraitsT::regex_iterator_type it(start, end, r, getMatchFlags(flags)); it != iteratorEnd; ++it)
{
boost::match_results<CharTraitsT::text_iterator_type> boost_match_results(*it);
match.setMatchResults(&boost_match_results);
ReplaceEntry* entry = converter(text, &match, converterState);
replacements.push_back(entry);
if (checkCountOfReplaces && 0 == --maxCount)
{
break;
}
}
return false;
}
template<class CharTraitsT>
void Replacer<CharTraitsT>::search(const char *text, const int textLength,
const int startPosition,
int maxCount,
const char *search,
searchResultHandler resultHandler,
void *resultHandlerState,
python_re_flags flags)
{
boost::regex_constants::syntax_option_type syntax_flags = getSyntaxFlags(flags);
CharTraitsT::regex_type r = CharTraitsT::regex_type(toStringType<CharTraitsT::string_type>(ConstString<char>(search)), syntax_flags);
CharTraitsT::text_iterator_type start(text, startPosition, textLength);
CharTraitsT::text_iterator_type end(text, textLength, textLength);
CharTraitsT::regex_iterator_type iteratorEnd;
BoostRegexMatch<CharTraitsT> match(text);
bool checkCountOfSearches = false;
if (maxCount > 0)
{
checkCountOfSearches = true;
}
for(CharTraitsT::regex_iterator_type it(start, end, r, getMatchFlags(flags)); it != iteratorEnd; ++it)
{
boost::match_results<CharTraitsT::text_iterator_type> boost_match_results(*it);
match.setMatchResults(&boost_match_results);
bool shouldContinue = resultHandler(text, &match, resultHandlerState);
if (!shouldContinue || (checkCountOfSearches && 0 == --maxCount))
{
break;
}
}
}
}
#endif // REPLACER_20140209_H