summaryrefslogtreecommitdiff
path: root/lib/diff/lcs
diff options
context:
space:
mode:
authorAustin Ziegler <austin@zieglers.ca>2019-01-27 00:15:29 -0500
committerAustin Ziegler <austin@zieglers.ca>2019-01-27 23:26:05 -0500
commit45ea1b30c7de0ad840f72afb82fd41eb2b9590db (patch)
tree22f879cbd21a183bac5c17f754c0aa598531d968 /lib/diff/lcs
parent07ed577eba341f0ff0d7eebf4c1c2cc23083cba7 (diff)
downloaddiff-lcs-45ea1b30c7de0ad840f72afb82fd41eb2b9590db.tar.gz
Applied Rubocop rules that I like
- Other linting configuration also applied. - Soft-deprecating versions older than 2.3.
Diffstat (limited to 'lib/diff/lcs')
-rw-r--r--lib/diff/lcs/array.rb2
-rw-r--r--lib/diff/lcs/backports.rb9
-rw-r--r--lib/diff/lcs/block.rb2
-rw-r--r--lib/diff/lcs/callbacks.rb23
-rw-r--r--lib/diff/lcs/change.rb65
-rw-r--r--lib/diff/lcs/htmldiff.rb33
-rw-r--r--lib/diff/lcs/hunk.rb99
-rw-r--r--lib/diff/lcs/internals.rb59
-rw-r--r--lib/diff/lcs/ldiff.rb49
-rw-r--r--lib/diff/lcs/string.rb2
10 files changed, 178 insertions, 165 deletions
diff --git a/lib/diff/lcs/array.rb b/lib/diff/lcs/array.rb
index 1acd8c9..5c250f6 100644
--- a/lib/diff/lcs/array.rb
+++ b/lib/diff/lcs/array.rb
@@ -1,4 +1,4 @@
-# -*- ruby encoding: utf-8 -*-
+# frozen_string_literal: true
require 'diff/lcs'
diff --git a/lib/diff/lcs/backports.rb b/lib/diff/lcs/backports.rb
new file mode 100644
index 0000000..214a59a
--- /dev/null
+++ b/lib/diff/lcs/backports.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+unless 0.respond_to?(:positive?)
+ class Fixnum # rubocop:disable Lint/UnifiedInteger, Style/Documentation
+ def positive?
+ self > 0 # rubocop:disable Styel/NumericPredicate
+ end
+ end
+end
diff --git a/lib/diff/lcs/block.rb b/lib/diff/lcs/block.rb
index 8518727..430702d 100644
--- a/lib/diff/lcs/block.rb
+++ b/lib/diff/lcs/block.rb
@@ -1,4 +1,4 @@
-# -*- ruby encoding: utf-8 -*-
+# frozen_string_literal: true
# A block is an operation removing, adding, or changing a group of items.
# Basically, this is just a list of changes, where each change adds or
diff --git a/lib/diff/lcs/callbacks.rb b/lib/diff/lcs/callbacks.rb
index 8eec5fc..93c860d 100644
--- a/lib/diff/lcs/callbacks.rb
+++ b/lib/diff/lcs/callbacks.rb
@@ -1,8 +1,8 @@
-# -*- ruby encoding: utf-8 -*-
+# frozen_string_literal: true
require 'diff/lcs/change'
-module Diff::LCS
+module Diff::LCS # rubocop:disable Style/Documentation
# This callback object implements the default set of callback events,
# which only returns the event itself. Note that #finished_a and
# #finished_b are not implemented -- I haven't yet figured out where they
@@ -17,14 +17,17 @@ module Diff::LCS
def match(event)
event
end
+
# Called when the old value is discarded in favour of the new value.
def discard_a(event)
event
end
+
# Called when the new value is discarded in favour of the old value.
def discard_b(event)
event
end
+
# Called when both the old and new values have changed.
def change(event)
event
@@ -108,12 +111,12 @@ class Diff::LCS::DiffCallbacks
@hunk = []
@diffs = []
- if block_given?
- begin
- yield self
- ensure
- self.finish
- end
+ return unless block_given?
+
+ begin
+ yield self
+ ensure
+ finish
end
end
@@ -123,7 +126,7 @@ class Diff::LCS::DiffCallbacks
finish_hunk
end
- def match(event)
+ def match(_event)
finish_hunk
end
@@ -241,7 +244,7 @@ end
# will compute and display the necessary components to show two sequences
# and their minimized differences side by side, just like the Unix utility
# +sdiff+.
-#
+#
# same same
# before | after
# old < -
diff --git a/lib/diff/lcs/change.rb b/lib/diff/lcs/change.rb
index 9229069..75e1f84 100644
--- a/lib/diff/lcs/change.rb
+++ b/lib/diff/lcs/change.rb
@@ -1,16 +1,16 @@
-# -*- ruby encoding: utf-8 -*-
+# frozen_string_literal: true
# Represents a simplistic (non-contextual) change. Represents the removal or
# addition of an element from either the old or the new sequenced
# enumerable.
class Diff::LCS::Change
- IntClass = 1.class # Fixnum is deprecated in Ruby 2.4
+ IntClass = 1.class # Fixnum is deprecated in Ruby 2.4 # rubocop:disable Naming/ConstantName
# The only actions valid for changes are '+' (add), '-' (delete), '='
# (no change), '!' (changed), '<' (tail changes from first sequence), or
# '>' (tail changes from second sequence). The last two ('<>') are only
# found with Diff::LCS::diff and Diff::LCS::sdiff.
- VALID_ACTIONS = %W(+ - = ! > <)
+ VALID_ACTIONS = %w(+ - = ! > <).freeze
def self.valid_action?(action)
VALID_ACTIONS.include? action
@@ -27,18 +27,16 @@ class Diff::LCS::Change
def initialize(*args)
@action, @position, @element = *args
- unless Diff::LCS::Change.valid_action?(@action)
- raise "Invalid Change Action '#{@action}'"
- end
- raise "Invalid Position Type" unless @position.kind_of? IntClass
+ fail "Invalid Change Action '#{@action}'" unless Diff::LCS::Change.valid_action?(@action)
+ fail 'Invalid Position Type' unless @position.kind_of? IntClass
end
- def inspect
- to_a.inspect
+ def inspect(*_args)
+ "#<#{self.class}: #{to_a.inspect}>"
end
def to_a
- [ @action, @position, @element ]
+ [@action, @position, @element]
end
def self.from_a(arr)
@@ -49,7 +47,7 @@ class Diff::LCS::Change
when 3
Diff::LCS::Change.new(*(arr[0...3]))
else
- raise "Invalid change array format provided."
+ fail 'Invalid change array format provided.'
end
end
@@ -57,15 +55,15 @@ class Diff::LCS::Change
def ==(other)
(self.class == other.class) and
- (self.action == other.action) and
- (self.position == other.position) and
- (self.element == other.element)
+ (action == other.action) and
+ (position == other.position) and
+ (element == other.element)
end
def <=>(other)
- r = self.action <=> other.action
- r = self.position <=> other.position if r.zero?
- r = self.element <=> other.element if r.zero?
+ r = action <=> other.action
+ r = position <=> other.position if r.zero?
+ r = element <=> other.element if r.zero?
r
end
@@ -114,28 +112,19 @@ class Diff::LCS::ContextChange < Diff::LCS::Change
def initialize(*args)
@action, @old_position, @old_element, @new_position, @new_element = *args
- unless Diff::LCS::Change.valid_action?(@action)
- raise "Invalid Change Action '#{@action}'"
- end
- unless @old_position.nil? or @old_position.kind_of? IntClass
- raise "Invalid (Old) Position Type"
- end
- unless @new_position.nil? or @new_position.kind_of? IntClass
- raise "Invalid (New) Position Type"
- end
+ fail "Invalid Change Action '#{@action}'" unless Diff::LCS::Change.valid_action?(@action)
+ fail 'Invalid (Old) Position Type' unless @old_position.nil? or @old_position.kind_of? IntClass
+ fail 'Invalid (New) Position Type' unless @new_position.nil? or @new_position.kind_of? IntClass
end
def to_a
- [ @action,
- [ @old_position, @old_element ],
- [ @new_position, @new_element ]
+ [
+ @action,
+ [@old_position, @old_element],
+ [@new_position, @new_element]
]
end
- def inspect(*args)
- to_a.inspect
- end
-
def self.from_a(arr)
Diff::LCS::Change.from_a(arr)
end
@@ -163,11 +152,11 @@ class Diff::LCS::ContextChange < Diff::LCS::Change
def ==(other)
(self.class == other.class) and
- (@action == other.action) and
- (@old_position == other.old_position) and
- (@new_position == other.new_position) and
- (@old_element == other.old_element) and
- (@new_element == other.new_element)
+ (@action == other.action) and
+ (@old_position == other.old_position) and
+ (@new_position == other.new_position) and
+ (@old_element == other.old_element) and
+ (@new_element == other.new_element)
end
def <=>(other)
diff --git a/lib/diff/lcs/htmldiff.rb b/lib/diff/lcs/htmldiff.rb
index 8b79994..f12220b 100644
--- a/lib/diff/lcs/htmldiff.rb
+++ b/lib/diff/lcs/htmldiff.rb
@@ -1,14 +1,15 @@
-# -*- ruby encoding: utf-8 -*-
+# frozen_string_literal: true
require 'cgi'
+# Produce a simple HTML diff view.
class Diff::LCS::HTMLDiff
class << self
attr_accessor :can_expand_tabs #:nodoc:
end
self.can_expand_tabs = true
- class Callbacks
+ class Callbacks #:nodoc:
attr_accessor :output
attr_accessor :match_class
attr_accessor :only_a_class
@@ -18,14 +19,14 @@ class Diff::LCS::HTMLDiff
@output = output
options ||= {}
- @match_class = options[:match_class] || "match"
- @only_a_class = options[:only_a_class] || "only_a"
- @only_b_class = options[:only_b_class] || "only_b"
+ @match_class = options[:match_class] || 'match'
+ @only_a_class = options[:only_a_class] || 'only_a'
+ @only_b_class = options[:only_b_class] || 'only_b'
end
def htmlize(element, css_class)
- element = "&nbsp;" if element.empty?
- %Q|<pre class="#{__send__(css_class)}">#{element}</pre>\n|
+ element = '&nbsp;' if element.empty?
+ %Q(<pre class="#{__send__(css_class)}">#{element}</pre>\n)
end
private :htmlize
@@ -49,8 +50,8 @@ class Diff::LCS::HTMLDiff
:expand_tabs => nil,
:output => nil,
:css => nil,
- :title => nil,
- }
+ :title => nil
+ }.freeze
DEFAULT_CSS = <<-CSS
body { margin: 0; }
@@ -96,13 +97,13 @@ h1 { margin-left: 2em; }
def verify_options
@options[:expand_tabs] ||= 4
- @options[:expand_tabs] = 4 if @options[:expand_tabs] < 0
+ @options[:expand_tabs] = 4 if @options[:expand_tabs].negative?
@options[:output] ||= $stdout
@options[:css] ||= DEFAULT_CSS.dup
- @options[:title] ||= "diff"
+ @options[:title] ||= 'diff'
end
private :verify_options
@@ -111,16 +112,16 @@ h1 { margin-left: 2em; }
def run
verify_options
- if @options[:expand_tabs] > 0 && self.class.can_expand_tabs
+ if @options[:expand_tabs].positive? && self.class.can_expand_tabs
formatter = Text::Format.new
formatter.tabstop = @options[:expand_tabs]
- @left.map! { |line| formatter.expand(line.chomp) }
- @right.map! { |line| formatter.expand(line.chomp) }
+ @left.map! do |line| formatter.expand(line.chomp) end
+ @right.map! do |line| formatter.expand(line.chomp) end
end
- @left.map! { |line| CGI.escapeHTML(line.chomp) }
- @right.map! { |line| CGI.escapeHTML(line.chomp) }
+ @left.map! do |line| CGI.escapeHTML(line.chomp) end
+ @right.map! do |line| CGI.escapeHTML(line.chomp) end
@options[:output] << <<-OUTPUT
<html>
diff --git a/lib/diff/lcs/hunk.rb b/lib/diff/lcs/hunk.rb
index 05c3fb6..58a68db 100644
--- a/lib/diff/lcs/hunk.rb
+++ b/lib/diff/lcs/hunk.rb
@@ -1,4 +1,4 @@
-# -*- ruby encoding: utf-8 -*-
+# frozen_string_literal: true
require 'diff/lcs/block'
@@ -10,9 +10,9 @@ class Diff::LCS::Hunk
# the piece of data.
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) ]
+ @blocks = [Diff::LCS::Block.new(piece)]
if String.method_defined?(:encoding)
- @preferred_data_encoding = data_old.fetch(0, data_new.fetch(0,'') ).encoding
+ @preferred_data_encoding = data_old.fetch(0, data_new.fetch(0, '')).encoding
end
@data_old = data_old
@data_new = data_new
@@ -55,19 +55,21 @@ class Diff::LCS::Hunk
# Change the "start" and "end" fields to note that context should be added
# to this hunk.
attr_accessor :flag_context
- undef :flag_context=;
- def flag_context=(context) #:nodoc:
+ undef :flag_context=
+ def flag_context=(context) #:nodoc: # rubocop:disable Lint/DuplicateMethods
return if context.nil? or context.zero?
- add_start = (context > @start_old) ? @start_old : context
+ add_start = context > @start_old ? @start_old : context
@start_old -= add_start
@start_new -= add_start
- if (@end_old + context) > @data_old.size
- add_end = @data_old.size - @end_old
- else
- add_end = context
- end
+ add_end =
+ if (@end_old + context) > @data_old.size
+ @data_old.size - @end_old
+ else
+ context
+ end
+
@end_old += add_end
@end_new += add_end
end
@@ -76,15 +78,13 @@ class Diff::LCS::Hunk
# a truthy value so that if there is no overlap, you can know the merge
# was skipped.
def merge(hunk)
- if overlaps?(hunk)
- @start_old = hunk.start_old
- @start_new = hunk.start_new
- blocks.unshift(*hunk.blocks)
- else
- nil
- end
+ return unless overlaps?(hunk)
+
+ @start_old = hunk.start_old
+ @start_new = hunk.start_new
+ blocks.unshift(*hunk.blocks)
end
- alias_method :unshift, :merge
+ alias unshift merge
# Determines whether there is an overlap between this hunk and the
# provided hunk. This will be true if the difference between the two hunks
@@ -108,15 +108,15 @@ class Diff::LCS::Hunk
when :reverse_ed, :ed_finish
ed_diff(format)
else
- raise "Unknown diff format #{format}."
+ fail "Unknown diff format #{format}."
end
end
# Note that an old diff can't have any context. Therefore, we know that
# there's only one block in the hunk.
def old_diff
- warn "Expecting only one block in an old diff hunk!" if @blocks.size > 1
- op_act = { "+" => 'a', "-" => 'd', "!" => "c" }
+ warn 'Expecting only one block in an old diff hunk!' if @blocks.size > 1
+ op_act = { '+' => 'a', '-' => 'd', '!' => 'c' }
block = @blocks[0]
@@ -126,9 +126,16 @@ class Diff::LCS::Hunk
s = encode("#{context_range(:old)}#{op_act[block.op]}#{context_range(:new)}\n")
# If removing anything, just print out all the remove lines in the hunk
# which is just all the remove lines in the block.
- @data_old[@start_old .. @end_old].each { |e| s << encode("< ") + e + encode("\n") } unless block.remove.empty?
- s << encode("---\n") if block.op == "!"
- @data_new[@start_new .. @end_new].each { |e| s << encode("> ") + e + encode("\n") } unless block.insert.empty?
+ unless block.remove.empty?
+ @data_old[@start_old..@end_old].each { |e| s << encode('< ') + e + encode("\n") }
+ end
+
+ s << encode("---\n") if block.op == '!'
+
+ unless block.insert.empty?
+ @data_new[@start_new..@end_new].each { |e| s << encode('> ') + e + encode("\n") }
+ end
+
s
end
private :old_diff
@@ -148,7 +155,7 @@ class Diff::LCS::Hunk
# 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(' ')) }
+ outlist = @data_old[lo..hi].map { |e| e.insert(0, encode(' ')) }
@blocks.each do |block|
block.remove.each do |item|
@@ -177,9 +184,9 @@ class Diff::LCS::Hunk
# Print out file 1 part for each block in context diff format if there
# are any blocks that remove items
lo, hi = @start_old, @end_old
- removes = @blocks.select { |e| not e.remove.empty? }
+ removes = @blocks.reject { |e| e.remove.empty? }
if removes
- outlist = @data_old[lo .. hi].map { |e| e.insert(0, encode(' ')) }
+ outlist = @data_old[lo..hi].map { |e| e.insert(0, encode(' ')) }
removes.each do |block|
block.remove.each do |item|
@@ -191,9 +198,9 @@ class Diff::LCS::Hunk
s << encode("\n--- #{r} ----\n")
lo, hi = @start_new, @end_new
- inserts = @blocks.select { |e| not e.insert.empty? }
+ inserts = @blocks.reject { |e| e.insert.empty? }
if inserts
- outlist = @data_new[lo .. hi].collect { |e| e.insert(0, encode(' ')) }
+ outlist = @data_new[lo..hi].collect { |e| e.insert(0, encode(' ')) }
inserts.each do |block|
block.insert.each do |item|
outlist[item.position - lo][0, 1] = encode(block.op) # + or !
@@ -206,17 +213,18 @@ class Diff::LCS::Hunk
private :context_diff
def ed_diff(format)
- op_act = { "+" => 'a', "-" => 'd', "!" => "c" }
- warn "Expecting only one block in an old diff hunk!" if @blocks.size > 1
-
- if format == :reverse_ed
- s = encode("#{op_act[@blocks[0].op]}#{context_range(:old)}\n")
- else
- s = encode("#{context_range(:old, ' ')}#{op_act[@blocks[0].op]}\n")
- end
+ op_act = { '+' => 'a', '-' => 'd', '!' => 'c' }
+ warn 'Expecting only one block in an old diff hunk!' if @blocks.size > 1
+
+ s =
+ if format == :reverse_ed
+ encode("#{op_act[@blocks[0].op]}#{context_range(:old)}\n")
+ else
+ encode("#{context_range(:old, ' ')}#{op_act[@blocks[0].op]}\n")
+ end
unless @blocks[0].insert.empty?
- @data_new[@start_new .. @end_new].each { |e| s << e + encode("\n") }
+ @data_new[@start_new..@end_new].each do |e| s << e + encode("\n") end
s << encode(".\n")
end
s
@@ -225,7 +233,7 @@ class Diff::LCS::Hunk
# Generate a range of item numbers to print. Only print 1 number if the
# range has only one item in it. Otherwise, it's 'start,end'
- def context_range(mode, op = ',')
+ def context_range(mode, op = ',') # rubocop:disable Naming/UncommunicativeMethodParamName
case mode
when :old
s, e = (@start_old + 1), (@end_old + 1)
@@ -233,7 +241,7 @@ class Diff::LCS::Hunk
s, e = (@start_new + 1), (@end_new + 1)
end
- (s < e) ? "#{s}#{op}#{e}" : "#{e}"
+ s < e ? "#{s}#{op}#{e}" : e.to_s
end
private :context_range
@@ -249,8 +257,8 @@ class Diff::LCS::Hunk
end
length = e - s + 1
- first = (length < 2) ? e : s # "strange, but correct"
- (length == 1) ? "#{first}" : "#{first},#{length}"
+ first = length < 2 ? e : s # "strange, but correct"
+ length == 1 ? first.to_s : "#{first},#{length}"
end
private :unified_range
@@ -263,10 +271,11 @@ class Diff::LCS::Hunk
args.map { |arg| arg.encode(string.encoding) }
end
else
- def encode(literal, target_encoding = nil)
+ def encode(literal, _target_encoding = nil)
literal
end
- def encode_as(string, *args)
+
+ def encode_as(_string, *args)
args
end
end
diff --git a/lib/diff/lcs/internals.rb b/lib/diff/lcs/internals.rb
index 17d1d06..c3360d5 100644
--- a/lib/diff/lcs/internals.rb
+++ b/lib/diff/lcs/internals.rb
@@ -1,4 +1,4 @@
-# -*- ruby encoding: utf-8 -*-
+# frozen_string_literal: true
class << Diff::LCS
def diff_traversal(method, seq1, seq2, callbacks, &block)
@@ -45,8 +45,7 @@ class << Diff::LCS::Internals
vector = []
# Prune off any common elements at the beginning...
- while ((a_start <= a_finish) and (b_start <= b_finish) and
- (a[a_start] == b[b_start]))
+ while (a_start <= a_finish) and (b_start <= b_finish) and (a[a_start] == b[b_start])
vector[a_start] = b_start
a_start += 1
b_start += 1
@@ -54,8 +53,7 @@ class << Diff::LCS::Internals
b_start = a_start
# Now the end...
- while ((a_start <= a_finish) and (b_start <= b_finish) and
- (a[a_finish] == b[b_finish]))
+ while (a_start <= a_finish) and (b_start <= b_finish) and (a[a_finish] == b[b_finish])
vector[a_finish] = b_finish
a_finish -= 1
b_finish -= 1
@@ -68,7 +66,7 @@ class << Diff::LCS::Internals
links = []
string = a.kind_of?(String)
- (a_start .. a_finish).each do |i|
+ (a_start..a_finish).each do |i|
ai = string ? a[i, 1] : a[i]
bm = b_matches[ai]
k = nil
@@ -78,13 +76,13 @@ class << Diff::LCS::Internals
else
k = replace_next_larger(thresh, j, k)
end
- links[k] = [ (k > 0) ? links[k - 1] : nil, i, j ] unless k.nil?
+ links[k] = [k.positive? ? links[k - 1] : nil, i, j] unless k.nil?
end
end
unless thresh.empty?
link = links[thresh.size - 1]
- while not link.nil?
+ until link.nil?
vector[link[1]] = link[2]
link = link[0]
end
@@ -98,7 +96,7 @@ class << Diff::LCS::Internals
# Diff::LCS::Change objects to the object form of same) and detection of
# whether the patchset represents changes to be made.
def analyze_patchset(patchset, depth = 0)
- raise "Patchset too complex" if depth > 1
+ fail 'Patchset too complex' if depth > 1
has_changes = false
@@ -110,7 +108,7 @@ class << Diff::LCS::Internals
# ]
# ]
- patchset = patchset.map do |hunk|
+ patchset = patchset.map { |hunk|
case hunk
when Diff::LCS::Change
has_changes ||= !hunk.unchanged?
@@ -128,11 +126,11 @@ class << Diff::LCS::Internals
hunk.flatten
end
else
- raise ArgumentError, "Cannot normalise a hunk of class #{hunk.class}."
+ fail ArgumentError, "Cannot normalise a hunk of class #{hunk.class}."
end
- end
+ }
- [ has_changes, patchset.flatten(1) ]
+ [has_changes, patchset.flatten(1)]
end
# Examine the patchset and the source to see in which direction the
@@ -173,13 +171,11 @@ class << Diff::LCS::Internals
when '!'
if le == change.old_element
left_match += 1
+ elsif re == change.new_element
+ right_match += 1
else
- if re == change.new_element
- right_match += 1
- else
- left_miss += 1
- right_miss += 1
- end
+ left_miss += 1
+ right_miss += 1
end
end
when Diff::LCS::Change
@@ -209,16 +205,16 @@ class << Diff::LCS::Internals
end
end
- break if (not limit.nil?) && (count > limit)
+ break if !limit.nil? && (count > limit)
end
- no_left = (left_match == 0) && (left_miss > 0)
- no_right = (right_match == 0) && (right_miss > 0)
+ no_left = left_match.zero? && left_miss.positive?
+ no_right = right_match.zero? && right_miss.positive?
- case [ no_left, no_right ]
- when [ false, true ]
+ case [no_left, no_right]
+ when [false, true]
:patch
- when [ true, false ]
+ when [true, false]
:unpatch
else
case left_match <=> right_match
@@ -235,7 +231,8 @@ class << Diff::LCS::Internals
:patch
end
else
- raise "The provided patchset does not appear to apply to the provided enumerable as either source or destination value."
+ fail "The provided patchset does not appear to apply to the provided \
+enumerable as either source or destination value."
end
end
end
@@ -258,14 +255,14 @@ class << Diff::LCS::Internals
# Binary search for the insertion point
last_index ||= enum.size
first_index = 0
- while (first_index <= last_index)
+ while first_index <= last_index
i = (first_index + last_index) >> 1
found = enum[i]
- if value == found
- return nil
- elsif value > found
+ return nil if value == found
+
+ if value > found
first_index = i + 1
else
last_index = i - 1
@@ -275,7 +272,7 @@ class << Diff::LCS::Internals
# The insertion point is in first_index; overwrite the next larger
# value.
enum[first_index] = value
- return first_index
+ first_index
end
private :replace_next_larger
diff --git a/lib/diff/lcs/ldiff.rb b/lib/diff/lcs/ldiff.rb
index c789f46..d385f72 100644
--- a/lib/diff/lcs/ldiff.rb
+++ b/lib/diff/lcs/ldiff.rb
@@ -1,4 +1,4 @@
-# -*- ruby encoding: utf-8 -*-
+# frozen_string_literal: true
require 'optparse'
require 'ostruct'
@@ -7,7 +7,7 @@ require 'diff/lcs/hunk'
module Diff::LCS::Ldiff #:nodoc:
BANNER = <<-COPYRIGHT
ldiff #{Diff::LCS::VERSION}
- Copyright 2004-2014 Austin Ziegler
+ Copyright 2004-2019 Austin Ziegler
Part of Diff::LCS.
https://github.com/halostatue/diff-lcs
@@ -15,7 +15,7 @@ ldiff #{Diff::LCS::VERSION}
This program is free software. It may be redistributed and/or modified under
the terms of the GPL version 2 (or later), the Perl Artistic licence, or the
MIT licence.
-COPYRIGHT
+ COPYRIGHT
end
class << Diff::LCS::Ldiff
@@ -23,33 +23,42 @@ class << Diff::LCS::Ldiff
attr_reader :file_old, :file_new #:nodoc:
attr_reader :data_old, :data_new #:nodoc:
- def run(args, input = $stdin, output = $stdout, error = $stderr) #:nodoc:
+ def run(args, _input = $stdin, output = $stdout, error = $stderr) #:nodoc:
@binary = nil
args.options do |o|
o.banner = "Usage: #{File.basename($0)} [options] oldfile newfile"
- o.separator ""
- o.on('-c', '-C', '--context [LINES]', Numeric, 'Displays a context diff with LINES lines', 'of context. Default 3 lines.') do |ctx|
+ o.separator ''
+ o.on(
+ '-c', '-C', '--context [LINES]', Numeric,
+ 'Displays a context diff with LINES lines', 'of context. Default 3 lines.'
+ ) do |ctx|
@format = :context
@lines = ctx || 3
end
- o.on('-u', '-U', '--unified [LINES]', Numeric, 'Displays a unified diff with LINES lines', 'of context. Default 3 lines.') do |ctx|
+ o.on(
+ '-u', '-U', '--unified [LINES]', Numeric,
+ 'Displays a unified diff with LINES lines', 'of context. Default 3 lines.'
+ ) do |ctx|
@format = :unified
@lines = ctx || 3
end
- o.on('-e', 'Creates an \'ed\' script to change', 'oldfile to newfile.') do |ctx|
+ o.on('-e', 'Creates an \'ed\' script to change', 'oldfile to newfile.') do |_ctx|
@format = :ed
end
- o.on('-f', 'Creates an \'ed\' script to change', 'oldfile to newfile in reverse order.') do |ctx|
+ o.on('-f', 'Creates an \'ed\' script to change', 'oldfile to newfile in reverse order.') do |_ctx|
@format = :reverse_ed
end
- o.on('-a', '--text', 'Treat the files as text and compare them', 'line-by-line, even if they do not seem', 'to be text.') do |txt|
+ o.on(
+ '-a', '--text',
+ 'Treat the files as text and compare them', 'line-by-line, even if they do not seem', 'to be text.'
+ ) do |_txt|
@binary = false
end
- o.on('--binary', 'Treats the files as binary.') do |bin|
+ o.on('--binary', 'Treats the files as binary.') do |_bin|
@binary = true
end
- o.on('-q', '--brief', 'Report only whether or not the files', 'differ, not the details.') do |ctx|
+ o.on('-q', '--brief', 'Report only whether or not the files', 'differ, not the details.') do |_ctx|
@format = :report
end
o.on_tail('--help', 'Shows this text.') do
@@ -60,7 +69,7 @@ class << Diff::LCS::Ldiff
error << Diff::LCS::Ldiff::BANNER
return 0
end
- o.on_tail ""
+ o.on_tail ''
o.on_tail 'By default, runs produces an "old-style" diff, with output like UNIX diff.'
o.parse!
end
@@ -97,8 +106,7 @@ class << Diff::LCS::Ldiff
if @binary.nil?
old_txt = data_old[0, 4096].scan(/\0/).empty?
new_txt = data_new[0, 4096].scan(/\0/).empty?
- @binary = (not old_txt) or (not new_txt)
- old_txt = new_txt = nil
+ @binary = !old_txt or !new_txt
end
unless @binary
@@ -143,12 +151,11 @@ class << Diff::LCS::Ldiff
diffs.each do |piece|
begin
- hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, @lines,
- file_length_difference)
+ hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, @lines, file_length_difference)
file_length_difference = hunk.file_length_difference
next unless oldhunk
- next if (@lines > 0) and hunk.merge(oldhunk)
+ next if @lines.postive? and hunk.merge(oldhunk)
output << oldhunk.diff(@format) << "\n"
ensure
@@ -158,10 +165,8 @@ class << Diff::LCS::Ldiff
output << oldhunk.diff(@format) << "\n"
- if @format == :ed
- output.reverse_each { |e| real_output << e.diff(:ed_finish) }
- end
+ output.reverse_each { |e| real_output << e.diff(:ed_finish) } if @format == :ed
- return 1
+ 1
end
end
diff --git a/lib/diff/lcs/string.rb b/lib/diff/lcs/string.rb
index 8545bcf..9ab32e9 100644
--- a/lib/diff/lcs/string.rb
+++ b/lib/diff/lcs/string.rb
@@ -1,4 +1,4 @@
-# -*- ruby encoding: utf-8 -*-
+# frozen_string_literal: true
class String
include Diff::LCS