Skip to content

Commit

Permalink
Modification to fix failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
jonclayden committed Nov 10, 2023
1 parent 1228b4d commit c856df1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ static char * ore_build_pattern (SEXP pattern_)
return pattern;
}

static Rboolean ore_group_name_vector (SEXP vec, regex_t *regex)
// Insert group names into an R character vector of appropriate size
Rboolean ore_group_name_vector (SEXP vec, regex_t *regex)
{
const int n_groups = onig_number_of_captures(regex);

Expand Down
2 changes: 2 additions & 0 deletions src/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ regex_t * ore_retrieve (SEXP regex_, encoding_t *encoding);

void ore_free (regex_t *regex, SEXP source);

Rboolean ore_group_name_vector (SEXP vec, regex_t *regex);

SEXP ore_build (SEXP pattern_, SEXP options_, SEXP encoding_name_, SEXP syntax_name_);

#endif
23 changes: 21 additions & 2 deletions src/match.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ void ore_char_matrix (SEXP mat, const char **data, const int n_regions, const in
SEXP ore_search_all (SEXP regex_, SEXP text_, SEXP all_, SEXP start_, SEXP simplify_, SEXP incremental_)
{
// Convert R objects to C types
SEXP group_names = getAttrib(regex_, install("groupNames"));
const Rboolean all = asLogical(all_) == TRUE;
const Rboolean simplify = asLogical(simplify_) == TRUE;
const Rboolean incremental = (asLogical(incremental_) == TRUE) && !all;
Expand All @@ -271,6 +270,26 @@ SEXP ore_search_all (SEXP regex_, SEXP text_, SEXP all_, SEXP start_, SEXP simpl
text_t *text = ore_text(text_);
regex_t *regex = ore_retrieve(regex_, text->encoding);

SEXP group_names = R_NilValue;
Rboolean group_names_protected = FALSE;
if (inherits(regex_, "ore"))
group_names = getAttrib(regex_, install("groupNames"));
else
{
const int n_groups = onig_number_of_captures(regex);
if (n_groups > 0)
{
PROTECT(group_names = NEW_CHARACTER(n_groups));
if (ore_group_name_vector(group_names, regex))
group_names_protected = TRUE;
else
{
UNPROTECT(1);
group_names = R_NilValue;
}
}
}

// Obtain the length of the start vector (which will be recycled if necessary)
const int start_len = length(start_);

Expand Down Expand Up @@ -440,7 +459,7 @@ SEXP ore_search_all (SEXP regex_, SEXP text_, SEXP all_, SEXP start_, SEXP simpl
ore_free(regex, regex_);
ore_text_done(text);

UNPROTECT(using_file ? 1 : 2);
UNPROTECT(2 + group_names_protected - using_file);

// Return just the first (and only) element of the full list, if requested
if (simplify && text->length == 1)
Expand Down

0 comments on commit c856df1

Please sign in to comment.