|
33 | 33 | Regexp.linear_time?(/a*(?:(?<=a)a*)*b/).should == true |
34 | 34 | end |
35 | 35 |
|
36 | | - it "returns true for negative lookahead" do |
37 | | - Regexp.linear_time?(/a*(?:(?!a*)a*)*b/).should == true |
38 | | - end |
39 | | - |
40 | 36 | it "returns true for negative lookbehind" do |
41 | 37 | Regexp.linear_time?(/a*(?:(?<!a)a*)*b/).should == true |
42 | 38 | end |
43 | 39 |
|
44 | | - it "returns true for atomic groups" do |
45 | | - Regexp.linear_time?(/a*(?:(?>a)a*)*b/).should == true |
| 40 | + # There are two known ways to make Regexp linear: |
| 41 | + # * Using a DFA (deterministic finite-state automaton) Regexp engine, which always matches in linear time (e.g. TruffleRuby with TRegex) |
| 42 | + # * Caching position and state to avoid catastrophic backtracking (e.g. CRuby: https://bugs.ruby-lang.org/issues/19104) |
| 43 | + # |
| 44 | + # Both approach should be allowed and given that DFA Regexp engines |
| 45 | + # are much faster there should be no specs preventing using them. |
| 46 | + uses_regexp_caching = RUBY_ENGINE == 'ruby' |
| 47 | + uses_dfa_regexp_engine = !uses_regexp_caching |
| 48 | + |
| 49 | + # The following specs should not be relied upon, |
| 50 | + # they are here only to illustrate differences between Regexp engines. |
| 51 | + guard -> { uses_regexp_caching } do |
| 52 | + it "returns true for negative lookahead" do |
| 53 | + Regexp.linear_time?(/a*(?:(?!a*)a*)*b/).should == true |
| 54 | + end |
| 55 | + |
| 56 | + it "returns true for atomic groups" do |
| 57 | + Regexp.linear_time?(/a*(?:(?>a)a*)*b/).should == true |
| 58 | + end |
| 59 | + |
| 60 | + it "returns true for possessive quantifiers" do |
| 61 | + Regexp.linear_time?(/a*(?:(?:a)?+a*)*b/).should == true |
| 62 | + end |
| 63 | + |
| 64 | + it "returns true for positive lookbehind with capture group" do |
| 65 | + Regexp.linear_time?(/.(?<=(a))/).should == true |
| 66 | + end |
46 | 67 | end |
47 | 68 |
|
48 | | - it "returns true for possessive quantifiers" do |
49 | | - Regexp.linear_time?(/a*(?:(?:a)?+a*)*b/).should == true |
| 69 | + # The following specs should not be relied upon, |
| 70 | + # they are here only to illustrate differences between Regexp engines. |
| 71 | + guard -> { uses_dfa_regexp_engine } do |
| 72 | + it "returns true for non-recursive subexpression call" do |
| 73 | + Regexp.linear_time?(/(?<a>a){0}\g<a>/).should == true |
| 74 | + end |
| 75 | + |
| 76 | + it "returns true for positive lookahead with capture group" do |
| 77 | + Regexp.linear_time?(/x+(?=(a))/).should == true |
| 78 | + end |
50 | 79 | end |
51 | 80 | end |
0 commit comments