diffing 0.1.0 → 0.2.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: 00a7b9d972e0846119c0771badcf7f95b7caf1d3
4
- data.tar.gz: 5174c797bcb153d30fb75e1caa22cb909e0d0736
3
+ metadata.gz: 2a9db88ed0327a83b434f94b73fa9a425d14dbe8
4
+ data.tar.gz: 071b087aa7c10b65a685daa44a548c73176fbd88
5
5
  SHA512:
6
- metadata.gz: 60aaebb3b18e13ede7385c6979081b0a118d2c2d5c1efb78f67d71b44ef77c84cd8f66e1f61da2618b767884f66a92ad8589ad90f7379cd22e8ab1d68f5a8942
7
- data.tar.gz: 40bc19b32d599b8c5c51f38b37a94e0883526a13ab1c9c7ebd689ab1e252de3cd60e0f189b03dd5697aa2f14307d160a75064db0a71ac604e2283a64f08a2017
6
+ metadata.gz: d8e5f2ed5e39eabfa3b035bda196fe1d74d44978579aacda5354d000ebefb905fad14e0b164050990ef6b90e28e95ecb361e87f036afdef96c49c4decc831696
7
+ data.tar.gz: 502929c52ffc80ed63cf86b0109aeaea91e15382b224ed3aefc66741532ae0ca70e8b13b37ad6e0389b45c0198e7453bbefb7d10b0eedd427e4e4b5f5a4ea72b
data/README.md CHANGED
@@ -20,7 +20,7 @@ to = "Hi! I am two string for diffing"
20
20
  diff = Diffing.by_chars( from, to )
21
21
 
22
22
  diff.as_ascii
23
- # => H{"ello" >> "i"}! I am{+" two"} string for diffing{-" test"}
23
+ # => H{`ello`>>`i`}! I am{+` two`} string for diffing{-` test`}
24
24
  diff.as_html
25
25
  # => H<del>ello</del><ins>i</ins>! I am<ins> two</ins> string for diffing<del> test</del>
26
26
  ```
@@ -30,9 +30,9 @@ diff.as_html
30
30
  diff = Diffing.by_words( from, to )
31
31
 
32
32
  diff.as_ascii
33
- # => {"Hello!" >> "Hi!"} I am {+"two"} string for diffing {-"test"}
33
+ # => {`Hello!`>>`Hi!`} I am{+` two`} string for diffing{-` test`}
34
34
  diff.as_html
35
- # => <del>Hello!</del><ins>Hi!</ins> I am <ins>two</ins> string for diffing <del>test</del>
35
+ # => <del>Hello!</del><ins>Hi!</ins> I am<ins> two</ins> string for diffing<del> test</del>
36
36
  ```
37
37
 
38
38
  #### By lines
@@ -40,7 +40,7 @@ diff.as_html
40
40
  diff = Diffing.by_lines( from, to )
41
41
 
42
42
  diff.as_ascii
43
- # => {"Hello! I am string for diffing test" >> "Hi! I am two string for diffing"}
43
+ # => {`Hello! I am string for diffing test`>>`Hi! I am two string for diffing`}
44
44
  diff.as_html
45
45
  # => <del>Hello! I am string for diffing test</del><ins>Hi! I am two string for diffing</ins>
46
46
  ```
@@ -71,21 +71,15 @@ module CustomFormat
71
71
  end
72
72
 
73
73
 
74
- Diffing.by_chars.format( CustomFormat )
74
+ Diffing.by_chars( from, to ).format( CustomFormat )
75
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 )
76
+ Diffing.by_words( from, to ).format( CustomFormat )
77
+ # => (Hello! => Hi!) I am(++ two) string for diffing(-- test)
78
+ Diffing.by_lines( from, to ).format( CustomFormat )
79
79
  # => (Hello! I am string for diffing test => Hi! I am two string for diffing)
80
80
 
81
81
  ```
82
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
83
 
90
84
  ## Custom use separated parts
91
85
 
@@ -98,10 +92,19 @@ Diffing.by_words( from, to ).parts.map { |part|
98
92
  result << "<delete:#{ part.delete }>" if part.delete?
99
93
  result
100
94
 
101
- }.join( '' )
102
- # => <insert:Hi!><delete:Hello!><source:I am><insert:two><source:string for diffing><delete:test>
95
+ }.join
96
+ # => <insert:Hi!><delete:Hello!><source: I am><insert: two><source: string for diffing><delete: test>
103
97
  ```
104
98
 
99
+
100
+ ## Custom pattern of parts
101
+
102
+ ```ruby
103
+ Diffing::Diff.new( from, to, /.{,3}\s?/ ).as_ascii
104
+ # => {`Hello! `>>`Hi! `}I a{`m string `>>`m two string `}for diffin{`g test`>>`g`}
105
+ ```
106
+
107
+
105
108
  ## Copyright
106
109
 
107
110
  Copyright © 2015 Denis Churbanov
data/lib/diffing/diff.rb CHANGED
@@ -5,36 +5,33 @@ module Diffing
5
5
 
6
6
  attr_reader :parts
7
7
 
8
- def initialize( from, to, delimiter = '' )
9
- @delimiter = delimiter
10
- @parts = calcucate( split( from.to_s ), split( to.to_s ) ).flatten
8
+ def initialize( from, to, pattern = nil )
9
+ @pattern = pattern
10
+ @parts = calcucate( split( from.to_s ), split( to.to_s ) ).flatten
11
11
  end
12
12
 
13
- def format( format )
13
+ def format( format )
14
14
  result = []
15
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
16
+ result << format.source( part.source ) if part.source?
17
+ result << format.insert( part.insert ) if part.insert? && !part.delete?
18
+ result << format.delete( part.delete ) if part.delete? && !part.insert?
19
+ result << format.replace( part.delete, part.insert ) if part.insert? && part.delete?
23
20
  end
24
- result.join @delimiter
21
+ result.join
25
22
  end
26
-
27
- def as_ascii
28
- format Format::Ascii
23
+
24
+ def as_ascii
25
+ format Format::Ascii
29
26
  end
30
-
31
- def as_html
32
- format Format::Html
27
+
28
+ def as_html
29
+ format Format::Html
33
30
  end
34
31
 
35
- alias :to_s :as_ascii
36
- alias :inspect :as_ascii
37
-
32
+ alias :to_s :as_ascii
33
+ alias :inspect :as_ascii
34
+
38
35
 
39
36
  private
40
37
 
@@ -78,7 +75,7 @@ module Diffing
78
75
  end
79
76
 
80
77
  def find_middle_index( set, search_subset )
81
- return set.index( search_subset ) if @delimiter == ''
78
+ return set.index( search_subset ) unless @pattern
82
79
  subsets_each( set, search_subset.size ){ |subset, first, last| return first if subset == search_subset }
83
80
  nil
84
81
  end
@@ -92,11 +89,11 @@ module Diffing
92
89
  end
93
90
 
94
91
  def split( set )
95
- @delimiter == '' ? set : set.split( @delimiter )
92
+ @pattern ? set.scan( @pattern ) : set
96
93
  end
97
94
 
98
95
  def join( set )
99
- @delimiter == '' ? set : set.join( @delimiter )
96
+ @pattern ? set.join : set
100
97
  end
101
98
 
102
99
  end
@@ -12,15 +12,15 @@ module Diffing
12
12
  end
13
13
 
14
14
  def insert( value )
15
- "{+\"#{ value }\"}"
15
+ "{+`#{ value }`}"
16
16
  end
17
17
 
18
18
  def delete( value )
19
- "{-\"#{ value }\"}"
19
+ "{-`#{ value }`}"
20
20
  end
21
21
 
22
22
  def replace( from, to )
23
- "{\"#{ from }\" >> \"#{ to }\"}"
23
+ "{`#{ from }`>>`#{ to }`}"
24
24
  end
25
25
 
26
26
 
data/lib/diffing/part.rb CHANGED
@@ -20,11 +20,7 @@ module Diffing
20
20
  def delete?
21
21
  not @delete.empty?
22
22
  end
23
-
24
- def replace?
25
- not @insert.empty? and not @delete.empty?
26
- end
27
-
23
+
28
24
  end
29
25
 
30
26
 
data/lib/diffing.rb CHANGED
@@ -9,16 +9,16 @@ module Diffing
9
9
  class << self
10
10
 
11
11
 
12
- def by_lines( from, to )
13
- Diff.new from, to, "\n"
12
+ def by_chars( from, to )
13
+ Diff.new from, to
14
14
  end
15
15
 
16
16
  def by_words( from, to )
17
- Diff.new from, to, " "
17
+ Diff.new from, to, /\S+|\s+/
18
18
  end
19
-
20
- def by_chars( from, to )
21
- Diff.new from, to
19
+
20
+ def by_lines( from, to )
21
+ Diff.new from, to, /[^\n]+|\n+/
22
22
  end
23
23
 
24
24
 
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.1.0
4
+ version: 0.2.0
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-04 00:00:00.000000000 Z
11
+ date: 2015-08-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Diffing of two strings
14
14
  email: 4urbanoff@gmail.com