]> git.cworth.org Git - sup/commitdiff
reduce quote parsing worst-case behavior
authorWilliam Morgan <wmorgan-sup@masanjin.net>
Mon, 8 Jun 2009 18:07:51 +0000 (14:07 -0400)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Mon, 8 Jun 2009 18:07:51 +0000 (14:07 -0400)
Split a quote regex into two to increase performance on certain long strings.

Thanks to Edward Z. Yang.

lib/sup/message.rb

index 5993729ecc290495f70b326731b5f11b3fe251ad..5372fc7d5e6e23f5632a7406add7409a6cf86306 100644 (file)
@@ -26,7 +26,6 @@ class Message
 
   QUOTE_PATTERN = /^\s{0,4}[>|\}]/
   BLOCK_QUOTE_PATTERN = /^-----\s*Original Message\s*----+$/
-  QUOTE_START_PATTERN = /\w.*:$/
   SIG_PATTERN = /(^-- ?$)|(^\s*----------+\s*$)|(^\s*_________+\s*$)|(^\s*--~--~-)|(^\s*--\+\+\*\*==)/
 
   MAX_SIG_DISTANCE = 15 # lines from the end
@@ -449,7 +448,11 @@ private
       when :text
         newstate = nil
 
-        if line =~ QUOTE_PATTERN || (line =~ QUOTE_START_PATTERN && nextline =~ QUOTE_PATTERN)
+        ## the following /:$/ followed by /\w/ is an attempt to detect the
+        ## start of a quote. this is split into two regexen because the
+        ## original regex /\w.*:$/ had very poor behavior on long lines
+        ## like ":a:a:a:a:a" that occurred in certain emails.
+        if line =~ QUOTE_PATTERN || (line =~ /:$/ && line =~ /\w/ && nextline =~ QUOTE_PATTERN)
           newstate = :quote
         elsif line =~ SIG_PATTERN && (lines.length - i) < MAX_SIG_DISTANCE
           newstate = :sig