string_language 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/string_helpers.rb +58 -0
  3. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b75b38f67ab6f28e81dcca0bc77137ebc679e3b
4
- data.tar.gz: b38a9e0a9bbd33166a2accb787de707079813a71
3
+ metadata.gz: b276927107e9da6ec0ef1de905944d8a947fafe4
4
+ data.tar.gz: 3f55a399e1f40017315c2b4d86c2fc25dc68451e
5
5
  SHA512:
6
- metadata.gz: bc3e8c283db464d8c2c5320f21da401a02b7aed9f0c8e1f0aac7b6aa43be7cc80ec52407518bf98095867e96a25aed73c55767a063014e6ef1b19d5b921d2626
7
- data.tar.gz: 0262b6795e8f5916671394896091d395bf31ac5c466409d012d113f20422166c7bd491f213e843c2fa11bfdf55455351238bdf244301a91b2aacb19aa64a24ae
6
+ metadata.gz: ca3842738b186cb5b9440d3b863f63a3efbb0b58c58834baa77ec51c5d57dc404e34825fa589971fdd88bc023b32f72d8121e8177aaa6ed608297c19486d919b
7
+ data.tar.gz: 7c05738fa63b786a385712d17c4839d8b143fc946670a99f785654525cd259be22f09a891a34f9466566c10c578cb293dea36b5ef1c6d968d42b46305c43cb0a
@@ -0,0 +1,58 @@
1
+ module StringHelpers
2
+
3
+ # Make the spacing around the i-th bracket/qoute in stringA equal to that around
4
+ # the i-th bracket/quote in stringB
5
+ def self.space_equally stringA, stringB
6
+
7
+ # "'a' ( bla ) (c )" => ["", "'", "a", "' ", "", "( ", "bla )", " (", "c )"]
8
+
9
+ brackets_and_quotes = "{(\['\"\])}"
10
+ regex = /(\s*[#{brackets_and_quotes}]\s*)/
11
+
12
+ splitA = stringA.split( regex )
13
+ splitB = stringB.split( regex )
14
+
15
+ i=0; while space_ith_match( splitA, splitB, regex, i ) do ; i+=1 ; end
16
+
17
+ splitA.join('')
18
+ end
19
+
20
+ private
21
+
22
+ def self.space_ith_match listA, listB, regex, i
23
+
24
+ matchA, indexA = *ith_match( listA, regex, i )
25
+ matchB, indexB = *ith_match( listB, regex, i )
26
+
27
+ return false if (matchA == nil or matchB == nil)
28
+
29
+ prefix = matchB.match( /^\s*/ )[0]
30
+ suffix = matchB.match( /\s*$/ )[0]
31
+ middle = matchA.gsub( /^\s*|\s*$/, '' )
32
+
33
+ listA[ indexA ] = prefix + middle + suffix
34
+ end
35
+
36
+ def self.ith_match list, regex, i
37
+
38
+ count = 0
39
+
40
+ list.each_with_index do |item,index|
41
+
42
+ count+=1 if item.match( regex )
43
+ return [item,index] if count == i
44
+ end
45
+
46
+ return [nil,nil]
47
+ end
48
+ end
49
+
50
+ # Quick Testing
51
+ =begin
52
+ a = "(Dit is een (test) 'van' de spacing)"
53
+ b = "( Deze was niet goed gespaced, zoals ( test) ' van ' dittum)"
54
+
55
+ puts a
56
+ puts b
57
+ puts StringHelpers::space_equally( b, a )
58
+ =end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: string_language
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cornelis Adriaan Schippers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-30 00:00:00.000000000 Z
11
+ date: 2013-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: curb
@@ -46,6 +46,7 @@ extra_rdoc_files: []
46
46
  files:
47
47
  - lib/google_translator.rb
48
48
  - lib/string_language.rb
49
+ - lib/string_helpers.rb
49
50
  homepage: http://rubygems.org/gems/string_language
50
51
  licenses:
51
52
  - MIT