msgpack 0.6.0 → 0.6.1

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: f57829db3ad4359a6065eced56e9fcd9bcbea1ce
4
- data.tar.gz: 5c2674055751c11c5756ef056dfc3ff871ce6036
3
+ metadata.gz: 9bb14c162f5f2190f44cd5b8613e384d89a6ac62
4
+ data.tar.gz: d37af929b9bb3cff9bef8ce2f75b36124824993a
5
5
  SHA512:
6
- metadata.gz: a9128c789359ddaa85199b3308e9b9d2e71889fd93b64813c247fad4a6cbc08059566006722c2349f1a3122dcf88eb0ff73fda1b2cea9c1b7e33eb79a7d0b5ed
7
- data.tar.gz: 6be7ba85981626bf0bb2c938e66449cadcc03cfe6d62611639364f163b871aab64ab0ff2ab83eb8af10db713b528c3e5364f5f8ee179604864fb3cffdd934827
6
+ metadata.gz: 4389bb67dd52c5cd6a3c0112e880f3b9fb07a4b10667bcf1e4552fd8ad3e4d2b34a53d4c6836574e50933bd21a79f5d3257f74b2ca53bcf6c941ac4494b036e2
7
+ data.tar.gz: ea5abfcbf7cbea580de4b376270b2568f0acb28496cb2e7b413d793a0327bb6859540e336544fac47b10bf80c96fb990361e1a46ea3027e3712d1428775ff0d9
@@ -17,7 +17,11 @@ module MessagePack
17
17
  # This packer writes serialzied objects into the IO when the internal buffer is filled.
18
18
  # _io_ must respond to write(string) or append(string) method.
19
19
  #
20
- # See also Buffer#initialize for options.
20
+ # Supported options:
21
+ #
22
+ # * *:compatibility_mode* serialize in older versions way, without str8 and bin types
23
+ #
24
+ # See also Buffer#initialize for other options.
21
25
  #
22
26
  def initialize(*args)
23
27
  end
@@ -21,7 +21,6 @@ import org.jcodings.Encoding;
21
21
 
22
22
  @JRubyClass(name="MessagePack::Buffer")
23
23
  public class Buffer extends RubyObject {
24
- private RubyHash options;
25
24
  private IRubyObject io;
26
25
  private ByteBuffer buffer;
27
26
  private boolean writeMode;
@@ -46,9 +45,6 @@ public class Buffer extends RubyObject {
46
45
  if (args[0].respondsTo("read") && args[0].respondsTo("write")) {
47
46
  this.io = args[0];
48
47
  }
49
- if (args[args.length - 1] instanceof RubyHash) {
50
- this.options = (RubyHash) args[args.length - 1];
51
- }
52
48
  }
53
49
  this.buffer = ByteBuffer.allocate(CACHE_LINE_SIZE - ARRAY_HEADER_SIZE);
54
50
  this.writeMode = true;
@@ -34,14 +34,16 @@ public class Encoder {
34
34
  private final Ruby runtime;
35
35
  private final Encoding binaryEncoding;
36
36
  private final Encoding utf8Encoding;
37
+ private final boolean compatibilityMode;
37
38
 
38
39
  private ByteBuffer buffer;
39
40
 
40
- public Encoder(Ruby runtime) {
41
+ public Encoder(Ruby runtime, boolean compatibilityMode) {
41
42
  this.runtime = runtime;
42
43
  this.buffer = ByteBuffer.allocate(CACHE_LINE_SIZE - ARRAY_HEADER_SIZE);
43
44
  this.binaryEncoding = runtime.getEncodingService().getAscii8bitEncoding();
44
45
  this.utf8Encoding = UTF8Encoding.INSTANCE;
46
+ this.compatibilityMode = compatibilityMode;
45
47
  }
46
48
 
47
49
  private void ensureRemainingCapacity(int c) {
@@ -190,7 +192,7 @@ public class Encoder {
190
192
 
191
193
  private void appendString(RubyString object) {
192
194
  Encoding encoding = object.getEncoding();
193
- boolean binary = encoding == binaryEncoding;
195
+ boolean binary = !compatibilityMode && encoding == binaryEncoding;
194
196
  if (encoding != utf8Encoding && encoding != binaryEncoding) {
195
197
  object = (RubyString) ((RubyString) object).encode(runtime.getCurrentContext(), runtime.getEncodingService().getEncoding(utf8Encoding));
196
198
  }
@@ -199,7 +201,7 @@ public class Encoder {
199
201
  if (length < 32 && !binary) {
200
202
  ensureRemainingCapacity(1 + length);
201
203
  buffer.put((byte) (length | FIXSTR));
202
- } else if (length <= 0xff) {
204
+ } else if (length <= 0xff && !compatibilityMode) {
203
205
  ensureRemainingCapacity(2 + length);
204
206
  buffer.put(binary ? BIN8 : STR8);
205
207
  buffer.put((byte) length);
@@ -32,7 +32,12 @@ public class Packer extends RubyObject {
32
32
 
33
33
  @JRubyMethod(name = "initialize", optional = 2)
34
34
  public IRubyObject initialize(ThreadContext ctx, IRubyObject[] args) {
35
- this.encoder = new Encoder(ctx.getRuntime());
35
+ boolean compatibilityMode = false;
36
+ if (args.length > 0 && args[args.length - 1] instanceof RubyHash) {
37
+ RubyHash options = (RubyHash) args[args.length - 1];
38
+ compatibilityMode = options.fastARef(ctx.getRuntime().newSymbol("compatibility_mode")).isTrue();
39
+ }
40
+ this.encoder = new Encoder(ctx.getRuntime(), compatibilityMode);
36
41
  this.buffer = new Buffer(ctx.getRuntime(), ctx.getRuntime().getModule("MessagePack").getClass("Buffer"));
37
42
  this.buffer.initialize(ctx, args);
38
43
  return this;
@@ -32,11 +32,15 @@ struct msgpack_packer_t {
32
32
 
33
33
  VALUE io;
34
34
  ID io_write_all_method;
35
+ bool compatibility_mode;
35
36
 
36
37
  ID to_msgpack_method;
37
38
  VALUE to_msgpack_arg;
38
39
 
39
40
  VALUE buffer_ref;
41
+
42
+ /* options */
43
+ bool comaptibility_mode;
40
44
  };
41
45
 
42
46
  #define PACKER_BUFFER_(pk) (&(pk)->buffer)
@@ -66,6 +70,11 @@ static inline void msgpack_packer_set_io(msgpack_packer_t* pk, VALUE io, ID io_w
66
70
 
67
71
  void msgpack_packer_reset(msgpack_packer_t* pk);
68
72
 
73
+ static inline void msgpack_packer_set_compat(msgpack_packer_t* pk, bool enable)
74
+ {
75
+ pk->compatibility_mode = enable;
76
+ }
77
+
69
78
  static inline void msgpack_packer_write_nil(msgpack_packer_t* pk)
70
79
  {
71
80
  msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
@@ -270,7 +279,7 @@ static inline void msgpack_packer_write_raw_header(msgpack_packer_t* pk, unsigne
270
279
  msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
271
280
  unsigned char h = 0xa0 | (uint8_t) n;
272
281
  msgpack_buffer_write_1(PACKER_BUFFER_(pk), h);
273
- } else if(n < 256) {
282
+ } else if(n < 256 && !pk->compatibility_mode) {
274
283
  msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 2);
275
284
  unsigned char be = (uint8_t) n;
276
285
  msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xd9, (const void*)&be, 1);
@@ -358,7 +367,6 @@ static inline bool msgpack_packer_is_utf8_compat_string(VALUE v, int encindex)
358
367
 
359
368
  static inline void msgpack_packer_write_string_value(msgpack_packer_t* pk, VALUE v)
360
369
  {
361
- /* TODO encoding conversion? */
362
370
  /* actual return type of RSTRING_LEN is long */
363
371
  unsigned long len = RSTRING_LEN(v);
364
372
  if(len > 0xffffffffUL) {
@@ -368,13 +376,14 @@ static inline void msgpack_packer_write_string_value(msgpack_packer_t* pk, VALUE
368
376
 
369
377
  #ifdef COMPAT_HAVE_ENCODING
370
378
  int encindex = ENCODING_GET(v);
371
- if(msgpack_packer_is_binary(v, encindex)) {
379
+ if(msgpack_packer_is_binary(v, encindex) && !pk->compatibility_mode) {
372
380
  /* write ASCII-8BIT string using Binary type */
373
381
  msgpack_packer_write_bin_header(pk, (unsigned int)len);
374
382
  msgpack_buffer_append_string(PACKER_BUFFER_(pk), v);
375
383
  } else {
376
384
  /* write UTF-8, US-ASCII, or 7bit-safe ascii-compatible string using String type directly */
377
- if(!msgpack_packer_is_utf8_compat_string(v, encindex)) {
385
+ /* in compatibility mode, packer packs String values as is */
386
+ if(!pk->compatibility_mode && !msgpack_packer_is_utf8_compat_string(v, encindex)) {
378
387
  /* transcode other strings to UTF-8 and write using String type */
379
388
  VALUE enc = rb_enc_from_encoding(rb_utf8_encoding()); /* rb_enc_from_encoding_index is not extern */
380
389
  v = rb_str_encode(v, enc, 0, Qnil);
@@ -87,13 +87,24 @@ static VALUE Packer_initialize(int argc, VALUE* argv, VALUE self)
87
87
  }
88
88
 
89
89
  PACKER(self, pk);
90
- MessagePack_Buffer_initialize(PACKER_BUFFER_(pk), io, options);
91
90
 
92
- // TODO MessagePack_Unpacker_initialize and options
91
+ MessagePack_Packer_initialize(pk, io, options);
93
92
 
94
93
  return self;
95
94
  }
96
95
 
96
+ void MessagePack_Packer_initialize(msgpack_packer_t* pk, VALUE io, VALUE options)
97
+ {
98
+ MessagePack_Buffer_initialize(PACKER_BUFFER_(pk), io, options);
99
+
100
+ if(options != Qnil) {
101
+ VALUE v;
102
+
103
+ v = rb_hash_aref(options, ID2SYM(rb_intern("compatibility_mode")));
104
+ msgpack_packer_set_compat(pk, RTEST(v));
105
+ }
106
+ }
107
+
97
108
  static VALUE Packer_buffer(VALUE self)
98
109
  {
99
110
  PACKER(self, pk);
@@ -221,11 +232,15 @@ VALUE MessagePack_pack(int argc, VALUE* argv)
221
232
 
222
233
  VALUE self = Packer_alloc(cMessagePack_Packer);
223
234
  PACKER(self, pk);
235
+
236
+ if (options != Qnil) {
237
+ pk->compatibility_mode = RTEST(rb_hash_aref(options, ID2SYM(rb_intern("compatibility_mode"))));
238
+ }
239
+
224
240
  //msgpack_packer_reset(s_packer);
225
241
  //msgpack_buffer_reset_io(PACKER_BUFFER_(s_packer));
226
242
 
227
- MessagePack_Buffer_initialize(PACKER_BUFFER_(pk), io, options);
228
- // TODO MessagePack_Unpacker_initialize and options
243
+ MessagePack_Packer_initialize(pk, io, options);
229
244
 
230
245
  msgpack_packer_write_value(pk, v);
231
246
 
@@ -26,5 +26,7 @@ void MessagePack_Packer_module_init(VALUE mMessagePack);
26
26
 
27
27
  VALUE MessagePack_pack(int argc, VALUE* argv);
28
28
 
29
+ void MessagePack_Packer_initialize(msgpack_packer_t* pk, VALUE io, VALUE options);
30
+
29
31
  #endif
30
32
 
@@ -1,3 +1,3 @@
1
1
  module MessagePack
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
3
3
  end
@@ -116,5 +116,23 @@ describe Packer do
116
116
  CustomPack02.new.to_msgpack(s04)
117
117
  s04.string.should == [1,2].to_msgpack
118
118
  end
119
+
120
+ context 'in compatibility mode' do
121
+ it 'does not use the bin types' do
122
+ packed = MessagePack.pack('hello'.force_encoding(Encoding::BINARY), compatibility_mode: true)
123
+ packed.should eq("\xA5hello")
124
+ packed = MessagePack.pack(('hello' * 100).force_encoding(Encoding::BINARY), compatibility_mode: true)
125
+ packed.should start_with("\xDA\x01\xF4")
126
+
127
+ packer = MessagePack::Packer.new(compatibility_mode: 1)
128
+ packed = packer.pack(('hello' * 100).force_encoding(Encoding::BINARY))
129
+ packed.to_str.should start_with("\xDA\x01\xF4")
130
+ end
131
+
132
+ it 'does not use the str8 type' do
133
+ packed = MessagePack.pack('x' * 32, compatibility_mode: true)
134
+ packed.should start_with("\xDA\x00\x20")
135
+ end
136
+ end
119
137
  end
120
138
 
@@ -36,9 +36,15 @@ describe MessagePack do
36
36
  ['negative floats', -2.1, "\xCB\xC0\x00\xCC\xCC\xCC\xCC\xCC\xCD"]
37
37
  ],
38
38
  'strings' => [
39
- ['strings', utf8enc('hello world'), "\xABhello world"],
39
+ ['tiny strings', utf8enc('hello world'), "\xABhello world"],
40
+ ['short strings', utf8enc('hello' * 5), "\xB9hellohellohellohellohello"],
40
41
  ['empty strings', utf8enc(''), "\xA0"]
41
42
  ],
43
+ 'binary strings' => [
44
+ ['tiny strings', asciienc('hello world'), "\xC4\vhello world"],
45
+ ['short strings', asciienc('hello' * 5), "\xC4\x19hellohellohellohellohello"],
46
+ ['empty strings', asciienc(''), "\xC4\x00"]
47
+ ],
42
48
  'arrays' => [
43
49
  ['empty arrays', [], "\x90"],
44
50
  ['arrays with strings', [utf8enc("hello"), utf8enc("world")], "\x92\xA5hello\xA5world"],
@@ -139,4 +145,18 @@ describe MessagePack do
139
145
  packed.index("w\xC3\xA5rld").should_not be_nil
140
146
  end
141
147
  end
148
+
149
+ context 'in compatibility mode' do
150
+ it 'does not use the bin types' do
151
+ packed = MessagePack.pack('hello'.force_encoding(Encoding::BINARY), compatibility_mode: true)
152
+ packed.should eq("\xA5hello")
153
+ packed = MessagePack.pack(('hello' * 100).force_encoding(Encoding::BINARY), compatibility_mode: true)
154
+ packed.should start_with("\xDA\x01\xF4")
155
+ end
156
+
157
+ it 'does not use the str8 type' do
158
+ packed = MessagePack.pack('x' * 32, compatibility_mode: true)
159
+ packed.should start_with("\xDA\x00\x20")
160
+ end
161
+ end
142
162
  end
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.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sadayuki Furuhashi
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-05-26 00:00:00.000000000 Z
12
+ date: 2015-07-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler