| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "gate/regexpressions.h" | ||
| 30 | #include "gate/results.h" | ||
| 31 | |||
| 32 | /* | ||
| 33 | This is a GATE framework port of | ||
| 34 | kokke's public domain code 'tiny-regex-c' | ||
| 35 | which is based on Rob-Pike's regex-code. | ||
| 36 | */ | ||
| 37 | |||
| 38 | /* Private functions: */ | ||
| 39 | ✗ | static gate_bool_t match_digit(char c) | |
| 40 | { | ||
| 41 | ✗ | return ((c >= '0') && (c <= '9')); | |
| 42 | } | ||
| 43 | ✗ | static gate_bool_t match_alpha(char c) | |
| 44 | { | ||
| 45 | ✗ | return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')); | |
| 46 | } | ||
| 47 | ✗ | static gate_bool_t match_whitespace(char c) | |
| 48 | { | ||
| 49 | ✗ | return ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r') || (c == '\f') || (c == '\v')); | |
| 50 | } | ||
| 51 | ✗ | static gate_bool_t match_alphanum(char c) | |
| 52 | { | ||
| 53 | ✗ | return ((c == '_') || match_alpha(c) || match_digit(c)); | |
| 54 | } | ||
| 55 | 42 | static gate_bool_t match_range(char c, const char* str) | |
| 56 | { | ||
| 57 |
4/4✓ Branch 0 taken 35 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 7 times.
|
42 | return ((c != '-') && (str[0] != '\0') && (str[0] != '-') |
| 58 |
3/4✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
|
28 | && (str[1] == '-') && (str[2] != '\0') |
| 59 |
5/6✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 2 times.
|
84 | && ((c >= str[0]) && (c <= str[2]))); |
| 60 | } | ||
| 61 | ✗ | static gate_bool_t is_meta_char(char c) | |
| 62 | { | ||
| 63 | ✗ | return ((c == 's') || (c == 'S') || (c == 'w') || (c == 'W') || (c == 'd') || (c == 'D')); | |
| 64 | } | ||
| 65 | |||
| 66 | ✗ | static gate_bool_t match_meta_char(char c, const char* str) | |
| 67 | { | ||
| 68 | ✗ | switch (str[0]) | |
| 69 | { | ||
| 70 | ✗ | case 'd': return match_digit(c); | |
| 71 | ✗ | case 'D': return !match_digit(c); | |
| 72 | ✗ | case 'w': return match_alphanum(c); | |
| 73 | ✗ | case 'W': return !match_alphanum(c); | |
| 74 | ✗ | case 's': return match_whitespace(c); | |
| 75 | ✗ | case 'S': return !match_whitespace(c); | |
| 76 | ✗ | default: return (c == str[0]); | |
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | 42 | static gate_bool_t match_char_class(char c, const char* str) | |
| 81 | { | ||
| 82 | do | ||
| 83 | { | ||
| 84 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 28 times.
|
42 | if (match_range(c, str)) |
| 85 | { | ||
| 86 | 14 | return true; | |
| 87 | } | ||
| 88 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | else if (str[0] == '\\') |
| 89 | { | ||
| 90 | /* Escape-char: increment str-ptr and match on next char */ | ||
| 91 | ✗ | str += 1; | |
| 92 | ✗ | if (match_meta_char(c, str)) | |
| 93 | { | ||
| 94 | ✗ | return true; | |
| 95 | } | ||
| 96 | ✗ | else if ((c == str[0]) && !is_meta_char(c)) | |
| 97 | { | ||
| 98 | ✗ | return true; | |
| 99 | } | ||
| 100 | } | ||
| 101 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | else if (c == str[0]) |
| 102 | { | ||
| 103 | ✗ | if (c == '-') | |
| 104 | { | ||
| 105 | ✗ | return ((str[-1] == '\0') || (str[1] == '\0')); | |
| 106 | } | ||
| 107 | else | ||
| 108 | { | ||
| 109 | ✗ | return true; | |
| 110 | } | ||
| 111 | } | ||
| 112 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7 times.
|
28 | } while (*str++ != '\0'); |
| 113 | |||
| 114 | 7 | return false; | |
| 115 | } | ||
| 116 | |||
| 117 | 118 | static gate_bool_t match_one(gate_regex_token_t token, char c) | |
| 118 | { | ||
| 119 |
3/10✓ Branch 0 taken 6 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 91 times.
|
118 | switch (token.type) |
| 120 | { | ||
| 121 | 6 | case GATE_REGEX_DOT: return true; | |
| 122 | 21 | case GATE_REGEX_CHAR_CLASS: return match_char_class(c, (const char*)token.ptr); | |
| 123 | ✗ | case GATE_REGEX_INV_CHAR_CLASS: return !match_char_class(c, (const char*)token.ptr); | |
| 124 | ✗ | case GATE_REGEX_DIGIT: return match_digit(c); | |
| 125 | ✗ | case GATE_REGEX_NOT_DIGIT: return !match_digit(c); | |
| 126 | ✗ | case GATE_REGEX_ALPHA: return match_alphanum(c); | |
| 127 | ✗ | case GATE_REGEX_NOT_ALPHA: return !match_alphanum(c); | |
| 128 | ✗ | case GATE_REGEX_WHITESPACE: return match_whitespace(c); | |
| 129 | ✗ | case GATE_REGEX_NOT_WHITESPACE: return !match_whitespace(c); | |
| 130 | 91 | default: return (token.chr == c); | |
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | static gate_bool_t match_pattern(gate_regex_token_t* pattern, const char* text, gate_size_t textlen, int* match_length); | ||
| 135 | |||
| 136 | 2 | 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) | |
| 137 | { | ||
| 138 | 2 | int prelen = *match_length; | |
| 139 | 2 | int textpos = 0; | |
| 140 |
4/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 1 times.
|
9 | while ((textpos < (int)textlen) && match_one(token, text[textpos])) |
| 141 | { | ||
| 142 | 7 | ++textpos; | |
| 143 | 7 | ++(*match_length); | |
| 144 | } | ||
| 145 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | while (textpos >= 0) |
| 146 | { | ||
| 147 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | if (match_pattern(pattern, &text[textpos], textlen - textpos, match_length)) |
| 148 | { | ||
| 149 | 2 | return true; | |
| 150 | } | ||
| 151 | 1 | --textpos; | |
| 152 | 1 | --(*match_length); | |
| 153 | } | ||
| 154 | |||
| 155 | ✗ | *match_length = prelen; | |
| 156 | ✗ | return false; | |
| 157 | } | ||
| 158 | |||
| 159 | 9 | 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) | |
| 160 | { | ||
| 161 | 9 | gate_size_t textpos = 0; | |
| 162 | |||
| 163 |
4/4✓ Branch 0 taken 23 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 8 times.
|
24 | while ((textpos < textlen) && match_one(token, text[textpos])) |
| 164 | { | ||
| 165 | 15 | ++textpos; | |
| 166 | 15 | ++(*match_length); | |
| 167 | } | ||
| 168 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 4 times.
|
15 | while (textpos > 0) |
| 169 | { | ||
| 170 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 2 taken 6 times.
|
11 | if (match_pattern(pattern, &text[textpos], textlen - textpos, match_length)) |
| 171 | { | ||
| 172 | 5 | return true; | |
| 173 | } | ||
| 174 | 6 | --textpos; | |
| 175 | 6 | --(*match_length); | |
| 176 | } | ||
| 177 | 4 | return false; | |
| 178 | } | ||
| 179 | |||
| 180 | 9 | 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) | |
| 181 | { | ||
| 182 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
|
9 | if (token.type == GATE_REGEX_UNUSED) |
| 183 | { | ||
| 184 | 8 | return true; | |
| 185 | } | ||
| 186 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (match_pattern(pattern, text, textlen, match_length)) |
| 187 | { | ||
| 188 | 1 | return true; | |
| 189 | } | ||
| 190 | ✗ | if ((textlen != 0) && match_one(token, text[0])) | |
| 191 | { | ||
| 192 | ✗ | if (match_pattern(pattern, &text[1], textlen - 1, match_length)) | |
| 193 | { | ||
| 194 | ✗ | ++(*match_length); | |
| 195 | ✗ | return true; | |
| 196 | } | ||
| 197 | } | ||
| 198 | ✗ | return false; | |
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | 78 | static gate_bool_t match_pattern(gate_regex_token_t* pattern, const char* text, gate_size_t textlen, int* match_length) | |
| 203 | { | ||
| 204 | 78 | int pre = *match_length; | |
| 205 | |||
| 206 | 78 | gate_size_t pattern_index = 0; | |
| 207 | 78 | gate_size_t textpos = 0; | |
| 208 | do | ||
| 209 | { | ||
| 210 |
4/4✓ Branch 0 taken 107 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 106 times.
|
115 | if ((pattern[pattern_index].type == GATE_REGEX_UNUSED) || (pattern[pattern_index].type == GATE_REGEX_QUESTIONMARK)) |
| 211 | { | ||
| 212 | 9 | return match_question(pattern[pattern_index], &pattern[pattern_index + 2], &text[textpos], textlen - textpos, match_length); | |
| 213 | } | ||
| 214 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 104 times.
|
106 | else if (pattern[pattern_index + 1].type == GATE_REGEX_STAR) |
| 215 | { | ||
| 216 | 2 | return match_star(pattern[pattern_index + 0], &pattern[pattern_index + 2], &text[textpos], textlen - textpos, match_length); | |
| 217 | } | ||
| 218 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 95 times.
|
104 | else if (pattern[pattern_index + 1].type == GATE_REGEX_PLUS) |
| 219 | { | ||
| 220 | 9 | return match_plus(pattern[pattern_index + 0], &pattern[pattern_index + 2], &text[textpos], textlen - textpos, match_length); | |
| 221 | } | ||
| 222 |
3/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
95 | else if ((pattern[pattern_index + 0].type == GATE_REGEX_END) && pattern[pattern_index + 1].type == GATE_REGEX_UNUSED) |
| 223 | { | ||
| 224 | 5 | return (textpos >= textlen); | |
| 225 | } | ||
| 226 | 90 | ++(*match_length); | |
| 227 |
4/4✓ Branch 0 taken 87 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 37 times.
✓ Branch 4 taken 50 times.
|
90 | } while ((textpos < textlen) && match_one(pattern[pattern_index++], text[textpos++])); |
| 228 | |||
| 229 | 53 | *match_length = pre; | |
| 230 | 53 | return false; | |
| 231 | } | ||
| 232 | |||
| 233 | |||
| 234 | 16 | gate_result_t gate_regex_init(gate_regex_t regex, gate_string_t const* pattern) | |
| 235 | { | ||
| 236 | 16 | gate_result_t result = GATE_RESULT_OK; | |
| 237 | |||
| 238 | do | ||
| 239 | { | ||
| 240 | 16 | gate_index_t buffer_index = 1; /* int ccl_bufidx = 1; */ | |
| 241 | 16 | gate_index_t pattern_index = 0; /* int i = 0; // index into pattern */ | |
| 242 | 16 | gate_index_t compiled_index = 0; /* int j = 0; // index into re_compiled */ | |
| 243 | |||
| 244 | char const* ptr_pattern; | ||
| 245 | gate_size_t pattern_length; | ||
| 246 | gate_regex_token_t* ptr_compiled; | ||
| 247 | unsigned char* ptr_buffer; | ||
| 248 | |||
| 249 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (regex == NULL) |
| 250 | { | ||
| 251 | ✗ | result = GATE_RESULT_INVALIDARG; | |
| 252 | ✗ | break; | |
| 253 | } | ||
| 254 | 16 | gate_mem_clear(regex, sizeof(struct gate_regex_class)); | |
| 255 | |||
| 256 | 16 | ptr_pattern = ®ex->pattern[0]; | |
| 257 | 16 | pattern_length = gate_string_to_buffer(pattern, regex->pattern, sizeof(regex->pattern)); | |
| 258 | |||
| 259 | 16 | ptr_compiled = ®ex->compiled[0]; | |
| 260 | 16 | ptr_buffer = ®ex->buffer[0]; | |
| 261 | |||
| 262 |
1/2✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
|
87 | while ((result == GATE_RESULT_OK) |
| 263 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 16 times.
|
87 | && (pattern_index < (gate_index_t)pattern_length) |
| 264 |
1/2✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
|
71 | && (compiled_index + 1 < GATE_REGEX_MAX_COMPILED)) |
| 265 | { | ||
| 266 | 71 | char current_char = ptr_pattern[pattern_index]; | |
| 267 | |||
| 268 |
9/9✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 43 times.
|
71 | switch (current_char) |
| 269 | { | ||
| 270 | /* Meta-characters: */ | ||
| 271 | 6 | case '^': { ptr_compiled[compiled_index].type = GATE_REGEX_BEGIN; break; } | |
| 272 | 6 | case '$': { ptr_compiled[compiled_index].type = GATE_REGEX_END; break; } | |
| 273 | 1 | case '.': { ptr_compiled[compiled_index].type = GATE_REGEX_DOT; break; } | |
| 274 | 2 | case '*': { ptr_compiled[compiled_index].type = GATE_REGEX_STAR; break; } | |
| 275 | 5 | case '+': { ptr_compiled[compiled_index].type = GATE_REGEX_PLUS; break; } | |
| 276 | 1 | case '?': { ptr_compiled[compiled_index].type = GATE_REGEX_QUESTIONMARK;break; } | |
| 277 | /*case '|': { ptr_compiled[compiled_index].type = GATE_REGEX_BRANCH; break; } <-- not working properly */ | ||
| 278 | /* Escaped character-classes (\s \w ...): */ | ||
| 279 | 3 | case '\\': | |
| 280 | { | ||
| 281 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (pattern_index + 1 < (gate_index_t)pattern_length) |
| 282 | { | ||
| 283 | /* Skip the escape-char '\\' */ | ||
| 284 | 3 | ++pattern_index; | |
| 285 | /* ... and check the next */ | ||
| 286 | 3 | current_char = ptr_pattern[pattern_index]; | |
| 287 |
1/7✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
|
3 | switch (current_char) |
| 288 | { | ||
| 289 | /* Meta-character: */ | ||
| 290 | ✗ | case 'd': { ptr_compiled[compiled_index].type = GATE_REGEX_DIGIT; break; } | |
| 291 | ✗ | case 'D': { ptr_compiled[compiled_index].type = GATE_REGEX_NOT_DIGIT; break; } | |
| 292 | ✗ | case 'w': { ptr_compiled[compiled_index].type = GATE_REGEX_ALPHA; break; } | |
| 293 | ✗ | case 'W': { ptr_compiled[compiled_index].type = GATE_REGEX_NOT_ALPHA; break; } | |
| 294 | ✗ | case 's': { ptr_compiled[compiled_index].type = GATE_REGEX_WHITESPACE; break; } | |
| 295 | ✗ | case 'S': { ptr_compiled[compiled_index].type = GATE_REGEX_NOT_WHITESPACE; break; } | |
| 296 | /* Escaped character, e.g. '.' or '$' */ | ||
| 297 | 3 | default: | |
| 298 | { | ||
| 299 | 3 | ptr_compiled[compiled_index].type = GATE_REGEX_CHAR; | |
| 300 | 3 | ptr_compiled[compiled_index].chr = current_char; | |
| 301 | 3 | break; | |
| 302 | } | ||
| 303 | } | ||
| 304 | } | ||
| 305 | else | ||
| 306 | { | ||
| 307 | ✗ | result = GATE_RESULT_INVALIDINPUT; | |
| 308 | } | ||
| 309 | 3 | break; | |
| 310 | } | ||
| 311 | /* Character class: */ | ||
| 312 | 4 | case '[': | |
| 313 | { | ||
| 314 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (pattern_index + 1 < (gate_index_t)pattern_length) |
| 315 | { | ||
| 316 | /* Remember where the char-buffer starts. */ | ||
| 317 | 4 | gate_index_t buffer_begin = buffer_index; | |
| 318 | |||
| 319 | /* Look-ahead to determine if negated */ | ||
| 320 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ptr_pattern[pattern_index + 1] == '^') |
| 321 | { | ||
| 322 | ✗ | ptr_compiled[compiled_index].type = GATE_REGEX_INV_CHAR_CLASS; | |
| 323 | ✗ | ++pattern_index; /* Increment i to avoid including '^' in the char-buffer */ | |
| 324 | } | ||
| 325 | else | ||
| 326 | { | ||
| 327 | 4 | ptr_compiled[compiled_index].type = GATE_REGEX_CHAR_CLASS; | |
| 328 | } | ||
| 329 | |||
| 330 | /* Copy characters inside [..] to buffer */ | ||
| 331 |
1/2✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 | while (pattern_index + 1 < (gate_index_t)pattern_length) |
| 332 | { | ||
| 333 | 16 | ++pattern_index; | |
| 334 | 16 | current_char = ptr_pattern[pattern_index]; | |
| 335 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
|
16 | if (current_char == ']') |
| 336 | { | ||
| 337 | /* end of character class reached*/ | ||
| 338 | 4 | break; | |
| 339 | } | ||
| 340 | |||
| 341 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (current_char == '\\') |
| 342 | { | ||
| 343 | ✗ | if (buffer_index >= GATE_REGEX_MAX_CHAR_CLASS_LEN - 1) | |
| 344 | { | ||
| 345 | ✗ | result = GATE_RESULT_OUTOFBOUNDS; | |
| 346 | ✗ | break; | |
| 347 | } | ||
| 348 | |||
| 349 | ✗ | ptr_buffer[buffer_index++] = ptr_pattern[pattern_index++]; | |
| 350 | } | ||
| 351 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | else if (buffer_index >= GATE_REGEX_MAX_CHAR_CLASS_LEN) |
| 352 | { | ||
| 353 | ✗ | result = GATE_RESULT_OUTOFBOUNDS; | |
| 354 | ✗ | break; | |
| 355 | } | ||
| 356 | 12 | ptr_buffer[buffer_index++] = ptr_pattern[pattern_index]; | |
| 357 | } | ||
| 358 | |||
| 359 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (buffer_index >= GATE_REGEX_MAX_CHAR_CLASS_LEN) |
| 360 | { | ||
| 361 | /* Catches cases such as [00000000000000000000000000000000000000][ */ | ||
| 362 | //fputs("exceeded internal buffer!\n", stderr); | ||
| 363 | ✗ | result = GATE_RESULT_OUTOFBOUNDS; | |
| 364 | ✗ | break; | |
| 365 | } | ||
| 366 | /* Null-terminate string end */ | ||
| 367 | 4 | ptr_buffer[buffer_index++] = 0; | |
| 368 | 4 | ptr_compiled[compiled_index].ptr = &ptr_buffer[buffer_begin]; | |
| 369 | } | ||
| 370 | else | ||
| 371 | { | ||
| 372 | ✗ | result = GATE_RESULT_INVALIDINPUT; | |
| 373 | } | ||
| 374 | 4 | break; | |
| 375 | } | ||
| 376 | /* Other characters: */ | ||
| 377 | 43 | default: | |
| 378 | { | ||
| 379 | 43 | ptr_compiled[compiled_index].type = GATE_REGEX_CHAR; | |
| 380 | 43 | ptr_compiled[compiled_index].chr = current_char; | |
| 381 | 43 | break; | |
| 382 | } | ||
| 383 | } | ||
| 384 | |||
| 385 | 71 | ++pattern_index; | |
| 386 | 71 | ++compiled_index; | |
| 387 | } | ||
| 388 | |||
| 389 | // finally completed | ||
| 390 | } while (0); | ||
| 391 | |||
| 392 | 16 | return result; | |
| 393 | } | ||
| 394 | |||
| 395 | |||
| 396 | 15 | gate_result_t gate_regex_create(gate_regex_t* new_regex, gate_string_t const* pattern) | |
| 397 | { | ||
| 398 | gate_result_t result; | ||
| 399 | 15 | gate_regex_t regex = (gate_regex_t)gate_mem_alloc(sizeof(struct gate_regex_class)); | |
| 400 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (NULL == regex) |
| 401 | { | ||
| 402 | ✗ | return GATE_RESULT_OUTOFMEMORY; | |
| 403 | } | ||
| 404 | 15 | result = gate_regex_init(regex, pattern); | |
| 405 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (GATE_FAILED(result)) |
| 406 | { | ||
| 407 | ✗ | gate_mem_dealloc(regex); | |
| 408 | } | ||
| 409 | else | ||
| 410 | { | ||
| 411 | 15 | *new_regex = regex; | |
| 412 | } | ||
| 413 | |||
| 414 | 15 | return result; | |
| 415 | } | ||
| 416 | |||
| 417 | 2 | gate_result_t gate_regex_clone(gate_regex_t* new_regex, gate_regex_t source) | |
| 418 | { | ||
| 419 | 2 | gate_result_t result = GATE_RESULT_INVALIDARG; | |
| 420 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | if ((source != NULL) && (new_regex != NULL)) |
| 421 | { | ||
| 422 | gate_string_t pattern; | ||
| 423 | 2 | gate_string_create_static(&pattern, source->pattern); | |
| 424 | 2 | result = gate_regex_create(new_regex, &pattern); | |
| 425 | } | ||
| 426 | 2 | return result; | |
| 427 | } | ||
| 428 | |||
| 429 | 18 | 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) | |
| 430 | { | ||
| 431 | 18 | gate_result_t ret = GATE_RESULT_NOMATCH; | |
| 432 |
2/4✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
|
18 | if ((regex != NULL) && (text != NULL)) |
| 433 | { | ||
| 434 | 18 | int match_length = 0; | |
| 435 | 18 | gate_size_t text_length = gate_string_length(text); | |
| 436 | |||
| 437 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
|
18 | if (regex->compiled[0].type == GATE_REGEX_BEGIN) |
| 438 | { | ||
| 439 |
2/2✓ Branch 2 taken 3 times.
✓ Branch 3 taken 5 times.
|
8 | if (match_pattern(®ex->compiled[1], gate_string_ptr(text, 0), text_length, &match_length)) |
| 440 | { | ||
| 441 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (ptr_match_pos) |
| 442 | { | ||
| 443 | 3 | *ptr_match_pos = 0; | |
| 444 | } | ||
| 445 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (ptr_match_length) |
| 446 | { | ||
| 447 | 3 | *ptr_match_length = (gate_size_t)match_length; | |
| 448 | } | ||
| 449 | 3 | ret = GATE_RESULT_OK; | |
| 450 | } | ||
| 451 | } | ||
| 452 | else | ||
| 453 | { | ||
| 454 | 10 | int index = -1; | |
| 455 | 10 | gate_size_t text_pos = 0; | |
| 456 | do | ||
| 457 | { | ||
| 458 | 55 | ++index; | |
| 459 |
2/2✓ Branch 2 taken 8 times.
✓ Branch 3 taken 47 times.
|
55 | if (match_pattern(®ex->compiled[0], gate_string_ptr(text, text_pos), text_length - text_pos, &match_length)) |
| 460 | { | ||
| 461 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (text_pos < text_length) |
| 462 | { | ||
| 463 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (ptr_match_pos) |
| 464 | { | ||
| 465 | 8 | *ptr_match_pos = (gate_size_t)index; | |
| 466 | } | ||
| 467 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (ptr_match_length) |
| 468 | { | ||
| 469 | 8 | *ptr_match_length = (gate_size_t)match_length; | |
| 470 | } | ||
| 471 | 8 | ret = GATE_RESULT_OK; | |
| 472 | } | ||
| 473 | 8 | break; | |
| 474 | } | ||
| 475 |
2/2✓ Branch 0 taken 45 times.
✓ Branch 1 taken 2 times.
|
47 | } while (text_pos++ < text->length); |
| 476 | } | ||
| 477 | } | ||
| 478 | 18 | return ret; | |
| 479 | } | ||
| 480 | |||
| 481 | 15 | void gate_regex_release(gate_regex_t regex) | |
| 482 | { | ||
| 483 |
1/2✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
|
15 | if (regex != NULL) |
| 484 | { | ||
| 485 | 15 | gate_mem_dealloc(regex); | |
| 486 | } | ||
| 487 | 15 | } | |
| 488 |