class Diff::LCS::Hunk
def unified_diff
def unified_diff # Calculate item number range. s = encode("@@ -#{unified_range(:old)} +#{unified_range(:new)} @@\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| e.insert(0, encode(' ')) } @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 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] num_added += 1 end end s << outlist.join(encode("\n")) end