Created
March 17, 2010 23:54
-
-
Save thejefflarson/335891 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#diff parsing to side by side differences | |
class Differ | |
attr_reader :rows | |
LINE_REGEX = /^((?:---|[><\\]))/ | |
CONTROL_REGEX = /(\d+(?:,\d+)?)([acd])(\d+(?:,\d+)?)/ | |
def initialize(left_file, right_file) | |
@rows = [] | |
@left_file = left_file | |
@right_file = right_file | |
@curr_control = {} | |
@raw_file = File.open(@left_file).read.split("\n") | |
@raw_file.unshift("") #diffs are 1 indexed | |
@index = 0 | |
get_diff | |
while(@diff.length > 0 && @index <= @raw_file.length) | |
build | |
end | |
end | |
private | |
def get_diff | |
@diff = %x(diff #{@left_file} #{@right_file} --speed-large-files --ignore-all-space --ignore-blank-lines).split("\n") | |
end | |
def build | |
control_string | |
advance | |
diff_string | |
end | |
def control_string | |
return if !@diff.first.match(CONTROL_REGEX) | |
p "control" | |
@index = !@curr_control[:from].nil? ? | |
@curr_control[:from].last.to_i+1 : 0 | |
@diff_line = @diff.shift | |
matches = @diff_line.match(CONTROL_REGEX) | |
@curr_control = {:from => matches[1].split(","), | |
:to => matches[3].split(","), | |
:control => matches[2]} | |
end | |
def diff_string | |
return if !@diff.first.match(LINE_REGEX) | |
@curr_diff = [[],[]] | |
while(!@diff.first.nil? && @diff.first.match(LINE_REGEX)) | |
p "diff" | |
@diff_line = @diff.shift | |
case @diff_line[0,1] | |
when "<" then @curr_diff[0] << @diff_line[2, @diff_line.length - 1] | |
when ">" then @curr_diff[1] << @diff_line[2, @diff_line.length - 1] | |
end | |
end | |
end_range = @curr_diff[0].length > @curr_diff[1].length ? | |
@curr_diff[0].length : @curr_diff[1].length | |
(0..end_range-1).each do |index| | |
@rows << [@curr_diff[0][index] || "", @curr_diff[1][index] || "", @curr_control[:control]] | |
end | |
end | |
def advance | |
while(@index < @curr_control[:from].first.to_i) | |
p "advance" | |
insert_step | |
end | |
end | |
def insert_step | |
@rows << [@raw_file[@index], @raw_file[@index], ""] | |
@index = @index + 1 | |
end | |
class << self | |
def run(directory=".") | |
Differ.new(Dir["#{directory}/*.txt"][0], Dir["#{directory}/*.txt"][1]) | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment