diffing 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/README.md +1 -1
- data/Rakefile +1 -1
- data/lib/diffing/diff.rb +39 -14
- data/lib/diffing/formats/ascii.rb +1 -1
- data/lib/diffing/formats/html.rb +1 -1
- data/lib/diffing/part.rb +5 -5
- data/spec/diffing/diff_spec.rb +31 -31
- data/spec/diffing/formats_spec.rb +29 -29
- data/spec/diffing/part_spec.rb +33 -33
- data/spec/diffing_spec.rb +19 -19
- data/spec/spec_helper.rb +3 -3
- metadata +2 -3
- data/.rspec +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78cda3e7cb88712418ded811e46b24ea9a313940
|
4
|
+
data.tar.gz: 1df3f815fa570ca10fdecea3c1003b6a96f39b4e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aee5a434d152027da2a979fed35ef57283ec0d93c4feb5d324a8de4eb216f6c4bd28ff6b7afdb5fc2e1f3b2e00777a21a0a271f6e42817df7afb283e6c0a0a5b
|
7
|
+
data.tar.gz: 74d41e0ad413a2e7f9031cc61370369878b02f22c10b16f1c16b1800456ccaabaa559000db8d14077d97dd8e3ad8c8f4fcd02355800047fb616db39a2f395f1a
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Diffing
|
2
2
|
|
3
3
|
[](https://travis-ci.org/4urbanoff/Diffing)
|
4
|
+
[](https://scrutinizer-ci.com/g/4urbanoff/Diffing/?branch=master)
|
4
5
|
[](https://codeclimate.com/github/4urbanoff/Diffing)
|
5
|
-
[](https://codeclimate.com/github/4urbanoff/Diffing/coverage)
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
data/Rakefile
CHANGED
data/lib/diffing/diff.rb
CHANGED
@@ -7,26 +7,26 @@ module Diffing
|
|
7
7
|
|
8
8
|
def initialize( from, to, pattern = nil )
|
9
9
|
@pattern = pattern
|
10
|
-
@parts = calculate( split( from.to_s ), split( to.to_s ) ).flatten
|
10
|
+
@parts = calculate( split( from.to_s ), split( to.to_s ) ).flatten
|
11
11
|
end
|
12
12
|
|
13
|
-
def format(
|
13
|
+
def format( formatter )
|
14
14
|
result = []
|
15
15
|
@parts.each do |part|
|
16
|
-
result <<
|
17
|
-
result <<
|
18
|
-
result <<
|
19
|
-
result <<
|
16
|
+
result << formatter.source( part.source ) if part.source?
|
17
|
+
result << formatter.insert( part.insert ) if part.insert? && !part.remove?
|
18
|
+
result << formatter.remove( part.remove ) if part.remove? && !part.insert?
|
19
|
+
result << formatter.change( part.remove, part.insert ) if part.insert? && part.remove?
|
20
20
|
end
|
21
21
|
result.join
|
22
22
|
end
|
23
23
|
|
24
24
|
def as_ascii
|
25
|
-
format Formats::Ascii
|
25
|
+
format Formats::Ascii
|
26
26
|
end
|
27
27
|
|
28
28
|
def as_html
|
29
|
-
format Formats::Html
|
29
|
+
format Formats::Html
|
30
30
|
end
|
31
31
|
|
32
32
|
alias :to_s :as_ascii
|
@@ -35,10 +35,10 @@ module Diffing
|
|
35
35
|
|
36
36
|
private
|
37
37
|
|
38
|
-
def calculate( from, to )
|
38
|
+
def calculate( from, to )
|
39
39
|
if found = find_middle( from, to )
|
40
40
|
from_l, to_l, source, from_r, to_r = found
|
41
|
-
[ calculate( from_l, to_l ), Part.new( source: join( source ) ), calculate( from_r, to_r ) ]
|
41
|
+
[ calculate( from_l, to_l ), Part.new( source: join( source ) ), calculate( from_r, to_r ) ]
|
42
42
|
else
|
43
43
|
[ Part.new( insert: join( to ), remove: join( from ) ) ]
|
44
44
|
end
|
@@ -46,7 +46,7 @@ module Diffing
|
|
46
46
|
|
47
47
|
def find_middle( from, to, min = 0, max = nil )
|
48
48
|
|
49
|
-
return nil if from.empty?
|
49
|
+
return nil if from.empty? || to.empty?
|
50
50
|
|
51
51
|
max = from.size unless max
|
52
52
|
size = min + ( ( max - min ) / 2.to_f ).round
|
@@ -56,7 +56,7 @@ module Diffing
|
|
56
56
|
if found = find_middle_index( to, subset )
|
57
57
|
|
58
58
|
return (
|
59
|
-
size != min
|
59
|
+
size != min && find_middle( from, to, size, max ) ||
|
60
60
|
(
|
61
61
|
from_l = from[ 0...first ]
|
62
62
|
to_l = to[ 0...found ]
|
@@ -70,13 +70,38 @@ module Diffing
|
|
70
70
|
|
71
71
|
end
|
72
72
|
|
73
|
-
size != max
|
73
|
+
size != max && find_middle( from, to, min, size ) || nil
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
def scan_index( set, search_subset )
|
78
|
+
|
79
|
+
offset = 0
|
80
|
+
while found = set.slice( offset..-1 ).index( search_subset ) do
|
81
|
+
yield found
|
82
|
+
offset += found + 1
|
83
|
+
end
|
74
84
|
|
75
85
|
end
|
76
86
|
|
77
87
|
def find_middle_index( set, search_subset )
|
78
88
|
return set.index( search_subset ) if set.is_a?( String ) && search_subset.is_a?( String )
|
79
|
-
|
89
|
+
|
90
|
+
set = set.dup
|
91
|
+
|
92
|
+
index = 0
|
93
|
+
|
94
|
+
scan_index( set.join, search_subset.join ) do |found|
|
95
|
+
|
96
|
+
size = 0
|
97
|
+
while size != found
|
98
|
+
break if size + set[0].size > found
|
99
|
+
size += set.shift.size
|
100
|
+
index += 1
|
101
|
+
end
|
102
|
+
|
103
|
+
return index if set.slice(0, search_subset.size ) == search_subset
|
104
|
+
end
|
80
105
|
nil
|
81
106
|
end
|
82
107
|
|
data/lib/diffing/formats/html.rb
CHANGED
data/lib/diffing/part.rb
CHANGED
@@ -3,10 +3,10 @@ module Diffing
|
|
3
3
|
|
4
4
|
class Part
|
5
5
|
|
6
|
-
attr_reader :source, :insert, :remove
|
6
|
+
attr_reader :source, :insert, :remove
|
7
7
|
|
8
|
-
def initialize( source: '', insert: '', remove: '' )
|
9
|
-
@source, @insert, @remove = source, insert, remove
|
8
|
+
def initialize( source: '', insert: '', remove: '' )
|
9
|
+
@source, @insert, @remove = source, insert, remove
|
10
10
|
end
|
11
11
|
|
12
12
|
def source?
|
@@ -17,8 +17,8 @@ module Diffing
|
|
17
17
|
not @insert.empty?
|
18
18
|
end
|
19
19
|
|
20
|
-
def remove?
|
21
|
-
not @remove.empty?
|
20
|
+
def remove?
|
21
|
+
not @remove.empty?
|
22
22
|
end
|
23
23
|
|
24
24
|
end
|
data/spec/diffing/diff_spec.rb
CHANGED
@@ -1,32 +1,32 @@
|
|
1
1
|
describe Diffing::Diff do
|
2
|
-
|
3
|
-
it '#initialize' do
|
4
|
-
expect( diff ).to an_instance_of( Diffing::Diff )
|
5
|
-
end
|
6
|
-
|
7
|
-
it '#calculate' do
|
8
|
-
ex = diff_by_chars.send( :calculate, 'ab', 'bc' ).flatten.map { |part|
|
9
|
-
( part.source?
|
10
|
-
}.join
|
11
|
-
expect( ex ).to eql( 'abc' )
|
12
|
-
ex = diff_by_words.send( :calculate, %w(a b), %w(b c) ).flatten.map { |part|
|
13
|
-
( part.source?
|
14
|
-
}.join
|
15
|
-
expect( ex ).to eql( 'abc' )
|
2
|
+
|
3
|
+
it '#initialize' do
|
4
|
+
expect( diff ).to an_instance_of( Diffing::Diff )
|
5
|
+
end
|
6
|
+
|
7
|
+
it '#calculate' do
|
8
|
+
ex = diff_by_chars.send( :calculate, 'ab', 'bc' ).flatten.map { |part|
|
9
|
+
( part.source? && part.source ) || ( part.insert? && part.insert ) || ( part.remove? && part.remove )
|
10
|
+
}.join
|
11
|
+
expect( ex ).to eql( 'abc' )
|
12
|
+
ex = diff_by_words.send( :calculate, %w(a b), %w(b c) ).flatten.map { |part|
|
13
|
+
( part.source? && part.source ) || ( part.insert? && part.insert ) || ( part.remove? && part.remove )
|
14
|
+
}.join
|
15
|
+
expect( ex ).to eql( 'abc' )
|
16
16
|
end
|
17
17
|
|
18
|
-
it '#find_middle' do
|
19
|
-
expect( diff_by_chars.send( :find_middle, '.mid..', '--mid-' ) ).to eq( ['.', '--', 'mid', '..', '-'] )
|
20
|
-
expect( diff_by_chars.send( :find_middle, '.mid..', '__===_' ) ).to eq( nil )
|
21
|
-
expect( diff_by_words.send( :find_middle, %w(. m i d . .), %w(- - m i d -) ) ).to eq( [['.'], ['-', '-'], ['m', 'i', 'd'], ['.', '.'], ['-']] )
|
22
|
-
expect( diff_by_words.send( :find_middle, %w(. m i d . .), %w(_ _ = = = _) ) ).to eq( nil )
|
18
|
+
it '#find_middle' do
|
19
|
+
expect( diff_by_chars.send( :find_middle, '.mid..', '--mid-' ) ).to eq( ['.', '--', 'mid', '..', '-'] )
|
20
|
+
expect( diff_by_chars.send( :find_middle, '.mid..', '__===_' ) ).to eq( nil )
|
21
|
+
expect( diff_by_words.send( :find_middle, %w(. m i d . .), %w(- - m i d -) ) ).to eq( [['.'], ['-', '-'], ['m', 'i', 'd'], ['.', '.'], ['-']] )
|
22
|
+
expect( diff_by_words.send( :find_middle, %w(. m i d . .), %w(_ _ = = = _) ) ).to eq( nil )
|
23
23
|
end
|
24
24
|
|
25
|
-
it '#find_middle_index' do
|
26
|
-
expect( diff_by_chars.send( :find_middle_index, '...mid...', 'mid' ) ).to eq( 3 )
|
27
|
-
expect( diff_by_words.send( :find_middle_index, %w(. . . m i d . . .), %w(m i d) ) ).to eq( 3 )
|
28
|
-
expect( diff_by_chars.send( :find_middle_index, '...mid...', 'nil' ) ).to eq( nil )
|
29
|
-
expect( diff_by_words.send( :find_middle_index, %w(. . . m i d . . .), %w(n i l) ) ).to eq( nil )
|
25
|
+
it '#find_middle_index' do
|
26
|
+
expect( diff_by_chars.send( :find_middle_index, '...mid...', 'mid' ) ).to eq( 3 )
|
27
|
+
expect( diff_by_words.send( :find_middle_index, %w(. . . m i d . . .), %w(m i d) ) ).to eq( 3 )
|
28
|
+
expect( diff_by_chars.send( :find_middle_index, '...mid...', 'nil' ) ).to eq( nil )
|
29
|
+
expect( diff_by_words.send( :find_middle_index, %w(. . . m i d . . .), %w(n i l) ) ).to eq( nil )
|
30
30
|
end
|
31
31
|
|
32
32
|
it '#subsets_each' do
|
@@ -35,14 +35,14 @@ describe Diffing::Diff do
|
|
35
35
|
expect( result ).to eq( [ ['abc', 0, 3], ['bcd', 1, 4], ['cde', 2, 5] ] )
|
36
36
|
end
|
37
37
|
|
38
|
-
it '#split' do
|
39
|
-
expect( diff_by_chars.send( :split, 'abc' ) ).to an_instance_of( String )
|
40
|
-
expect( diff_by_words.send( :split, 'a b' ) ).to an_instance_of( Array )
|
38
|
+
it '#split' do
|
39
|
+
expect( diff_by_chars.send( :split, 'abc' ) ).to an_instance_of( String )
|
40
|
+
expect( diff_by_words.send( :split, 'a b' ) ).to an_instance_of( Array )
|
41
41
|
end
|
42
42
|
|
43
|
-
it '#join' do
|
44
|
-
expect( diff.send( :join, 'abc' ) ).to eq( 'abc' )
|
45
|
-
expect( diff.send( :join, %w(a b c) ) ).to eq( 'abc' )
|
43
|
+
it '#join' do
|
44
|
+
expect( diff.send( :join, 'abc' ) ).to eq( 'abc' )
|
45
|
+
expect( diff.send( :join, %w(a b c) ) ).to eq( 'abc' )
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
end
|
@@ -1,29 +1,29 @@
|
|
1
|
-
describe Diffing::Formats do
|
2
|
-
|
3
|
-
Diffing::Formats.constants.each do |name|
|
4
|
-
|
5
|
-
name do
|
6
|
-
|
7
|
-
subject = Diffing::Formats.const_get name
|
8
|
-
|
9
|
-
it 'does not have method source' do
|
10
|
-
subject.source ''
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'does not have method insert' do
|
14
|
-
subject.insert ''
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'does not have method remove' do
|
18
|
-
subject.remove ''
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'does not have method change' do
|
22
|
-
subject.change '', ''
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
1
|
+
describe Diffing::Formats do
|
2
|
+
|
3
|
+
Diffing::Formats.constants.each do |name|
|
4
|
+
|
5
|
+
name do
|
6
|
+
|
7
|
+
subject = Diffing::Formats.const_get name
|
8
|
+
|
9
|
+
it 'does not have method source' do
|
10
|
+
subject.source ''
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'does not have method insert' do
|
14
|
+
subject.insert ''
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'does not have method remove' do
|
18
|
+
subject.remove ''
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'does not have method change' do
|
22
|
+
subject.change '', ''
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/spec/diffing/part_spec.rb
CHANGED
@@ -1,33 +1,33 @@
|
|
1
|
-
describe Diffing::Part do
|
2
|
-
|
3
|
-
subject { Diffing::Part.new( source: 'a', insert: 'b', remove: 'c' ) }
|
4
|
-
|
5
|
-
it '#initialize' do
|
6
|
-
expect( subject ).to an_instance_of( Diffing::Part )
|
7
|
-
end
|
8
|
-
|
9
|
-
it '#source' do
|
10
|
-
expect( subject.source ).to eq( 'a' )
|
11
|
-
end
|
12
|
-
|
13
|
-
it '#insert' do
|
14
|
-
expect( subject.insert ).to eq( 'b' )
|
15
|
-
end
|
16
|
-
|
17
|
-
it '#remove' do
|
18
|
-
expect( subject.remove ).to eq( 'c' )
|
19
|
-
end
|
20
|
-
|
21
|
-
it '#source?' do
|
22
|
-
expect( subject.source? ).to eq( true )
|
23
|
-
end
|
24
|
-
|
25
|
-
it '#insert?' do
|
26
|
-
expect( subject.insert? ).to eq( true )
|
27
|
-
end
|
28
|
-
|
29
|
-
it '#remove?' do
|
30
|
-
expect( subject.remove? ).to eq( true )
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
1
|
+
describe Diffing::Part do
|
2
|
+
|
3
|
+
subject { Diffing::Part.new( source: 'a', insert: 'b', remove: 'c' ) }
|
4
|
+
|
5
|
+
it '#initialize' do
|
6
|
+
expect( subject ).to an_instance_of( Diffing::Part )
|
7
|
+
end
|
8
|
+
|
9
|
+
it '#source' do
|
10
|
+
expect( subject.source ).to eq( 'a' )
|
11
|
+
end
|
12
|
+
|
13
|
+
it '#insert' do
|
14
|
+
expect( subject.insert ).to eq( 'b' )
|
15
|
+
end
|
16
|
+
|
17
|
+
it '#remove' do
|
18
|
+
expect( subject.remove ).to eq( 'c' )
|
19
|
+
end
|
20
|
+
|
21
|
+
it '#source?' do
|
22
|
+
expect( subject.source? ).to eq( true )
|
23
|
+
end
|
24
|
+
|
25
|
+
it '#insert?' do
|
26
|
+
expect( subject.insert? ).to eq( true )
|
27
|
+
end
|
28
|
+
|
29
|
+
it '#remove?' do
|
30
|
+
expect( subject.remove? ).to eq( true )
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
data/spec/diffing_spec.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
|
-
describe Diffing do
|
2
|
-
|
3
|
-
it 'does not exists' do
|
4
|
-
Diffing
|
5
|
-
end
|
6
|
-
|
7
|
-
it 'does not have method by_chars( from, to )' do
|
8
|
-
Diffing.by_chars "", ""
|
9
|
-
end
|
10
|
-
|
11
|
-
it 'does not have method by_words( from, to )' do
|
12
|
-
Diffing.by_words "", ""
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'does not have method by_lines( from, to )' do
|
16
|
-
Diffing.by_lines "", ""
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
1
|
+
describe Diffing do
|
2
|
+
|
3
|
+
it 'does not exists' do
|
4
|
+
Diffing
|
5
|
+
end
|
6
|
+
|
7
|
+
it 'does not have method by_chars( from, to )' do
|
8
|
+
Diffing.by_chars "", ""
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'does not have method by_words( from, to )' do
|
12
|
+
Diffing.by_words "", ""
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'does not have method by_lines( from, to )' do
|
16
|
+
Diffing.by_lines "", ""
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: diffing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Denis Churbanov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -58,7 +58,6 @@ files:
|
|
58
58
|
- Rakefile
|
59
59
|
- Gemfile
|
60
60
|
- README.md
|
61
|
-
- .rspec
|
62
61
|
homepage: https://github.com/4urbanoff/Diffing
|
63
62
|
licenses:
|
64
63
|
- MIT
|
data/.rspec
DELETED