include RegExp hpp RegExp RegExp const char pattern const char options

  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
#include "RegExp.hpp"
RegExp::RegExp(const char *pattern, const char *options, unsigned int max_matches) {
setPattern(pattern, options, max_matches);
}
RegExp *RegExp::setPattern(const char *pattern, const char *options, unsigned int _max_matches) {
if (re) free(re);
pcre_last_matches = pcre_last_offset = string_length = 0;
parseOptions(options);
re = pcre_compile (pattern, pcre_flags, &pcre_error, &pcre_error_offset, 0);
if (!re) {
string error = "PCRE error: ";
error.append(pcre_error).append(" at offset ").append(to_string(pcre_error_offset));
throw logic_error(error);
}
if (max_matches != _max_matches || !result_vector) {
max_matches = _max_matches * 3;
result_vector = result_vector ? (int *) realloc(result_vector, max_matches * sizeof(int)) : (int *) calloc(max_matches, sizeof(int));
}
return this;
}
RegExp *RegExp::parseOptions(const char *options) {
if (options) {
pcre_flags = 0;
const char *option = options;
while (*option) {
if (*option == 'i')
pcre_flags |= PCRE_CASELESS;
else if (*option == 'm')
pcre_flags |= PCRE_MULTILINE;
else if (*option == 's')
pcre_flags |= PCRE_DOTALL;
else if (*option == 'x')
pcre_flags |= PCRE_EXTENDED;
else if (*option == 'e') {
throw logic_error("Это вам не php, тут нет eval() ;)");
} else if (*option == 'A')
pcre_flags |= PCRE_ANCHORED;
else if (*option == 'D')
pcre_flags |= PCRE_DOLLAR_ENDONLY;
else if (*option == 'S') {
throw logic_error("Што это?");
} else if (*option == 'U')
pcre_flags |= PCRE_UNGREEDY;
else if (*option == 'X')
pcre_flags |= PCRE_EXTRA;
else if (*option == 'J')
pcre_flags |= PCRE_INFO_JCHANGED;
else if (*option == 'u')
pcre_flags |= PCRE_UTF8;
++option;
}
}
return this;
}
int RegExp::exec(const char *str, unsigned int len, int offset) {
pcre_last_string = str;
string_length = len;
int c = pcre_exec(re, 0, str, len, offset > -1 ? offset : pcre_last_offset, 0, result_vector, max_matches);
if (c < 0) {
if (c == PCRE_ERROR_NOMATCH)
return 0;
return c;
}
pcre_last_offset = result_vector[1];
pcre_last_matches = c;
return c;
}
int RegExp::exec(string &str, int offset) {
return exec(str.c_str(), str.length(), offset);
}
const string RegExp::getString(int n, const char *ptr) {
if ((!pcre_last_string && !ptr) || result_vector[n * 2] < 0 || result_vector[n * 2 + 1] < 0)
return "";
return string(ptr ? ptr : pcre_last_string, result_vector[n * 2], result_vector[n * 2 + 1] - result_vector[n * 2]);
}
const string RegExp::getString(int n, string &str) {
if (result_vector[n * 2] < 0 || result_vector[n * 2 + 1] < 0)
return "";
return string(str, result_vector[n * 2], result_vector[n * 2 + 1] - result_vector[n * 2]);
}
const RegExp::Match RegExp::getMatch(int n) {
Match m = {-1, -1};
if (!pcre_last_string || result_vector[n * 2] < 0 || result_vector[n * 2 + 1] < 0)
return m;
m.start = result_vector[n * 2];
m.end = result_vector[n * 2 + 1];
return m;
}
const string RegExp::operator[](int n) {
if (!pcre_last_string || result_vector[n * 2] < 0 || result_vector[n * 2 + 1] < 0)
return "";
return string(pcre_last_string, result_vector[n * 2], result_vector[n * 2 + 1] - result_vector[n * 2]);
}
// replace
int RegExp::replace(string &str, string &out, const char *replace, unsigned int replace_length, ReplaceCallback callback, unsigned int limit) {
char backrefs_exists = 0; // 1 - exists, 2 - not_exists, 0 - search
int end, start, replaced = 0;
int glocal_offset = 0;
while (exec(str, pcre_last_offset) > 0) {
start = result_vector[0] + glocal_offset;
end = result_vector[1] + glocal_offset;
if (callback) {
string replace_str = callback(this);
out.replace(out.begin() + start, out.begin() + end, replace_str);
glocal_offset += replace_str.length() - (end - start);
continue;
}
out.replace(out.begin() + start, out.begin() + end, replace, replace_length);
glocal_offset += replace_length - (end - start);
end += replace_length - (end - start);
// Обрабатывает бэкслэши
if (backrefs_exists != 2) {
int start_backref = -1, end_backref = -1;
bool is_brace = false;
char last_c = 0, backref;
if (backrefs_exists == 0)
backrefs_exists = 2;
for (int i = start; i < end; ++i) {
if (out[i] == '$') {
start_backref = i++;
// Если бэкреф инкапсулирован в фигурные скобки
is_brace = i < end && out[i] == '{';
if (is_brace) ++i;
// Парсим первую цифру номера бэкрефа
if (i < end && isdigit(out[i])) {
backref = out[i] - '0';
++i;
// Заэкранированный доллар, значит не нужно обрабатывать бэкрефы
if (last_c == '\\') {
out.erase(start_backref, 1);
glocal_offset -= 1;
end -= 1; i -= 1;
continue;
}
// Парсим вторую цифру номера бэкрефа
if (i < end && isdigit(out[i])) {
backref = (out[i] - '0') + (backref * 10);
++i;
}
if (!is_brace || (i < end && out[i] == '}')) {
// Ставим метку, что бэкрефы найдены
if (backrefs_exists == 2)
backrefs_exists = 1;
if (out[i] == '}') ++i;
end_backref = i;
if (backref < pcre_last_matches) {
// Замена бекрефа на найденный результат
int m_backref_start = result_vector[backref * 2];
int m_backref_end = result_vector[backref * 2 + 1];
int diff = (m_backref_end - m_backref_start) - (end_backref - start_backref);
out.replace(out.begin() + start_backref, out.begin() + end_backref, str.begin() + m_backref_start, str.begin() + m_backref_end);
glocal_offset += diff; end += diff; i += diff;
} else {
// Если бэкреф не найден в результате - выпиливаем его
out.erase(start_backref, end_backref - start_backref);
glocal_offset -= end_backref - start_backref;
end -= end_backref - start_backref;
i -= end_backref - start_backref;
}
}
}
}
if (i < end)
last_c = out[i];
}
}
++replaced;
if (limit && replaced == limit)
break;
}
return replaced;
}
RegExp *RegExp::freePattern() {
if (re) {
free(re);
re = NULL;
}
if (result_vector) {
free(result_vector);
result_vector = NULL;
}
return this;
}
RegExp::~RegExp() {
freePattern();
}