Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Regexp.linear_time? #6901

Merged
merged 5 commits into from Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/ruby/onigmo.h
Expand Up @@ -854,6 +854,8 @@ OnigPosition onig_search_gpos(OnigRegex, const OnigUChar* str, const OnigUChar*
ONIG_EXTERN
OnigPosition onig_match(OnigRegex, const OnigUChar* str, const OnigUChar* end, const OnigUChar* at, OnigRegion* region, OnigOptionType option);
ONIG_EXTERN
int onig_check_linear_time(OnigRegex reg);
ONIG_EXTERN
OnigRegion* onig_region_new(void);
ONIG_EXTERN
void onig_region_init(OnigRegion* region);
Expand Down
34 changes: 34 additions & 0 deletions re.c
Expand Up @@ -4194,6 +4194,39 @@ rb_reg_s_union_m(VALUE self, VALUE args)
return rb_reg_s_union(self, args);
}

/*
* call-seq:
* Regexp.linear_time?(re)
* Regexp.linear_time?(string, options = 0)
*
* Returns +true+ if matching against <tt>re</tt> can be
* done in linear time to the input string.
*
* Regexp.linear_time?(/re/) # => true
makenowjust marked this conversation as resolved.
Show resolved Hide resolved
*
*/
static VALUE
rb_reg_s_linear_time_p(int argc, VALUE *argv, VALUE self)
{
VALUE re;
VALUE src, opts = Qundef, n_flag = Qundef, kwargs;

rb_scan_args(argc, argv, "12:", &src, &opts, &n_flag, &kwargs);

if (RB_TYPE_P(src, T_REGEXP)) {
re = src;
if (opts != Qnil) {
rb_warn("flags ignored");
}
}
else {
re = rb_class_new_instance(argc, argv, rb_cRegexp);
}

rb_reg_check(re);
return RBOOL(onig_check_linear_time(RREGEXP_PTR(re)));
}

/* :nodoc: */
static VALUE
rb_reg_init_copy(VALUE copy, VALUE re)
Expand Down Expand Up @@ -4571,6 +4604,7 @@ Init_Regexp(void)
rb_define_singleton_method(rb_cRegexp, "union", rb_reg_s_union_m, -2);
rb_define_singleton_method(rb_cRegexp, "last_match", rb_reg_s_last_match, -1);
rb_define_singleton_method(rb_cRegexp, "try_convert", rb_reg_s_try_convert, 1);
rb_define_singleton_method(rb_cRegexp, "linear_time?", rb_reg_s_linear_time_p, -1);

rb_define_method(rb_cRegexp, "initialize", rb_reg_initialize_m, -1);
rb_define_method(rb_cRegexp, "initialize_copy", rb_reg_init_copy, 1);
Expand Down
16 changes: 15 additions & 1 deletion regexec.c
Expand Up @@ -694,7 +694,21 @@ static OnigPosition init_cache_index_table(regex_t* reg, OnigCacheIndex *table)
bytecode_error:
return ONIGERR_UNDEFINED_BYTECODE;
}
#endif /* USE_MATCH_CACHE */
#else /* USE_MATCH_CACHE */
static OnigPosition count_num_cache_opcode(regex_t* reg, long* num, long* table_size)
{
*num = NUM_CACHE_OPCODE_FAIL;
return 0;
}
#endif

extern int
onig_check_linear_time(OnigRegexType* reg)
{
long num = 0, table_size = 0;
count_num_cache_opcode(reg, &num, &table_size);
return num != NUM_CACHE_OPCODE_FAIL;
}

extern void
onig_region_clear(OnigRegion* region)
Expand Down
8 changes: 8 additions & 0 deletions test/ruby/test_regexp.rb
Expand Up @@ -1693,4 +1693,12 @@ def test_cache_optimization_square
assert_nil(/^a*b?a*$/ =~ "a" * 1000000 + "x")
end;
end

def test_linear_time_p
assert_send [Regexp, :linear_time?, /a/]
assert_send [Regexp, :linear_time?, 'a']
assert_send [Regexp, :linear_time?, 'a', Regexp::IGNORECASE]
assert_not_send [Regexp, :linear_time?, /(a)\1/]
assert_not_send [Regexp, :linear_time?, "(a)\\1"]
end
end