class LCS::Hunk

A Hunk is a group of Blocks which overlap because of the context surrounding each block. (So if we’re not using context, every hunk will contain one block.) Used in the diff program (bin/ldiff).

Attributes

blocks[R]
end_new[R]
end_old[R]
file_length_difference[R]
flag_context[RW]

Change the “start” and “end” fields to note that context should be added to this hunk.

start_new[R]
start_old[R]

Public Class Methods

new(data_old, data_new, piece, flag_context, file_length_difference) click to toggle source

Create a hunk using references to both the old and new data, as well as the piece of data.

# File lib/diff/lcs/hunk.rb, line 16
def initialize(data_old, data_new, piece, flag_context, file_length_difference)
  # At first, a hunk will have just one Block in it
  @blocks = [Diff::LCS::Block.new(piece)]

  if @blocks[0].remove.empty? && @blocks[0].insert.empty?
    fail "Cannot build a hunk from #{piece.inspect}; has no add or remove actions"
  end

  if String.method_defined?(:encoding)
    @preferred_data_encoding = data_old.fetch(0) { data_new.fetch(0, "") }.encoding
  end

  @data_old = data_old
  @data_new = data_new

  before = after = file_length_difference
  after += @blocks[0].diff_size
  @file_length_difference = after # The caller must get this manually
  @max_diff_size = @blocks.map { |e| e.diff_size.abs }.max

  # Save the start & end of each array. If the array doesn't exist (e.g.,
  # we're only adding items in this block), then figure out the line number
  # based on the line number of the other file and the current difference in
  # file lengths.
  if @blocks[0].remove.empty?
    a1 = a2 = nil
  else
    a1 = @blocks[0].remove[0].position
    a2 = @blocks[0].remove[-1].position
  end

  if @blocks[0].insert.empty?
    b1 = b2 = nil
  else
    b1 = @blocks[0].insert[0].position
    b2 = @blocks[0].insert[-1].position
  end

  @start_old = a1 || (b1 - before)
  @start_new = b1 || (a1 + before)
  @end_old = a2 || (b2 - after)
  @end_new = b2 || (a2 + after)

  self.flag_context = flag_context
end

Public Instance Methods

diff(format, last = false) click to toggle source

Returns a diff string based on a format.

# File lib/diff/lcs/hunk.rb, line 115
def diff(format, last = false)
  case format
  when :old
    old_diff(last)
  when :unified
    unified_diff(last)
  when :context
    context_diff(last)
  when :ed
    self
  when :reverse_ed, :ed_finish
    ed_diff(format, last)
  else
    fail "Unknown diff format #{format}."
  end
end
merge(hunk) click to toggle source

Merges this hunk and the provided hunk together if they overlap. Returns a truthy value so that if there is no overlap, you can know the merge was skipped.

# File lib/diff/lcs/hunk.rb, line 97
def merge(hunk)
  return unless overlaps?(hunk)

  @start_old = hunk.start_old
  @start_new = hunk.start_new
  blocks.unshift(*hunk.blocks)
end
Also aliased as: unshift
missing_last_newline?(data) click to toggle source
# File lib/diff/lcs/hunk.rb, line 331
def missing_last_newline?(data)
  newline = encode("\n")

  if data[-2]
    data[-2].end_with?(newline) && !data[-1].end_with?(newline)
  elsif data[-1]
    !data[-1].end_with?(newline)
  else
    true
  end
end
overlaps?(hunk) click to toggle source

Determines whether there is an overlap between this hunk and the provided hunk. This will be true if the difference between the two hunks start or end positions is within one position of each other.

# File lib/diff/lcs/hunk.rb, line 109
def overlaps?(hunk)
  hunk and (((@start_old - hunk.end_old) <= 1) or
            ((@start_new - hunk.end_new) <= 1))
end
unshift(hunk)
Alias for: merge