class Diff

module Aio::Base::Toolkit

Attributes

input_1[RW]

输入文件1

input_2[RW]

输入文件2

Public Class Methods

new(input_1, input_2) click to toggle source
# File lib/aio/base/toolkit/diff.rb, line 12
def initialize(input_1, input_2)
        self.input_1 = File.open(input_1)
        self.input_2 = File.open(input_2)
        @context_name = "context"
end

Public Instance Methods

context_name=(var) click to toggle source
# File lib/aio/base/toolkit/diff.rb, line 88
def context_name=(var)
        @context_name = var
end
diff() click to toggle source
# File lib/aio/base/toolkit/diff.rb, line 18
def diff
        in_1 = @input_1.dup
        in_2 = @input_2.dup.readlines
        # 行计数从1开始
        line_count = 0
        res_text = []

        in_1.each_line do |line_1|
                # 加入了区间,如果不是在这个区间内,则忽略
                line_count += 1
                unless @range.nil?
                        if ! @range.include?(line_count)
                                next
                        end
                end

                line_1 = line_1.chomp
                begin
                        line_2 = in_2[line_count - 1].chomp
                rescue EOFError
                        break
                end
                #new_line = "#{@context_name}[#{line_count}].match_block(/"
                new_line = "#{@context_name}.readline_match_block(/"
                
                # 按照单词进行比对
                word_1_arr = line_1.split(' ')
                word_2_arr = line_2.split(' ')

                word_arr = []
                merge_count = []
                word_1_arr.each_with_index do |word_1, i|

                        word_2 = word_2_arr[i]
                        
                        if word_1 == word_2
                                word_arr << inn(word_1)
                        else
                                word_arr << '(.*)'
                                merge_count << i
                        end
                end
                # 合并word_arr中相邻的(.*)
                real_word_arr = []
                word_arr.each_with_index do |word, i|
                        if word != '(.*)'
                                real_word_arr << word
                                next
                        end
                        if word_arr[i+1] == '(.*)'
                                next
                        end
                                # 加入(.*)
                                real_word_arr << word
                end

                new_line << real_word_arr.join(' ')
                new_line << '/)'

                res_text << new_line
        end

        res = res_text.join("\n")
        puts res
end
inn(word) click to toggle source

无害化处理

# File lib/aio/base/toolkit/diff.rb, line 93
def inn(word)
        word.gsub!('/', '\/')
        word.gsub!('(', '\(')
        word.gsub!(')', '\)')
        word.gsub!('.', '\.')
        word
end
set_range(first, last) click to toggle source
# File lib/aio/base/toolkit/diff.rb, line 84
def set_range(first, last)
        @range = Range.new(first, last)
end