| 323 | } |
| 324 | |
| 325 | static bool tp_compile_pattern(cbm_test_policy_t *policy, tp_pattern_t *pattern, bool path_mode) { |
| 326 | const char *text = pattern->text; |
| 327 | size_t start = 0, end = pattern->len; |
| 328 | if (path_mode) { |
| 329 | if (text[0] == '/') { |
| 330 | pattern->rooted = true; |
| 331 | start++; |
| 332 | } |
| 333 | size_t escapes = 0; |
| 334 | for (size_t i = end - 1; i && text[i - 1] == '\\'; i--) |
| 335 | escapes++; |
| 336 | if (text[end - 1] == '/' && !(escapes & 1U)) { |
| 337 | pattern->dir_only = true; |
| 338 | end--; |
| 339 | } |
| 340 | if (end <= start) { |
| 341 | pattern->disabled = true; |
| 342 | return true; |
| 343 | } |
| 344 | /* As in gitignore discovery, any interior slash anchors a path |
| 345 | * pattern, even when it appears within a class or is escaped. */ |
| 346 | if (memchr(text + start, '/', end - start)) |
| 347 | pattern->rooted = true; |
| 348 | } |
| 349 | pattern->tokens = cbm_arena_calloc(&policy->arena, (end - start) * sizeof(tp_token_t)); |
| 350 | if (!pattern->tokens) |
| 351 | return false; |
| 352 | for (size_t pos = start; pos < end;) { |
| 353 | tp_token_t *token = &pattern->tokens[pattern->token_count++]; |
| 354 | unsigned char ch = (unsigned char)text[pos++]; |
| 355 | if (ch == '\\' && pos < end) { |
| 356 | token->kind = TP_LITERAL; |
| 357 | token->literal = (unsigned char)text[pos++]; |
| 358 | } else if (ch == '*') { |
| 359 | size_t star_start = pos - 1; |
| 360 | while (pos < end && text[pos] == '*') |
| 361 | pos++; |
| 362 | /* Globstar is special only as an entire path component. An |
| 363 | * embedded run such as a**b is an ordinary star and |
| 364 | * must neither cross nor consume a separator. */ |
| 365 | bool globstar = pos - star_start >= 2 && |
| 366 | (star_start == start || text[star_start - 1] == '/') && |
| 367 | (pos == end || text[pos] == '/'); |
| 368 | if (globstar && pos < end) { |
| 369 | token->kind = TP_DOUBLE_STAR_SLASH; |
| 370 | pattern->rooted = path_mode; |
| 371 | pos++; |
| 372 | } else { |
| 373 | token->kind = globstar ? TP_DOUBLE_STAR : TP_STAR; |
| 374 | } |
| 375 | } else if (ch == '?') { |
| 376 | token->kind = TP_QUESTION; |
| 377 | } else if (ch == '[') { |
| 378 | size_t class_end = tp_class_end(text, end, pos - 1); |
| 379 | if (class_end > pos) { |
| 380 | if (!tp_compile_class(policy, text, pos - 1, class_end, token)) |
| 381 | return false; |
| 382 | pos = class_end; |
no test coverage detected