proquint 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 600b3df2f02f5225349f541dc4266bd25130cf68
4
+ data.tar.gz: af78b6ac37f47f43f47835356f73c0daec4f6c14
5
+ SHA512:
6
+ metadata.gz: d256e818fea8f45a4c09253dfb72bec737c153dae86a4577f5fb488c4a6b8c2b00a5484d8c985b68050538acdd9f47cd3e46a00fdf35a1943ac7db5cc686fd9b
7
+ data.tar.gz: 459f5580334a2cb37993da41c7122c38dba34fcb40f822483eaba85501d4f9e7999ae2f6410d8dabf780b5efc54c97f56d2651b8a70e4a3fc1b9bbe9d00a712b
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in proquint.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 JJ Buckley
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.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Proquint
2
+
3
+ A library for converting numbers to readable, pronounceable, spellable
4
+ identifiers.
5
+
6
+ See http://arxiv.org/html/0901.4016.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'proquint'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install proquint
23
+
24
+ ## Usage
25
+
26
+ require 'proquint'
27
+ Proquint.encode(1) #=> "babad"
28
+ Proquint.encode(1234567890123456) #=> "babah-karij-gufap-rorab"
29
+ Proquint.encode(0x462d53c8abac0) #=> "babah-karij-gufap-rorab"
30
+ Proquint.encode(0x1234567890abcdef) #=> "damuh-jinum-nafor-suloz"
31
+ Proquint.decode("nobol-gonad") #=> [38951, 14913]
32
+ Proquint.words2num([38951, 14913]) #=> 2552707649
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it ( https://github.com/[my-github-username]/proquint/fork )
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList["test/**/*_test.rb"]
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,3 @@
1
+ module Proquint
2
+ VERSION = "0.0.1"
3
+ end
data/lib/proquint.rb ADDED
@@ -0,0 +1,96 @@
1
+ require "proquint/version"
2
+
3
+ # A Proquint is a readable, spellable, pronouncable representation of a
4
+ # number. In Ruby, a proquint is a string, such as "badab-lufiv", which
5
+ # represents the number 0x407d92 (or 4226450 in decimal).
6
+ #
7
+ # See http://arxiv.org/html/0901.4016 for more details.
8
+ #
9
+ # The main API is provided by `encode` and `decode`, the former of which takes
10
+ # a number, or an array of words (unsigned 16-bit integers) to make a
11
+ # proquint, and the latter of which takes a proquint and returns an array of
12
+ # words. There are also some convenience methods for getting a complete number
13
+ # from an array of words, for getting an array of words from a number, and for
14
+ # converting between dotted-decimal IP-addresses and hex IP addresses.
15
+ module Proquint
16
+ CONSONANTS = %w[b d f g h j k l m n p r s t v z]
17
+ VOWELS = %w[a i o u]
18
+ REVERSE = {}
19
+ CONSONANTS.each_with_index { |c, i| REVERSE[c] = i }
20
+ VOWELS.each_with_index { |c, i| REVERSE[c] = i }
21
+ DQUAD_PATTERN = /^(\d{1,3}).(\d{1,3}).(\d{1,3}).(\d{1,3})$/
22
+ HEX_PATTERN = /^x(\h+)$/
23
+
24
+ def dquad2hex(dquad)
25
+ if dquad =~ DQUAD_PATTERN
26
+ i = (($1.to_i << 24) + ($2.to_i << 16) + ($3.to_i << 8) + $4.to_i)
27
+ "x#{i.to_s(16)}"
28
+ else
29
+ raise ArgumentError.new("Invalid dotted quad")
30
+ end
31
+ end
32
+
33
+ def hex2dquad(hex)
34
+ if hex =~ HEX_PATTERN
35
+ i = $1.to_i(16)
36
+ [(i & 0xff000000) >> 24, (i & 0xff0000) >> 16, (i & 0xff00) >> 8, i & 0xff].join('.')
37
+ else
38
+ raise ArgumentError.new("Invalid hex address")
39
+ end
40
+ end
41
+
42
+ def num2words(num)
43
+ words = [num & 0xffff]
44
+ while num > 0xffff
45
+ num >>= 16
46
+ words.unshift(num & 0xffff)
47
+ end
48
+ words
49
+ end
50
+
51
+ def words2num(words)
52
+ num = words.shift || 0
53
+ until words.empty?
54
+ num <<= 16
55
+ num += words.shift
56
+ end
57
+ num
58
+ end
59
+
60
+ # Convert a string, number or array of uint16s to a proquint
61
+ def encode(i, sep = "-")
62
+ case i
63
+ when Fixnum
64
+ raise ArgumentError.new("Can't encode negative numbers") if i < 0
65
+ return encode(num2words(i)) if i > 0xffff
66
+ CONSONANTS[(i & 0xf000) >> 12] +
67
+ VOWELS[(i & 0xc00) >> 10] +
68
+ CONSONANTS[(i & 0x3c0) >> 6] +
69
+ VOWELS[(i & 0x30) >> 4] +
70
+ CONSONANTS[i & 0xf]
71
+ when Array then i.map { |x| encode(x) }.join(sep)
72
+ when DQUAD_PATTERN then encode(dquad2hex(i)[1..-1].to_i(16))
73
+ when String then encode(str2words(i))
74
+ else raise ArgumentError.new("Can't encode #{i}")
75
+ end
76
+ end
77
+
78
+ # Convert a proquint to an array of numbers (between 0 and 0xffff)
79
+ def decode(s, sep = "-")
80
+ s.split(sep).map do |p|
81
+ p = p.downcase
82
+ raise ArgumentError.new("Invalid proquint") unless p.bytes.length == 5
83
+ (REVERSE[p[0]] << 12) +
84
+ (REVERSE[p[1]] << 10) +
85
+ (REVERSE[p[2]] << 6) +
86
+ (REVERSE[p[3]] << 4) +
87
+ (REVERSE[p[4]])
88
+ end
89
+ end
90
+
91
+ # Convert a proquint to a number
92
+ def quint2num(s, sep = "-")
93
+ words2num(decode(s))
94
+ end
95
+ extend self
96
+ end
data/proquint.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'proquint/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "proquint"
8
+ spec.version = Proquint::VERSION
9
+ spec.authors = ["JJ Buckley"]
10
+ spec.email = ["jj@bjjb.org"]
11
+ spec.summary = %q{Readable, spellable, pronounceable identifiers}
12
+ spec.homepage = "http://bjjb.github.io/proquint"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "minitest", "~> 5.4.3"
23
+ end
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+ require 'proquint'
3
+
4
+ describe Proquint do
5
+ it "can convert dotted quads to hex" do
6
+ Proquint.dquad2hex('127.0.0.1').must_equal 'x7f000001'
7
+ Proquint.dquad2hex('123.123.123.123').must_equal 'x7b7b7b7b'
8
+ end
9
+
10
+ it "can convert hex to dotted quad" do
11
+ Proquint.hex2dquad('x7f000001').must_equal '127.0.0.1'
12
+ Proquint.hex2dquad('x7b7b7b7b').must_equal '123.123.123.123'
13
+ end
14
+
15
+ it "can split large numbers into unsigned 16-bit integers" do
16
+ Proquint.num2words(0x1).must_equal [1]
17
+ Proquint.num2words(0x1111).must_equal [0x1111]
18
+ Proquint.num2words(0x10000).must_equal [1, 0]
19
+ Proquint.num2words(0x10001).must_equal [1, 1]
20
+ Proquint.num2words(0xa000a000a).must_equal [10, 10, 10]
21
+ end
22
+
23
+ it "can combine an array or unsigned 16-bit integers" do
24
+ Proquint.words2num([1]).must_equal 1
25
+ Proquint.words2num([1, 1]).must_equal 0x10001
26
+ Proquint.words2num([1, 0]).must_equal 0x10000
27
+ Proquint.words2num([10, 10, 10]).must_equal 0xa000a000a
28
+ end
29
+
30
+ it "can encode numbers" do
31
+ Proquint.encode(0).must_equal 'babab'
32
+ Proquint.encode(0x10000).must_equal 'babad-babab'
33
+ Proquint.encode(0x7f000001).must_equal 'lusab-babad'
34
+ Proquint.encode(1234567890123456).must_equal 'babah-karij-gufap-rorab'
35
+ Proquint.encode(0x1234567890abcdef).must_equal 'damuh-jinum-nafor-suloz'
36
+ end
37
+
38
+ it "can encode and decode IP addresses" do
39
+ Proquint.encode("192.168.1.1").must_equal "safom-bahad"
40
+ Proquint.decode("safom-bahad").must_equal [49320, 257]
41
+ end
42
+
43
+ it "can decode proquints" do
44
+ Proquint.decode('babab').must_equal [0]
45
+ Proquint.decode('babad-babab').must_equal [1, 0]
46
+ Proquint.decode('lusab-babad').must_equal [0x7f00, 1]
47
+ end
48
+ end
@@ -0,0 +1 @@
1
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proquint
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - JJ Buckley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.4.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.4.3
55
+ description:
56
+ email:
57
+ - jj@bjjb.org
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/proquint.rb
68
+ - lib/proquint/version.rb
69
+ - proquint.gemspec
70
+ - test/proquint_test.rb
71
+ - test/test_helper.rb
72
+ homepage: http://bjjb.github.io/proquint
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.2
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Readable, spellable, pronounceable identifiers
96
+ test_files:
97
+ - test/proquint_test.rb
98
+ - test/test_helper.rb