asciipack 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +20 -0
- data/Rakefile +12 -0
- data/asciipack.gemspec +24 -0
- data/lib/asciipack.rb +3 -0
- data/lib/asciipack/pack.rb +199 -0
- data/lib/asciipack/typemap.rb +35 -0
- data/lib/asciipack/unpack.rb +187 -0
- data/lib/asciipack/version.rb +3 -0
- data/spec/bench.rb +37 -0
- data/spec/format_spec.rb +122 -0
- data/spec/spec_helper.rb +3 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 68234c0bbe69684169776ff05c4792716712ede4
|
4
|
+
data.tar.gz: 784a5ee093464b729d8d6f067db6d9614cdbd423
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c6836d277b9b5a37539e839b7768e3cd618e40f1bef95d099573679f2def33579b058fee2e143a2006b57bb0be45a1249a20b4f712202e68000ab05d786d016e
|
7
|
+
data.tar.gz: 4b49fd6f986b0a5e7d2030499388a87ad8530b2fab0ba3d80682ca5c1628e0274e177bc38d06930f56639e0885798420932910acb1389a92ae99247c807f7e9f
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 ksss
|
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,20 @@
|
|
1
|
+
# AsciiPack
|
2
|
+
|
3
|
+
## Synopsis
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
demo = {"compact"=>true,"binary"=>0}
|
7
|
+
ap = AsciiPack.pack demo
|
8
|
+
p ap //=> "q2m7compactum6binaryf0"
|
9
|
+
|
10
|
+
unpacked = AsciiPack.unpack ap
|
11
|
+
p unpacked //=> {"compact"=>true,"binary"=>0}
|
12
|
+
```
|
13
|
+
|
14
|
+
## Install
|
15
|
+
|
16
|
+
```
|
17
|
+
gem install asciipack
|
18
|
+
```
|
19
|
+
|
20
|
+
see also [https://github.com/ksss/AsciiPack/blob/master/README.md](https://github.com/ksss/AsciiPack/blob/master/README.md)
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "bundler"
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec/core'
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
8
|
+
t.rspec_opts = ["-c", "-f progress", "-Ilib"]
|
9
|
+
t.pattern = "spec/**/*_spec.rb"
|
10
|
+
t.verbose = true
|
11
|
+
end
|
12
|
+
|
data/asciipack.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'asciipack/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "asciipack"
|
8
|
+
spec.version = AsciiPack::VERSION
|
9
|
+
spec.author = "ksss"
|
10
|
+
spec.email = "co000ri@gmail.com"
|
11
|
+
spec.description = %q{AsciiPack is an object serialization inspired by MessagePack.}
|
12
|
+
spec.summary = %q{AsciiPack is an object serialization inspired by MessagePack.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
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"
|
23
|
+
spec.add_development_dependency "rspec", ['~> 2.11']
|
24
|
+
end
|
data/lib/asciipack.rb
ADDED
@@ -0,0 +1,199 @@
|
|
1
|
+
require 'asciipack/typemap.rb'
|
2
|
+
|
3
|
+
module AsciiPack
|
4
|
+
class Packer
|
5
|
+
class << self
|
6
|
+
def pack (obj)
|
7
|
+
case
|
8
|
+
when obj.kind_of?(Hash)
|
9
|
+
map(obj);
|
10
|
+
when obj.kind_of?(Array)
|
11
|
+
array(obj);
|
12
|
+
when obj.kind_of?(Numeric)
|
13
|
+
if obj.kind_of?(Integer)
|
14
|
+
if 0 <= obj
|
15
|
+
if obj < 0x10
|
16
|
+
uint4 obj
|
17
|
+
elsif obj < 0x100
|
18
|
+
uint8 obj
|
19
|
+
elsif obj < 0x10000
|
20
|
+
uint16 obj
|
21
|
+
elsif obj < 0x100000000
|
22
|
+
uint32 obj
|
23
|
+
elsif obj < 0x10000000000000000
|
24
|
+
uint64 obj
|
25
|
+
end
|
26
|
+
else
|
27
|
+
if -0x8 <= obj
|
28
|
+
int4 obj
|
29
|
+
elsif -0x80 <= obj
|
30
|
+
int8 obj
|
31
|
+
elsif -0x8000 <= obj
|
32
|
+
int16 obj
|
33
|
+
elsif -0x80000000 <= obj
|
34
|
+
int32 obj
|
35
|
+
elsif -0x8000000000000000 <= obj
|
36
|
+
int64 obj
|
37
|
+
else
|
38
|
+
raise "pack size limit over"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
else
|
42
|
+
float64 obj
|
43
|
+
end
|
44
|
+
when obj.kind_of?(String)
|
45
|
+
case obj.length
|
46
|
+
when 0...0x10
|
47
|
+
str4 obj
|
48
|
+
when 0x10...0x100
|
49
|
+
str8 obj
|
50
|
+
when 0x100...0x10000
|
51
|
+
str16 obj
|
52
|
+
when 0x10000...0x100000000
|
53
|
+
str32 obj
|
54
|
+
else
|
55
|
+
raise "pack size limit over"
|
56
|
+
end
|
57
|
+
when obj.nil?
|
58
|
+
TypeMap.nil
|
59
|
+
when obj == false
|
60
|
+
TypeMap.false
|
61
|
+
when obj == true
|
62
|
+
TypeMap.true
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def int4 (obj)
|
69
|
+
TypeMap.int4 + ((obj & 0xf).to_s(16))
|
70
|
+
end
|
71
|
+
|
72
|
+
def int8 (obj)
|
73
|
+
TypeMap.int8 + ((obj & 0xff).to_s(16))
|
74
|
+
end
|
75
|
+
|
76
|
+
def int16 (obj)
|
77
|
+
TypeMap.int16 + ((obj & 0xffff).to_s(16))
|
78
|
+
end
|
79
|
+
|
80
|
+
def int16 (obj)
|
81
|
+
TypeMap.int16 + ((obj & 0xffff).to_s(16))
|
82
|
+
end
|
83
|
+
|
84
|
+
def int32 (obj)
|
85
|
+
TypeMap.int32 + ((obj & 0xffffffff).to_s(16))
|
86
|
+
end
|
87
|
+
|
88
|
+
def int64 (obj)
|
89
|
+
TypeMap.int64 + ((obj & 0xffffffffffffffff).to_s(16))
|
90
|
+
end
|
91
|
+
|
92
|
+
def uint4 (obj)
|
93
|
+
format_uint(TypeMap.uint4, 1, obj)
|
94
|
+
end
|
95
|
+
|
96
|
+
def uint8 (obj)
|
97
|
+
format_uint(TypeMap.uint8, 2, obj)
|
98
|
+
end
|
99
|
+
|
100
|
+
def uint16 (obj)
|
101
|
+
format_uint(TypeMap.uint16, 4, obj)
|
102
|
+
end
|
103
|
+
|
104
|
+
def uint32 (obj)
|
105
|
+
format_uint(TypeMap.uint32, 8, obj)
|
106
|
+
end
|
107
|
+
|
108
|
+
def uint64 (obj)
|
109
|
+
format_uint(TypeMap.uint64, 16, obj)
|
110
|
+
end
|
111
|
+
|
112
|
+
def float64 (obj)
|
113
|
+
unless obj.finite?
|
114
|
+
case obj.infinite?
|
115
|
+
when 1; return TypeMap.float64 + '7ff0000000000000' # +∞
|
116
|
+
when -1; return TypeMap.float64 + 'fff0000000000000' # -∞
|
117
|
+
else; return TypeMap.float64 + '7fffffffffffffff' # NAN
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
sign = obj < 0
|
122
|
+
obj *= -1 if sign
|
123
|
+
exp = ((Math.log(obj) / Math.log(2, Math::E)) + 1023).to_i
|
124
|
+
frac = obj * (2**(52 + 1023 - exp))
|
125
|
+
low = frac.to_i & 0xffffffff
|
126
|
+
exp |= 0x800 if sign
|
127
|
+
high = ((frac / 0x100000000).to_i & 0xfffff) | (exp << 20)
|
128
|
+
|
129
|
+
TypeMap.float64 + to_s16([
|
130
|
+
(high >> 24) & 0xff, (high >> 16) & 0xff,
|
131
|
+
(high >> 8) & 0xff, (high) & 0xff,
|
132
|
+
(low >> 24) & 0xff, (low >> 16) & 0xff,
|
133
|
+
(low >> 8) & 0xff, (low) & 0xff
|
134
|
+
])
|
135
|
+
end
|
136
|
+
|
137
|
+
def str4 (obj)
|
138
|
+
format_str TypeMap.str4, 1, obj
|
139
|
+
end
|
140
|
+
|
141
|
+
def str8 (obj)
|
142
|
+
format_str TypeMap.str8, 2, obj
|
143
|
+
end
|
144
|
+
|
145
|
+
def str16 (obj)
|
146
|
+
format_str TypeMap.str16, 4, obj
|
147
|
+
end
|
148
|
+
|
149
|
+
def str32 (obj)
|
150
|
+
format_str TypeMap.str32, 8, obj
|
151
|
+
end
|
152
|
+
|
153
|
+
def map (obj)
|
154
|
+
keys = [];
|
155
|
+
obj.each { |key, value|
|
156
|
+
keys.push(pack(key.to_s) + pack(value));
|
157
|
+
}
|
158
|
+
TypeMap.map + keys.length.to_s + keys.join('');
|
159
|
+
end
|
160
|
+
|
161
|
+
def array (obj)
|
162
|
+
keys = [];
|
163
|
+
obj.each { |value|
|
164
|
+
keys.push(pack(value));
|
165
|
+
}
|
166
|
+
TypeMap.array + keys.length.to_s + keys.join('');
|
167
|
+
end
|
168
|
+
|
169
|
+
def format_uint (type, length, num)
|
170
|
+
hex = num.to_s(16);
|
171
|
+
len = length - hex.length;
|
172
|
+
zero = '0' * len;
|
173
|
+
type + zero + hex;
|
174
|
+
end
|
175
|
+
|
176
|
+
def format_str (type, length, str)
|
177
|
+
hex = str.length.to_s(16);
|
178
|
+
len = length - hex.length;
|
179
|
+
zero = '0' * len;
|
180
|
+
type + zero + hex + str;
|
181
|
+
end
|
182
|
+
|
183
|
+
def to_s16 (a)
|
184
|
+
a.map { |i|
|
185
|
+
hex = i.to_s(16)
|
186
|
+
len = hex.length
|
187
|
+
i = 2 - len
|
188
|
+
('0' * i) + hex
|
189
|
+
}.join('')
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
class << self
|
195
|
+
def pack (obj)
|
196
|
+
Packer.pack obj
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module AsciiPack
|
2
|
+
class TypeMap
|
3
|
+
class << self
|
4
|
+
{
|
5
|
+
:int4 => 'a',
|
6
|
+
:int8 => 'b',
|
7
|
+
:int16 => 'c',
|
8
|
+
:int32 => 'd',
|
9
|
+
:int64 => 'e',
|
10
|
+
:uint4 => 'f',
|
11
|
+
:uint8 => 'g',
|
12
|
+
:uint16 => 'h',
|
13
|
+
:uint32 => 'i',
|
14
|
+
:uint64 => 'j',
|
15
|
+
:float32 => 'k',
|
16
|
+
:float64 => 'l',
|
17
|
+
:str4 => 'm',
|
18
|
+
:str8 => 'n',
|
19
|
+
:str16 => 'o',
|
20
|
+
:str32 => 'p',
|
21
|
+
:map => 'q',
|
22
|
+
:array => 'r',
|
23
|
+
:nil => 's',
|
24
|
+
:false => 't',
|
25
|
+
:true => 'u',
|
26
|
+
}.each { |name, char|
|
27
|
+
module_eval %{
|
28
|
+
def #{name}
|
29
|
+
"#{char}"
|
30
|
+
end
|
31
|
+
}
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,187 @@
|
|
1
|
+
require 'asciipack/typemap.rb'
|
2
|
+
|
3
|
+
module AsciiPack
|
4
|
+
class Unpacker
|
5
|
+
def initialize (ap)
|
6
|
+
@ap = ap
|
7
|
+
@ret = nil
|
8
|
+
@at = 0
|
9
|
+
@ch = @ap[0]
|
10
|
+
end
|
11
|
+
|
12
|
+
def unpack
|
13
|
+
move
|
14
|
+
case @ch
|
15
|
+
when TypeMap.int4; int4
|
16
|
+
when TypeMap.int8; int8
|
17
|
+
when TypeMap.int16; int16
|
18
|
+
when TypeMap.int32; int32
|
19
|
+
when TypeMap.int64; int64
|
20
|
+
when TypeMap.uint4; uint4
|
21
|
+
when TypeMap.uint8; uint8
|
22
|
+
when TypeMap.uint16; uint16
|
23
|
+
when TypeMap.uint32; uint32
|
24
|
+
when TypeMap.uint64; uint64
|
25
|
+
when TypeMap.float64; float64
|
26
|
+
when TypeMap.map; map
|
27
|
+
when TypeMap.array; array
|
28
|
+
when TypeMap.str4; str4
|
29
|
+
when TypeMap.str8; str8
|
30
|
+
when TypeMap.str16; str16
|
31
|
+
when TypeMap.str32; str32
|
32
|
+
when TypeMap.nil; nil
|
33
|
+
when TypeMap.false; false
|
34
|
+
when TypeMap.true; true
|
35
|
+
else raise "undefined type " + @ch.to_s
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def move
|
42
|
+
@ch = @ap[@at]
|
43
|
+
@at = @at + 1
|
44
|
+
@ch
|
45
|
+
end
|
46
|
+
|
47
|
+
def back
|
48
|
+
@ch = @ap[@at]
|
49
|
+
@at -= 1
|
50
|
+
@ch
|
51
|
+
end
|
52
|
+
|
53
|
+
def cut (len)
|
54
|
+
@ret = @ap[@at...(@at + len)]
|
55
|
+
@at += len
|
56
|
+
@ch = @ap[@at]
|
57
|
+
@ret
|
58
|
+
end
|
59
|
+
|
60
|
+
def length
|
61
|
+
ret = []
|
62
|
+
while (/\d/ =~ @ch)
|
63
|
+
ret << @ch
|
64
|
+
move()
|
65
|
+
end
|
66
|
+
back()
|
67
|
+
ret.join('').to_i
|
68
|
+
end
|
69
|
+
|
70
|
+
def int4
|
71
|
+
move
|
72
|
+
i = @ch.to_i(16)
|
73
|
+
return (@ch[0].to_i(16) < 0x8) ? i : i - 0x10;
|
74
|
+
end
|
75
|
+
|
76
|
+
def int8
|
77
|
+
c = cut(2)
|
78
|
+
i = c.to_i(16)
|
79
|
+
return (c[0].to_i(16) < 0x8) ? i : i - 0x100;
|
80
|
+
end
|
81
|
+
|
82
|
+
def int16
|
83
|
+
c = cut(4)
|
84
|
+
i = c.to_i(16)
|
85
|
+
return (c[0].to_i(16) < 0x8) ? i : i - 0x10000;
|
86
|
+
end
|
87
|
+
|
88
|
+
def int32
|
89
|
+
c = cut(8)
|
90
|
+
i = c.to_i(16)
|
91
|
+
return (c[0].to_i(16) < 0x8) ? i : i - 0x100000000;
|
92
|
+
end
|
93
|
+
|
94
|
+
def int64
|
95
|
+
c = cut(16)
|
96
|
+
i = c.to_i(16)
|
97
|
+
return (c[0].to_i(16) < 0x8) ? i : i - 0x10000000000000000;
|
98
|
+
end
|
99
|
+
|
100
|
+
def uint4
|
101
|
+
move.to_i(16)
|
102
|
+
end
|
103
|
+
|
104
|
+
def uint8
|
105
|
+
cut(2).to_i(16)
|
106
|
+
end
|
107
|
+
|
108
|
+
def uint16
|
109
|
+
cut(4).to_i(16)
|
110
|
+
end
|
111
|
+
|
112
|
+
def uint32
|
113
|
+
cut(8).to_i(16)
|
114
|
+
end
|
115
|
+
|
116
|
+
def uint64
|
117
|
+
cut(16).to_i(16)
|
118
|
+
end
|
119
|
+
|
120
|
+
def float64
|
121
|
+
# IEEE 752 format
|
122
|
+
hex = cut(3)
|
123
|
+
num = hex.to_i(16)
|
124
|
+
sign = num & 0x800
|
125
|
+
exp = (num & 0x7ff) - 1023
|
126
|
+
frac = ('1' + cut(13)).to_i(16)
|
127
|
+
|
128
|
+
if hex == '7ff' && frac != 0
|
129
|
+
return Float.NAN
|
130
|
+
elsif hex == '7ff' && frac == 0
|
131
|
+
return Float.INFINITY
|
132
|
+
elsif hex == 'fff' && frac == 0
|
133
|
+
return -1 / 0.0
|
134
|
+
end
|
135
|
+
|
136
|
+
((sign == 0 ? 1 : -1) * frac * (2**(exp - 52))).to_f
|
137
|
+
end
|
138
|
+
|
139
|
+
def map
|
140
|
+
move
|
141
|
+
hash = {}
|
142
|
+
len = length()
|
143
|
+
len.times {
|
144
|
+
key = unpack
|
145
|
+
hash[key] = unpack
|
146
|
+
}
|
147
|
+
hash
|
148
|
+
end
|
149
|
+
|
150
|
+
def array
|
151
|
+
move
|
152
|
+
array = []
|
153
|
+
len = length()
|
154
|
+
len.times {
|
155
|
+
array << unpack
|
156
|
+
}
|
157
|
+
array
|
158
|
+
end
|
159
|
+
|
160
|
+
def str4
|
161
|
+
len = cut(1).to_i(16)
|
162
|
+
cut(len)
|
163
|
+
end
|
164
|
+
|
165
|
+
def str8
|
166
|
+
len = cut(2).to_i(16)
|
167
|
+
cut(len)
|
168
|
+
end
|
169
|
+
|
170
|
+
def str16
|
171
|
+
len = cut(4).to_i(16)
|
172
|
+
cut(len)
|
173
|
+
end
|
174
|
+
|
175
|
+
def str32
|
176
|
+
len = cut(8).to_i(16)
|
177
|
+
cut(len)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
class << self
|
182
|
+
def unpack (obj)
|
183
|
+
unpacker = Unpacker.new obj
|
184
|
+
unpacker.unpack
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
data/spec/bench.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
|
3
|
+
lib = File.expand_path('../../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'asciipack'
|
7
|
+
require 'json'
|
8
|
+
require 'msgpack'
|
9
|
+
require 'benchmark'
|
10
|
+
|
11
|
+
value = {}
|
12
|
+
30.times { |i|
|
13
|
+
value[i] = case i % 5
|
14
|
+
when 0; 0xffffffff
|
15
|
+
when 1; " " * 0xffff
|
16
|
+
when 2; [1.1, -1.1, 1/3, 5]
|
17
|
+
when 3; JSON.parse(value.to_json)
|
18
|
+
when 4; nil
|
19
|
+
end
|
20
|
+
}
|
21
|
+
json = JSON.generate value
|
22
|
+
ap = AsciiPack.pack value
|
23
|
+
ms = Marshal.dump value
|
24
|
+
msg = MessagePack.pack value
|
25
|
+
n = 10
|
26
|
+
|
27
|
+
Benchmark.bm do |x|
|
28
|
+
x.report("JSON.generate") {n.times{ JSON.generate value }}
|
29
|
+
x.report("JSON.parse") {n.times{ JSON.parse json }}
|
30
|
+
x.report("AsciiPack.pack") {n.times{ AsciiPack.pack value }}
|
31
|
+
x.report("AsciiPack.unpack") {n.times{ AsciiPack.unpack ap }}
|
32
|
+
x.report("Marshal.dump") {n.times{ Marshal.dump value }}
|
33
|
+
x.report("Marshal.load") {n.times{ Marshal.load ms }}
|
34
|
+
x.report("MessagePack.pack") {n.times{ MessagePack.pack value }}
|
35
|
+
x.report("MessagePack.unpack") {n.times{ MessagePack.unpack msg }}
|
36
|
+
end
|
37
|
+
|
data/spec/format_spec.rb
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe AsciiPack do
|
6
|
+
it "int 4" do
|
7
|
+
format -1, T.int4, 2
|
8
|
+
format -8, T.int4, 2
|
9
|
+
end
|
10
|
+
|
11
|
+
it "int 8" do
|
12
|
+
format -0x9, T.int8, 3
|
13
|
+
format -0x80, T.int8, 3
|
14
|
+
end
|
15
|
+
|
16
|
+
it "int 16" do
|
17
|
+
format -0x81, T.int16, 5
|
18
|
+
format -0x8000, T.int16, 5
|
19
|
+
end
|
20
|
+
|
21
|
+
it "int 32" do
|
22
|
+
format -0x8001, T.int32, 9
|
23
|
+
format -0x80000000, T.int32, 9
|
24
|
+
end
|
25
|
+
|
26
|
+
it "int 64" do
|
27
|
+
format -0x80000001, T.int64, 17
|
28
|
+
format -0x8000000000000000, T.int64, 17
|
29
|
+
end
|
30
|
+
|
31
|
+
it "uint 4" do
|
32
|
+
format 0x0, T.uint4, 2
|
33
|
+
format 0xf, T.uint4, 2
|
34
|
+
end
|
35
|
+
|
36
|
+
it "uint 8" do
|
37
|
+
format 0x10, T.uint8, 3
|
38
|
+
format 0xff, T.uint8, 3
|
39
|
+
end
|
40
|
+
|
41
|
+
it "uint 16" do
|
42
|
+
format 0x100, T.uint16, 5
|
43
|
+
format 0xffff, T.uint16, 5
|
44
|
+
end
|
45
|
+
|
46
|
+
it "uint 32" do
|
47
|
+
format 0x10000, T.uint32, 9
|
48
|
+
format 0xffffffff, T.uint32, 9
|
49
|
+
end
|
50
|
+
|
51
|
+
it "uint 64" do
|
52
|
+
format 0x100000000, T.uint64, 17
|
53
|
+
format 0xffffffffffffffff, T.uint64, 17
|
54
|
+
end
|
55
|
+
|
56
|
+
it "float 64" do
|
57
|
+
format 1/3.0, T.float64, 17
|
58
|
+
format 1.0, T.float64, 17
|
59
|
+
format 0.1, T.float64, 17
|
60
|
+
format -0.1, T.float64, 17
|
61
|
+
expect(AsciiPack.pack(-0.1)).to eq(T.float64 + 'bfb999999999999a')
|
62
|
+
expect(AsciiPack.pack(1.0)).to eq(T.float64 + '3ff0000000000000')
|
63
|
+
expect(AsciiPack.pack(1.0000000000000002)).to eq(T.float64 + '3ff0000000000001')
|
64
|
+
expect(AsciiPack.pack(1.0000000000000004)).to eq(T.float64 + '3ff0000000000002')
|
65
|
+
expect(AsciiPack.pack(1/3.0)).to eq(T.float64 + '3fd5555555555555')
|
66
|
+
expect(AsciiPack.pack(Float::NAN)).to eq(T.float64 + '7fffffffffffffff')
|
67
|
+
expect(AsciiPack.pack(1 / 0.0)).to eq(T.float64 + '7ff0000000000000')
|
68
|
+
expect(AsciiPack.pack(-1 / 0.0)).to eq(T.float64 + 'fff0000000000000')
|
69
|
+
end
|
70
|
+
|
71
|
+
it "str 4" do
|
72
|
+
format "", T.str4, 2
|
73
|
+
format " " * 0xf, T.str4, 17
|
74
|
+
end
|
75
|
+
|
76
|
+
it "str 8" do
|
77
|
+
format "a" * 0x10, T.str8, 3 + 0x10
|
78
|
+
format "a" * 0xff, T.str8, 3 + 0xff
|
79
|
+
end
|
80
|
+
|
81
|
+
it "str 16" do
|
82
|
+
format "a" * 0x100, T.str16, 5 + 0x100
|
83
|
+
format "a" * 0xffff, T.str16, 5 + 0xffff
|
84
|
+
end
|
85
|
+
|
86
|
+
it "str 32" do
|
87
|
+
format "a" * 0x10000, T.str32, 9 + 0x10000
|
88
|
+
# too late
|
89
|
+
# format "a" * 0xffffffff, T.str32, 9 + 0xffffffff
|
90
|
+
end
|
91
|
+
|
92
|
+
it "map" do
|
93
|
+
format({}, T.map, 2)
|
94
|
+
format({"hash" => {}}, T.map, 10)
|
95
|
+
expect(AsciiPack.pack({})).to eq('q0')
|
96
|
+
end
|
97
|
+
|
98
|
+
it "array" do
|
99
|
+
format([], T.array, 2)
|
100
|
+
format([1,2,3], T.array, 8)
|
101
|
+
expect(AsciiPack.pack([])).to eq('r0')
|
102
|
+
end
|
103
|
+
|
104
|
+
it "nil" do
|
105
|
+
format nil, T.nil, 1
|
106
|
+
end
|
107
|
+
|
108
|
+
it "false" do
|
109
|
+
format false, T.false, 1
|
110
|
+
end
|
111
|
+
|
112
|
+
it "true" do
|
113
|
+
format true, T.true, 1
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def format (object, first, length)
|
118
|
+
ap = AsciiPack.pack(object)
|
119
|
+
expect(ap[0]).to eq(first)
|
120
|
+
expect(ap.length).to eq(length)
|
121
|
+
expect(AsciiPack.unpack(ap)).to eq(object)
|
122
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: asciipack
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ksss
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-01 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: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.11'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.11'
|
55
|
+
description: AsciiPack is an object serialization inspired by MessagePack.
|
56
|
+
email: co000ri@gmail.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- .gitignore
|
62
|
+
- Gemfile
|
63
|
+
- LICENSE.txt
|
64
|
+
- README.md
|
65
|
+
- Rakefile
|
66
|
+
- asciipack.gemspec
|
67
|
+
- lib/asciipack.rb
|
68
|
+
- lib/asciipack/pack.rb
|
69
|
+
- lib/asciipack/typemap.rb
|
70
|
+
- lib/asciipack/unpack.rb
|
71
|
+
- lib/asciipack/version.rb
|
72
|
+
- spec/bench.rb
|
73
|
+
- spec/format_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
homepage: ''
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.0.3
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: AsciiPack is an object serialization inspired by MessagePack.
|
99
|
+
test_files:
|
100
|
+
- spec/bench.rb
|
101
|
+
- spec/format_spec.rb
|
102
|
+
- spec/spec_helper.rb
|