Skip to content

Commit

Permalink
Upd pcre2
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen van der Heijden committed Nov 28, 2017
1 parent a688354 commit 7b10995
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 16 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
ibcleri (0.9.4)

* Replaced pcre with pcre2.

-- Jeroen van der Heijden <jeroen@transceptor.technology> 28 Nov 2017

libcleri (0.9.3)

* Fixed compiler warnings.
Expand Down
1 change: 1 addition & 0 deletions inc/cleri/grammar.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct cleri_grammar_s
{
cleri_t * start;
pcre2_code * re_keywords;
pcre2_match_data * match_data;
};

#endif /* CLERI_GRAMMAR_H_ */
1 change: 1 addition & 0 deletions inc/cleri/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct cleri_parse_s
const cleri_olist_t * expect;
cleri_expecting_t * expecting;
pcre2_code * re_keywords;
pcre2_match_data * match_data;
cleri_kwcache_t * kwcache;
};

Expand Down
1 change: 1 addition & 0 deletions inc/cleri/regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ cleri_t * cleri_regex(uint32_t gid, const char * pattern);
struct cleri_regex_s
{
pcre2_code * regex;
pcre2_match_data * match_data;
};

#endif /* CLERI_REGEX_H_ */
2 changes: 1 addition & 1 deletion inc/cleri/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#define LIBCLERI_VERSION_MAJOR 0
#define LIBCLERI_VERSION_MINOR 9
#define LIBCLERI_VERSION_PATCH 3
#define LIBCLERI_VERSION_PATCH 4

#define LIBCLERI_STRINGIFY(num) #num
#define LIBCLERI_VERSION_STR(major,minor,patch) \
Expand Down
2 changes: 1 addition & 1 deletion makefile.init
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MAJOR := 0
MINOR := 9
PATCH := 3
PATCH := 4
VERSION := $(MAJOR).$(MINOR).$(PATCH)
12 changes: 12 additions & 0 deletions src/grammar.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ cleri_grammar_t * cleri_grammar(cleri_t * start, const char * re_keywords)
return NULL;
}

grammar->match_data = \
pcre2_match_data_create_from_pattern(grammar->re_keywords, NULL);

if (grammar->match_data == NULL)
{
pcre2_code_free(grammar->re_keywords);
fprintf(stderr, "error: cannot create matsch data\n");
free(grammar);
return NULL;
}

/* bind root element and increment the reference counter */
grammar->start = start;
cleri_incref(start);
Expand All @@ -75,6 +86,7 @@ cleri_grammar_t * cleri_grammar(cleri_t * start, const char * re_keywords)

void cleri_grammar_free(cleri_grammar_t * grammar)
{
pcre2_match_data_free(grammar->match_data);
pcre2_code_free(grammar->re_keywords);
cleri_free(grammar->start);
free(grammar);
Expand Down
10 changes: 4 additions & 6 deletions src/kwcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,25 @@ static void KWCACHE_kw_match(
const char * str)
{
int pcre_exec_ret;
pcre2_match_data * match_data;

PCRE2_SIZE * ovector;

match_data = pcre2_match_data_create_from_pattern(pr->re_keywords, NULL);
pcre_exec_ret = pcre2_match(
pr->re_keywords,
(PCRE2_SPTR8) str,
strlen(str),
0, // start looking at this point
0, // OPTIONS
match_data,
pr->match_data,
NULL);

if (pcre_exec_ret < 0)
{
pcre2_match_data_free(match_data);
return;
}

ovector = pcre2_get_ovector_pointer(match_data);
ovector = pcre2_get_ovector_pointer(pr->match_data);
kwcache->len = ovector[1];

pcre2_match_data_free(match_data);

}
1 change: 1 addition & 0 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ cleri_parse_t * cleri_parse(cleri_grammar_t * grammar, const char * str)
}

pr->re_keywords = grammar->re_keywords;
pr->match_data = grammar->match_data;

/* do the actual parsing */
cleri__parse_walk(
Expand Down
25 changes: 17 additions & 8 deletions src/regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ cleri_t * cleri_regex(uint32_t gid, const char * pattern)
return NULL;
}

cl_object->via.regex->match_data = pcre2_match_data_create_from_pattern(
cl_object->via.regex->regex,
NULL);

if (cl_object->via.regex->match_data == NULL)
{
pcre2_code_free(cl_object->via.regex->regex);
fprintf(stderr, "error: cannot create matsch data\n");
free(cl_object->via.regex);
free(cl_object);
return NULL;
return NULL;
}

return cl_object;
}

Expand All @@ -89,6 +103,7 @@ cleri_t * cleri_regex(uint32_t gid, const char * pattern)
*/
static void REGEX_free(cleri_t * cl_object)
{
pcre2_match_data_free(cl_object->via.regex->match_data);
pcre2_code_free(cl_object->via.regex->regex);
free(cl_object->via.regex);
}
Expand All @@ -103,11 +118,7 @@ static cleri_node_t * REGEX_parse(
cleri_rule_store_t * rule __attribute__((unused)))
{
int pcre_exec_ret;
pcre2_match_data * match_data;
PCRE2_SIZE * ovector;
match_data = pcre2_match_data_create_from_pattern(
cl_obj->via.regex->regex,
NULL);
const char * str = parent->str + parent->len;
cleri_node_t * node;

Expand All @@ -117,19 +128,18 @@ static cleri_node_t * REGEX_parse(
strlen(str),
0, // start looking at this point
0, // OPTIONS
match_data,
cl_obj->via.regex->match_data,
NULL);

if (pcre_exec_ret < 0)
{
pcre2_match_data_free(match_data);
if (cleri__expecting_update(pr->expecting, cl_obj, str) == -1)
{
pr->is_valid = -1; /* error occurred */
}
return NULL;
}
ovector = pcre2_get_ovector_pointer(match_data);
ovector = pcre2_get_ovector_pointer(cl_obj->via.regex->match_data);

/* since each regex pattern should start with ^ we now sub_str_vec[0]
* should be 0. sub_str_vec[1] contains the end position in the sting
Expand All @@ -151,6 +161,5 @@ static cleri_node_t * REGEX_parse(
pr->is_valid = -1; /* error occurred */
}

pcre2_match_data_free(match_data);
return node;
}

0 comments on commit 7b10995

Please sign in to comment.