Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
class method · added in Ruby 3.2
Regexp.linear_time?
Regexp.linear_time?(re)
Regexp.linear_time?(string, options = 0)
re.c
View on GitHub ↗
static VALUE
rb_reg_s_linear_time_p(int argc, VALUE *argv, VALUE self)
{
struct reg_init_args args;
VALUE re = reg_extract_args(argc, argv, &args);
if (NIL_P(re)) {
re = reg_init_args(rb_reg_alloc(), args.str, args.enc, args.flags);
}
return RBOOL(onig_check_linear_time(RREGEXP_PTR(re)));
}
Returns true if matching against re can be done in linear time to the input string.
Example
Regexp.linear_time?(/re/) # => true
Note that this is a property of the ruby interpreter, not of the argument regular expression. Identical regexp can or cannot run in linear time depending on your ruby binary. Neither forward nor backward compatibility is guaranteed about the return value of this method. Our current algorithm is (*1) but this is subject to change in the future. Alternative implementations can also behave differently. They might always return false for everything.
Ruby version changes
-
3.2
-
Regexp.linear_time?is introduced. [Feature #19194]
-