@@ -42,7 +42,7 @@ def initialize(string1, string2, options = {})
42
42
43
43
def diff
44
44
@diff ||= begin
45
- paths = case options [ :source ]
45
+ @ paths = case options [ :source ]
46
46
when 'strings'
47
47
[ tempfile ( string1 ) , tempfile ( string2 ) ]
48
48
when 'files'
@@ -51,10 +51,10 @@ def diff
51
51
52
52
if WINDOWS
53
53
# don't use open3 on windows
54
- cmd = sprintf '"%s" %s %s' , diff_bin , diff_options . join ( ' ' ) , paths . map { |s | %("#{ s } ") } . join ( ' ' )
54
+ cmd = sprintf '"%s" %s %s' , diff_bin , diff_options . join ( ' ' ) , @ paths. map { |s | %("#{ s } ") } . join ( ' ' )
55
55
diff = `#{ cmd } `
56
56
else
57
- diff = Open3 . popen3 ( diff_bin , *( diff_options + paths ) ) { |i , o , e | o . read }
57
+ diff = Open3 . popen3 ( diff_bin , *( diff_options + @ paths) ) { |i , o , e | o . read }
58
58
end
59
59
diff . force_encoding ( 'ASCII-8BIT' ) if diff . respond_to? ( :valid_encoding? ) && !diff . valid_encoding?
60
60
if diff =~ /\A \s *\Z / && !options [ :allow_empty_diff ]
@@ -84,9 +84,20 @@ def diff
84
84
85
85
def each
86
86
lines = case @options [ :include_diff_info ]
87
- when false then diff . split ( "\n " ) . reject { |x | x =~ /^(---|\+ \+ \+ |@@|\\ \\ )/ } . map { |line | line + "\n " }
88
- when true then diff . split ( "\n " ) . map { |line | line + "\n " }
87
+ when false
88
+ # this "primes" the diff and sets up the paths we'll reference below.
89
+ diff
90
+
91
+ # caching this regexp improves the performance of the loop by a
92
+ # considerable amount.
93
+ regexp = /^(--- "?#{ @paths [ 0 ] } "?|\+ \+ \+ "?#{ @paths [ 1 ] } "?|@@|\\ \\ )/
94
+
95
+ diff . split ( "\n " ) . reject { |x | x =~ regexp } . map { |line | line + "\n " }
96
+
97
+ when true
98
+ diff . split ( "\n " ) . map { |line | line + "\n " }
89
99
end
100
+
90
101
if block_given?
91
102
lines . each { |line | yield line }
92
103
else
0 commit comments