Skip to content

Commit 94797d3

Browse files
committed
Fix pathological backtracking in signature regexp
This commit fixes the pathological backtracking in the regexp so that we don't need the RE2 gem anymore. The problem was the `(\w+\s*){1,3}` part. Since we accept 0 or more spaces, and 1 to 3 times, the regex engine would keep backtracking to figure out if it should do 1, 2, or 3 matches. We know we want at least one match, and it *must* be followed by one or more spaces. If we remove the space before the "y", we can eliminate the backtracking by making at least one space required (as the original regular expression indicated)
1 parent 7ef96a4 commit 94797d3

1 file changed

Lines changed: 2 additions & 8 deletions

File tree

lib/email_reply_parser.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,8 @@ def read(text)
132132

133133
private
134134
EMPTY = "".freeze
135-
SIGNATURE = '(?m)(--\s*$|__\s*$|\w-$)|(^(\w+\s*){1,3} ym morf tneS$)'
136-
137-
begin
138-
require 're2'
139-
SIG_REGEX = RE2::Regexp.new(SIGNATURE)
140-
rescue LoadError
141-
SIG_REGEX = Regexp.new(SIGNATURE)
142-
end
135+
SIGNATURE = '(?m)(--\s*$|__\s*$|\w-$)|(^(\w+\s+){1,3}ym morf tneS$)'
136+
SIG_REGEX = Regexp.new(SIGNATURE)
143137

144138
### Line-by-Line Parsing
145139

0 commit comments

Comments
 (0)