class Diff::LCS::Hunk
def unified_diff(last = false)
def unified_diff(last = false) # Calculate item number range. s = encode("@@ -#{unified_range(:old, last)} +#{unified_range(:new, last)} @@\n") # Outlist starts containing the hunk of the old file. Removing an item # just means putting a '-' in front of it. Inserting an item requires # getting it from the new file and splicing it in. We splice in # +num_added+ items. Remove blocks use +num_added+ because splicing # changed the length of outlist. # # We remove +num_removed+ items. Insert blocks use +num_removed+ # because their item numbers -- corresponding to positions in the NEW # file -- don't take removed items into account. lo, hi, num_added, num_removed = @start_old, @end_old, 0, 0 outlist = @data_old[lo..hi].map { |e| String.new("#{encode(' ')}#{e.chomp}") } last_block = blocks[-1] if last old_missing_newline = missing_last_newline?(@data_old) new_missing_newline = missing_last_newline?(@data_new) end @blocks.each do |block| block.remove.each do |item| op = item.action.to_s # - offset = item.position - lo + num_added outlist[offset][0, 1] = encode(op) num_removed += 1 end if last && block == last_block && old_missing_newline && !new_missing_newline outlist << encode('\\ No newline at end of file') num_removed += 1 end block.insert.each do |item| op = item.action.to_s # + offset = item.position - @start_new + num_removed outlist[offset, 0] = encode(op) + @data_new[item.position].chomp num_added += 1 end end outlist << encode('\\ No newline at end of file') if last && new_missing_newline s << outlist.join(encode("\n")) s end