asciipack 0.1.2 → 0.1.3
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 +4 -4
- data/lib/asciipack/packer.rb +28 -30
- data/lib/asciipack/unpacker.rb +51 -35
- data/lib/asciipack/version.rb +1 -1
- data/spec/bench.rb +4 -2
- data/spec/format_spec.rb +16 -16
- data/spec/spec_helper.rb +65 -1
- metadata +2 -3
- data/lib/asciipack/typemap.rb +0 -68
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12c016c4cbf6dc6932200cd28774219195a6897b
|
4
|
+
data.tar.gz: db20a4941ad342f86992244148e3fda496f32c5c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd8780bb46e2fa092cd8035efccd1174e6f8c7c659f2d3f2a702694e46f29d5dc78cd555e21678dbdadba2b63f2feef17e51d0ac8726b06f4a036d3758607282
|
7
|
+
data.tar.gz: f0a6ec0a80d7f49f28e5c08177323779e8c3491f0084d3742e09e596bb7311c59c214252e2957a9f2ccbe7da9063a9d8d71fd221ab207404c340fbc88fcbdd6f
|
data/lib/asciipack/packer.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'asciipack/typemap.rb'
|
2
|
-
|
3
1
|
module AsciiPack
|
4
2
|
class Packer
|
5
3
|
class << self
|
@@ -55,38 +53,38 @@ module AsciiPack
|
|
55
53
|
raise "pack size limit over"
|
56
54
|
end
|
57
55
|
when obj.nil?
|
58
|
-
|
56
|
+
"W"
|
59
57
|
when obj == false
|
60
|
-
|
58
|
+
"X"
|
61
59
|
when obj == true
|
62
|
-
|
60
|
+
"Y"
|
63
61
|
end
|
64
62
|
end
|
65
63
|
|
66
64
|
private
|
67
65
|
|
68
66
|
def int4 (obj)
|
69
|
-
|
67
|
+
"a" + ((obj & 0xf).to_s(16))
|
70
68
|
end
|
71
69
|
|
72
70
|
def int8 (obj)
|
73
|
-
|
71
|
+
"b" + ((obj & 0xff).to_s(16))
|
74
72
|
end
|
75
73
|
|
76
74
|
def int16 (obj)
|
77
|
-
|
75
|
+
"c" + ((obj & 0xffff).to_s(16))
|
78
76
|
end
|
79
77
|
|
80
78
|
def int16 (obj)
|
81
|
-
|
79
|
+
"c" + ((obj & 0xffff).to_s(16))
|
82
80
|
end
|
83
81
|
|
84
82
|
def int32 (obj)
|
85
|
-
|
83
|
+
"d" + ((obj & 0xffffffff).to_s(16))
|
86
84
|
end
|
87
85
|
|
88
86
|
def int64 (obj)
|
89
|
-
|
87
|
+
"e" + ((obj & 0xffffffffffffffff).to_s(16))
|
90
88
|
end
|
91
89
|
|
92
90
|
def positive_fixint (obj)
|
@@ -94,27 +92,27 @@ module AsciiPack
|
|
94
92
|
end
|
95
93
|
|
96
94
|
def uint8 (obj)
|
97
|
-
format_uint(
|
95
|
+
format_uint("g", 2, obj)
|
98
96
|
end
|
99
97
|
|
100
98
|
def uint16 (obj)
|
101
|
-
format_uint(
|
99
|
+
format_uint("h", 4, obj)
|
102
100
|
end
|
103
101
|
|
104
102
|
def uint32 (obj)
|
105
|
-
format_uint(
|
103
|
+
format_uint("i", 8, obj)
|
106
104
|
end
|
107
105
|
|
108
106
|
def uint64 (obj)
|
109
|
-
format_uint(
|
107
|
+
format_uint("j", 16, obj)
|
110
108
|
end
|
111
109
|
|
112
110
|
def float64 (obj)
|
113
111
|
unless obj.finite?
|
114
112
|
case obj.infinite?
|
115
|
-
when 1; return
|
116
|
-
when -1; return
|
117
|
-
else; return
|
113
|
+
when 1; return "l" + '7ff0000000000000' # +∞
|
114
|
+
when -1; return "l" + 'fff0000000000000' # -∞
|
115
|
+
else; return "l" + '7fffffffffffffff' # NAN
|
118
116
|
end
|
119
117
|
end
|
120
118
|
|
@@ -126,7 +124,7 @@ module AsciiPack
|
|
126
124
|
exp |= 0x800 if sign
|
127
125
|
high = ((frac / 0x100000000).to_i & 0xfffff) | (exp << 20)
|
128
126
|
|
129
|
-
|
127
|
+
"l" + to_s16([
|
130
128
|
(high >> 24) & 0xff, (high >> 16) & 0xff,
|
131
129
|
(high >> 8) & 0xff, (high) & 0xff,
|
132
130
|
(low >> 24) & 0xff, (low >> 16) & 0xff,
|
@@ -139,15 +137,15 @@ module AsciiPack
|
|
139
137
|
end
|
140
138
|
|
141
139
|
def str8 (obj)
|
142
|
-
format_str
|
140
|
+
format_str "n", 2, obj
|
143
141
|
end
|
144
142
|
|
145
143
|
def str16 (obj)
|
146
|
-
format_str
|
144
|
+
format_str "o", 4, obj
|
147
145
|
end
|
148
146
|
|
149
147
|
def str32 (obj)
|
150
|
-
format_str
|
148
|
+
format_str "p", 8, obj
|
151
149
|
end
|
152
150
|
|
153
151
|
def map (obj)
|
@@ -156,13 +154,13 @@ module AsciiPack
|
|
156
154
|
keys.push(pack(key) + pack(value))
|
157
155
|
}
|
158
156
|
if keys.length < 0x10
|
159
|
-
f = [
|
157
|
+
f = ["r", 1]
|
160
158
|
elsif keys.length < 0x100
|
161
|
-
f = [
|
159
|
+
f = ["s", 2]
|
162
160
|
elsif keys.length < 0x10000
|
163
|
-
f = [
|
161
|
+
f = ["t", 4]
|
164
162
|
elsif keys.length < 0x100000000
|
165
|
-
f = [
|
163
|
+
f = ["u", 8]
|
166
164
|
else
|
167
165
|
raise "pack size limit over"
|
168
166
|
end
|
@@ -175,13 +173,13 @@ module AsciiPack
|
|
175
173
|
keys.push(pack(value));
|
176
174
|
}
|
177
175
|
if keys.length < 0x10
|
178
|
-
f = [
|
176
|
+
f = ["v", 1]
|
179
177
|
elsif keys.length < 0x100
|
180
|
-
f = [
|
178
|
+
f = ["w", 2]
|
181
179
|
elsif keys.length < 0x10000
|
182
|
-
f = [
|
180
|
+
f = ["x", 4]
|
183
181
|
elsif keys.length < 0x100000000
|
184
|
-
f = [
|
182
|
+
f = ["y", 8]
|
185
183
|
else
|
186
184
|
raise "pack size limit over"
|
187
185
|
end
|
data/lib/asciipack/unpacker.rb
CHANGED
@@ -1,43 +1,63 @@
|
|
1
|
-
require 'asciipack/typemap.rb'
|
2
|
-
|
3
1
|
module AsciiPack
|
4
2
|
class Unpacker
|
3
|
+
@@fixmap = {
|
4
|
+
"0" => 0x0,
|
5
|
+
"1" => 0x1,
|
6
|
+
"2" => 0x2,
|
7
|
+
"3" => 0x3,
|
8
|
+
"4" => 0x4,
|
9
|
+
"5" => 0x5,
|
10
|
+
"6" => 0x6,
|
11
|
+
"7" => 0x7,
|
12
|
+
"8" => 0x8,
|
13
|
+
"9" => 0x9,
|
14
|
+
"A" => 0xa,
|
15
|
+
"B" => 0xb,
|
16
|
+
"C" => 0xc,
|
17
|
+
"D" => 0xd,
|
18
|
+
"E" => 0xe,
|
19
|
+
"F" => 0xf,
|
20
|
+
"W" => nil,
|
21
|
+
"X" => false,
|
22
|
+
"Y" => true,
|
23
|
+
}.freeze
|
24
|
+
|
5
25
|
def initialize (ap)
|
6
26
|
@ap = ap
|
7
|
-
@ret = nil
|
8
27
|
@at = 0
|
9
28
|
@ch = @ap[0]
|
10
29
|
end
|
11
30
|
|
12
31
|
def unpack
|
13
32
|
move
|
33
|
+
|
34
|
+
if @@fixmap.key?(@ch)
|
35
|
+
return @@fixmap[@ch]
|
36
|
+
end
|
37
|
+
|
14
38
|
case @ch
|
15
|
-
when
|
39
|
+
when "a"; int4
|
40
|
+
when "b"; int8
|
41
|
+
when "c"; int16
|
42
|
+
when "d"; int32
|
43
|
+
when "e"; int64
|
44
|
+
when "g"; uint8
|
45
|
+
when "h"; uint16
|
46
|
+
when "i"; uint32
|
47
|
+
when "j"; uint64
|
48
|
+
when "l"; float64
|
49
|
+
when "n"; str8
|
50
|
+
when "o"; str16
|
51
|
+
when "p"; str32
|
52
|
+
when "r"; map4
|
53
|
+
when "s"; map8
|
54
|
+
when "t"; map16
|
55
|
+
when "u"; map32
|
56
|
+
when "v"; array4
|
57
|
+
when "w"; array8
|
58
|
+
when "x"; array16
|
59
|
+
when "y"; array32
|
16
60
|
when /[G-V]/; fixstr
|
17
|
-
when TypeMap.int4; int4
|
18
|
-
when TypeMap.int8; int8
|
19
|
-
when TypeMap.int16; int16
|
20
|
-
when TypeMap.int32; int32
|
21
|
-
when TypeMap.int64; int64
|
22
|
-
when TypeMap.uint8; uint8
|
23
|
-
when TypeMap.uint16; uint16
|
24
|
-
when TypeMap.uint32; uint32
|
25
|
-
when TypeMap.uint64; uint64
|
26
|
-
when TypeMap.float64; float64
|
27
|
-
when TypeMap.map4; map4
|
28
|
-
when TypeMap.map8; map8
|
29
|
-
when TypeMap.map16; map16
|
30
|
-
when TypeMap.map32; map32
|
31
|
-
when TypeMap.array4; array4
|
32
|
-
when TypeMap.array8; array8
|
33
|
-
when TypeMap.array16; array16
|
34
|
-
when TypeMap.array32; array32
|
35
|
-
when TypeMap.str8; str8
|
36
|
-
when TypeMap.str16; str16
|
37
|
-
when TypeMap.str32; str32
|
38
|
-
when TypeMap.nil; nil
|
39
|
-
when TypeMap.false; false
|
40
|
-
when TypeMap.true; true
|
41
61
|
else raise "undefined type " + @ch.to_s
|
42
62
|
end
|
43
63
|
end
|
@@ -51,10 +71,10 @@ private
|
|
51
71
|
end
|
52
72
|
|
53
73
|
def cut (len)
|
54
|
-
|
74
|
+
ret = @ap[@at...(@at + len)]
|
55
75
|
@at += len
|
56
76
|
@ch = @ap[@at]
|
57
|
-
|
77
|
+
ret
|
58
78
|
end
|
59
79
|
|
60
80
|
def int4
|
@@ -87,10 +107,6 @@ private
|
|
87
107
|
(c[0].to_i(16) < 0x8) ? i : i - 0x10000000000000000;
|
88
108
|
end
|
89
109
|
|
90
|
-
def positive_fixint
|
91
|
-
@ch.to_i(16)
|
92
|
-
end
|
93
|
-
|
94
110
|
def uint8
|
95
111
|
cut(2).to_i(16)
|
96
112
|
end
|
@@ -156,7 +172,7 @@ private
|
|
156
172
|
def array32; array(8) end
|
157
173
|
|
158
174
|
def fixstr
|
159
|
-
len = @ch.ord - 71 # 71 =
|
175
|
+
len = @ch.ord - 71 # 71 = "G".ord
|
160
176
|
cut(len)
|
161
177
|
end
|
162
178
|
|
data/lib/asciipack/version.rb
CHANGED
data/spec/bench.rb
CHANGED
@@ -36,12 +36,14 @@ def bench(name)
|
|
36
36
|
100000.times {
|
37
37
|
yield
|
38
38
|
}
|
39
|
-
p name + ': ' + (Time.now - t).to_s + '
|
39
|
+
p name + ': ' + (Time.now - t).to_s + 's'
|
40
40
|
end
|
41
41
|
|
42
|
+
tt = Time.now
|
42
43
|
{
|
43
44
|
"positive fixint" => 0,
|
44
45
|
"uint 4" => 16,
|
46
|
+
"int 4" => -1,
|
45
47
|
"fixstr" => "",
|
46
48
|
"str 8" => '0123456789abcdef',
|
47
49
|
"float 64" => 1/3,
|
@@ -52,4 +54,4 @@ end
|
|
52
54
|
json_asciipack key, value
|
53
55
|
}
|
54
56
|
|
55
|
-
p
|
57
|
+
p 'total: ' + (Time.now - tt).to_s + 's'
|
data/spec/format_spec.rb
CHANGED
@@ -74,29 +74,29 @@ describe AsciiPack do
|
|
74
74
|
expect(AsciiPack.pack(-1 / 0.0)).to eq(T.float64 + 'fff0000000000000')
|
75
75
|
end
|
76
76
|
|
77
|
-
it "
|
78
|
-
format "", T.
|
79
|
-
format " ", T.
|
80
|
-
format "あ", T.
|
81
|
-
format "漢字", T.
|
82
|
-
format " " * 0xe, T.
|
83
|
-
format " " * 0xf, T.
|
77
|
+
it "fixbin" do
|
78
|
+
format "", T.fixbin_0, 1
|
79
|
+
format " ", T.fixbin_1, 2
|
80
|
+
format "あ", T.fixbin_1, 2
|
81
|
+
format "漢字", T.fixbin_2, 3
|
82
|
+
format " " * 0xe, T.fixbin_E, 15
|
83
|
+
format " " * 0xf, T.fixbin_F, 16
|
84
84
|
end
|
85
85
|
|
86
|
-
it "
|
87
|
-
format "a" * 0x10, T.
|
88
|
-
format "a" * 0xff, T.
|
86
|
+
it "bin 8" do
|
87
|
+
format "a" * 0x10, T.bin8, 3 + 0x10
|
88
|
+
format "a" * 0xff, T.bin8, 3 + 0xff
|
89
89
|
end
|
90
90
|
|
91
|
-
it "
|
92
|
-
format "a" * 0x100, T.
|
93
|
-
format "a" * 0xffff, T.
|
91
|
+
it "bin 16" do
|
92
|
+
format "a" * 0x100, T.bin16, 5 + 0x100
|
93
|
+
format "a" * 0xffff, T.bin16, 5 + 0xffff
|
94
94
|
end
|
95
95
|
|
96
|
-
it "
|
97
|
-
format "a" * 0x10000, T.
|
96
|
+
it "bin 32" do
|
97
|
+
format "a" * 0x10000, T.bin32, 9 + 0x10000
|
98
98
|
# FIXME too late
|
99
|
-
# format "a" * 0xffffffff, T.
|
99
|
+
# format "a" * 0xffffffff, T.bin32, 9 + 0xffffffff
|
100
100
|
end
|
101
101
|
|
102
102
|
it "map 4" do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,67 @@
|
|
1
1
|
require 'asciipack'
|
2
|
+
require 'ostruct'
|
2
3
|
|
3
|
-
T =
|
4
|
+
T = OpenStruct.new({
|
5
|
+
:int4 => 'a',
|
6
|
+
:int8 => 'b',
|
7
|
+
:int16 => 'c',
|
8
|
+
:int32 => 'd',
|
9
|
+
:int64 => 'e',
|
10
|
+
# (blank) => 'f',
|
11
|
+
:uint8 => 'g',
|
12
|
+
:uint16 => 'h',
|
13
|
+
:uint32 => 'i',
|
14
|
+
:uint64 => 'j',
|
15
|
+
:float32 => 'k',
|
16
|
+
:float64 => 'l',
|
17
|
+
# (blank) => 'm',
|
18
|
+
:bin8 => 'n',
|
19
|
+
:bin16 => 'o',
|
20
|
+
:bin32 => 'p',
|
21
|
+
# (blank) => 'q',
|
22
|
+
:map4 => 'r',
|
23
|
+
:map8 => 's',
|
24
|
+
:map16 => 't',
|
25
|
+
:map32 => 'u',
|
26
|
+
:array4 => 'v',
|
27
|
+
:array8 => 'w',
|
28
|
+
:array16 => 'x',
|
29
|
+
:array32 => 'y',
|
30
|
+
# (blank) => 'z',
|
31
|
+
:positive_fixint_0 => '0',
|
32
|
+
:positive_fixint_1 => '1',
|
33
|
+
:positive_fixint_2 => '2',
|
34
|
+
:positive_fixint_3 => '3',
|
35
|
+
:positive_fixint_4 => '4',
|
36
|
+
:positive_fixint_5 => '5',
|
37
|
+
:positive_fixint_6 => '6',
|
38
|
+
:positive_fixint_7 => '7',
|
39
|
+
:positive_fixint_8 => '8',
|
40
|
+
:positive_fixint_9 => '9',
|
41
|
+
:positive_fixint_A => 'A',
|
42
|
+
:positive_fixint_B => 'B',
|
43
|
+
:positive_fixint_C => 'C',
|
44
|
+
:positive_fixint_D => 'D',
|
45
|
+
:positive_fixint_E => 'E',
|
46
|
+
:positive_fixint_F => 'F',
|
47
|
+
:fixbin_0 => 'G',
|
48
|
+
:fixbin_1 => 'H',
|
49
|
+
:fixbin_2 => 'I',
|
50
|
+
:fixbin_3 => 'J',
|
51
|
+
:fixbin_4 => 'K',
|
52
|
+
:fixbin_5 => 'L',
|
53
|
+
:fixbin_6 => 'M',
|
54
|
+
:fixbin_7 => 'N',
|
55
|
+
:fixbin_8 => 'O',
|
56
|
+
:fixbin_9 => 'P',
|
57
|
+
:fixbin_A => 'Q',
|
58
|
+
:fixbin_B => 'R',
|
59
|
+
:fixbin_C => 'S',
|
60
|
+
:fixbin_D => 'T',
|
61
|
+
:fixbin_E => 'U',
|
62
|
+
:fixbin_F => 'V',
|
63
|
+
:nil => 'W',
|
64
|
+
:false => 'X',
|
65
|
+
:true => 'Y',
|
66
|
+
# (blank) => 'Z',
|
67
|
+
})
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: asciipack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ksss
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,7 +66,6 @@ files:
|
|
66
66
|
- asciipack.gemspec
|
67
67
|
- lib/asciipack.rb
|
68
68
|
- lib/asciipack/packer.rb
|
69
|
-
- lib/asciipack/typemap.rb
|
70
69
|
- lib/asciipack/unpacker.rb
|
71
70
|
- lib/asciipack/version.rb
|
72
71
|
- spec/bench.rb
|
data/lib/asciipack/typemap.rb
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
require 'ostruct'
|
2
|
-
|
3
|
-
module AsciiPack
|
4
|
-
TypeMap = OpenStruct.new({
|
5
|
-
:int4 => 'a',
|
6
|
-
:int8 => 'b',
|
7
|
-
:int16 => 'c',
|
8
|
-
:int32 => 'd',
|
9
|
-
:int64 => 'e',
|
10
|
-
# (blank) => 'f',
|
11
|
-
:uint8 => 'g',
|
12
|
-
:uint16 => 'h',
|
13
|
-
:uint32 => 'i',
|
14
|
-
:uint64 => 'j',
|
15
|
-
:float32 => 'k',
|
16
|
-
:float64 => 'l',
|
17
|
-
# (blank) => 'm',
|
18
|
-
:str8 => 'n',
|
19
|
-
:str16 => 'o',
|
20
|
-
:str32 => 'p',
|
21
|
-
# (blank) => 'q',
|
22
|
-
:map4 => 'r',
|
23
|
-
:map8 => 's',
|
24
|
-
:map16 => 't',
|
25
|
-
:map32 => 'u',
|
26
|
-
:array4 => 'v',
|
27
|
-
:array8 => 'w',
|
28
|
-
:array16 => 'x',
|
29
|
-
:array32 => 'y',
|
30
|
-
# (blank) => 'z',
|
31
|
-
:positive_fixint_0 => '0',
|
32
|
-
:positive_fixint_1 => '1',
|
33
|
-
:positive_fixint_2 => '2',
|
34
|
-
:positive_fixint_3 => '3',
|
35
|
-
:positive_fixint_4 => '4',
|
36
|
-
:positive_fixint_5 => '5',
|
37
|
-
:positive_fixint_6 => '6',
|
38
|
-
:positive_fixint_7 => '7',
|
39
|
-
:positive_fixint_8 => '8',
|
40
|
-
:positive_fixint_9 => '9',
|
41
|
-
:positive_fixint_A => 'A',
|
42
|
-
:positive_fixint_B => 'B',
|
43
|
-
:positive_fixint_C => 'C',
|
44
|
-
:positive_fixint_D => 'D',
|
45
|
-
:positive_fixint_E => 'E',
|
46
|
-
:positive_fixint_F => 'F',
|
47
|
-
:fixstr_0 => 'G',
|
48
|
-
:fixstr_1 => 'H',
|
49
|
-
:fixstr_2 => 'I',
|
50
|
-
:fixstr_3 => 'J',
|
51
|
-
:fixstr_4 => 'K',
|
52
|
-
:fixstr_5 => 'L',
|
53
|
-
:fixstr_6 => 'M',
|
54
|
-
:fixstr_7 => 'N',
|
55
|
-
:fixstr_8 => 'O',
|
56
|
-
:fixstr_9 => 'P',
|
57
|
-
:fixstr_A => 'Q',
|
58
|
-
:fixstr_B => 'R',
|
59
|
-
:fixstr_C => 'S',
|
60
|
-
:fixstr_D => 'T',
|
61
|
-
:fixstr_E => 'U',
|
62
|
-
:fixstr_F => 'V',
|
63
|
-
:nil => 'W',
|
64
|
-
:false => 'X',
|
65
|
-
:true => 'Y',
|
66
|
-
# (blank) => 'Z',
|
67
|
-
})
|
68
|
-
end
|