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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/regexpressions.h"
#include "gate/results.h"
/*
This is a GATE framework port of
kokke's public domain code 'tiny-regex-c'
which is based on Rob-Pike's regex-code.
*/
/* Private functions: */
static gate_bool_t match_digit(char c)
{
return ((c >= '0') && (c <= '9'));
}
static gate_bool_t match_alpha(char c)
{
return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
}
static gate_bool_t match_whitespace(char c)
{
return ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r') || (c == '\f') || (c == '\v'));
}
static gate_bool_t match_alphanum(char c)
{
return ((c == '_') || match_alpha(c) || match_digit(c));
}
static gate_bool_t match_range(char c, const char* str)
{
return ((c != '-') && (str[0] != '\0') && (str[0] != '-') &&
(str[1] == '-') && (str[1] != '\0') &&<--- Redundant condition: If 'EXPR == '-'', the comparison 'EXPR != '\0'' is always true.
(str[2] != '\0') && ((c >= str[0]) && (c <= str[2])));
}
static gate_bool_t is_meta_char(char c)
{
return ((c == 's') || (c == 'S') || (c == 'w') || (c == 'W') || (c == 'd') || (c == 'D'));
}
static gate_bool_t match_meta_char(char c, const char* str)
{
switch (str[0])
{
case 'd': return match_digit(c);
case 'D': return !match_digit(c);
case 'w': return match_alphanum(c);
case 'W': return !match_alphanum(c);
case 's': return match_whitespace(c);
case 'S': return !match_whitespace(c);
default: return (c == str[0]);
}
}
static gate_bool_t match_char_class(char c, const char* str)
{
do
{
if (match_range(c, str))
{
return true;
}
else if (str[0] == '\\')
{
/* Escape-char: increment str-ptr and match on next char */
str += 1;
if (match_meta_char(c, str))
{
return true;
}
else if ((c == str[0]) && !is_meta_char(c))
{
return true;
}
}
else if (c == str[0])
{
if (c == '-')
{
return ((str[-1] == '\0') || (str[1] == '\0'));
}
else
{
return true;
}
}
} while (*str++ != '\0');
return false;
}
static gate_bool_t match_one(gate_regex_token_t token, char c)
{
switch (token.type)
{
case GATE_REGEX_DOT: return true;
case GATE_REGEX_CHAR_CLASS: return match_char_class(c, (const char*)token.ptr);
case GATE_REGEX_INV_CHAR_CLASS: return !match_char_class(c, (const char*)token.ptr);
case GATE_REGEX_DIGIT: return match_digit(c);
case GATE_REGEX_NOT_DIGIT: return !match_digit(c);
case GATE_REGEX_ALPHA: return match_alphanum(c);
case GATE_REGEX_NOT_ALPHA: return !match_alphanum(c);
case GATE_REGEX_WHITESPACE: return match_whitespace(c);
case GATE_REGEX_NOT_WHITESPACE: return !match_whitespace(c);
default: return (token.chr == c);
}
}
static gate_bool_t match_pattern(gate_regex_token_t* pattern, const char* text, gate_size_t textlen, int* match_length);
static gate_bool_t match_star(gate_regex_token_t token, gate_regex_token_t* pattern, const char* text, gate_size_t textlen, int* match_length)
{
int prelen = *match_length;
int textpos = 0;
while ((textpos < (int)textlen) && match_one(token, text[textpos]))
{
++textpos;
++(*match_length);
}
while (textpos >= 0)
{
if (match_pattern(pattern, &text[textpos], textlen - textpos, match_length))
{
return true;
}
--textpos;
--(*match_length);
}
*match_length = prelen;
return false;
}
static gate_bool_t match_plus(gate_regex_token_t token, gate_regex_token_t* pattern, const char* text, gate_size_t textlen, int* match_length)
{
gate_size_t textpos = 0;
while ((textpos < textlen) && match_one(token, text[textpos]))
{
++textpos;
++(*match_length);
}
while (textpos > 0)
{
if (match_pattern(pattern, &text[textpos], textlen - textpos, match_length))
{
return true;
}
--textpos;
--(*match_length);
}
return false;
}
static gate_bool_t match_question(gate_regex_token_t token, gate_regex_token_t* pattern, const char* text, gate_size_t textlen, int* match_length)
{
if (token.type == GATE_REGEX_UNUSED)
{
return true;
}
if (match_pattern(pattern, text, textlen, match_length))
{
return true;
}
if ((textlen != 0) && match_one(token, text[0]))
{
if (match_pattern(pattern, &text[1], textlen - 1, match_length))
{
++(*match_length);
return true;
}
}
return false;
}
static gate_bool_t match_pattern(gate_regex_token_t* pattern, const char* text, gate_size_t textlen, int* match_length)
{
int pre = *match_length;
gate_size_t pattern_index = 0;
gate_size_t textpos = 0;
do
{
if ((pattern[pattern_index].type == GATE_REGEX_UNUSED) || (pattern[pattern_index].type == GATE_REGEX_QUESTIONMARK))
{
return match_question(pattern[pattern_index], &pattern[pattern_index + 2], &text[textpos], textlen - textpos, match_length);
}
else if (pattern[pattern_index + 1].type == GATE_REGEX_STAR)
{
return match_star(pattern[pattern_index + 0], &pattern[pattern_index + 2], &text[textpos], textlen - textpos, match_length);
}
else if (pattern[pattern_index + 1].type == GATE_REGEX_PLUS)
{
return match_plus(pattern[pattern_index + 0], &pattern[pattern_index + 2], &text[textpos], textlen - textpos, match_length);
}
else if ((pattern[pattern_index + 0].type == GATE_REGEX_END) && pattern[pattern_index + 1].type == GATE_REGEX_UNUSED)
{
return (textpos >= textlen);
}
++(*match_length);
} while ((textpos < textlen) && match_one(pattern[pattern_index++], text[textpos++]));
*match_length = pre;
return false;
}
gate_result_t gate_regex_init(gate_regex_t regex, gate_string_t const* pattern)
{
gate_result_t result = GATE_RESULT_OK;
char current_char;
gate_index_t buffer_index = 1; //int ccl_bufidx = 1;
gate_index_t pattern_index = 0; //int i = 0; /* index into pattern */
gate_index_t compiled_index = 0; // int j = 0; /* index into re_compiled */
gate_index_t buffer_begin;
char const* ptr_pattern;
gate_size_t pattern_length;
gate_regex_token_t* ptr_compiled;<--- The scope of the variable 'ptr_compiled' can be reduced. [+]The scope of the variable 'ptr_compiled' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
unsigned char* ptr_buffer;
do
{
if (regex == NULL)
{
result = GATE_RESULT_INVALIDARG;
break;
}
gate_mem_clear(regex, sizeof(struct gate_regex_class));
ptr_pattern = ®ex->pattern[0];
pattern_length = gate_string_to_buffer(pattern, regex->pattern, sizeof(regex->pattern));
ptr_compiled = ®ex->compiled[0];
ptr_buffer = ®ex->buffer[0];
while ((result == GATE_RESULT_OK)
&& (pattern_index < (gate_index_t)pattern_length)
&& (compiled_index + 1 < GATE_REGEX_MAX_COMPILED))
{
current_char = ptr_pattern[pattern_index];
switch (current_char)
{
/* Meta-characters: */
case '^': { ptr_compiled[compiled_index].type = GATE_REGEX_BEGIN; break; }
case '$': { ptr_compiled[compiled_index].type = GATE_REGEX_END; break; }
case '.': { ptr_compiled[compiled_index].type = GATE_REGEX_DOT; break; }
case '*': { ptr_compiled[compiled_index].type = GATE_REGEX_STAR; break; }
case '+': { ptr_compiled[compiled_index].type = GATE_REGEX_PLUS; break; }
case '?': { ptr_compiled[compiled_index].type = GATE_REGEX_QUESTIONMARK; break; }
/*case '|': { ptr_compiled[compiled_index].type = GATE_REGEX_BRANCH; break; } <-- not working properly */
/* Escaped character-classes (\s \w ...): */
case '\\':
{
if (pattern_index + 1 < (gate_index_t)pattern_length)
{
/* Skip the escape-char '\\' */
++pattern_index;
/* ... and check the next */
current_char = ptr_pattern[pattern_index];
switch (current_char)
{
/* Meta-character: */
case 'd': { ptr_compiled[compiled_index].type = GATE_REGEX_DIGIT; break; }
case 'D': { ptr_compiled[compiled_index].type = GATE_REGEX_NOT_DIGIT; break; }
case 'w': { ptr_compiled[compiled_index].type = GATE_REGEX_ALPHA; break; }
case 'W': { ptr_compiled[compiled_index].type = GATE_REGEX_NOT_ALPHA; break; }
case 's': { ptr_compiled[compiled_index].type = GATE_REGEX_WHITESPACE; break; }
case 'S': { ptr_compiled[compiled_index].type = GATE_REGEX_NOT_WHITESPACE; break; }
/* Escaped character, e.g. '.' or '$' */
default:
{
ptr_compiled[compiled_index].type = GATE_REGEX_CHAR;
ptr_compiled[compiled_index].chr = current_char;
break;
}
}
}
else
{
result = GATE_RESULT_INVALIDINPUT;
}
break;
}
/* Character class: */
case '[':
{
if (pattern_index + 1 < (gate_index_t)pattern_length)
{
/* Remember where the char-buffer starts. */
buffer_begin = buffer_index;
/* Look-ahead to determine if negated */
if (ptr_pattern[pattern_index + 1] == '^')
{
ptr_compiled[compiled_index].type = GATE_REGEX_INV_CHAR_CLASS;
++pattern_index; /* Increment i to avoid including '^' in the char-buffer */
}
else
{
ptr_compiled[compiled_index].type = GATE_REGEX_CHAR_CLASS;
}
/* Copy characters inside [..] to buffer */
while (pattern_index + 1 < (gate_index_t)pattern_length)
{
++pattern_index;
current_char = ptr_pattern[pattern_index];
if (current_char == ']')
{
/* end of character class reached*/
break;
}
if (current_char == '\\')
{
if (buffer_index >= GATE_REGEX_MAX_CHAR_CLASS_LEN - 1)
{
result = GATE_RESULT_OUTOFBOUNDS;
break;
}
ptr_buffer[buffer_index++] = ptr_pattern[pattern_index++];
}
else if (buffer_index >= GATE_REGEX_MAX_CHAR_CLASS_LEN)
{
result = GATE_RESULT_OUTOFBOUNDS;
break;
}
ptr_buffer[buffer_index++] = ptr_pattern[pattern_index];
}
if (buffer_index >= GATE_REGEX_MAX_CHAR_CLASS_LEN)
{
/* Catches cases such as [00000000000000000000000000000000000000][ */
//fputs("exceeded internal buffer!\n", stderr);
result = GATE_RESULT_OUTOFBOUNDS;
break;
}
/* Null-terminate string end */
ptr_buffer[buffer_index++] = 0;
ptr_compiled[compiled_index].ptr = &ptr_buffer[buffer_begin];
}
else
{
result = GATE_RESULT_INVALIDINPUT;
}
break;
}
/* Other characters: */
default:
{
ptr_compiled[compiled_index].type = GATE_REGEX_CHAR;
ptr_compiled[compiled_index].chr = current_char;
break;
}
}
++pattern_index;
++compiled_index;
}
// finally completed
} while (0);
return result;
}
gate_result_t gate_regex_create(gate_regex_t* new_regex, gate_string_t const* pattern)
{
gate_result_t result;
gate_regex_t regex = (gate_regex_t)gate_mem_alloc(sizeof(struct gate_regex_class));
if (NULL == regex)
{
return GATE_RESULT_OUTOFMEMORY;
}
result = gate_regex_init(regex, pattern);
if (GATE_FAILED(result))
{
gate_mem_dealloc(regex);
}
else
{
*new_regex = regex;
}
return result;
}
gate_result_t gate_regex_clone(gate_regex_t* new_regex, gate_regex_t source)
{
gate_result_t result = GATE_RESULT_INVALIDARG;
if ((source != NULL) && (new_regex != NULL))
{
gate_string_t pattern;
gate_string_create_static(&pattern, source->pattern);
result = gate_regex_create(new_regex, &pattern);
}
return result;
}
gate_result_t gate_regex_match(gate_regex_t regex, gate_string_t const* text, gate_size_t* ptr_match_pos, gate_size_t* ptr_match_length)
{
gate_result_t ret = GATE_RESULT_NOMATCH;
int index;<--- The scope of the variable 'index' can be reduced. [+]The scope of the variable 'index' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
int match_length = 0;
gate_size_t text_pos;
gate_size_t text_length;
if ((regex != NULL) && (text != NULL))
{
text_length = gate_string_length(text);
if (regex->compiled[0].type == GATE_REGEX_BEGIN)
{
if (match_pattern(®ex->compiled[1], gate_string_ptr(text, 0), text_length, &match_length))
{
if (ptr_match_pos)
{
*ptr_match_pos = 0;
}
if (ptr_match_length)
{
*ptr_match_length = (gate_size_t)match_length;
}
ret = GATE_RESULT_OK;
}
}
else
{
index = -1;
text_pos = 0;
do
{
++index;
if (match_pattern(®ex->compiled[0], gate_string_ptr(text, text_pos), text_length - text_pos, &match_length))
{
if (text_pos < text_length)
{
if (ptr_match_pos)
{
*ptr_match_pos = (gate_size_t)index;
}
if (ptr_match_length)
{
*ptr_match_length = (gate_size_t)match_length;
}
ret = GATE_RESULT_OK;
}
break;
}
} while (text_pos++ < text->length);
}
}
return ret;
}
void gate_regex_release(gate_regex_t regex)
{
if (regex != NULL)
{
gate_mem_dealloc(regex);
}
}
|