diffing 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2b0ad9e1502ceedf9c36ce7b7727d432e4dc0e8a
4
+ data.tar.gz: e032328d0c4b9fac5a994391d40e60aa431390ff
5
+ SHA512:
6
+ metadata.gz: 97e2c07c8e2b6d3b20041af31b81b2d7fe1662a245e900bd142a1dcb6acd0a038b499bfcd211f99a53acd5c426f5ebfbc5fef1452526bd87cc628a16d5810d40
7
+ data.tar.gz: a4a4379f225d3d19ed7b255ca5a9c55c6f1bdeea273dc980169bb6cd38a0d155ef911de84233ce20e2ca534af5d9ec43f35775dc745bf3aeb43b58c841bea214
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ from 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 4urbanoff
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ Diffing two strings
File without changes
@@ -0,0 +1 @@
1
+ #!/usr/bin/env ruby
@@ -0,0 +1,26 @@
1
+ require 'diffing/diff'
2
+ require 'diffing/part'
3
+ require 'diffing/formats/ascii'
4
+
5
+
6
+ module Diffing
7
+
8
+ class << self
9
+
10
+
11
+ def by_lines( from, to )
12
+ Diff.new from, to, "\n"
13
+ end
14
+
15
+ def by_words( from, to )
16
+ Diff.new from, to, " "
17
+ end
18
+
19
+ def by_chars( from, to )
20
+ Diff.new from, to
21
+ end
22
+
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,101 @@
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
@@ -0,0 +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
@@ -0,0 +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
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: diffing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - 4urbanoff
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Diffing two strings
14
+ email:
15
+ - 4urbanoff@gmail.com
16
+ executables:
17
+ - diffing
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/diffing/diff.rb
22
+ - lib/diffing/formats/ascii.rb
23
+ - lib/diffing/part.rb
24
+ - lib/diffing.rb
25
+ - LICENSE.txt
26
+ - Rakefile
27
+ - Gemfile
28
+ - README.md
29
+ - bin/diffing
30
+ homepage: ''
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.0.14
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: ''
54
+ test_files: []