zero_formatter 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/Gemfile +4 -0
- data/Guardfile +21 -0
- data/README.md +40 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/circle.yml +10 -0
- data/lib/zero_formatter.rb +18 -0
- data/lib/zero_formatter/serializer.rb +111 -0
- data/lib/zero_formatter/serializer/bool.rb +15 -0
- data/lib/zero_formatter/serializer/fp.rb +27 -0
- data/lib/zero_formatter/serializer/int.rb +51 -0
- data/lib/zero_formatter/serializer/object.rb +24 -0
- data/lib/zero_formatter/serializer/string.rb +32 -0
- data/lib/zero_formatter/serializer/time.rb +51 -0
- data/lib/zero_formatter/serializer/uint.rb +51 -0
- data/lib/zero_formatter/utils.rb +104 -0
- data/lib/zero_formatter/version.rb +3 -0
- data/lib/zero_formatter/zero_formattable.rb +27 -0
- data/zero_formatter.gemspec +32 -0
- metadata +176 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ce0b71bd71b66c966b57ea8f569d94f8fd9edd99
|
4
|
+
data.tar.gz: 20c1425f252012ccfe9a19c320b948eeebbdadc6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d17bc2ae62ef26d739758a048af5cdf40d2afd14c8b5595bd9033b0954b2d22d04c8af433c2172d4a198ca446b4a91dd84d8f3f853453c3dace22edfecf77058
|
7
|
+
data.tar.gz: da46cc4176988bd8cd762e9969183b2e1994967244ed2395d2596d1f3d1bf93b30666f12598d6982f1f45abff1c6b42d7ba9abd4dfb6e074d28eb2d933d81ac5
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec features) \
|
6
|
+
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
|
7
|
+
|
8
|
+
## Note: if you are using the `directories` clause above and you are not
|
9
|
+
## watching the project directory ('.'), then you will want to move
|
10
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
11
|
+
#
|
12
|
+
# $ mkdir config
|
13
|
+
# $ mv Guardfile config/
|
14
|
+
# $ ln -s config/Guardfile .
|
15
|
+
#
|
16
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
17
|
+
#
|
18
|
+
guard "minitest" do
|
19
|
+
watch %r{^test/.+\.rb}
|
20
|
+
watch(%r{^lib/(.+)\.rb}){|m| "test/#{m[1]}_test.rb" }
|
21
|
+
end
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# ZeroFormatter
|
2
|
+
|
3
|
+
[![CircleCI](https://circleci.com/gh/aki017/ZeroFormatter.rb.svg?style=svg)](https://circleci.com/gh/aki017/ZeroFormatter.rb)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/aki017/ZeroFormatter.rb/badges/gpa.svg)](https://codeclimate.com/github/aki017/ZeroFormatter.rb)
|
5
|
+
[![Test Coverage](https://codeclimate.com/github/aki017/ZeroFormatter.rb/badges/coverage.svg)](https://codeclimate.com/github/aki017/ZeroFormatter.rb/coverage)
|
6
|
+
|
7
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/zero_formatter`. To experiment with that code, run `bin/console` for an interactive prompt.
|
8
|
+
|
9
|
+
TODO: Delete this and the text above, and describe your gem
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'zero_formatter'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install zero_formatter
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
TODO: Write usage instructions here
|
30
|
+
|
31
|
+
## Development
|
32
|
+
|
33
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
34
|
+
|
35
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/aki017/ZeroFormatter.rb .
|
40
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "zero_formatter"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/circle.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "zero_formatter/version"
|
2
|
+
require "zero_formatter/utils"
|
3
|
+
require "zero_formatter/serializer"
|
4
|
+
require "zero_formatter/zero_formattable"
|
5
|
+
|
6
|
+
module ZeroFormatter
|
7
|
+
def self.load(klass, bytes)
|
8
|
+
serializer.load(klass, bytes)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.dump(data)
|
12
|
+
serializer.dump(data)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.serializer
|
16
|
+
@serializer ||= Serializer.new
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require "zero_formatter/serializer/int"
|
2
|
+
require "zero_formatter/serializer/uint"
|
3
|
+
require "zero_formatter/serializer/fp"
|
4
|
+
require "zero_formatter/serializer/bool"
|
5
|
+
require "zero_formatter/serializer/string"
|
6
|
+
require "zero_formatter/serializer/time"
|
7
|
+
|
8
|
+
module ZeroFormatter
|
9
|
+
class Serializer
|
10
|
+
def initialize(*args)
|
11
|
+
add_implement Int8Serializer
|
12
|
+
add_implement Int16Serializer
|
13
|
+
add_implement Int32Serializer
|
14
|
+
add_implement Int64Serializer
|
15
|
+
|
16
|
+
add_implement UInt8Serializer
|
17
|
+
add_implement UInt16Serializer
|
18
|
+
add_implement UInt32Serializer
|
19
|
+
add_implement UInt64Serializer
|
20
|
+
|
21
|
+
add_implement FloatSerializer
|
22
|
+
add_implement DoubleSerializer
|
23
|
+
|
24
|
+
add_implement BoolSerializer
|
25
|
+
|
26
|
+
add_implement CharSerializer
|
27
|
+
add_implement StringSerializer
|
28
|
+
|
29
|
+
add_implement TimeSpanSerializer
|
30
|
+
add_implement TimeSerializer
|
31
|
+
add_implement TimeWithOffsetSerializer
|
32
|
+
end
|
33
|
+
|
34
|
+
def dump(value)
|
35
|
+
last_index = value.class.fields.last[:index]
|
36
|
+
|
37
|
+
data_headers = []
|
38
|
+
data_header = "".force_encoding("ASCII-8bit")
|
39
|
+
data = "".force_encoding("ASCII-8bit")
|
40
|
+
header_size = 4 + 4 + (last_index+1)*4
|
41
|
+
|
42
|
+
value.class.fields.each do |field|
|
43
|
+
data_headers[field[:index]] = header_size + data.bytesize
|
44
|
+
|
45
|
+
v = value.send(field[:name])
|
46
|
+
if field[:options][:nullable]
|
47
|
+
data << Utils.write_u1(v.nil? ? 0 : 1)
|
48
|
+
end
|
49
|
+
|
50
|
+
data << get_serializer(field[:type]).serialize(v)
|
51
|
+
end
|
52
|
+
|
53
|
+
data_headers.each do |h|
|
54
|
+
data_header << Utils.write_s4(h || -1)
|
55
|
+
end
|
56
|
+
|
57
|
+
data_size = header_size + data.bytesize
|
58
|
+
|
59
|
+
response = "".force_encoding("ASCII-8bit")
|
60
|
+
response << Utils.write_s4(data_size)
|
61
|
+
response << Utils.write_s4(last_index)
|
62
|
+
response << data_header
|
63
|
+
response << data
|
64
|
+
response
|
65
|
+
end
|
66
|
+
|
67
|
+
def load(klass, bytes)
|
68
|
+
object_size = Utils.read_s4(bytes, 0)
|
69
|
+
raise "Object size missmatch, expected=#{object_size}, actual=#{bytes.bytesize}" if object_size != bytes.bytesize
|
70
|
+
|
71
|
+
last_index = Utils.read_s4(bytes, 4)
|
72
|
+
raise "Last index is negative, #{last_index}" if last_index < 0
|
73
|
+
raise "Last index is bigger, expected=#{klass.fields.last[:index]}, actual=#{last_index}" if klass.fields.last[:index] < last_index
|
74
|
+
|
75
|
+
result = klass.new
|
76
|
+
|
77
|
+
klass.fields.each do |field|
|
78
|
+
index = field[:index]
|
79
|
+
offset = Utils.read_s4(bytes, 8+4*index)
|
80
|
+
if field[:options][:nullable]
|
81
|
+
if Utils.read_u1(bytes, offset) == 0
|
82
|
+
result.send("#{field[:name]}=", nil)
|
83
|
+
next
|
84
|
+
end
|
85
|
+
offset += 1
|
86
|
+
end
|
87
|
+
|
88
|
+
value = get_serializer(field[:type]).deserialize(bytes, offset)
|
89
|
+
|
90
|
+
result.send("#{field[:name]}=", value)
|
91
|
+
end
|
92
|
+
result
|
93
|
+
end
|
94
|
+
private
|
95
|
+
def add_implement(klass)
|
96
|
+
@implements ||= []
|
97
|
+
@implements << klass
|
98
|
+
gen_type_table
|
99
|
+
end
|
100
|
+
|
101
|
+
def get_serializer(type)
|
102
|
+
@type_table[type] || raise("Unknown type #{type}")
|
103
|
+
end
|
104
|
+
|
105
|
+
def gen_type_table
|
106
|
+
@type_table = Hash[@implements.flat_map{|klass|
|
107
|
+
klass::Alias.map{|a| [a, klass]}
|
108
|
+
}]
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ZeroFormatter
|
2
|
+
class Serializer
|
3
|
+
module BoolSerializer
|
4
|
+
extend self
|
5
|
+
Alias = %i(bool boolean)
|
6
|
+
def serialize(value)
|
7
|
+
Utils.write_u1(value ? 1 : 0)
|
8
|
+
end
|
9
|
+
|
10
|
+
def deserialize(bytes, offset=0)
|
11
|
+
Utils.read_u1(bytes, offset)==1
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ZeroFormatter
|
2
|
+
class Serializer
|
3
|
+
module FloatSerializer
|
4
|
+
extend self
|
5
|
+
Alias = %i(single float)
|
6
|
+
def serialize(value)
|
7
|
+
Utils.write_f4(value)
|
8
|
+
end
|
9
|
+
|
10
|
+
def deserialize(bytes, offset=0)
|
11
|
+
Utils.read_f4(bytes, offset)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module DoubleSerializer
|
16
|
+
extend self
|
17
|
+
Alias = %i(double)
|
18
|
+
def serialize(value)
|
19
|
+
Utils.write_d8(value)
|
20
|
+
end
|
21
|
+
|
22
|
+
def deserialize(bytes, offset=0)
|
23
|
+
Utils.read_d8(bytes, offset)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module ZeroFormatter
|
2
|
+
class Serializer
|
3
|
+
module Int8Serializer
|
4
|
+
extend self
|
5
|
+
Alias = %i(sbyte int8 int_8)
|
6
|
+
def serialize(value)
|
7
|
+
Utils.write_s1(value)
|
8
|
+
end
|
9
|
+
|
10
|
+
def deserialize(bytes, offset=0)
|
11
|
+
Utils.read_s1(bytes, offset)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module Int16Serializer
|
16
|
+
extend self
|
17
|
+
Alias = %i(short int16 int_16)
|
18
|
+
def serialize(value)
|
19
|
+
Utils.write_s2(value)
|
20
|
+
end
|
21
|
+
|
22
|
+
def deserialize(bytes, offset=0)
|
23
|
+
Utils.read_s2(bytes, offset)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module Int32Serializer
|
28
|
+
extend self
|
29
|
+
Alias = %i(int int32 int_32)
|
30
|
+
def serialize(value)
|
31
|
+
Utils.write_s4(value)
|
32
|
+
end
|
33
|
+
|
34
|
+
def deserialize(bytes, offset=0)
|
35
|
+
Utils.read_s4(bytes, offset)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module Int64Serializer
|
40
|
+
extend self
|
41
|
+
Alias = %i(long int64 int_64)
|
42
|
+
def serialize(value)
|
43
|
+
Utils.write_s8(value)
|
44
|
+
end
|
45
|
+
|
46
|
+
def deserialize(bytes, offset=0)
|
47
|
+
Utils.read_s8(bytes, offset)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ZeroFormatter
|
2
|
+
module Serializer
|
3
|
+
class ObjectSerializer
|
4
|
+
def initialize(klass)
|
5
|
+
@klass = klass
|
6
|
+
end
|
7
|
+
|
8
|
+
def serialize(value)
|
9
|
+
ZeroFormatter.dump(klass, bytes)
|
10
|
+
end
|
11
|
+
|
12
|
+
def deserialize(bytes)
|
13
|
+
ZeroFormatter.parse(klass, bytes)
|
14
|
+
object_size = read_s4(bytes, 0)
|
15
|
+
raise "Object size missmatch, expected=#{object_size}, actual=#{bytes.bytesize}" if object_size != bytes.bytesize
|
16
|
+
|
17
|
+
field_num = read_s4(bytes, 4)
|
18
|
+
raise "Field count is negative, #{field_num}" if field_num < 0
|
19
|
+
|
20
|
+
field_map =
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module ZeroFormatter
|
2
|
+
class Serializer
|
3
|
+
module CharSerializer
|
4
|
+
extend self
|
5
|
+
Alias = %i(char)
|
6
|
+
def serialize(value)
|
7
|
+
bytesize = (value || "").bytesize
|
8
|
+
result = value || ""
|
9
|
+
(result.encode("utf-16le").bytes + [0, 0]).pack("CC")
|
10
|
+
end
|
11
|
+
|
12
|
+
def deserialize(bytes, offset=0)
|
13
|
+
bytes.byteslice(offset, 2).force_encoding("utf-16le").encode(__ENCODING__)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module StringSerializer
|
18
|
+
extend self
|
19
|
+
Alias = %i(string str)
|
20
|
+
def serialize(value)
|
21
|
+
value ||= ""
|
22
|
+
|
23
|
+
Utils.write_s4(value.bytesize) << value
|
24
|
+
end
|
25
|
+
|
26
|
+
def deserialize(bytes, offset=0)
|
27
|
+
len = Utils.read_s4(bytes, offset)
|
28
|
+
bytes.byteslice(offset+4, len)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module ZeroFormatter
|
2
|
+
class Serializer
|
3
|
+
module TimeSpanSerializer
|
4
|
+
extend self
|
5
|
+
Alias = %i(timespan duration)
|
6
|
+
def serialize(value)
|
7
|
+
value ||= { seconds: 0, nanos: 0 }
|
8
|
+
Utils.write_s8(value[:seconds]) + Utils.write_s4(value[:nanos])
|
9
|
+
end
|
10
|
+
|
11
|
+
def deserialize(bytes, offset=0)
|
12
|
+
value = {
|
13
|
+
seconds: Utils.read_s8(bytes, offset),
|
14
|
+
nanos: Utils.read_s4(bytes, offset+8)
|
15
|
+
}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module TimeSerializer
|
20
|
+
extend self
|
21
|
+
Alias = %i(datetime time)
|
22
|
+
def serialize(value)
|
23
|
+
value ||= Time.at(0)
|
24
|
+
Utils.write_s8(value.to_i) << Utils.write_s4(value.nsec)
|
25
|
+
end
|
26
|
+
|
27
|
+
def deserialize(bytes, offset=0)
|
28
|
+
value = Time.at(Utils.read_s8(bytes, offset), Utils.read_s4(bytes, offset+8)/1000.0)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module TimeWithOffsetSerializer
|
33
|
+
extend self
|
34
|
+
Alias = %i(datetime_with_offset time_with_offset)
|
35
|
+
def serialize(value)
|
36
|
+
value ||= {time: Time.at(0), time_offset: 0}
|
37
|
+
Utils.write_s8(value[:time].to_i + value[:time_offset]*60) << Utils.write_s4(value[:time].nsec) << Utils.write_s2(value[:time_offset])
|
38
|
+
end
|
39
|
+
|
40
|
+
def deserialize(bytes, offset=0)
|
41
|
+
time = Time.at(Utils.read_s8(bytes, offset), Utils.read_s4(bytes, offset+8)/1000.0)
|
42
|
+
time_offset = Utils.read_s2(bytes, offset+8+4)
|
43
|
+
time -= time_offset*60
|
44
|
+
{
|
45
|
+
time: time,
|
46
|
+
time_offset: time_offset
|
47
|
+
}
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module ZeroFormatter
|
2
|
+
class Serializer
|
3
|
+
module UInt8Serializer
|
4
|
+
extend self
|
5
|
+
Alias = %i(byte uint8 uint_8)
|
6
|
+
def serialize(value)
|
7
|
+
Utils.write_u1(value)
|
8
|
+
end
|
9
|
+
|
10
|
+
def deserialize(bytes, offset=0)
|
11
|
+
Utils.read_u1(bytes, offset)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module UInt16Serializer
|
16
|
+
extend self
|
17
|
+
Alias = %i(ushort uint16 uint_16)
|
18
|
+
def serialize(value)
|
19
|
+
Utils.write_u2(value)
|
20
|
+
end
|
21
|
+
|
22
|
+
def deserialize(bytes, offset=0)
|
23
|
+
Utils.read_u2(bytes, offset)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module UInt32Serializer
|
28
|
+
extend self
|
29
|
+
Alias = %i(uint uint32 uint_32)
|
30
|
+
def serialize(value)
|
31
|
+
Utils.write_u4(value)
|
32
|
+
end
|
33
|
+
|
34
|
+
def deserialize(bytes, offset=0)
|
35
|
+
Utils.read_u4(bytes, offset)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module UInt64Serializer
|
40
|
+
extend self
|
41
|
+
Alias = %i(ulong uint64 uint_64)
|
42
|
+
def serialize(value)
|
43
|
+
Utils.write_u8(value)
|
44
|
+
end
|
45
|
+
|
46
|
+
def deserialize(bytes, offset=0)
|
47
|
+
Utils.read_u8(bytes, offset)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
module ZeroFormatter
|
2
|
+
class Utils
|
3
|
+
class << self
|
4
|
+
def read_s1(bytes, index)
|
5
|
+
to_signed(bytes.getbyte(index), 1 << (8 -1))
|
6
|
+
end
|
7
|
+
|
8
|
+
def read_u1(bytes, index)
|
9
|
+
bytes.getbyte(index)
|
10
|
+
end
|
11
|
+
|
12
|
+
def read_u2(bytes, index)
|
13
|
+
bytes.byteslice(index, 2).unpack('v')[0]
|
14
|
+
end
|
15
|
+
|
16
|
+
def read_s2(bytes, index)
|
17
|
+
to_signed(read_u2(bytes, index), 1 << (16 - 1))
|
18
|
+
end
|
19
|
+
|
20
|
+
def read_u4(bytes, index)
|
21
|
+
bytes.byteslice(index, 4).unpack('V')[0]
|
22
|
+
end
|
23
|
+
|
24
|
+
def read_s4(bytes, index)
|
25
|
+
to_signed(read_u4(bytes, index), 1 << (32 - 1))
|
26
|
+
end
|
27
|
+
|
28
|
+
def read_u8(bytes, index)
|
29
|
+
l, h = bytes.byteslice(index, 8).unpack('VV')
|
30
|
+
(h << 32) + l
|
31
|
+
end
|
32
|
+
|
33
|
+
def read_s8(bytes, index)
|
34
|
+
to_signed(read_u8(bytes, index), 1 << (64 - 1))
|
35
|
+
end
|
36
|
+
|
37
|
+
def read_f4(bytes, index)
|
38
|
+
bytes.byteslice(index, 4).unpack('e')[0]
|
39
|
+
end
|
40
|
+
|
41
|
+
def read_d8(bytes, index)
|
42
|
+
bytes.byteslice(index, 8).unpack('E')[0]
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_signed(v, mask)
|
46
|
+
(v & ~mask) - (v & mask)
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
def write_u1(value)
|
53
|
+
value ||= 0
|
54
|
+
[value].pack("C")
|
55
|
+
end
|
56
|
+
|
57
|
+
def write_s1(value)
|
58
|
+
value ||= 0
|
59
|
+
[value].pack("c")
|
60
|
+
end
|
61
|
+
|
62
|
+
def write_u2(value)
|
63
|
+
value ||= 0
|
64
|
+
[value].pack('v')
|
65
|
+
end
|
66
|
+
|
67
|
+
def write_s2(value)
|
68
|
+
value ||= 0
|
69
|
+
[value].pack('v')
|
70
|
+
end
|
71
|
+
|
72
|
+
def write_u4(value)
|
73
|
+
value ||= 0
|
74
|
+
[value].pack('V')
|
75
|
+
end
|
76
|
+
|
77
|
+
def write_s4(value)
|
78
|
+
value ||= 0
|
79
|
+
[value].pack('V')
|
80
|
+
end
|
81
|
+
|
82
|
+
def write_u8(value)
|
83
|
+
value ||= 0
|
84
|
+
[value].pack('Q!<')
|
85
|
+
end
|
86
|
+
|
87
|
+
def write_s8(value)
|
88
|
+
value ||= 0
|
89
|
+
value += (2 ** 64) if value < 0
|
90
|
+
[value].pack('q!<')
|
91
|
+
end
|
92
|
+
|
93
|
+
def write_f4(value)
|
94
|
+
value ||= 0.0
|
95
|
+
[value].pack("e")
|
96
|
+
end
|
97
|
+
|
98
|
+
def write_d8(value)
|
99
|
+
value ||= 0.0
|
100
|
+
[value].pack("E")
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ZeroFormatter
|
2
|
+
module ZeroFormattable
|
3
|
+
def self.included(klass)
|
4
|
+
klass.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def fields
|
9
|
+
@fields || []
|
10
|
+
end
|
11
|
+
|
12
|
+
def field(name, type, options={})
|
13
|
+
@fields ||= []
|
14
|
+
index = options[:index] || @fields.size == 0 ? 0 : @fields.last[:index] + 1
|
15
|
+
@fields << {
|
16
|
+
index: index,
|
17
|
+
name: name,
|
18
|
+
type: type,
|
19
|
+
options: options,
|
20
|
+
}
|
21
|
+
|
22
|
+
attr_accessor name
|
23
|
+
return
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'zero_formatter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "zero_formatter"
|
8
|
+
spec.version = ZeroFormatter::VERSION
|
9
|
+
spec.authors = ["aki017"]
|
10
|
+
spec.email = ["aki@aki017.info"]
|
11
|
+
|
12
|
+
spec.summary = %q{ZeroFormatter ruby port}
|
13
|
+
spec.description = %q{ZeroFormatter ruby port}
|
14
|
+
spec.homepage = "https://github.com/aki017/zeroformatter.rb"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
26
|
+
spec.add_development_dependency "guard", "~> 2.14.0"
|
27
|
+
spec.add_development_dependency "guard-minitest", "~> 2.4.6"
|
28
|
+
spec.add_development_dependency "simplecov", "~> 0.12.0"
|
29
|
+
spec.add_development_dependency "codeclimate-test-reporter", "~> 1.0.3"
|
30
|
+
spec.add_development_dependency "minitest-reporters", "~> 1.1.12"
|
31
|
+
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zero_formatter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- aki017
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-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.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
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.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.14.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.14.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard-minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 2.4.6
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 2.4.6
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.12.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.12.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: codeclimate-test-reporter
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.0.3
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.0.3
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: minitest-reporters
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.1.12
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 1.1.12
|
125
|
+
description: ZeroFormatter ruby port
|
126
|
+
email:
|
127
|
+
- aki@aki017.info
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- Gemfile
|
134
|
+
- Guardfile
|
135
|
+
- README.md
|
136
|
+
- Rakefile
|
137
|
+
- bin/console
|
138
|
+
- bin/setup
|
139
|
+
- circle.yml
|
140
|
+
- lib/zero_formatter.rb
|
141
|
+
- lib/zero_formatter/serializer.rb
|
142
|
+
- lib/zero_formatter/serializer/bool.rb
|
143
|
+
- lib/zero_formatter/serializer/fp.rb
|
144
|
+
- lib/zero_formatter/serializer/int.rb
|
145
|
+
- lib/zero_formatter/serializer/object.rb
|
146
|
+
- lib/zero_formatter/serializer/string.rb
|
147
|
+
- lib/zero_formatter/serializer/time.rb
|
148
|
+
- lib/zero_formatter/serializer/uint.rb
|
149
|
+
- lib/zero_formatter/utils.rb
|
150
|
+
- lib/zero_formatter/version.rb
|
151
|
+
- lib/zero_formatter/zero_formattable.rb
|
152
|
+
- zero_formatter.gemspec
|
153
|
+
homepage: https://github.com/aki017/zeroformatter.rb
|
154
|
+
licenses: []
|
155
|
+
metadata: {}
|
156
|
+
post_install_message:
|
157
|
+
rdoc_options: []
|
158
|
+
require_paths:
|
159
|
+
- lib
|
160
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
161
|
+
requirements:
|
162
|
+
- - ">="
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
version: '0'
|
165
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
166
|
+
requirements:
|
167
|
+
- - ">="
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
version: '0'
|
170
|
+
requirements: []
|
171
|
+
rubyforge_project:
|
172
|
+
rubygems_version: 2.5.1
|
173
|
+
signing_key:
|
174
|
+
specification_version: 4
|
175
|
+
summary: ZeroFormatter ruby port
|
176
|
+
test_files: []
|