summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAustin Ziegler <austin@zieglers.ca>2022-07-05 15:43:30 -0400
committerGitHub <noreply@github.com>2022-07-05 15:43:30 -0400
commitcab33a91e2894558b962b4b2236a213c6d63c54e (patch)
treefc14bc4c8a44e20026e1252a8cd435907d412ee1 /lib
parent3062997fbbe19cb6099a65a5dbcd0aba7b9b17c7 (diff)
parent0270cb87e09dd444ad6822a777bb7999fd6f6e85 (diff)
downloaddiff-lcs-main.tar.gz
Merge pull request #81 from halostatue/switch-to-standardrb-formattingHEADmain
Switch to standardrb formatting
Diffstat (limited to 'lib')
-rw-r--r--lib/diff-lcs.rb2
-rw-r--r--lib/diff/lcs.rb112
-rw-r--r--lib/diff/lcs/array.rb2
-rw-r--r--lib/diff/lcs/backports.rb4
-rw-r--r--lib/diff/lcs/block.rb8
-rw-r--r--lib/diff/lcs/callbacks.rb16
-rw-r--r--lib/diff/lcs/change.rb38
-rw-r--r--lib/diff/lcs/htmldiff.rb40
-rw-r--r--lib/diff/lcs/hunk.rb57
-rw-r--r--lib/diff/lcs/internals.rb34
-rw-r--r--lib/diff/lcs/ldiff.rb68
11 files changed, 200 insertions, 181 deletions
diff --git a/lib/diff-lcs.rb b/lib/diff-lcs.rb
index 250392f..bc07bf9 100644
--- a/lib/diff-lcs.rb
+++ b/lib/diff-lcs.rb
@@ -1,3 +1,3 @@
# frozen_string_literal: true
-require 'diff/lcs'
+require "diff/lcs"
diff --git a/lib/diff/lcs.rb b/lib/diff/lcs.rb
index 288cfc2..54ef893 100644
--- a/lib/diff/lcs.rb
+++ b/lib/diff/lcs.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-module Diff; end unless defined? Diff # rubocop:disable Style/Documentation
+module Diff; end unless defined? Diff
# == How Diff Works (by Mark-Jason Dominus)
#
@@ -49,13 +49,13 @@ module Diff; end unless defined? Diff # rubocop:disable Style/Documentation
# a x b y c z p d q
# a b c a x b y c z
module Diff::LCS
- VERSION = '1.5.0'
+ VERSION = "1.5.0"
end
-require 'diff/lcs/callbacks'
-require 'diff/lcs/internals'
+require "diff/lcs/callbacks"
+require "diff/lcs/internals"
-module Diff::LCS # rubocop:disable Style/Documentation
+module Diff::LCS
# Returns an Array containing the longest common subsequence(s) between
# +self+ and +other+. See Diff::LCS#lcs.
#
@@ -67,7 +67,7 @@ module Diff::LCS # rubocop:disable Style/Documentation
# identically for key purposes. That is:
#
# O.new('a').eql?(O.new('a')) == true
- def lcs(other, &block) #:yields self[i] if there are matched subsequences:
+ def lcs(other, &block) # :yields: self[i] if there are matched subsequences
Diff::LCS.lcs(self, other, &block)
end
@@ -101,7 +101,7 @@ module Diff::LCS # rubocop:disable Style/Documentation
def patch(patchset)
Diff::LCS.patch(self, patchset)
end
- alias unpatch patch
+ alias_method :unpatch, :patch
# Attempts to patch +self+ with the provided +patchset+. A new sequence based
# on +self+ and the +patchset+ will be created. See Diff::LCS#patch. Does no
@@ -141,11 +141,11 @@ module Diff::LCS # rubocop:disable Style/Documentation
end
class << Diff::LCS
- def lcs(seq1, seq2, &block) #:yields seq1[i] for each matched:
+ def lcs(seq1, seq2, &block) # :yields: seq1[i] for each matched
matches = Diff::LCS::Internals.lcs(seq1, seq2)
ret = []
- string = seq1.kind_of? String
- matches.each_with_index do |_e, i|
+ string = seq1.is_a? String
+ matches.each_index do |i|
next if matches[i].nil?
v = string ? seq1[i, 1] : seq1[i]
@@ -154,7 +154,7 @@ class << Diff::LCS
end
ret
end
- alias LCS lcs
+ alias_method :LCS, :lcs
# #diff computes the smallest set of additions and deletions necessary to
# turn the first sequence into the second, and returns a description of these
@@ -165,7 +165,7 @@ class << Diff::LCS
# Class argument is provided for +callbacks+, #diff will attempt to
# initialise it. If the +callbacks+ object (possibly initialised) responds to
# #finish, it will be called.
- def diff(seq1, seq2, callbacks = nil, &block) # :yields diff changes:
+ def diff(seq1, seq2, callbacks = nil, &block) # :yields: diff changes
diff_traversal(:diff, seq1, seq2, callbacks || Diff::LCS::DiffCallbacks, &block)
end
@@ -197,7 +197,7 @@ class << Diff::LCS
# # insert
# end
# end
- def sdiff(seq1, seq2, callbacks = nil, &block) #:yields diff changes:
+ def sdiff(seq1, seq2, callbacks = nil, &block) # :yields: diff changes
diff_traversal(:sdiff, seq1, seq2, callbacks || Diff::LCS::SDiffCallbacks, &block)
end
@@ -282,12 +282,12 @@ class << Diff::LCS
# <tt>callbacks#discard_b</tt> will be called after the end of the sequence
# is reached, if +a+ has not yet reached the end of +A+ or +b+ has not yet
# reached the end of +B+.
- def traverse_sequences(seq1, seq2, callbacks = Diff::LCS::SequenceCallbacks) #:yields change events:
+ def traverse_sequences(seq1, seq2, callbacks = Diff::LCS::SequenceCallbacks) # :yields: change events
callbacks ||= Diff::LCS::SequenceCallbacks
matches = Diff::LCS::Internals.lcs(seq1, seq2)
run_finished_a = run_finished_b = false
- string = seq1.kind_of?(String)
+ string = seq1.is_a?(String)
a_size = seq1.size
b_size = seq2.size
@@ -299,7 +299,7 @@ class << Diff::LCS
ax = string ? seq1[ai, 1] : seq1[ai]
bx = string ? seq2[bj, 1] : seq2[bj]
- event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.discard_a(event)
end
@@ -310,13 +310,13 @@ class << Diff::LCS
break unless bj < b_line
bx = string ? seq2[bj, 1] : seq2[bj]
- event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.discard_b(event)
bj += 1
end
bx = string ? seq2[bj, 1] : seq2[bj]
- event = Diff::LCS::ContextChange.new('=', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("=", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.match(event)
bj += 1
@@ -326,13 +326,13 @@ class << Diff::LCS
# The last entry (if any) processed was a match. +ai+ and +bj+ point just
# past the last matching lines in their sequences.
- while (ai < a_size) or (bj < b_size)
+ while (ai < a_size) || (bj < b_size)
# last A?
- if ai == a_size and bj < b_size
- if callbacks.respond_to?(:finished_a) and !run_finished_a
+ if ai == a_size && bj < b_size
+ if callbacks.respond_to?(:finished_a) && !run_finished_a
ax = string ? seq1[-1, 1] : seq1[-1]
bx = string ? seq2[bj, 1] : seq2[bj]
- event = Diff::LCS::ContextChange.new('>', (a_size - 1), ax, bj, bx)
+ event = Diff::LCS::ContextChange.new(">", (a_size - 1), ax, bj, bx)
event = yield event if block_given?
callbacks.finished_a(event)
run_finished_a = true
@@ -340,7 +340,7 @@ class << Diff::LCS
ax = string ? seq1[ai, 1] : seq1[ai]
loop do
bx = string ? seq2[bj, 1] : seq2[bj]
- event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.discard_b(event)
bj += 1
@@ -350,11 +350,11 @@ class << Diff::LCS
end
# last B?
- if bj == b_size and ai < a_size
- if callbacks.respond_to?(:finished_b) and !run_finished_b
+ if bj == b_size && ai < a_size
+ if callbacks.respond_to?(:finished_b) && !run_finished_b
ax = string ? seq1[ai, 1] : seq1[ai]
bx = string ? seq2[-1, 1] : seq2[-1]
- event = Diff::LCS::ContextChange.new('<', ai, ax, (b_size - 1), bx)
+ event = Diff::LCS::ContextChange.new("<", ai, ax, (b_size - 1), bx)
event = yield event if block_given?
callbacks.finished_b(event)
run_finished_b = true
@@ -362,7 +362,7 @@ class << Diff::LCS
bx = string ? seq2[bj, 1] : seq2[bj]
loop do
ax = string ? seq1[ai, 1] : seq1[ai]
- event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.discard_a(event)
ai += 1
@@ -374,7 +374,7 @@ class << Diff::LCS
if ai < a_size
ax = string ? seq1[ai, 1] : seq1[ai]
bx = string ? seq2[bj, 1] : seq2[bj]
- event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.discard_a(event)
ai += 1
@@ -383,7 +383,7 @@ class << Diff::LCS
if bj < b_size
ax = string ? seq1[ai, 1] : seq1[ai]
bx = string ? seq2[bj, 1] : seq2[bj]
- event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.discard_b(event)
bj += 1
@@ -478,14 +478,14 @@ class << Diff::LCS
b_size = seq2.size
ai = bj = mb = 0
ma = -1
- string = seq1.kind_of?(String)
+ string = seq1.is_a?(String)
# Process all the lines in the match vector.
loop do
# Find next match indices +ma+ and +mb+
loop do
ma += 1
- break unless ma < matches.size and matches[ma].nil?
+ break unless ma < matches.size && matches[ma].nil?
end
break if ma >= matches.size # end of matches?
@@ -493,36 +493,36 @@ class << Diff::LCS
mb = matches[ma]
# Change(seq2)
- while (ai < ma) or (bj < mb)
+ while (ai < ma) || (bj < mb)
ax = string ? seq1[ai, 1] : seq1[ai]
bx = string ? seq2[bj, 1] : seq2[bj]
case [(ai < ma), (bj < mb)]
when [true, true]
if callbacks.respond_to?(:change)
- event = Diff::LCS::ContextChange.new('!', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("!", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.change(event)
ai += 1
else
- event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.discard_a(event)
ai += 1
ax = string ? seq1[ai, 1] : seq1[ai]
- event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.discard_b(event)
end
bj += 1
when [true, false]
- event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.discard_a(event)
ai += 1
when [false, true]
- event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.discard_b(event)
bj += 1
@@ -532,43 +532,43 @@ class << Diff::LCS
# Match
ax = string ? seq1[ai, 1] : seq1[ai]
bx = string ? seq2[bj, 1] : seq2[bj]
- event = Diff::LCS::ContextChange.new('=', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("=", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.match(event)
ai += 1
bj += 1
end
- while (ai < a_size) or (bj < b_size)
+ while (ai < a_size) || (bj < b_size)
ax = string ? seq1[ai, 1] : seq1[ai]
bx = string ? seq2[bj, 1] : seq2[bj]
case [(ai < a_size), (bj < b_size)]
when [true, true]
if callbacks.respond_to?(:change)
- event = Diff::LCS::ContextChange.new('!', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("!", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.change(event)
ai += 1
else
- event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.discard_a(event)
ai += 1
ax = string ? seq1[ai, 1] : seq1[ai]
- event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.discard_b(event)
end
bj += 1
when [true, false]
- event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.discard_a(event)
ai += 1
when [false, true]
- event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
+ event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
event = yield event if block_given?
callbacks.discard_b(event)
bj += 1
@@ -576,10 +576,12 @@ class << Diff::LCS
end
end
- PATCH_MAP = { #:nodoc:
- :patch => { '+' => '+', '-' => '-', '!' => '!', '=' => '=' }.freeze,
- :unpatch => { '+' => '-', '-' => '+', '!' => '!', '=' => '=' }.freeze
+ # standard:disable Style/HashSyntax
+ PATCH_MAP = { # :nodoc:
+ :patch => {"+" => "+", "-" => "-", "!" => "!", "=" => "="}.freeze,
+ :unpatch => {"+" => "-", "-" => "+", "!" => "!", "=" => "="}.freeze
}.freeze
+ # standard:enable Style/HashSyntax
# Applies a +patchset+ to the sequence +src+ according to the +direction+
# (<tt>:patch</tt> or <tt>:unpatch</tt>), producing a new sequence.
@@ -627,7 +629,7 @@ class << Diff::LCS
return src.respond_to?(:dup) ? src.dup : src unless has_changes
- string = src.kind_of?(String)
+ string = src.is_a?(String)
# Start with a new empty type of the source's class
res = src.class.new
@@ -655,14 +657,14 @@ class << Diff::LCS
end
case action
- when '-' # Remove details from the old string
+ when "-" # Remove details from the old string
while ai < op
res << (string ? src[ai, 1] : src[ai])
ai += 1
bj += 1
end
ai += 1
- when '+'
+ when "+"
while bj < np
res << (string ? src[ai, 1] : src[ai])
ai += 1
@@ -671,7 +673,7 @@ class << Diff::LCS
res << el
bj += 1
- when '='
+ when "="
# This only appears in sdiff output with the SDiff callback.
# Therefore, we only need to worry about dealing with a single
# element.
@@ -679,7 +681,7 @@ class << Diff::LCS
ai += 1
bj += 1
- when '!'
+ when "!"
while ai < op
res << (string ? src[ai, 1] : src[ai])
ai += 1
@@ -693,14 +695,14 @@ class << Diff::LCS
end
when Diff::LCS::Change
case action
- when '-'
+ when "-"
while ai < change.position
res << (string ? src[ai, 1] : src[ai])
ai += 1
bj += 1
end
ai += 1
- when '+'
+ when "+"
while bj < change.position
res << (string ? src[ai, 1] : src[ai])
ai += 1
@@ -736,4 +738,4 @@ class << Diff::LCS
end
end
-require 'diff/lcs/backports'
+require "diff/lcs/backports"
diff --git a/lib/diff/lcs/array.rb b/lib/diff/lcs/array.rb
index 5c250f6..663918a 100644
--- a/lib/diff/lcs/array.rb
+++ b/lib/diff/lcs/array.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-require 'diff/lcs'
+require "diff/lcs"
class Array
include Diff::LCS
diff --git a/lib/diff/lcs/backports.rb b/lib/diff/lcs/backports.rb
index 642fc9c..3d2a768 100644
--- a/lib/diff/lcs/backports.rb
+++ b/lib/diff/lcs/backports.rb
@@ -1,9 +1,9 @@
# frozen_string_literal: true
unless 0.respond_to?(:positive?)
- class Fixnum # rubocop:disable Lint/UnifiedInteger, Style/Documentation
+ class Fixnum # standard:disable Lint/UnifiedInteger
def positive?
- self > 0 # rubocop:disable Style/NumericPredicate
+ self > 0
end
end
end
diff --git a/lib/diff/lcs/block.rb b/lib/diff/lcs/block.rb
index 430702d..226ed6f 100644
--- a/lib/diff/lcs/block.rb
+++ b/lib/diff/lcs/block.rb
@@ -25,13 +25,13 @@ class Diff::LCS::Block
def op
case [@remove.empty?, @insert.empty?]
when [false, false]
- '!'
+ "!"
when [false, true]
- '-'
+ "-"
when [true, false]
- '+'
+ "+"
else # [true, true]
- '^'
+ "^"
end
end
end
diff --git a/lib/diff/lcs/callbacks.rb b/lib/diff/lcs/callbacks.rb
index 2a7665b..2c5a779 100644
--- a/lib/diff/lcs/callbacks.rb
+++ b/lib/diff/lcs/callbacks.rb
@@ -1,8 +1,8 @@
# frozen_string_literal: true
-require 'diff/lcs/change'
+require "diff/lcs/change"
-module Diff::LCS # rubocop:disable Style/Documentation
+module Diff::LCS
# 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
@@ -50,7 +50,9 @@ module Diff::LCS # rubocop:disable Style/Documentation
BalancedCallbacks = DefaultCallbacks
def self.callbacks_for(callbacks)
- callbacks.new rescue callbacks
+ callbacks.new
+ rescue
+ callbacks
end
end
@@ -107,7 +109,7 @@ class Diff::LCS::DiffCallbacks
# Returns the difference set collected during the diff process.
attr_reader :diffs
- def initialize # :yields self:
+ def initialize # :yields: self
@hunk = []
@diffs = []
@@ -131,11 +133,11 @@ class Diff::LCS::DiffCallbacks
end
def discard_a(event)
- @hunk << Diff::LCS::Change.new('-', event.old_position, event.old_element)
+ @hunk << Diff::LCS::Change.new("-", event.old_position, event.old_element)
end
def discard_b(event)
- @hunk << Diff::LCS::Change.new('+', event.new_position, event.new_element)
+ @hunk << Diff::LCS::Change.new("+", event.new_position, event.new_element)
end
def finish_hunk
@@ -302,7 +304,7 @@ class Diff::LCS::SDiffCallbacks
# Returns the difference set collected during the diff process.
attr_reader :diffs
- def initialize #:yields self:
+ def initialize # :yields: self
@diffs = []
yield self if block_given?
end
diff --git a/lib/diff/lcs/change.rb b/lib/diff/lcs/change.rb
index 76faf83..c86f102 100644
--- a/lib/diff/lcs/change.rb
+++ b/lib/diff/lcs/change.rb
@@ -10,7 +10,7 @@ class Diff::LCS::Change
# (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(+ - = ! > <).freeze
+ VALID_ACTIONS = %w[+ - = ! > <].freeze
def self.valid_action?(action)
VALID_ACTIONS.include? action
@@ -28,7 +28,7 @@ class Diff::LCS::Change
@action, @position, @element = *args
fail "Invalid Change Action '#{@action}'" unless Diff::LCS::Change.valid_action?(@action)
- fail 'Invalid Position Type' unless @position.kind_of? IntClass
+ fail "Invalid Position Type" unless @position.is_a? IntClass
end
def inspect(*_args)
@@ -39,7 +39,7 @@ class Diff::LCS::Change
[@action, @position, @element]
end
- alias to_ary to_a
+ alias_method :to_ary, :to_a
def self.from_a(arr)
arr = arr.flatten(1)
@@ -49,7 +49,7 @@ class Diff::LCS::Change
when 3
Diff::LCS::Change.new(*(arr[0...3]))
else
- fail 'Invalid change array format provided.'
+ fail "Invalid change array format provided."
end
end
@@ -70,27 +70,27 @@ class Diff::LCS::Change
end
def adding?
- @action == '+'
+ @action == "+"
end
def deleting?
- @action == '-'
+ @action == "-"
end
def unchanged?
- @action == '='
+ @action == "="
end
def changed?
- @action == '!'
+ @action == "!"
end
def finished_a?
- @action == '>'
+ @action == ">"
end
def finished_b?
- @action == '<'
+ @action == "<"
end
end
@@ -115,8 +115,8 @@ class Diff::LCS::ContextChange < Diff::LCS::Change
@action, @old_position, @old_element, @new_position, @new_element = *args
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
+ fail "Invalid (Old) Position Type" unless @old_position.nil? || @old_position.is_a?(IntClass)
+ fail "Invalid (New) Position Type" unless @new_position.nil? || @new_position.is_a?(IntClass)
end
def to_a
@@ -127,7 +127,7 @@ class Diff::LCS::ContextChange < Diff::LCS::Change
]
end
- alias to_ary to_a
+ alias_method :to_ary, :to_a
def self.from_a(arr)
Diff::LCS::Change.from_a(arr)
@@ -139,15 +139,15 @@ class Diff::LCS::ContextChange < Diff::LCS::Change
ea = event.to_a
case ea[0]
- when '-'
+ when "-"
ea[2][1] = nil
- when '<'
- ea[0] = '-'
+ when "<"
+ ea[0] = "-"
ea[2][1] = nil
- when '+'
+ when "+"
ea[1][1] = nil
- when '>'
- ea[0] = '+'
+ when ">"
+ ea[0] = "+"
ea[1][1] = nil
end
diff --git a/lib/diff/lcs/htmldiff.rb b/lib/diff/lcs/htmldiff.rb
index f12220b..54adc2a 100644
--- a/lib/diff/lcs/htmldiff.rb
+++ b/lib/diff/lcs/htmldiff.rb
@@ -1,15 +1,15 @@
# frozen_string_literal: true
-require 'cgi'
+require "cgi"
# Produce a simple HTML diff view.
class Diff::LCS::HTMLDiff
class << self
- attr_accessor :can_expand_tabs #:nodoc:
+ attr_accessor :can_expand_tabs # :nodoc:
end
self.can_expand_tabs = true
- class Callbacks #:nodoc:
+ class Callbacks # :nodoc:
attr_accessor :output
attr_accessor :match_class
attr_accessor :only_a_class
@@ -19,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?
+ %(<pre class="#{__send__(css_class)}">#{element}</pre>\n)
end
private :htmlize
@@ -46,13 +46,16 @@ class Diff::LCS::HTMLDiff
end
end
+ # standard:disable Style/HashSyntax
DEFAULT_OPTIONS = {
:expand_tabs => nil,
:output => nil,
:css => nil,
:title => nil
}.freeze
+ # standard:enable Style/HashSyntax
+ # standard:disable Layout/HeredocIndentation
DEFAULT_CSS = <<-CSS
body { margin: 0; }
.diff
@@ -86,11 +89,12 @@ pre
}
h1 { margin-left: 2em; }
CSS
+ # standard:enable Layout/HeredocIndentation
def initialize(left, right, options = nil)
- @left = left
- @right = right
- @options = options
+ @left = left
+ @right = right
+ @options = options
@options = DEFAULT_OPTIONS.dup if @options.nil?
end
@@ -103,7 +107,7 @@ h1 { margin-left: 2em; }
@options[:css] ||= DEFAULT_CSS.dup
- @options[:title] ||= 'diff'
+ @options[:title] ||= "diff"
end
private :verify_options
@@ -116,13 +120,14 @@ h1 { margin-left: 2em; }
formatter = Text::Format.new
formatter.tabstop = @options[:expand_tabs]
- @left.map! do |line| formatter.expand(line.chomp) end
- @right.map! do |line| formatter.expand(line.chomp) end
+ @left.map! { |line| formatter.expand(line.chomp) }
+ @right.map! { |line| formatter.expand(line.chomp) }
end
- @left.map! do |line| CGI.escapeHTML(line.chomp) end
- @right.map! do |line| CGI.escapeHTML(line.chomp) end
+ @left.map! { |line| CGI.escapeHTML(line.chomp) }
+ @right.map! { |line| CGI.escapeHTML(line.chomp) }
+ # standard:disable Layout/HeredocIndentation
@options[:output] << <<-OUTPUT
<html>
<head>
@@ -137,14 +142,17 @@ h1 { margin-left: 2em; }
<span class="only_b">Only in New</span></p>
<div class="diff">
OUTPUT
+ # standard:enable Layout/HeredocIndentation
callbacks = Callbacks.new(@options[:output])
Diff::LCS.traverse_sequences(@left, @right, callbacks)
+ # standard:disable Layout/HeredocIndentation
@options[:output] << <<-OUTPUT
</div>
</body>
</html>
OUTPUT
+ # standard:enable Layout/HeredocIndentation
end
end
diff --git a/lib/diff/lcs/hunk.rb b/lib/diff/lcs/hunk.rb
index 49b520e..2cef5ed 100644
--- a/lib/diff/lcs/hunk.rb
+++ b/lib/diff/lcs/hunk.rb
@@ -1,13 +1,13 @@
# frozen_string_literal: true
-require 'diff/lcs/block'
+require "diff/lcs/block"
# 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).
class Diff::LCS::Hunk
- OLD_DIFF_OP_ACTION = { '+' => 'a', '-' => 'd', '!' => 'c' }.freeze #:nodoc:
- ED_DIFF_OP_ACTION = { '+' => 'a', '-' => 'd', '!' => 'c' }.freeze #:nodoc:
+ OLD_DIFF_OP_ACTION = {"+" => "a", "-" => "d", "!" => "c"}.freeze # :nodoc:
+ ED_DIFF_OP_ACTION = {"+" => "a", "-" => "d", "!" => "c"}.freeze # :nodoc:
private_constant :OLD_DIFF_OP_ACTION, :ED_DIFF_OP_ACTION if respond_to?(:private_constant)
@@ -22,7 +22,7 @@ class Diff::LCS::Hunk
end
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
@@ -33,7 +33,6 @@ class Diff::LCS::Hunk
@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
@@ -54,8 +53,8 @@ class Diff::LCS::Hunk
@start_old = a1 || (b1 - before)
@start_new = b1 || (a1 + before)
- @end_old = a2 || (b2 - after)
- @end_new = b2 || (a2 + after)
+ @end_old = a2 || (b2 - after)
+ @end_new = b2 || (a2 + after)
self.flag_context = flag_context
end
@@ -67,10 +66,10 @@ class Diff::LCS::Hunk
# Change the "start" and "end" fields to note that context should be added
# to this hunk.
- attr_accessor :flag_context # rubocop:disable Layout/EmptyLinesAroundAttributeAccessor
+ attr_accessor :flag_context
undef :flag_context=
- def flag_context=(context) #:nodoc: # rubocop:disable Lint/DuplicateMethods
- return if context.nil? or context.zero?
+ def flag_context=(context) # :nodoc: # standard:disable Lint/DuplicateMethods
+ return if context.nil? || context.zero?
add_start = context > @start_old ? @start_old : context
@@ -102,7 +101,7 @@ class Diff::LCS::Hunk
@start_new = hunk.start_new
blocks.unshift(*hunk.blocks)
end
- alias unshift merge
+ alias_method :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
@@ -133,24 +132,24 @@ class Diff::LCS::Hunk
# 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(_last = false)
- warn 'Expecting only one block in an old diff hunk!' if @blocks.size > 1
+ warn "Expecting only one block in an old diff hunk!" if @blocks.size > 1
block = @blocks[0]
# Calculate item number range. Old diff range is just like a context
# diff range, except the ranges are on one line with the action between
# them.
- s = encode("#{context_range(:old, ',')}#{OLD_DIFF_OP_ACTION[block.op]}#{context_range(:new, ',')}\n")
+ s = encode("#{context_range(:old, ",")}#{OLD_DIFF_OP_ACTION[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.
unless block.remove.empty?
- @data_old[@start_old..@end_old].each { |e| s << encode('< ') + e.chomp + encode("\n") }
+ @data_old[@start_old..@end_old].each { |e| s << encode("< ") + e.chomp + encode("\n") }
end
- s << encode("---\n") if block.op == '!'
+ s << encode("---\n") if block.op == "!"
unless block.insert.empty?
- @data_new[@start_new..@end_new].each { |e| s << encode('> ') + e.chomp + encode("\n") }
+ @data_new[@start_new..@end_new].each { |e| s << encode("> ") + e.chomp + encode("\n") }
end
s
@@ -172,7 +171,9 @@ 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| String.new("#{encode(' ')}#{e.chomp}") }
+ # standard:disable Performance/UnfreezeString
+ outlist = @data_old[lo..hi].map { |e| String.new("#{encode(" ")}#{e.chomp}") }
+ # standard:enable Performance/UnfreezeString
last_block = blocks[-1]
@@ -183,7 +184,7 @@ class Diff::LCS::Hunk
@blocks.each do |block|
block.remove.each do |item|
- op = item.action.to_s # -
+ op = item.action.to_s # -
offset = item.position - lo + num_added
outlist[offset][0, 1] = encode(op)
num_removed += 1
@@ -195,7 +196,7 @@ class Diff::LCS::Hunk
end
block.insert.each do |item|
- op = item.action.to_s # +
+ op = item.action.to_s # +
offset = item.position - @start_new + num_removed
outlist[offset, 0] = encode(op) + @data_new[item.position].chomp
num_added += 1
@@ -212,8 +213,8 @@ class Diff::LCS::Hunk
def context_diff(last = false)
s = encode("***************\n")
- s << encode("*** #{context_range(:old, ',', last)} ****\n")
- r = context_range(:new, ',', last)
+ s << encode("*** #{context_range(:old, ",", last)} ****\n")
+ r = context_range(:new, ",", last)
if last
old_missing_newline = missing_last_newline?(@data_old)
@@ -226,7 +227,9 @@ class Diff::LCS::Hunk
removes = @blocks.reject { |e| e.remove.empty? }
unless removes.empty?
- outlist = @data_old[lo..hi].map { |e| String.new("#{encode(' ')}#{e.chomp}") }
+ # standard:disable Performance/UnfreezeString
+ outlist = @data_old[lo..hi].map { |e| String.new("#{encode(" ")}#{e.chomp}") }
+ # standard:enable Performance/UnfreezeString
last_block = removes[-1]
@@ -248,7 +251,9 @@ class Diff::LCS::Hunk
inserts = @blocks.reject { |e| e.insert.empty? }
unless inserts.empty?
- outlist = @data_new[lo..hi].map { |e| String.new("#{encode(' ')}#{e.chomp}") }
+ # standard:disable Performance/UnfreezeString
+ outlist = @data_new[lo..hi].map { |e| String.new("#{encode(" ")}#{e.chomp}") }
+ # standard:enable Performance/UnfreezeString
last_block = inserts[-1]
@@ -269,13 +274,13 @@ class Diff::LCS::Hunk
private :context_diff
def ed_diff(format, _last = false)
- warn 'Expecting only one block in an old diff hunk!' if @blocks.size > 1
+ warn "Expecting only one block in an old diff hunk!" if @blocks.size > 1
s =
if format == :reverse_ed
- encode("#{ED_DIFF_OP_ACTION[@blocks[0].op]}#{context_range(:old, ',')}\n")
+ encode("#{ED_DIFF_OP_ACTION[@blocks[0].op]}#{context_range(:old, ",")}\n")
else
- encode("#{context_range(:old, ' ')}#{ED_DIFF_OP_ACTION[@blocks[0].op]}\n")
+ encode("#{context_range(:old, " ")}#{ED_DIFF_OP_ACTION[@blocks[0].op]}\n")
end
unless @blocks[0].insert.empty?
diff --git a/lib/diff/lcs/internals.rb b/lib/diff/lcs/internals.rb
index ef77667..8a9160a 100644
--- a/lib/diff/lcs/internals.rb
+++ b/lib/diff/lcs/internals.rb
@@ -13,7 +13,7 @@ class << Diff::LCS
if block
callbacks.diffs.map do |hunk|
- if hunk.kind_of? Array
+ if hunk.is_a? Array
hunk.map { |hunk_block| block[hunk_block] }
else
block[hunk]
@@ -45,14 +45,14 @@ class << Diff::LCS::Internals
vector = []
# Collect 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) && (b_start <= b_finish) && (a[a_start] == b[b_start])
vector[a_start] = b_start
a_start += 1
b_start += 1
end
# 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) && (b_start <= b_finish) && (a[a_finish] == b[b_finish])
vector[a_finish] = b_finish
a_finish -= 1
b_finish -= 1
@@ -63,8 +63,8 @@ class << Diff::LCS::Internals
b_matches = position_hash(b, b_start..b_finish)
thresh = []
- links = []
- string = a.kind_of?(String)
+ links = []
+ string = a.is_a?(String)
(a_start..a_finish).each do |i|
ai = string ? a[i, 1] : a[i]
@@ -75,7 +75,7 @@ class << Diff::LCS::Internals
# it may have an optimization purpose
# An attempt to remove it: https://github.com/halostatue/diff-lcs/pull/72
# Why it is reintroduced: https://github.com/halostatue/diff-lcs/issues/78
- if k and (thresh[k] > j) and (thresh[k - 1] < j)
+ if k && (thresh[k] > j) && (thresh[k - 1] < j)
thresh[k] = j
else
k = replace_next_larger(thresh, j, k)
@@ -100,7 +100,7 @@ class << Diff::LCS::Internals
# the object form of same) and detection of whether the patchset represents
# changes to be made.
def analyze_patchset(patchset, depth = 0)
- fail 'Patchset too complex' if depth > 1
+ fail "Patchset too complex" if depth > 1
has_changes = false
new_patchset = []
@@ -145,7 +145,7 @@ class << Diff::LCS::Internals
# Diff::LCS::Change as its source, as an array will cause the creation
# of one of the above.
def intuit_diff_direction(src, patchset, limit = nil)
- string = src.kind_of?(String)
+ string = src.is_a?(String)
count = left_match = left_miss = right_match = right_miss = 0
patchset.each do |change|
@@ -157,22 +157,22 @@ class << Diff::LCS::Internals
re = string ? src[change.new_position, 1] : src[change.new_position]
case change.action
- when '-' # Remove details from the old string
+ when "-" # Remove details from the old string
if le == change.old_element
left_match += 1
else
left_miss += 1
end
- when '+'
+ when "+"
if re == change.new_element
right_match += 1
else
right_miss += 1
end
- when '='
+ when "="
left_miss += 1 if le != change.old_element
right_miss += 1 if re != change.new_element
- when '!'
+ when "!"
if le == change.old_element
left_match += 1
elsif re == change.new_element
@@ -189,19 +189,19 @@ class << Diff::LCS::Internals
element = string ? src[change.position, 1] : src[change.position]
case change.action
- when '-'
+ when "-"
if element == change.element
left_match += 1
else
left_miss += 1
end
- when '+'
+ when "+"
if element == change.element
right_match += 1
else
right_miss += 1
end
- when '='
+ when "="
if element != change.element
left_miss += 1
right_miss += 1
@@ -251,7 +251,7 @@ enumerable as either source or destination value."
# This operation preserves the sort order.
def replace_next_larger(enum, value, last_index = nil)
# Off the end?
- if enum.empty? or (value > enum[-1])
+ if enum.empty? || (value > enum[-1])
enum << value
return enum.size - 1
end
@@ -296,7 +296,7 @@ enumerable as either source or destination value."
# positions it occupies in the Enumerable, optionally restricted to the
# elements specified in the range of indexes specified by +interval+.
def position_hash(enum, interval)
- string = enum.kind_of?(String)
+ string = enum.is_a?(String)
hash = Hash.new { |h, k| h[k] = [] }
interval.each do |i|
k = string ? enum[i, 1] : enum[i]
diff --git a/lib/diff/lcs/ldiff.rb b/lib/diff/lcs/ldiff.rb
index 17b374c..961458c 100644
--- a/lib/diff/lcs/ldiff.rb
+++ b/lib/diff/lcs/ldiff.rb
@@ -1,10 +1,11 @@
# frozen_string_literal: true
-require 'optparse'
-require 'ostruct'
-require 'diff/lcs/hunk'
+require "optparse"
+require "ostruct"
+require "diff/lcs/hunk"
-module Diff::LCS::Ldiff #:nodoc:
+module Diff::LCS::Ldiff # :nodoc:
+ # standard:disable Layout/HeredocIndentation
BANNER = <<-COPYRIGHT
ldiff #{Diff::LCS::VERSION}
Copyright 2004-2019 Austin Ziegler
@@ -16,60 +17,61 @@ ldiff #{Diff::LCS::VERSION}
the terms of the GPL version 2 (or later), the Perl Artistic licence, or the
MIT licence.
COPYRIGHT
+ # standard:enable Layout/HeredocIndentation
end
class << Diff::LCS::Ldiff
- attr_reader :format, :lines #:nodoc:
- attr_reader :file_old, :file_new #:nodoc:
- attr_reader :data_old, :data_new #:nodoc:
+ attr_reader :format, :lines # :nodoc:
+ 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.separator ""
o.on(
- '-c', '-C', '--context [LINES]', Integer,
- 'Displays a context diff with LINES lines', 'of context. Default 3 lines.'
+ "-c", "-C", "--context [LINES]", Integer,
+ "Displays a context diff with LINES lines", "of context. Default 3 lines."
) do |ctx|
@format = :context
- @lines = ctx || 3
+ @lines = ctx || 3
end
o.on(
- '-u', '-U', '--unified [LINES]', Integer,
- 'Displays a unified diff with LINES lines', 'of context. Default 3 lines.'
+ "-u", "-U", "--unified [LINES]", Integer,
+ "Displays a unified diff with LINES lines", "of context. Default 3 lines."
) do |ctx|
@format = :unified
- @lines = ctx || 3
+ @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.'
+ "-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
+ o.on_tail("--help", "Shows this text.") do
error << o
return 0
end
- o.on_tail('--version', 'Shows the version of Diff::LCS.') do
+ o.on_tail("--version", "Shows the version of Diff::LCS.") do
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
@@ -81,17 +83,17 @@ class << Diff::LCS::Ldiff
# Defaults are for old-style diff
@format ||= :old
- @lines ||= 0
+ @lines ||= 0
file_old, file_new = *ARGV
case @format
when :context
- char_old = '*' * 3
- char_new = '-' * 3
+ char_old = "*" * 3
+ char_new = "-" * 3
when :unified
- char_old = '-' * 3
- char_new = '+' * 3
+ char_old = "-" * 3
+ char_new = "+" * 3
end
# After we've read up to a certain point in each file, the number of
@@ -128,10 +130,10 @@ class << Diff::LCS::Ldiff
return 1
end
- if (@format == :unified) or (@format == :context)
- ft = File.stat(file_old).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.000000000 %z')
+ if (@format == :unified) || (@format == :context)
+ ft = File.stat(file_old).mtime.localtime.strftime("%Y-%m-%d %H:%M:%S.000000000 %z")
output << "#{char_old} #{file_old}\t#{ft}\n"
- ft = File.stat(file_new).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.000000000 %z')
+ ft = File.stat(file_new).mtime.localtime.strftime("%Y-%m-%d %H:%M:%S.000000000 %z")
output << "#{char_new} #{file_new}\t#{ft}\n"
end
@@ -150,7 +152,7 @@ class << Diff::LCS::Ldiff
file_length_difference = hunk.file_length_difference
next unless oldhunk
- next if @lines.positive? and hunk.merge(oldhunk)
+ next if @lines.positive? && hunk.merge(oldhunk)
output << oldhunk.diff(@format)
output << "\n" if @format == :unified