Branch data Line data Source code
1 : : #include "precizer.h"
2 : :
3 : : /**
4 : : *
5 : : * Checks whether PCRE2 regular expression matches a comparison string or not
6 : : *
7 : : */
8 : 4500 : REGEXP regexp_match(
9 : : const char *regexp,
10 : : const char *relative_path,
11 : : bool *showed_once)
12 : : {
13 : 4500 : bool match = false;
14 : :
15 : : /* for pcre2_compile */
16 : : pcre2_code *re;
17 : : PCRE2_SIZE erroffset;
18 : : int errcode;
19 : : PCRE2_UCHAR8 buffer[MAX_CHARACTERS];
20 : :
21 : : /* for pcre2_match */
22 : : int rc;
23 : : #if 0
24 : : PCRE2_SIZE *ovector;
25 : : #endif
26 : :
27 : 4500 : const unsigned char *pattern = (const unsigned char *)regexp;
28 : 4500 : size_t pattern_size = strlen(regexp);
29 : :
30 : 4500 : const unsigned char *subject = (const unsigned char *)relative_path;
31 : 4500 : size_t subject_size = strlen(relative_path);
32 : 4500 : uint32_t options = 0;
33 : :
34 : : pcre2_match_data *match_data;
35 : :
36 : 4500 : re = pcre2_compile(pattern,pattern_size,options,&errcode,&erroffset,NULL);
37 : :
38 [ - + ]: 4500 : if(re == NULL)
39 : : {
40 [ # # ]: 0 : if(*showed_once == false)
41 : : {
42 : 0 : *showed_once = true;
43 : 0 : pcre2_get_error_message(errcode,buffer,MAX_CHARACTERS);
44 : 0 : slog(ERROR,"PCRE2 regular expression %s has an error:%d %s\n",pattern,errcode,buffer);
45 : : }
46 : 0 : return(REGEXP_ERROR);
47 : : }
48 : :
49 : 4500 : match_data = pcre2_match_data_create_from_pattern(re,NULL);
50 : 4500 : rc = pcre2_match(re,subject,subject_size,0,options,match_data,NULL);
51 : :
52 [ - + ]: 4500 : if(rc == 0)
53 : : {
54 [ # # ]: 0 : if(*showed_once == false)
55 : : {
56 : 0 : *showed_once = true;
57 : 0 : pcre2_get_error_message(errcode,buffer,MAX_CHARACTERS);
58 : 0 : slog(ERROR,"PCRE2 regular expression %s has an error: %d \"offset vector too small\"\n",pattern,rc);
59 : : }
60 : 0 : return(REGEXP_ERROR);
61 [ + + ]: 4500 : } else if(rc > 0){
62 : : #if 0
63 : : ovector = pcre2_get_ovector_pointer(match_data);
64 : : #endif
65 : :
66 [ + + ]: 1144 : for(PCRE2_SIZE i = 0; i < (size_t)rc; i++)
67 : : {
68 : : #if 0
69 : : PCRE2_SPTR start = subject + ovector[2*i];
70 : : PCRE2_SIZE slen = ovector[2*i+1] - ovector[2*i];
71 : : printf( "%2ld: %.*s\n",i,(int)slen,(const char *)start );
72 : : #endif
73 : 572 : match = true;
74 : : }
75 : : }
76 : : #if 0
77 : : else if(rc < 0){
78 : : printf("No match\n");
79 : : }
80 : : #endif
81 : :
82 : 4500 : pcre2_match_data_free(match_data);
83 : 4500 : pcre2_code_free(re);
84 : :
85 : 4500 : return(match);
86 : : }
|