lib/emaildiff.rb in diff-0.3.5 vs lib/emaildiff.rb in diff-0.3.6
- old
+ new
@@ -44,62 +44,69 @@
def diff(b)
Diff.new(self, b)
end
def patch_email(diff,starttoken = nil,endtoken = nil)
- newary = nil
- if diff.difftype == String
- newary = diff.difftype.new('')
- else
- newary = diff.difftype.new
- end
+ found_diff = 0
+ newary = diff.difftype.new
ai = 0
bi = 0
diff.diffs.each { |d|
d.each { |mod|
quoted = 0
case mod[0]
when '-'
- if ai < mod[1]
+ if ai < mod[1] && !starttoken.nil? && found_diff == 1
quoted = 1
end
- newary << starttoken if quoted == 1 && !starttoken.nil?
+ newary << starttoken if quoted == 1
while ai < mod[1]
- newary << diff.orig_b[bi] if !starttoken.nil?
+ newary << diff.orig_b[bi] if quoted == 1 || found_diff == 0
ai += 1
bi += 1
end
- newary << endtoken if quoted == 1 && !starttoken.nil?
+ newary << endtoken if quoted == 1
ai += 1
when '+'
- if bi < mod[1]
+ if bi < mod[1] && !starttoken.nil? && found_diff == 1
quoted = 1
end
- newary << starttoken if quoted == 1 && !starttoken.nil?
+ newary << starttoken if quoted == 1
while bi < mod[1]
- newary << diff.orig_b[bi] if !starttoken.nil?
+ newary << diff.orig_b[bi] if quoted == 1 || found_diff == 0
ai += 1
bi += 1
end
- newary << endtoken if quoted == 1 && !starttoken.nil?
+ newary << endtoken if quoted == 1
newary << diff.orig_b[mod[1]]
+ found_diff = 1
bi += 1
else
raise "Unknown diff action"
end
}
}
quoted = 0
- if ai < self.length
+ if ai < self.length && !starttoken.nil? && found_diff == 1
quoted = 1
end
- newary << starttoken if quoted == 1 && !starttoken.nil?
+ newary << starttoken if quoted == 1
while ai < self.length
- newary << diff.orig_b[bi] if !starttoken.nil?
+ newary << diff.orig_b[bi] if quoted == 1 or found_diff == 0
ai += 1
bi += 1
end
- newary << endtoken if quoted == 1 && !starttoken.nil?
+ newary << endtoken if quoted == 1
+ if found_diff == 0
+ if !starttoken.nil?
+ if newary.length > 0
+ newary.unshift starttoken
+ newary << endtoken
+ end
+ else
+ return diff.difftype.new
+ end
+ end
return newary
end
end
class Array