bagua 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NmI4ZTQxZTI2ODM0YTI3ZWQyN2QwMTkyYzdjYjk3NTNhYzNjOTg3NA==
5
+ data.tar.gz: !binary |-
6
+ MjExZDQzODliNzhhNDE2MTE2Nzk3MDUyODVjYWNiYzZmZmM4ZTIwOQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZmExMDczZmU0MDBhMzBmYmVjMjI3OWNkNjYzOTgxMWVkNDc0YTg3ZWFiZTYw
10
+ NDUzZjhlZDBkZjkwYWFjYmEzNGVkN2NiNWZiMjU1YzQzNGRjNGU1MjdlYWU4
11
+ Nzk1NTY5Y2Q2OWQ2ZTdjZDkwZjY5NTMwYTY4ZjBmYmIxMmVmOTk=
12
+ data.tar.gz: !binary |-
13
+ OTIzYWU4MjQ2ODQ0MDRkOTdjYjIwNDU0NjE1NGM3YzQ2ZmUwMTE1OGExZTZj
14
+ YTg1ODg1MDRiY2VmYmE2MDAxMzc1ODY4M2Y5ZWNiYmNkMjE5Mzc5ZTY3Y2Jk
15
+ YjRmZmE5OWU5N2Q5ZDEyNzQwNGNlZjBiNmU2ZmExNTI5Y2FjMWE=
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ .DS_Store
2
+ .yardoc
3
+ Gemfile.lock
4
+ doc
5
+ pkg
6
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ lib/**/*.rb - LICENSE --title Bagua
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Scott Bai
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,69 @@
1
+ # Bagua
2
+
3
+ **Bagua** is a gem that encodes text as strings of [_trigrams_](http://en.wikipedia.org/wiki/Bagua) or [_hexagrams_](http://en.wikipedia.org/wiki/List_of_hexagrams_of_the_I_Ching).
4
+
5
+ Trigrams and hexagrams are symbols used in Taoist cosmology, e.g. in the [I Ching](http://en.wikipedia.org/wiki/I_Ching). A trigram consists of three parallel horizontal lines, each of which can be either broken or unbroken. Because each line can be in one of 2 states, and there are 3 lines in total, there are a total of 2^3 = 8 possible combinations, as shown below.
6
+
7
+ ☰ ☱ ☲ ☳ ☴ ☵ ☶ ☷
8
+
9
+ Each trigram above can be interpreted as a 3-bit binary sequence, where ☰ is 000 and ☷ is 111. Similarly, a hexagram can be interpreted as a 6-bit binary sequence, as it consists of six lines, or two trigrams put together. Consequently, as [noted by Leibniz](http://en.wikipedia.org/wiki/Gottfried_Wilhelm_Leibniz#Sinophile), sequences of trigrams or hexagrams can be used to represent arbitrary binary data.
10
+
11
+ Because a plain text string is just a representation of an array of numbers, it follows that any string can be encoded as a sequence of trigrams or hexagrams. These trigrams and hexagrams themselves are also just plain text (Unicode) characters.
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+ require 'bagua'
17
+ ```
18
+
19
+ ### Encoding
20
+
21
+
22
+ ```ruby
23
+ # trigrams
24
+ Bagua::Tri.encode("abcde") # => "☳☰☲☶☱☱☴☳☳☱☰☶☲☴"
25
+ ```
26
+
27
+ Historically, hexagrams have been listed in several different sequences. **Bagua** can encode hexagrams using either the default binary sequence ([Fu Xi](http://en.wikipedia.org/wiki/Fu_Xi)'s sequence) or the [King Wen sequence](http://en.wikipedia.org/wiki/King_Wen_sequence).
28
+
29
+ ```ruby
30
+ # hexagrams in binary sequence
31
+ Bagua::Hex.encode("abcde", :binary) # => "䷘䷕䷹䷟䷐䷙䷤"
32
+ ```
33
+
34
+
35
+ ```ruby
36
+ # same as above but shorter
37
+ Bagua::Hex.encode("abcde") # => "䷘䷕䷹䷟䷐䷙䷤"
38
+ ```
39
+
40
+
41
+ ```ruby
42
+ # King Wen sequence
43
+ Bagua::Hex.encode("abcde", :wen) # => "䷘䷖䷉䷣䷙䷆䷔"
44
+ ```
45
+
46
+
47
+ ### Decoding
48
+
49
+ ```ruby
50
+ # trigrams
51
+ Bagua::Tri.decode("☳☰☲☶☱☱☴☳☳☱☰☶☲☴") # => "abcde"
52
+ ```
53
+
54
+ ```ruby
55
+ # hexagrams in binary sequence
56
+ Bagua::Hex.decode("䷘䷕䷹䷟䷐䷙䷤", :binary) # => "abcde"
57
+ ```
58
+
59
+
60
+ ```ruby
61
+ # same as above but shorter
62
+ Bagua::Hex.decode("䷘䷕䷹䷟䷐䷙䷤") # => "abcde"
63
+ ```
64
+
65
+
66
+ ```ruby
67
+ # King Wen sequence
68
+ Bagua::Hex.decode("䷘䷖䷉䷣䷙䷆䷔", :wen) # => "abcde"
69
+ ```
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ #! /usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'bundler/gem_tasks'
5
+ require 'rspec/core/rake_task'
6
+
7
+ task :default => :test
8
+ task :test => :spec
9
+
10
+ desc "Run RSpec specs"
11
+ RSpec::Core::RakeTask.new(:spec) do |task|
12
+ task.rspec_opts = '--color --format progress'
13
+ task.fail_on_error = false
14
+ end
data/bagua.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'bagua/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'bagua'
9
+ spec.version = Bagua::VERSION
10
+ spec.authors = ['Scott Bai']
11
+ spec.email = ['hyperplanedrifter@gmail.com']
12
+ spec.description = 'Encodes plain text as sequences of trigrams or hexagrams.'
13
+ spec.summary = 'Trigram/hexagram encoder'
14
+ spec.homepage = 'http://github.com/sbai/bagua'
15
+ spec.license = 'MIT'
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake', '~> 10.0.3'
23
+ spec.add_development_dependency 'yard', '~> 0.8.3'
24
+ spec.add_development_dependency 'redcarpet', '~> 2.2.2'
25
+ spec.add_development_dependency 'rspec', '~> 2.13.0'
26
+ end
data/lib/bagua.rb ADDED
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+ require_relative 'bagua/version'
3
+ require_relative 'bagua/hex'
4
+ require_relative 'bagua/tri'
5
+
6
+ # Provides encoding and decoding functions for trigrams and hexagrams.
7
+ module Bagua
8
+ # Converts an array of bytes (integers representable in 8 bits) to an array
9
+ # of ntets (integers representable in n bits).
10
+ def self.bytes_to_ntets(bytes, n)
11
+ countn = 0
12
+ current = 0
13
+ ntets = []
14
+ bytes.each do |byte|
15
+ 7.downto(0) do |b|
16
+ bit = (byte >> b) & 1
17
+ current = (current << 1) | bit
18
+ countn = (countn + 1) % n
19
+ if (countn == 0)
20
+ ntets.push(current)
21
+ current = 0
22
+ end
23
+ end
24
+ end
25
+
26
+ # handle leftover bits when n doesn't divide evenly into bytes.length*8
27
+ if (countn > 0)
28
+ current = current << (n - countn)
29
+ ntets.push(current)
30
+ end
31
+
32
+ return ntets
33
+ end
34
+
35
+ # Converts an array of ntets (integers representable in n bits) to an array
36
+ # of bytes (integers representable in 8 bits).
37
+ def self.ntets_to_bytes(ntets, n)
38
+ count8 = 0
39
+ current = 0
40
+ bytes = []
41
+ ntets.each do |ntet|
42
+ (n - 1).downto(0) do |b|
43
+ bit = (ntet >> b) & 1
44
+ current = (current << 1) | bit
45
+ count8 = (count8 + 1) % 8
46
+ if (count8 == 0)
47
+ bytes.push(current)
48
+ current = 0
49
+ end
50
+ end
51
+ end
52
+
53
+ # handle leftover bits when 8 doesn't divide evenly into ntets.length*n
54
+ if (count8 > 0)
55
+ current = current << (8 - count8)
56
+ bytes.push(current)
57
+ end
58
+
59
+ return bytes
60
+ end
61
+ end
data/lib/bagua/hex.rb ADDED
@@ -0,0 +1,139 @@
1
+ module Bagua
2
+ # Provides encoding and decoding functions for hexagrams.
3
+ class Hex
4
+ # Encodes a string into a string of hexagrams representing the byte values
5
+ # of each character in the string.
6
+ def self.encode(str, sequence = :binary)
7
+ return from_bytes(str.unpack('C*'), sequence)
8
+ end
9
+
10
+ # Decodes a string of hexagrams into a UTF-8 string.
11
+ # @note Chomps trailing null characters, as they are the result of zero
12
+ # padding added when the number of bits being encoded in the hexagrams
13
+ # is not an even multiple of 6.
14
+ def self.decode(grams, sequence = :binary)
15
+ return to_bytes(grams, sequence).pack('C*').chomp("\000").force_encoding('utf-8')
16
+ end
17
+
18
+ # Returns the hexagram representation of the input bytes.
19
+ def self.from_bytes(bytes, sequence = :binary)
20
+ sextets = Bagua::bytes_to_ntets(bytes, 6)
21
+
22
+ if (sequence == :wen)
23
+ return sextets.map { |sextet| gram(sextet) }.join("")
24
+ elsif (sequence == :binary)
25
+ return sextets.map { |sextet| gram_bin(sextet) }.join("")
26
+ else
27
+ raise "invalid sequence type"
28
+ end
29
+ end
30
+
31
+ # Returns the byte representation of a string of hexagrams.
32
+ def self.to_bytes(grams, sequence = :binary)
33
+ ucodes = grams.unpack('U*')
34
+
35
+ sextets = ucodes.map { |ucode| ucode - HEX_BASE }
36
+
37
+ if (sequence == :wen)
38
+ elsif (sequence == :binary)
39
+ wen_to_bin = BIN_TO_WEN.invert()
40
+ sextets.map! { |sextet| wen_to_bin[sextet]}
41
+ else
42
+ raise "invalid sequence type"
43
+ end
44
+
45
+ bytes = Bagua::ntets_to_bytes(sextets, 6)
46
+ return bytes
47
+ end
48
+
49
+ private
50
+
51
+ # Decimal representation of the Unicode code point of the first hexagram
52
+ # character. The code point for any hexagram can by obtained by adding an
53
+ # offset between 0 and 63 to HEX_BASE.
54
+ HEX_BASE = 19904
55
+
56
+ # Binary sequence mapping for hexagram characters. This is needed because
57
+ # the hexagram characters are ordered by King Wen's sequence in Unicode.
58
+ BIN_TO_WEN = {
59
+ 0 => 0,
60
+ 1 => 42,
61
+ 2 => 13,
62
+ 3 => 33,
63
+ 4 => 8,
64
+ 5 => 4,
65
+ 6 => 25,
66
+ 7 => 10,
67
+ 8 => 9,
68
+ 9 => 57,
69
+ 10 => 37,
70
+ 11 => 53,
71
+ 12 => 60,
72
+ 13 => 59,
73
+ 14 => 40,
74
+ 15 => 18,
75
+ 16 => 12,
76
+ 17 => 48,
77
+ 18 => 29,
78
+ 19 => 54,
79
+ 20 => 36,
80
+ 21 => 62,
81
+ 22 => 21,
82
+ 23 => 35,
83
+ 24 => 24,
84
+ 25 => 16,
85
+ 26 => 20,
86
+ 27 => 50,
87
+ 28 => 41,
88
+ 29 => 2,
89
+ 30 => 26,
90
+ 31 => 23,
91
+ 32 => 43,
92
+ 33 => 27,
93
+ 34 => 49,
94
+ 35 => 31,
95
+ 36 => 56,
96
+ 37 => 47,
97
+ 38 => 17,
98
+ 39 => 45,
99
+ 40 => 5,
100
+ 41 => 46,
101
+ 42 => 63,
102
+ 43 => 39,
103
+ 44 => 58,
104
+ 45 => 28,
105
+ 46 => 3,
106
+ 47 => 6,
107
+ 48 => 32,
108
+ 49 => 30,
109
+ 50 => 55,
110
+ 51 => 61,
111
+ 52 => 52,
112
+ 53 => 38,
113
+ 54 => 51,
114
+ 55 => 14,
115
+ 56 => 11,
116
+ 57 => 44,
117
+ 58 => 34,
118
+ 59 => 15,
119
+ 60 => 19,
120
+ 61 => 7,
121
+ 62 => 22,
122
+ 63 => 1
123
+ }
124
+
125
+ # Returns the hexagram in King Wen's sequence that corresponds to the
126
+ # number num.
127
+ # @note If num >= 64, only the lowest six bits are used.
128
+ def self.gram(num)
129
+ return [HEX_BASE + (num & 63)].pack('U')
130
+ end
131
+
132
+ # Returns the hexagram in binary sequence that corresponds to the number
133
+ # num.
134
+ # @note If num >= 64, only the lowest six bits are used.
135
+ def self.gram_bin(num)
136
+ return gram(BIN_TO_WEN[num & 63])
137
+ end
138
+ end
139
+ end
data/lib/bagua/tri.rb ADDED
@@ -0,0 +1,49 @@
1
+ module Bagua
2
+ # Provides encoding and decoding functions for trigrams.
3
+ class Tri
4
+ # Encodes a string into a string of trigrams representing the byte values
5
+ # of each character in the string.
6
+ def self.encode(str)
7
+ return from_bytes(str.unpack('C*'))
8
+ end
9
+
10
+ # Decodes a string of trigrams into a UTF-8 string.
11
+ # @note Chomps trailing null characters, as they are the result of zero
12
+ # padding added when the number of bits being encoded in the trigrams
13
+ # is not an even multiple of 3.
14
+ # @todo Add an option to choose the encoding.
15
+ def self.decode(grams)
16
+ return to_bytes(grams).pack('C*').chomp("\000").force_encoding('utf-8')
17
+ end
18
+
19
+ # Returns the trigram representation of the input bytes.
20
+ def self.from_bytes(bytes)
21
+ trios = Bagua::bytes_to_ntets(bytes, 3)
22
+ grams = trios.map { |trio| gram(trio) }
23
+ return grams.join("")
24
+ end
25
+
26
+ # Returns the byte representation of a string of trigrams.
27
+ def self.to_bytes(grams)
28
+ ucodes = grams.unpack('U*')
29
+ trios = ucodes.map { |ucode| ucode - TRI_BASE }
30
+ bytes = Bagua::ntets_to_bytes(trios, 3)
31
+ return bytes
32
+ end
33
+
34
+ private
35
+
36
+ # Decimal representation of the Unicode code point of the first trigram
37
+ # character. The code point for any trigram can by obtained by adding an
38
+ # offset between 0 and 7 to TRI_BASE.
39
+ TRI_BASE = 9776
40
+
41
+ # Returns the trigram representation of num.
42
+ # If num >= 8, only the lowest three bits are used.
43
+ # @note The trigrams are arranged in binary order, as they are arranged in
44
+ # Unicode.
45
+ def self.gram(num)
46
+ return [TRI_BASE + (num & 7)].pack('U')
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module Bagua
2
+ VERSION = "0.1.0" # Version number of this module.
3
+ end
@@ -0,0 +1,6 @@
1
+ 0,䷀䷀
2
+ 1,䷀䷐
3
+ 255,䷿䷰
4
+ 0,0,䷀䷀䷀
5
+ 255,255,䷿䷿䷼
6
+ 255,255,255,䷿䷿䷿䷿
@@ -0,0 +1,6 @@
1
+ 0,䷀䷀
2
+ 1,䷀䷌
3
+ 255,䷁䷠
4
+ 0,0,䷀䷀䷀
5
+ 255,255,䷁䷁䷓
6
+ 255,255,255,䷁䷁䷁䷁
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ require 'bagua'
4
+
5
+ module Bagua
6
+ describe "Hex" do
7
+ describe "::from_bytes" do
8
+ before(:all) do
9
+ require 'csv'
10
+ options = { :converters => [ :numeric ] }
11
+ @examples_wen = CSV.open('spec/bagua/hex/from_bytes.csv', 'r', options)
12
+ @examples_bin = CSV.open('spec/bagua/hex/from_bytes_bin.csv', 'r', options)
13
+ end
14
+
15
+ it "works for all King Wen sequence examples" do
16
+ @examples_wen.each do |row|
17
+ Hex.from_bytes(row[0..-2],:wen).should == row[-1]
18
+ end
19
+ end
20
+
21
+ it "works for all binary sequence examples" do
22
+ @examples_bin.each do |row|
23
+ Hex.from_bytes(row[0..-2],:binary).should == row[-1]
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,64 @@
1
+ 0,䷀
2
+ 1,䷁
3
+ 2,䷂
4
+ 3,䷃
5
+ 4,䷄
6
+ 5,䷅
7
+ 6,䷆
8
+ 7,䷇
9
+ 8,䷈
10
+ 9,䷉
11
+ 10,䷊
12
+ 11,䷋
13
+ 12,䷌
14
+ 13,䷍
15
+ 14,䷎
16
+ 15,䷏
17
+ 16,䷐
18
+ 17,䷑
19
+ 18,䷒
20
+ 19,䷓
21
+ 20,䷔
22
+ 21,䷕
23
+ 22,䷖
24
+ 23,䷗
25
+ 24,䷘
26
+ 25,䷙
27
+ 26,䷚
28
+ 27,䷛
29
+ 28,䷜
30
+ 29,䷝
31
+ 30,䷞
32
+ 31,䷟
33
+ 32,䷠
34
+ 33,䷡
35
+ 34,䷢
36
+ 35,䷣
37
+ 36,䷤
38
+ 37,䷥
39
+ 38,䷦
40
+ 39,䷧
41
+ 40,䷨
42
+ 41,䷩
43
+ 42,䷪
44
+ 43,䷫
45
+ 44,䷬
46
+ 45,䷭
47
+ 46,䷮
48
+ 47,䷯
49
+ 48,䷰
50
+ 49,䷱
51
+ 50,䷲
52
+ 51,䷳
53
+ 52,䷴
54
+ 53,䷵
55
+ 54,䷶
56
+ 55,䷷
57
+ 56,䷸
58
+ 57,䷹
59
+ 58,䷺
60
+ 59,䷻
61
+ 60,䷼
62
+ 61,䷽
63
+ 62,䷾
64
+ 63,䷿
@@ -0,0 +1,64 @@
1
+ 0,䷀
2
+ 1,䷪
3
+ 2,䷍
4
+ 3,䷡
5
+ 4,䷈
6
+ 5,䷄
7
+ 6,䷙
8
+ 7,䷊
9
+ 8,䷉
10
+ 9,䷹
11
+ 10,䷥
12
+ 11,䷵
13
+ 12,䷼
14
+ 13,䷻
15
+ 14,䷨
16
+ 15,䷒
17
+ 16,䷌
18
+ 17,䷰
19
+ 18,䷝
20
+ 19,䷶
21
+ 20,䷤
22
+ 21,䷾
23
+ 22,䷕
24
+ 23,䷣
25
+ 24,䷘
26
+ 25,䷐
27
+ 26,䷔
28
+ 27,䷲
29
+ 28,䷩
30
+ 29,䷂
31
+ 30,䷚
32
+ 31,䷗
33
+ 32,䷫
34
+ 33,䷛
35
+ 34,䷱
36
+ 35,䷟
37
+ 36,䷸
38
+ 37,䷯
39
+ 38,䷑
40
+ 39,䷭
41
+ 40,䷅
42
+ 41,䷮
43
+ 42,䷿
44
+ 43,䷧
45
+ 44,䷺
46
+ 45,䷜
47
+ 46,䷃
48
+ 47,䷆
49
+ 48,䷠
50
+ 49,䷞
51
+ 50,䷷
52
+ 51,䷽
53
+ 52,䷴
54
+ 53,䷦
55
+ 54,䷳
56
+ 55,䷎
57
+ 56,䷋
58
+ 57,䷬
59
+ 58,䷢
60
+ 59,䷏
61
+ 60,䷓
62
+ 61,䷇
63
+ 62,䷖
64
+ 63,䷁
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ require 'bagua'
4
+
5
+ module Bagua
6
+ describe "Hex" do
7
+ describe "::gram" do
8
+ before(:all) do
9
+ require 'csv'
10
+ options = { :converters => [ :numeric ] }
11
+ @examples = CSV.open('spec/bagua/hex/gram.csv', 'r', options)
12
+ end
13
+
14
+ it "works for all example valid inputs" do
15
+ @examples.each do |row|
16
+ Hex.gram(row[0]).should == row[1]
17
+ end
18
+ end
19
+ end
20
+
21
+ describe "::gram_bin" do
22
+ before(:all) do
23
+ require 'csv'
24
+ options = { :converters => [ :numeric ] }
25
+ @examples = CSV.open('spec/bagua/hex/gram_bin.csv', 'r', options)
26
+ end
27
+
28
+ it "works for all example valid inputs" do
29
+ @examples.each do |row|
30
+ Hex.gram_bin(row[0]).should == row[1]
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ require 'bagua'
4
+
5
+ module Bagua
6
+ describe "Hex" do
7
+ before(:all) do
8
+ require 'csv'
9
+ options = { :converters => [ :numeric ] }
10
+ @examples = CSV.open('spec/bagua/lorem.csv', 'r', options)
11
+ end
12
+
13
+ it "decode(encode(str)) should equal str" do
14
+ @examples.each do |row|
15
+ Hex.decode(Hex.encode(row[0])).should == row[0]
16
+ end
17
+ end
18
+
19
+ it "decode(encode(str,:wen),:wen) should equal str" do
20
+ @examples.each do |row|
21
+ Hex.decode(Hex.encode(row[0],:wen),:wen).should == row[0]
22
+ end
23
+ end
24
+
25
+ it "decode(encode(str,:binary),:binary) should equal str" do
26
+ @examples.each do |row|
27
+ Hex.decode(Hex.encode(row[0],:binary),:binary).should == row[0]
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean tristique massa odio, in rhoncus ipsum consectetur id. Donec accumsan mauris at quam cursus sagittis. Nunc ornare urna vitae dui interdum, a dictum diam venenatis. Sed varius porttitor ante vitae sodales. Etiam eu suscipit leo. Pellentesque eget placerat turpis. Duis bibendum nunc turpis. Nam bibendum mi nec risus viverra, ut gravida lacus mollis. Pellentesque sed ornare enim, non iaculis elit. Morbi laoreet facilisis sodales. Pellentesque ac elit elit. Aliquam accumsan condimentum rhoncus. Donec tristique orci eu enim ornare, sed ullamcorper mi sollicitudin. Suspendisse potenti. Maecenas id ornare nisi. Sed sed fringilla turpis."
2
+ "開無就掲昭質違併更作度済円県新気。集別評内結提家権携理来部果残取厘諭害綺国。殺成報荏山書勝浴夜誉組困。車希併昇辞小置紀保判整高代購富。氏電話将到手稿設力牟相留証劇応質。馬検遊展院治新二準果一子。向俳処体障演高令公政降申過年根供信新元阪。外認予闘生来時安創供止例趣瞬書。現写拡号所兵変草薬載回仕品多四目視無食乾。"
3
+ "Эю окюррырэт зэнтынтиаэ мыа, эи эжт адмодум квюаырэндум. Эа зймюл дэбетиз про, хаж долорюм долорэм йн. Ипзум антеопам но прё, пэр эа граэкы промпта модыратиюз. Экз ыюм клита лэгыры, но эжт чтэт ёнанй орнатюс."
4
+ "計お職文へづ文社ハモ聞1短ヌレ透重ア光最ス内指フヱヘ誌広白ツテフ越8人ユケ昨免髪事フクべっ国洗も投最評済包ま。資き禁測長きーたは田組ハホキウ護掲ヘサメ載方ニミ下界ワトネヘ夜場すうでラ百球ンめわ者歳ぜへぎク名1次業スぼちょ状長やべつ躍済チト首阪のル読道事ンぜむ之細き理督積ざるく。31想将抗タエ特決セチ均妙だ堂対経ゆ覚早ヲネキ点属ぶ作信くごゆ特45干悟栽ろぜっ。"
@@ -0,0 +1,14 @@
1
+ 0,☰☰☰
2
+ 1,☰☰☲
3
+ 7,☰☱☶
4
+ 8,☰☲☰
5
+ 16,☰☴☰
6
+ 32,☱☰☰
7
+ 64,☲☰☰
8
+ 128,☴☰☰
9
+ 255,☷☷☶
10
+ 255,0,☷☷☶☰☰☰
11
+ 255,1,☷☷☶☰☰☴
12
+ 255,255,☷☷☷☷☷☴
13
+ 0,0,0,0,0,0,☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰
14
+ 255,255,255,255,255,255,☷☷☷☷☷☷☷☷☷☷☷☷☷☷☷☷
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+
3
+ require 'bagua'
4
+
5
+ module Bagua
6
+ describe "Tri" do
7
+ describe "::from_bytes" do
8
+ before(:all) do
9
+ require 'csv'
10
+ options = { :converters => [ :numeric ] }
11
+ @examples = CSV.open('spec/bagua/tri/from_bytes.csv', 'r', options)
12
+ end
13
+
14
+ it "works for all example valid inputs" do
15
+ @examples.each do |row|
16
+ Tri.from_bytes(row[0..-2]).should == row[-1]
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ 0,☰
2
+ 1,☱
3
+ 2,☲
4
+ 3,☳
5
+ 4,☴
6
+ 5,☵
7
+ 6,☶
8
+ 7,☷
9
+ 8,☰
10
+ 9,☱
11
+ 10,☲
12
+ 11,☳
13
+ 12,☴
14
+ 13,☵
15
+ 14,☶
16
+ 15,☷
17
+ 1024,☰
18
+ 1025,☱
19
+ 1026,☲
20
+ 1027,☳
21
+ 1028,☴
22
+ 1029,☵
23
+ 1030,☶
24
+ 1031,☷
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+
3
+ require 'bagua'
4
+
5
+ module Bagua
6
+ describe "Tri" do
7
+ describe "::gram" do
8
+ before(:all) do
9
+ require 'csv'
10
+ options = { :converters => [ :numeric ] }
11
+ @examples = CSV.open('spec/bagua/tri/gram.csv', 'r', options)
12
+ end
13
+
14
+ it "works for all example valid inputs" do
15
+ @examples.each do |row|
16
+ Tri.gram(row[0]).should == row[1]
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ require 'bagua'
4
+
5
+ module Bagua
6
+ describe "Tri" do
7
+ before(:all) do
8
+ require 'csv'
9
+ options = { :converters => [ :numeric ] }
10
+ @examples = CSV.open('spec/bagua/lorem.csv', 'r', options)
11
+ end
12
+
13
+ it "decode(encode(str)) should equal str" do
14
+ @examples.each do |row|
15
+ Tri.decode(Tri.encode(row[0])).should == row[0]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ # This file was generated by the `rspec --init` command. Conventionally, all
7
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
8
+ # Require this file using `require "spec_helper"` to ensure that it is only
9
+ # loaded once.
10
+ #
11
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
12
+ RSpec.configure do |config|
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ config.run_all_when_everything_filtered = true
15
+ config.filter_run :focus
16
+
17
+ # Run specs in random order to surface order dependencies. If you find an
18
+ # order dependency and want to debug it, you can fix the order by providing
19
+ # the seed, which is printed after each run.
20
+ # --seed 1234
21
+ config.order = 'random'
22
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bagua
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Scott Bai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-25 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
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.3
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.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: yard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.8.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.8.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: redcarpet
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.2.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 2.2.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 2.13.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 2.13.0
83
+ description: Encodes plain text as sequences of trigrams or hexagrams.
84
+ email:
85
+ - hyperplanedrifter@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - .yardopts
93
+ - Gemfile
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - bagua.gemspec
98
+ - lib/bagua.rb
99
+ - lib/bagua/hex.rb
100
+ - lib/bagua/tri.rb
101
+ - lib/bagua/version.rb
102
+ - spec/bagua/hex/from_bytes.csv
103
+ - spec/bagua/hex/from_bytes_bin.csv
104
+ - spec/bagua/hex/from_bytes_spec.rb
105
+ - spec/bagua/hex/gram.csv
106
+ - spec/bagua/hex/gram_bin.csv
107
+ - spec/bagua/hex/gram_spec.rb
108
+ - spec/bagua/hex/hex_spec.rb
109
+ - spec/bagua/lorem.csv
110
+ - spec/bagua/tri/from_bytes.csv
111
+ - spec/bagua/tri/from_bytes_spec.rb
112
+ - spec/bagua/tri/gram.csv
113
+ - spec/bagua/tri/gram_spec.rb
114
+ - spec/bagua/tri/tri_spec.rb
115
+ - spec/spec_helper.rb
116
+ homepage: http://github.com/sbai/bagua
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.0.7
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Trigram/hexagram encoder
140
+ test_files:
141
+ - spec/bagua/hex/from_bytes.csv
142
+ - spec/bagua/hex/from_bytes_bin.csv
143
+ - spec/bagua/hex/from_bytes_spec.rb
144
+ - spec/bagua/hex/gram.csv
145
+ - spec/bagua/hex/gram_bin.csv
146
+ - spec/bagua/hex/gram_spec.rb
147
+ - spec/bagua/hex/hex_spec.rb
148
+ - spec/bagua/lorem.csv
149
+ - spec/bagua/tri/from_bytes.csv
150
+ - spec/bagua/tri/from_bytes_spec.rb
151
+ - spec/bagua/tri/gram.csv
152
+ - spec/bagua/tri/gram_spec.rb
153
+ - spec/bagua/tri/tri_spec.rb
154
+ - spec/spec_helper.rb
155
+ has_rdoc: