diffing 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d8ac1c37dffd7e67d6b3f13890414c6c2d78ed9e
4
- data.tar.gz: 356faa930542c3f4e5bd9be4637a2ad06cee8028
3
+ metadata.gz: 00a7b9d972e0846119c0771badcf7f95b7caf1d3
4
+ data.tar.gz: 5174c797bcb153d30fb75e1caa22cb909e0d0736
5
5
  SHA512:
6
- metadata.gz: b9ef2fe7b2aecc6ec334337145a0e706721491311bb1d9587b6085c2a87004dfacc7926444950ac73758b547a4a4aef73cd45b8c3dbe2cac251b3c389ff582c6
7
- data.tar.gz: 093b08812f08bea5649e6a8decd5f6da5cddf7aed82a4d7d1dc248e3b0e49a907e5888fef821c1debf4bd74973eba35f274efa7f7aa685daeea3cdde4c623a5d
6
+ metadata.gz: 60aaebb3b18e13ede7385c6979081b0a118d2c2d5c1efb78f67d71b44ef77c84cd8f66e1f61da2618b767884f66a92ad8589ad90f7379cd22e8ab1d68f5a8942
7
+ data.tar.gz: 40bc19b32d599b8c5c51f38b37a94e0883526a13ab1c9c7ebd689ab1e252de3cd60e0f189b03dd5697aa2f14307d160a75064db0a71ac604e2283a64f08a2017
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- from 'https://rubygems.org'
2
-
3
- gemspec
1
+ from 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014 4urbanoff
1
+ Copyright (c) 2015 4urbanoff
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -1 +1,107 @@
1
- Diffing two strings
1
+ # Diffing
2
+
3
+ ## Installation
4
+
5
+ ```
6
+ sudo gem install differ
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ ```ruby
12
+ require 'diffing'
13
+
14
+ from = "Hello! I am string for diffing test"
15
+ to = "Hi! I am two string for diffing"
16
+ ```
17
+
18
+ #### By chars
19
+ ```ruby
20
+ diff = Diffing.by_chars( from, to )
21
+
22
+ diff.as_ascii
23
+ # => H{"ello" >> "i"}! I am{+" two"} string for diffing{-" test"}
24
+ diff.as_html
25
+ # => H<del>ello</del><ins>i</ins>! I am<ins> two</ins> string for diffing<del> test</del>
26
+ ```
27
+
28
+ #### By words
29
+ ```ruby
30
+ diff = Diffing.by_words( from, to )
31
+
32
+ diff.as_ascii
33
+ # => {"Hello!" >> "Hi!"} I am {+"two"} string for diffing {-"test"}
34
+ diff.as_html
35
+ # => <del>Hello!</del><ins>Hi!</ins> I am <ins>two</ins> string for diffing <del>test</del>
36
+ ```
37
+
38
+ #### By lines
39
+ ```ruby
40
+ diff = Diffing.by_lines( from, to )
41
+
42
+ diff.as_ascii
43
+ # => {"Hello! I am string for diffing test" >> "Hi! I am two string for diffing"}
44
+ diff.as_html
45
+ # => <del>Hello! I am string for diffing test</del><ins>Hi! I am two string for diffing</ins>
46
+ ```
47
+
48
+ ## Custom format
49
+
50
+ ```ruby
51
+ module CustomFormat
52
+ class << self
53
+
54
+ def source( value )
55
+ value
56
+ end
57
+
58
+ def insert( value )
59
+ "(++#{ value })"
60
+ end
61
+
62
+ def delete( value )
63
+ "(--#{ value })"
64
+ end
65
+
66
+ def replace( from, to )
67
+ "(#{ from } => #{ to })"
68
+ end
69
+
70
+ end
71
+ end
72
+
73
+
74
+ Diffing.by_chars.format( CustomFormat )
75
+ # => H(ello => i)! I am(++ two) string for diffing(-- test)
76
+ Diffing.by_words.format( CustomFormat )
77
+ # => (Hello! => Hi!) I am (++two) string for diffing (--test)
78
+ Diffing.by_lines.format( CustomFormat )
79
+ # => (Hello! I am string for diffing test => Hi! I am two string for diffing)
80
+
81
+ ```
82
+
83
+ ## Custom delimiter
84
+
85
+ ```ruby
86
+ Diffing::Diff.new( from, to, 'i' ).as_ascii
87
+ # => {"Hello! I am str" >> "Hi! I am two str"}ing for diffi{"ng test" >> "ng"}
88
+ ```
89
+
90
+ ## Custom use separated parts
91
+
92
+ ```ruby
93
+ Diffing.by_words( from, to ).parts.map { |part|
94
+
95
+ result = ''
96
+ result << "<source:#{ part.source }>" if part.source?
97
+ result << "<insert:#{ part.insert }>" if part.insert?
98
+ result << "<delete:#{ part.delete }>" if part.delete?
99
+ result
100
+
101
+ }.join( '' )
102
+ # => <insert:Hi!><delete:Hello!><source:I am><insert:two><source:string for diffing><delete:test>
103
+ ```
104
+
105
+ ## Copyright
106
+
107
+ Copyright © 2015 Denis Churbanov
data/lib/diffing.rb CHANGED
@@ -1,12 +1,13 @@
1
1
  require 'diffing/diff'
2
2
  require 'diffing/part'
3
3
  require 'diffing/formats/ascii'
4
+ require 'diffing/formats/html'
4
5
 
5
6
 
6
7
  module Diffing
7
8
 
8
9
  class << self
9
-
10
+
10
11
 
11
12
  def by_lines( from, to )
12
13
  Diff.new from, to, "\n"
@@ -19,7 +20,7 @@ module Diffing
19
20
  def by_chars( from, to )
20
21
  Diff.new from, to
21
22
  end
22
-
23
+
23
24
 
24
25
  end
25
26
 
data/lib/diffing/diff.rb CHANGED
@@ -1,101 +1,104 @@
1
- module Diffing
2
-
3
- class Diff
4
-
5
-
6
- attr_reader :parts
7
-
8
- def initialize( from, to, delimiter = '' )
9
- @delimiter = delimiter
10
- @parts = calcucate( split( from.to_s ), split( to.to_s ) ).flatten
11
- end
12
-
13
- def format( format = Format::Ascii )
14
- result = []
15
- @parts.each do |part|
16
- result << format.source( part.source ) if part.source?
17
- if part.replace? and format.respond_to?( :replace )
18
- result << format.replace( part.delete, part.insert )
19
- else
20
- result << format.insert( part.insert ) if part.insert?
21
- result << format.delete( part.delete ) if part.delete?
22
- end
23
- end
24
- result.join @delimiter
25
- end
26
-
27
- def to_s
28
- format
29
- end
30
-
31
- def inspect
32
- format
33
- end
34
-
35
-
36
- private
37
-
38
- def calcucate( from, to )
39
- if found = find_middle( from, to )
40
- from_l, to_l, source, from_r, to_r = found
41
- [ calcucate( from_l, to_l ), Part.new( source: join( source ) ), calcucate( from_r, to_r ) ]
42
- else
43
- [ Part.new( insert: join( to ), delete: join( from ) ) ]
44
- end
45
- end
46
-
47
- def find_middle( from, to, min = 0, max = nil )
48
-
49
- return nil if from.empty? or to.empty?
50
-
51
- max = from.size unless max
52
- size = min + ( ( max - min ) / 2.to_f ).round
53
-
54
- subsets_each( from, size ) do |subset, first, last|
55
-
56
- if found = find_middle_index( to, subset )
57
-
58
- return (
59
- size != min and find_middle( from, to, size, max ) or
60
- (
61
- from_l = from[ 0...first ]
62
- to_l = to[ 0...found ]
63
- from_r = from[ last...from.size ]
64
- to_r = to[ found + subset.size...to.size ]
65
- [ from_l, to_l, subset, from_r, to_r ]
66
- )
67
- )
68
-
69
- end
70
-
71
- end
72
-
73
- size != max and find_middle( from, to, min, size ) or nil
74
-
75
- end
76
-
77
- def find_middle_index( set, search_subset )
78
- return set.index( search_subset ) if @delimiter == ''
79
- subsets_each( set, search_subset.size ){ |subset, first, last| return first if subset == search_subset }
80
- nil
81
- end
82
-
83
- def subsets_each( from, size )
84
- ( from.size - size + 1 ).times do |first|
85
- last = first + size
86
- subset = from[ first...last ]
87
- yield subset, first, last
88
- end
89
- end
90
-
91
- def split( set )
92
- @delimiter == '' ? set : set.split( @delimiter )
93
- end
94
-
95
- def join( set )
96
- @delimiter == '' ? set : set.join( @delimiter )
97
- end
98
-
99
- end
100
-
101
- end
1
+ module Diffing
2
+
3
+ class Diff
4
+
5
+
6
+ attr_reader :parts
7
+
8
+ def initialize( from, to, delimiter = '' )
9
+ @delimiter = delimiter
10
+ @parts = calcucate( split( from.to_s ), split( to.to_s ) ).flatten
11
+ end
12
+
13
+ def format( format )
14
+ result = []
15
+ @parts.each do |part|
16
+ result << format.source( part.source ) if part.source?
17
+ if part.replace? and format.respond_to?( :replace )
18
+ result << format.replace( part.delete, part.insert )
19
+ else
20
+ result << format.insert( part.insert ) if part.insert?
21
+ result << format.delete( part.delete ) if part.delete?
22
+ end
23
+ end
24
+ result.join @delimiter
25
+ end
26
+
27
+ def as_ascii
28
+ format Format::Ascii
29
+ end
30
+
31
+ def as_html
32
+ format Format::Html
33
+ end
34
+
35
+ alias :to_s :as_ascii
36
+ alias :inspect :as_ascii
37
+
38
+
39
+ private
40
+
41
+ def calcucate( from, to )
42
+ if found = find_middle( from, to )
43
+ from_l, to_l, source, from_r, to_r = found
44
+ [ calcucate( from_l, to_l ), Part.new( source: join( source ) ), calcucate( from_r, to_r ) ]
45
+ else
46
+ [ Part.new( insert: join( to ), delete: join( from ) ) ]
47
+ end
48
+ end
49
+
50
+ def find_middle( from, to, min = 0, max = nil )
51
+
52
+ return nil if from.empty? or to.empty?
53
+
54
+ max = from.size unless max
55
+ size = min + ( ( max - min ) / 2.to_f ).round
56
+
57
+ subsets_each( from, size ) do |subset, first, last|
58
+
59
+ if found = find_middle_index( to, subset )
60
+
61
+ return (
62
+ size != min and find_middle( from, to, size, max ) or
63
+ (
64
+ from_l = from[ 0...first ]
65
+ to_l = to[ 0...found ]
66
+ from_r = from[ last...from.size ]
67
+ to_r = to[ found + subset.size...to.size ]
68
+ [ from_l, to_l, subset, from_r, to_r ]
69
+ )
70
+ )
71
+
72
+ end
73
+
74
+ end
75
+
76
+ size != max and find_middle( from, to, min, size ) or nil
77
+
78
+ end
79
+
80
+ def find_middle_index( set, search_subset )
81
+ return set.index( search_subset ) if @delimiter == ''
82
+ subsets_each( set, search_subset.size ){ |subset, first, last| return first if subset == search_subset }
83
+ nil
84
+ end
85
+
86
+ def subsets_each( from, size )
87
+ ( from.size - size + 1 ).times do |first|
88
+ last = first + size
89
+ subset = from[ first...last ]
90
+ yield subset, first, last
91
+ end
92
+ end
93
+
94
+ def split( set )
95
+ @delimiter == '' ? set : set.split( @delimiter )
96
+ end
97
+
98
+ def join( set )
99
+ @delimiter == '' ? set : set.join( @delimiter )
100
+ end
101
+
102
+ end
103
+
104
+ end
@@ -1,33 +1,33 @@
1
- module Diffing
2
-
3
- module Format
4
-
5
- module Ascii
6
-
7
- class << self
8
-
9
-
10
- def source( value )
11
- value
12
- end
13
-
14
- def insert( value )
15
- "{+\"#{ value }\"}"
16
- end
17
-
18
- def delete( value )
19
- "{-\"#{ value }\"}"
20
- end
21
-
22
- def replace( from, to )
23
- "{\"#{ from }\" >> \"#{ to }\"}"
24
- end
25
-
26
-
27
- end
28
-
29
- end
30
-
31
- end
32
-
33
- end
1
+ module Diffing
2
+
3
+ module Format
4
+
5
+ module Ascii
6
+
7
+ class << self
8
+
9
+
10
+ def source( value )
11
+ value
12
+ end
13
+
14
+ def insert( value )
15
+ "{+\"#{ value }\"}"
16
+ end
17
+
18
+ def delete( value )
19
+ "{-\"#{ value }\"}"
20
+ end
21
+
22
+ def replace( from, to )
23
+ "{\"#{ from }\" >> \"#{ to }\"}"
24
+ end
25
+
26
+
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,33 @@
1
+ module Diffing
2
+
3
+ module Format
4
+
5
+ module Html
6
+
7
+ class << self
8
+
9
+
10
+ def source( value )
11
+ value
12
+ end
13
+
14
+ def insert( value )
15
+ "<ins>#{ value }</ins>"
16
+ end
17
+
18
+ def delete( value )
19
+ "<del>#{ value }</del>"
20
+ end
21
+
22
+ def replace( from, to )
23
+ delete( from ) + insert( to )
24
+ end
25
+
26
+
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
33
+ end
data/lib/diffing/part.rb CHANGED
@@ -1,31 +1,31 @@
1
- module Diffing
2
-
3
-
4
- class Part
5
-
6
- attr_reader :source, :insert, :delete
7
-
8
- def initialize( source: '', insert: '', delete: '' )
9
- @source, @insert, @delete = source, insert, delete
10
- end
11
-
12
- def source?
13
- not @source.empty?
14
- end
15
-
16
- def insert?
17
- not @insert.empty?
18
- end
19
-
20
- def delete?
21
- not @delete.empty?
22
- end
23
-
24
- def replace?
25
- not @insert.empty? and not @delete.empty?
26
- end
27
-
28
- end
29
-
30
-
31
- end
1
+ module Diffing
2
+
3
+
4
+ class Part
5
+
6
+ attr_reader :source, :insert, :delete
7
+
8
+ def initialize( source: '', insert: '', delete: '' )
9
+ @source, @insert, @delete = source, insert, delete
10
+ end
11
+
12
+ def source?
13
+ not @source.empty?
14
+ end
15
+
16
+ def insert?
17
+ not @insert.empty?
18
+ end
19
+
20
+ def delete?
21
+ not @delete.empty?
22
+ end
23
+
24
+ def replace?
25
+ not @insert.empty? and not @delete.empty?
26
+ end
27
+
28
+ end
29
+
30
+
31
+ end
metadata CHANGED
@@ -1,24 +1,24 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diffing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - 4urbanoff
7
+ - Denis Churbanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2015-08-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Diffing two strings
14
- email:
15
- - 4urbanoff@gmail.com
13
+ description: Diffing of two strings
14
+ email: 4urbanoff@gmail.com
16
15
  executables: []
17
16
  extensions: []
18
17
  extra_rdoc_files: []
19
18
  files:
20
19
  - lib/diffing/diff.rb
21
20
  - lib/diffing/formats/ascii.rb
21
+ - lib/diffing/formats/html.rb
22
22
  - lib/diffing/part.rb
23
23
  - lib/diffing.rb
24
24
  - LICENSE.txt
@@ -48,5 +48,5 @@ rubyforge_project:
48
48
  rubygems_version: 2.0.14
49
49
  signing_key:
50
50
  specification_version: 4
51
- summary: ''
51
+ summary: Gem for calculating two strings difference
52
52
  test_files: []