msgpack 0.7.2 → 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b10edb0e5314270ccc2fb9f8f36c6496ff26a92
4
- data.tar.gz: 0258a0e56bc3ba4f052d2ce45cda0bd2e24dc1c6
3
+ metadata.gz: 4cdb90287933e109cb65b2cfa74d7bd6818e040e
4
+ data.tar.gz: 07461ed30e0f027158b3dc71f7abdaf08e45f2dd
5
5
  SHA512:
6
- metadata.gz: 24c445326d2c6b8cf769969ce02b9dd56c6ff4336794e41bce41a7c8fe9448037bb48ce2a8c5bb5595e3522dc23a5dd351788d738959935b7302e7d2c652b178
7
- data.tar.gz: 06dddef0dc53502d70810e869e0ff9bc01976aca014d8530a11e724e4ef0e5ab9c3800c5e990419dce264fe8513b7ed46d63405db2f34ce8af0f2031084921eb
6
+ metadata.gz: 4e625fd2e06ea1470f0e5b109460dc5206bdb408402a73a49040ec093e2c11b8f02f81beab8ee267875410acfe0f0cea72f88c74bb21a7bdaf5eb53e0db3ad16
7
+ data.tar.gz: b1ab3226c75553fc3f8988b8b661965492c6f45ce2f7a68c4f4747e31a215c6c2f6bd56486483e9bfe3c101c62fccfbe783b8ed431b09440b2b66419cb116d6d
data/ChangeLog CHANGED
@@ -1,3 +1,7 @@
1
+ 2016-01-07 version 0.7.3:
2
+
3
+ * Add Packer#write_float32 method to pack floating point numbers into FLOAT of messagepack.
4
+
1
5
  2016-01-06 version 0.7.2:
2
6
 
3
7
  * Improved compatibility of Unpacker between CRuby and JRuby to accept stream-like object
data/Rakefile CHANGED
@@ -65,7 +65,7 @@ namespace :build do
65
65
  desc 'Build gems for Windows per rake-compiler-dock'
66
66
  task :windows do
67
67
  require 'rake_compiler_dock'
68
- RakeCompilerDock.sh 'bundle && rake cross native gem RUBY_CC_VERSION=1.9.3:2.0.0:2.1.6:2.2.2'
68
+ RakeCompilerDock.sh 'bundle && rake cross native gem RUBY_CC_VERSION=1.9.3:2.0.0:2.1.6:2.2.2:2.3.0'
69
69
  end
70
70
  end
71
71
 
@@ -109,6 +109,18 @@ module MessagePack
109
109
  def write_map_header(n)
110
110
  end
111
111
 
112
+ #
113
+ # Serializes _value_ as 32-bit single precision float into internal buffer.
114
+ # _value_ will be approximated with the nearest possible single precision float, thus
115
+ # being potentially lossy. However, the serialized string will only take up 5 bytes
116
+ # instead of 9 bytes compared to directly serializing a 64-bit double precision Ruby Float.
117
+ #
118
+ # @param value [Numeric]
119
+ # @return [Packer] self
120
+ #
121
+ def write_float32(value)
122
+ end
123
+
112
124
  #
113
125
  # Flushes data in the internal buffer to the internal IO. Same as _buffer.flush.
114
126
  # If internal IO is not set, it does nothing.
@@ -8,6 +8,7 @@ import org.jruby.Ruby;
8
8
  import org.jruby.RubyObject;
9
9
  import org.jruby.RubyNil;
10
10
  import org.jruby.RubyBoolean;
11
+ import org.jruby.RubyNumeric;
11
12
  import org.jruby.RubyBignum;
12
13
  import org.jruby.RubyInteger;
13
14
  import org.jruby.RubyFixnum;
@@ -85,6 +86,11 @@ public class Encoder {
85
86
  return readRubyString();
86
87
  }
87
88
 
89
+ public IRubyObject encodeFloat32(RubyNumeric numeric) {
90
+ appendFloat32(numeric);
91
+ return readRubyString();
92
+ }
93
+
88
94
  private void appendObject(IRubyObject object) {
89
95
  appendObject(object, null);
90
96
  }
@@ -182,8 +188,8 @@ public class Encoder {
182
188
 
183
189
  private void appendFloat(RubyFloat object) {
184
190
  double value = object.getDoubleValue();
185
- float f = (float) value;
186
191
  //TODO: msgpack-ruby original does encode this value as Double, not float
192
+ // float f = (float) value;
187
193
  // if (Double.compare(f, value) == 0) {
188
194
  // ensureRemainingCapacity(5);
189
195
  // buffer.put(FLOAT32);
@@ -195,6 +201,13 @@ public class Encoder {
195
201
  // }
196
202
  }
197
203
 
204
+ private void appendFloat32(RubyNumeric object) {
205
+ float value = (float) object.getDoubleValue();
206
+ ensureRemainingCapacity(5);
207
+ buffer.put(FLOAT32);
208
+ buffer.putFloat(value);
209
+ }
210
+
198
211
  private void appendString(RubyString object) {
199
212
  Encoding encoding = object.getEncoding();
200
213
  boolean binary = !compatibilityMode && encoding == binaryEncoding;
@@ -7,6 +7,7 @@ import org.jruby.RubyObject;
7
7
  import org.jruby.RubyArray;
8
8
  import org.jruby.RubyHash;
9
9
  import org.jruby.RubyIO;
10
+ import org.jruby.RubyNumeric;
10
11
  import org.jruby.RubyInteger;
11
12
  import org.jruby.RubyFixnum;
12
13
  import org.jruby.runtime.Block;
@@ -114,6 +115,16 @@ public class Packer extends RubyObject {
114
115
  return this;
115
116
  }
116
117
 
118
+ @JRubyMethod(name = "write_float32")
119
+ public IRubyObject writeFloat32(ThreadContext ctx, IRubyObject numeric) {
120
+ Ruby runtime = ctx.runtime;
121
+ if (!(numeric instanceof RubyNumeric)) {
122
+ throw runtime.newArgumentError("Expected numeric");
123
+ }
124
+ buffer.write(ctx, encoder.encodeFloat32((RubyNumeric) numeric));
125
+ return this;
126
+ }
127
+
117
128
  @JRubyMethod(name = "write_array_header")
118
129
  public IRubyObject writeArrayHeader(ThreadContext ctx, IRubyObject size) {
119
130
  int s = (int) size.convertToInteger().getLongValue();
@@ -255,6 +255,18 @@ static inline void msgpack_packer_write_u64(msgpack_packer_t* pk, uint64_t v)
255
255
  }
256
256
  }
257
257
 
258
+ static inline void msgpack_packer_write_float(msgpack_packer_t* pk, float v)
259
+ {
260
+ msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 5);
261
+ union {
262
+ float f;
263
+ uint32_t u32;
264
+ char mem[4];
265
+ } castbuf = { v };
266
+ castbuf.u32 = _msgpack_be_float(castbuf.u32);
267
+ msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xca, castbuf.mem, 4);
268
+ }
269
+
258
270
  static inline void msgpack_packer_write_double(msgpack_packer_t* pk, double v)
259
271
  {
260
272
  msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 9);
@@ -149,6 +149,17 @@ static VALUE Packer_write_map_header(VALUE self, VALUE n)
149
149
  return self;
150
150
  }
151
151
 
152
+ static VALUE Packer_write_float32(VALUE self, VALUE numeric)
153
+ {
154
+ if(!rb_obj_is_kind_of(numeric, rb_cNumeric)) {
155
+ rb_raise(rb_eArgError, "Expected numeric");
156
+ }
157
+
158
+ PACKER(self, pk);
159
+ msgpack_packer_write_float(pk, (float)rb_num2dbl(numeric));
160
+ return self;
161
+ }
162
+
152
163
  static VALUE Packer_write_ext(VALUE self, VALUE type, VALUE data)
153
164
  {
154
165
  PACKER(self, pk);
@@ -344,6 +355,7 @@ void MessagePack_Packer_module_init(VALUE mMessagePack)
344
355
  rb_define_method(cMessagePack_Packer, "write_array_header", Packer_write_array_header, 1);
345
356
  rb_define_method(cMessagePack_Packer, "write_map_header", Packer_write_map_header, 1);
346
357
  rb_define_method(cMessagePack_Packer, "write_ext", Packer_write_ext, 2);
358
+ rb_define_method(cMessagePack_Packer, "write_float32", Packer_write_float32, 1);
347
359
  rb_define_method(cMessagePack_Packer, "flush", Packer_flush, 0);
348
360
 
349
361
  /* delegation methods */
@@ -1,3 +1,3 @@
1
1
  module MessagePack
2
- VERSION = "0.7.2"
2
+ VERSION = "0.7.3"
3
3
  end
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
26
26
  s.add_development_dependency 'rake', ['~> 0.9.2']
27
27
  s.add_development_dependency 'rake-compiler', ['~> 0.9.4']
28
28
  if /java/ !~ RUBY_PLATFORM
29
- s.add_development_dependency 'rake-compiler-dock', ['~> 0.4.3']
29
+ s.add_development_dependency 'rake-compiler-dock', ['~> 0.5.0']
30
30
  end
31
31
  s.add_development_dependency 'rspec', ['~> 3.3']
32
32
  s.add_development_dependency 'yard', ['~> 0.8.2']
@@ -134,6 +134,30 @@ describe MessagePack::Packer do
134
134
  packer.to_s.should == "\x81"
135
135
  end
136
136
 
137
+ describe '#write_float32' do
138
+ tests = [
139
+ ['small floats', 3.14, "\xCA\x40\x48\xF5\xC3"],
140
+ ['big floats', Math::PI * 1_000_000_000_000_000_000, "\xCA\x5E\x2E\x64\xB7"],
141
+ ['negative floats', -2.1, "\xCA\xC0\x06\x66\x66"],
142
+ ['integer', 123, "\xCA\x42\xF6\x00\x00"],
143
+ ]
144
+
145
+ tests.each do |ctx, numeric, packed|
146
+ context("with #{ctx}") do
147
+ it("encodes #{numeric} as float32") do
148
+ packer.write_float32(numeric)
149
+ packer.to_s.should == packed
150
+ end
151
+ end
152
+ end
153
+
154
+ context 'with non numeric' do
155
+ it 'raises argument error' do
156
+ expect { packer.write_float32('abc') }.to raise_error(ArgumentError)
157
+ end
158
+ end
159
+ end
160
+
137
161
  it 'flush' do
138
162
  io = StringIO.new
139
163
  pk = MessagePack::Packer.new(io)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: msgpack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sadayuki Furuhashi
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-01-06 00:00:00.000000000 Z
13
+ date: 2016-01-07 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -60,14 +60,14 @@ dependencies:
60
60
  requirements:
61
61
  - - "~>"
62
62
  - !ruby/object:Gem::Version
63
- version: 0.4.3
63
+ version: 0.5.0
64
64
  type: :development
65
65
  prerelease: false
66
66
  version_requirements: !ruby/object:Gem::Requirement
67
67
  requirements:
68
68
  - - "~>"
69
69
  - !ruby/object:Gem::Version
70
- version: 0.4.3
70
+ version: 0.5.0
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: rspec
73
73
  requirement: !ruby/object:Gem::Requirement
@@ -237,7 +237,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
237
237
  version: '0'
238
238
  requirements: []
239
239
  rubyforge_project: msgpack
240
- rubygems_version: 2.4.5
240
+ rubygems_version: 2.4.5.1
241
241
  signing_key:
242
242
  specification_version: 4
243
243
  summary: MessagePack, a binary-based efficient data interchange format.