jebediah 1.0.3 → 1.0.5

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/bin/git-jeb +1 -1
  3. data/bin/jebcycle +1 -1
  4. data/lib/jebediah.rb +49 -4
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bdbeb051e39880dea4cae5d7e0fe7b2d0230c946
4
- data.tar.gz: 36c446d38ec20cdd3e56de3ffa7a02f73c7b7b2b
3
+ metadata.gz: 65156594909c339c72e889552c4ab526ccb3cfc3
4
+ data.tar.gz: 1957eaecb4b60820b57390a086e670b64decb0a0
5
5
  SHA512:
6
- metadata.gz: 77acab3b464132a814bf2d151e5cc6c63a1167a51a809bced8829c1f9d60902ab68520aadc6aaecbda5a2c100e9cdc4ceba8853ef1633e1c9da16852b478d739
7
- data.tar.gz: afccaf5b9598c4c109e102c8bd88efff861972ac040323c43a7ef5f21fac5f71bdad4d1efde00f2fe80e573b99aa4509410eb03cb192d1823613ea5ac66915b7
6
+ metadata.gz: ea17b8636c130ddf20c474bbeef9f5a108748e7986f8f2d7fffde6524100cc1e12fd4fc448825f038d3b463e923665241870b6289cfdf459af77b561d3fbffb4
7
+ data.tar.gz: 9a97c11e631d2ef7eeaeff005f6b7e0bd9bf54f73b5d74b7137c15fc9445420b0ab115e057cf0a25ca169c7a7b587c65bc3cd75b6d48fb9ecd9aadfb2396b96c
@@ -6,4 +6,4 @@ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
6
6
 
7
7
  require 'jebediah'
8
8
  jeb = Jebediah.new()
9
- puts jeb.renderResult(jeb.process(`git log --format="%H" -n1`))
9
+ puts jeb.renderResult(jeb.process(`git log --format="%h" -n1`))
@@ -7,7 +7,7 @@ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
7
7
  require 'jebediah'
8
8
 
9
9
  def jeb
10
- commit = `git log --format='%h' -n 1`.strip
10
+ commit = `git log --format='%H' -n 1`.strip
11
11
  `jeb #{commit}`.strip
12
12
  end
13
13
 
@@ -1,6 +1,6 @@
1
1
  class Jebediah
2
2
  def self.version
3
- return "1.0.3"
3
+ return "1.0.5"
4
4
  end
5
5
 
6
6
  def initialize(dictPaths=nil)
@@ -23,6 +23,20 @@ class Jebediah
23
23
  return @dictionaries.length
24
24
  end
25
25
 
26
+ # Maximum hexadecimal string length that can safely be addressed by the dictionary set
27
+ def maxStringLength
28
+ weight = @dictionaries.inject(1) { |x, d| x * d.length }
29
+ bits = (Math.log(weight)/Math.log(2)).floor
30
+ chars = bits/4
31
+ end
32
+
33
+ # Returns maximum number addressable in the dictionary space
34
+ def hashFromString(str)
35
+ str = str[2..-1] if str.start_with?("0x")
36
+ str = str.to_s[0..maxStringLength-1]
37
+ hash = Integer("0x" + str)
38
+ end
39
+
26
40
  # Load in dictionaries from paths
27
41
  def loadDictionaries(dictPaths)
28
42
  @dictionaries = []
@@ -50,10 +64,18 @@ class Jebediah
50
64
  str =~ /^[0-9a-fA-F]+$/
51
65
  end
52
66
 
67
+ def isHyphenated?(str)
68
+ str =~ /^[\w-]+$/
69
+ end
70
+
53
71
  # Processes arbitrary input formatted as a string
54
72
  def processString(str)
55
73
  if isHash?(str) then
56
74
  return { :type => 'phrase', :result => phraseForHash(str) }
75
+ elsif(isHyphenated?(str)) then
76
+ terms = dehyphenatePhrase(str)
77
+ return { :type => 'hash', :result => hashForPhrase(terms) } unless terms.nil?
78
+ return { :type => 'unreadable' }
57
79
  else
58
80
  terms = str.split(' ')
59
81
  return { :type => 'hash', :result => hashForPhrase(terms) } if phraseLength == terms.length
@@ -107,7 +129,13 @@ class Jebediah
107
129
  weight = 1
108
130
  hash = 0
109
131
 
110
- phrase = phrase.gsub(/\s+/m, ' ').strip.split(' ') if phrase.is_a?(String)
132
+ if phrase.is_a?(String) then
133
+ if isHyphenated?(phrase) then
134
+ phrase = dehyphenatePhrase(phrase)
135
+ else
136
+ phrase = phrase.gsub(/\s+/m, ' ').strip.split(' ')
137
+ end
138
+ end
111
139
 
112
140
  # If the phrase doesn't have the same number of words as our nomenclature requires, we can't convert
113
141
  if phrase.length != @dictionaries.length then
@@ -130,6 +158,24 @@ class Jebediah
130
158
  "%07x" % hash
131
159
  end
132
160
 
161
+ def longestMatchInDictionary(words, dict)
162
+ words.count.times.map { |n| words[0..n].join("-") }.select { |phrase| dict.include?(phrase) }.last.split("-")
163
+ end
164
+
165
+ def dehyphenatePhrase(phrase)
166
+ phrase = phrase.split("-") if phrase.is_a?(String)
167
+ split = []
168
+ @dictionaries.each do |dict|
169
+ return nil if phrase.empty?
170
+ match = longestMatchInDictionary(phrase, dict)
171
+ split << match.join("-")
172
+ phrase = phrase[match.count .. -1]
173
+ end
174
+
175
+ return nil unless phrase.empty?
176
+ split
177
+ end
178
+
133
179
  # Convert a hash into a phrase, e.g. "abc4321" -> "rightward succeeded seal"
134
180
  # Hash can be supplied as an integer, or hexadecimal string.
135
181
  #
@@ -157,9 +203,8 @@ class Jebediah
157
203
  # S_n - s_n = S_(n-1),
158
204
 
159
205
  begin
160
- hash = "0x" + hash if hash.is_a?(String) and !hash.start_with?("0x")
161
206
  weight = @dictionaries.inject(1) { |x, dict| dict.length * x } # L0 L1 L2 ... L_n
162
- sum = Integer(hash) % weight
207
+ sum = hashFromString(hash)
163
208
  lines = [ 0 ] * @dictionaries.length # We fill from the end backwards, so allocate the total size up front
164
209
 
165
210
  (@dictionaries.length-1).downto(0) do |n|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jebediah
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Acres
@@ -46,7 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
46
46
  version: '0'
47
47
  requirements: []
48
48
  rubyforge_project:
49
- rubygems_version: 2.0.3
49
+ rubygems_version: 2.0.14
50
50
  signing_key:
51
51
  specification_version: 4
52
52
  summary: Converts hashes to names, and names to hashes