msgpack 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/ext/pack.c +19 -7
  2. data/ext/unpack.c +28 -5
  3. metadata +3 -3
data/ext/pack.c CHANGED
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * MessagePack for Ruby packing routine
3
3
  *
4
- * Copyright (C) 2008-2009 FURUHASHI Sadayuki
4
+ * Copyright (C) 2008-2010 FURUHASHI Sadayuki
5
5
  *
6
6
  * Licensed under the Apache License, Version 2.0 (the "License");
7
7
  * you may not use this file except in compliance with the License.
@@ -18,6 +18,9 @@
18
18
  #include "ruby.h"
19
19
  #include "msgpack/pack_define.h"
20
20
 
21
+ static ID s_to_msgpack;
22
+ static ID s_append;
23
+
21
24
  #define msgpack_pack_inline_func(name) \
22
25
  static inline void msgpack_pack ## name
23
26
 
@@ -27,7 +30,9 @@
27
30
  #define msgpack_pack_user VALUE
28
31
 
29
32
  #define msgpack_pack_append_buffer(user, buf, len) \
30
- rb_str_buf_cat(user, (const void*)buf, len)
33
+ ((TYPE(user) == T_STRING) ? \
34
+ rb_str_buf_cat(user, (const void*)buf, len) : \
35
+ rb_funcall(user, s_append, 1, rb_str_new((const void*)buf,len)))
31
36
 
32
37
  #include "msgpack/pack_template.h"
33
38
 
@@ -36,8 +41,6 @@
36
41
  #include "st.h" // ruby hash
37
42
  #endif
38
43
 
39
- static ID s_to_msgpack;
40
-
41
44
  #define ARG_BUFFER(name, argc, argv) \
42
45
  VALUE name; \
43
46
  if(argc == 1) { \
@@ -142,15 +145,24 @@ static VALUE MessagePack_Hash_to_msgpack(int argc, VALUE *argv, VALUE self)
142
145
  }
143
146
 
144
147
 
145
- static VALUE MessagePack_pack(VALUE self, VALUE data)
148
+ static VALUE MessagePack_pack(int argc, VALUE* argv, VALUE self)
146
149
  {
147
- return rb_funcall(data, s_to_msgpack, 0);
150
+ VALUE out;
151
+ if(argc == 1) {
152
+ out = rb_str_buf_new(0);
153
+ } else if(argc == 2) {
154
+ out = argv[1];
155
+ } else {
156
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
157
+ }
158
+ return rb_funcall(argv[0], s_to_msgpack, 1, out);
148
159
  }
149
160
 
150
161
 
151
162
  void Init_msgpack_pack(VALUE mMessagePack)
152
163
  {
153
164
  s_to_msgpack = rb_intern("to_msgpack");
165
+ s_append = rb_intern("<<");
154
166
  rb_define_method_id(rb_cNilClass, s_to_msgpack, MessagePack_NilClass_to_msgpack, -1);
155
167
  rb_define_method_id(rb_cTrueClass, s_to_msgpack, MessagePack_TrueClass_to_msgpack, -1);
156
168
  rb_define_method_id(rb_cFalseClass, s_to_msgpack, MessagePack_FalseClass_to_msgpack, -1);
@@ -160,6 +172,6 @@ void Init_msgpack_pack(VALUE mMessagePack)
160
172
  rb_define_method_id(rb_cString, s_to_msgpack, MessagePack_String_to_msgpack, -1);
161
173
  rb_define_method_id(rb_cArray, s_to_msgpack, MessagePack_Array_to_msgpack, -1);
162
174
  rb_define_method_id(rb_cHash, s_to_msgpack, MessagePack_Hash_to_msgpack, -1);
163
- rb_define_module_function(mMessagePack, "pack", MessagePack_pack, 1);
175
+ rb_define_module_function(mMessagePack, "pack", MessagePack_pack, -1);
164
176
  }
165
177
 
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * MessagePack for Ruby unpacking routine
3
3
  *
4
- * Copyright (C) 2008-2009 FURUHASHI Sadayuki
4
+ * Copyright (C) 2008-2010 FURUHASHI Sadayuki
5
5
  *
6
6
  * Licensed under the Apache License, Version 2.0 (the "License");
7
7
  * you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
19
19
  #include "msgpack/unpack_define.h"
20
20
 
21
21
  static ID s_sysread;
22
+ static ID s_readpartial;
22
23
 
23
24
  typedef struct {
24
25
  int finished;
@@ -28,6 +29,7 @@ typedef struct {
28
29
  VALUE buffer;
29
30
  VALUE stream;
30
31
  VALUE streambuf;
32
+ ID stream_append_method;
31
33
  } unpack_user;
32
34
 
33
35
 
@@ -110,8 +112,14 @@ static inline int template_callback_map(unpack_user* u, unsigned int n, VALUE* o
110
112
  static inline int template_callback_map_item(unpack_user* u, VALUE* c, VALUE k, VALUE v)
111
113
  { rb_hash_aset(*c, k, v); return 0; }
112
114
 
115
+ #ifdef RSTRING_EMBED_LEN_MAX
116
+ #define COW_MIN_SIZE RSTRING_EMBED_LEN_MAX
117
+ #else
118
+ #define COW_MIN_SIZE ((sizeof(VALUE)*3)/sizeof(char)-1)
119
+ #endif
120
+
113
121
  static inline int template_callback_raw(unpack_user* u, const char* b, const char* p, unsigned int l, VALUE* o)
114
- { *o = (l == 0) ? rb_str_new(0,0) : rb_str_substr(u->source, p - b, l); return 0; }
122
+ { *o = (l <= COW_MIN_SIZE) ? rb_str_new(p, l) : rb_str_substr(u->source, p - b, l); return 0; }
115
123
 
116
124
 
117
125
  #include "msgpack/unpack_template.h"
@@ -177,6 +185,15 @@ static VALUE MessagePack_Unpacker_reset(VALUE self)
177
185
  return self;
178
186
  }
179
187
 
188
+ static ID append_method_of(VALUE stream)
189
+ {
190
+ if(rb_respond_to(stream, s_sysread)) {
191
+ return s_sysread;
192
+ } else {
193
+ return s_readpartial;
194
+ }
195
+ }
196
+
180
197
  static VALUE MessagePack_Unpacker_initialize(int argc, VALUE *argv, VALUE self)
181
198
  {
182
199
  VALUE stream;
@@ -198,6 +215,7 @@ static VALUE MessagePack_Unpacker_initialize(int argc, VALUE *argv, VALUE self)
198
215
  mp->user.buffer = rb_str_new("",0);
199
216
  mp->user.stream = stream;
200
217
  mp->user.streambuf = rb_str_new("",0);
218
+ mp->user.stream_append_method = append_method_of(stream);
201
219
  return self;
202
220
  }
203
221
 
@@ -293,7 +311,9 @@ static VALUE MessagePack_Unpacker_stream_get(VALUE self)
293
311
  static VALUE MessagePack_Unpacker_stream_set(VALUE self, VALUE val)
294
312
  {
295
313
  UNPACKER(self, mp);
296
- return mp->user.stream = val;
314
+ mp->user.stream = val;
315
+ mp->user.stream_append_method = append_method_of(val);
316
+ return val;
297
317
  }
298
318
 
299
319
  static VALUE MessagePack_Unpacker_fill(VALUE self)
@@ -306,10 +326,12 @@ static VALUE MessagePack_Unpacker_fill(VALUE self)
306
326
 
307
327
  size_t len;
308
328
  if(RSTRING_LEN(mp->user.buffer) == 0) {
309
- rb_funcall(mp->user.stream, s_sysread, 2, LONG2FIX(64*1024), mp->user.buffer);
329
+ rb_funcall(mp->user.stream, mp->user.stream_append_method, 2,
330
+ LONG2FIX(64*1024), mp->user.buffer);
310
331
  len = RSTRING_LEN(mp->user.buffer);
311
332
  } else {
312
- rb_funcall(mp->user.stream, s_sysread, 2, LONG2FIX(64*1024), mp->user.streambuf);
333
+ rb_funcall(mp->user.stream, mp->user.stream_append_method, 2,
334
+ LONG2FIX(64*1024), mp->user.streambuf);
313
335
  len = RSTRING_LEN(mp->user.streambuf);
314
336
  rb_str_cat(mp->user.buffer, RSTRING_PTR(mp->user.streambuf), RSTRING_LEN(mp->user.streambuf));
315
337
  }
@@ -422,6 +444,7 @@ static VALUE MessagePack_unpack(VALUE self, VALUE data)
422
444
  void Init_msgpack_unpack(VALUE mMessagePack)
423
445
  {
424
446
  s_sysread = rb_intern("sysread");
447
+ s_readpartial = rb_intern("readpartial");
425
448
  eUnpackError = rb_define_class_under(mMessagePack, "UnpackError", rb_eStandardError);
426
449
  cUnpacker = rb_define_class_under(mMessagePack, "Unpacker", rb_cObject);
427
450
  rb_define_alloc_func(cUnpacker, MessagePack_Unpacker_alloc);
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.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - FURUHASHI Sadayuki
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-02 00:00:00 +09:00
12
+ date: 2010-03-31 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -41,7 +41,7 @@ files:
41
41
  - ChangeLog
42
42
  - AUTHORS
43
43
  has_rdoc: true
44
- homepage: http://msgpack.sourceforge.jp/
44
+ homepage: http://msgpack.sourceforge.net/
45
45
  licenses: []
46
46
 
47
47
  post_install_message: