@@ -114,8 +114,8 @@ struct deserialize_opts {
114114#if HAVE_RUBY_ENCODING_H
115115#include "ruby/encoding.h"
116116#define STR_NEW (p ,n ) \
117- ({ VALUE _str; \
118- _str = rb_enc_str_new((p), (n), rb_utf8_encoding()); \
117+ ({ \
118+ VALUE _str = rb_enc_str_new((p), (n), rb_utf8_encoding()); \
119119 rb_encoding* internal_encoding = rb_default_internal_encoding();\
120120 if (internal_encoding) { \
121121 _str = rb_str_export_to_enc(_str, internal_encoding); \
@@ -127,8 +127,7 @@ struct deserialize_opts {
127127#endif
128128
129129static void write_utf8 (bson_buffer_t buffer , VALUE string , int allow_null ) {
130- result_t status ;
131- status = validate_utf8_encoding (
130+ result_t status = validate_utf8_encoding (
132131 (const char * )RSTRING_PTR (string ), RSTRING_LEN (string ), allow_null );
133132
134133 if (status == HAS_NULL ) {
@@ -163,19 +162,17 @@ static void write_utf8(bson_buffer_t buffer, VALUE string, int allow_null) {
163162#ifdef _WIN32 || _MSC_VER
164163#define INT2STRING (buffer , i ) \
165164 { \
166- int vslength; \
167- vslength = _scprintf("%d", i) + 1; \
165+ int vslength = _scprintf("%d", i) + 1; \
168166 *buffer = malloc(vslength); \
169167 _snprintf(*buffer, vslength, "%d", i); \
170168 }
171169#define FREE_INTSTRING (buffer ) free(buffer)
172170#else
173- #define INT2STRING (buffer , i ) \
174- { \
175- int vslength; \
176- vslength = snprintf(NULL, 0, "%d", i) + 1; \
177- *buffer = malloc(vslength); \
178- snprintf(*buffer, vslength, "%d", i); \
171+ #define INT2STRING (buffer , i ) \
172+ { \
173+ int vslength = snprintf(NULL, 0, "%d", i) + 1; \
174+ *buffer = malloc(vslength); \
175+ snprintf(*buffer, vslength, "%d", i); \
179176 }
180177#define FREE_INTSTRING (buffer ) free(buffer)
181178#endif
@@ -286,10 +283,8 @@ static void serialize_regex(bson_buffer_t buffer, VALUE key, VALUE pattern, long
286283
287284 has_extra = rb_funcall (value , rb_intern ("respond_to?" ), 1 , rb_str_new2 ("extra_options_str" ));
288285 if (TYPE (has_extra ) == T_TRUE ) {
289- VALUE extra ;
290- bson_buffer_position old_position ;
291- extra = rb_funcall (value , rb_intern ("extra_options_str" ), 0 );
292- old_position = bson_buffer_get_position (buffer );
286+ VALUE extra = rb_funcall (value , rb_intern ("extra_options_str" ), 0 );
287+ bson_buffer_position old_position = bson_buffer_get_position (buffer );
293288 SAFE_WRITE (buffer , RSTRING_PTR (extra ), RSTRING_LENINT (extra ));
294289 qsort (bson_buffer_get_buffer (buffer ) + old_position , RSTRING_LEN (extra ), sizeof (char ), cmp_char );
295290 }
@@ -298,10 +293,8 @@ static void serialize_regex(bson_buffer_t buffer, VALUE key, VALUE pattern, long
298293}
299294
300295static int write_element (VALUE key , VALUE value , VALUE extra , int allow_id ) {
301- VALUE check_keys ;
302- bson_buffer_t buffer ;
303- buffer = (bson_buffer_t )NUM2LL (rb_ary_entry (extra , 0 ));
304- check_keys = rb_ary_entry (extra , 1 );
296+ bson_buffer_t buffer = (bson_buffer_t )NUM2LL (rb_ary_entry (extra , 0 ));
297+ VALUE check_keys = rb_ary_entry (extra , 1 );
305298
306299 if (TYPE (key ) == T_SYMBOL ) {
307300 // TODO better way to do this... ?
@@ -372,8 +365,7 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
372365 }
373366 case T_FLOAT :
374367 {
375- double d ;
376- d = NUM2DBL (value );
368+ double d = NUM2DBL (value );
377369 write_name_and_type (buffer , key , 0x01 );
378370 SAFE_WRITE (buffer , (char * )& d , 8 );
379371 break ;
@@ -431,10 +423,8 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
431423 }
432424 case T_SYMBOL :
433425 {
434- const char * str_value ;
435- int length ;
436- str_value = rb_id2name (SYM2ID (value ));
437- length = (int )strlen (str_value ) + 1 ;
426+ const char * str_value = rb_id2name (SYM2ID (value ));
427+ int length = (int )strlen (str_value ) + 1 ;
438428 write_name_and_type (buffer , key , 0x0E );
439429 SAFE_WRITE (buffer , (char * )& length , 4 );
440430 SAFE_WRITE (buffer , str_value , length );
@@ -446,12 +436,10 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
446436 const char * cls = rb_obj_classname (value );
447437 if (strcmp (cls , "BSON::Binary" ) == 0 ||
448438 strcmp (cls , "ByteBuffer" ) == 0 ) {
449- VALUE string_data ;
450- int length ;
439+ VALUE string_data = rb_funcall ( value , rb_intern ( "to_s" ), 0 ) ;
440+ int length = RSTRING_LENINT ( string_data ) ;
451441 const char subtype = strcmp (cls , "ByteBuffer" ) ?
452442 (const char )FIX2INT (rb_funcall (value , rb_intern ("subtype" ), 0 )) : 2 ;
453- string_data = rb_funcall (value , rb_intern ("to_s" ), 0 );
454- length = RSTRING_LENINT (string_data );
455443 write_name_and_type (buffer , key , 0x05 );
456444 if (subtype == 2 ) {
457445 const int other_length = length + 4 ;
@@ -466,13 +454,11 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
466454 break ;
467455 }
468456 if (strcmp (cls , "BSON::ObjectId" ) == 0 ) {
469- VALUE as_array ;
470- as_array = rb_funcall (value , rb_intern ("to_a" ), 0 );
471457 int i ;
458+ VALUE as_array = rb_funcall (value , rb_intern ("to_a" ), 0 );
472459 write_name_and_type (buffer , key , 0x07 );
473460 for (i = 0 ; i < 12 ; i ++ ) {
474- char byte ;
475- byte = (char )FIX2INT (rb_ary_entry (as_array , i ));
461+ char byte = (char )FIX2INT (rb_ary_entry (as_array , i ));
476462 SAFE_WRITE (buffer , & byte , 1 );
477463 }
478464 break ;
@@ -559,8 +545,7 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
559545 }
560546 if (strcmp (cls , "ActiveSupport::Multibyte::Chars" ) == 0 ) {
561547 int length ;
562- VALUE str ;
563- str = StringValue (value );
548+ VALUE str = StringValue (value );
564549 write_name_and_type (buffer , key , 0x02 );
565550 length = RSTRING_LENINT (str ) + 1 ;
566551 SAFE_WRITE (buffer , (char * )& length , 4 );
@@ -579,13 +564,10 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
579564 }
580565 case T_DATA :
581566 {
582- const char * cls ;
583- cls = rb_obj_classname (value );
567+ const char * cls = rb_obj_classname (value );
584568 if (strcmp (cls , "Time" ) == 0 ) {
585- double t ;
586- long long time_since_epoch ;
587- t = NUM2DBL (rb_funcall (value , rb_intern ("to_f" ), 0 ));
588- time_since_epoch = (long long )round (t * 1000 );
569+ double t = NUM2DBL (rb_funcall (value , rb_intern ("to_f" ), 0 ));
570+ long long time_since_epoch = (long long )round (t * 1000 );
589571 write_name_and_type (buffer , key , 0x09 );
590572 SAFE_WRITE (buffer , (const char * )& time_since_epoch , 8 );
591573 break ;
@@ -607,17 +589,14 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
607589 }
608590 case T_REGEXP :
609591 {
610- VALUE pattern ;
611- long flags ;
612- pattern = RREGEXP_SRC (value );
613- flags = RREGEXP_OPTIONS (value );
592+ VALUE pattern = RREGEXP_SRC (value );
593+ long flags = RREGEXP_OPTIONS (value );
614594 serialize_regex (buffer , key , pattern , flags , value , 1 );
615595 break ;
616596 }
617597 default :
618598 {
619- const char * cls ;
620- cls = rb_obj_classname (value );
599+ const char * cls = rb_obj_classname (value );
621600 bson_buffer_free (buffer );
622601 rb_raise (InvalidDocument , "Cannot serialize an object of class %s (type %d) into BSON." , cls , TYPE (value ));
623602 break ;
@@ -635,17 +614,14 @@ static int write_element_with_id(VALUE key, VALUE value, VALUE extra) {
635614}
636615
637616static void write_doc (bson_buffer_t buffer , VALUE hash , VALUE check_keys , VALUE move_id ) {
638- bson_buffer_position start_position , length_location ;
617+ bson_buffer_position start_position = bson_buffer_get_position (buffer );
618+ bson_buffer_position length_location = bson_buffer_save_space (buffer , 4 );
639619 bson_buffer_position length ;
640620 int allow_id ;
641621 int max_size ;
642622 int (* write_function )(VALUE , VALUE , VALUE ) = NULL ;
643- VALUE id_str , id_sym ;
644-
645- start_position = bson_buffer_get_position (buffer );
646- length_location = bson_buffer_save_space (buffer , 4 );
647- id_str = rb_str_new2 ("_id" );
648- id_sym = ID2SYM (rb_intern ("_id" ));
623+ VALUE id_str = rb_str_new2 ("_id" );
624+ VALUE id_sym = ID2SYM (rb_intern ("_id" ));
649625
650626 if (length_location == -1 ) {
651627 rb_raise (rb_eNoMemError , "failed to allocate memory in buffer.c" );
@@ -655,12 +631,10 @@ static void write_doc(bson_buffer_t buffer, VALUE hash, VALUE check_keys, VALUE
655631 if (move_id == Qtrue ) {
656632 allow_id = 0 ;
657633 if (rb_funcall (hash , rb_intern ("has_key?" ), 1 , id_str ) == Qtrue ) {
658- VALUE id ;
659- id = rb_hash_aref (hash , id_str );
634+ VALUE id = rb_hash_aref (hash , id_str );
660635 write_element_with_id (id_str , id , pack_extra (buffer , check_keys ));
661636 } else if (rb_funcall (hash , rb_intern ("has_key?" ), 1 , id_sym ) == Qtrue ) {
662- VALUE id ;
663- id = rb_hash_aref (hash , id_sym );
637+ VALUE id = rb_hash_aref (hash , id_sym );
664638 write_element_with_id (id_sym , id , pack_extra (buffer , check_keys ));
665639 }
666640 }
@@ -670,8 +644,7 @@ static void write_doc(bson_buffer_t buffer, VALUE hash, VALUE check_keys, VALUE
670644 if ((rb_obj_classname (hash ), "Hash" ) == 0 ) {
671645 if ((rb_funcall (hash , rb_intern ("has_key?" ), 1 , id_str ) == Qtrue ) &&
672646 (rb_funcall (hash , rb_intern ("has_key?" ), 1 , id_sym ) == Qtrue )) {
673- VALUE oid_sym ;
674- oid_sym = rb_hash_delete (hash , id_sym );
647+ VALUE oid_sym = rb_hash_delete (hash , id_sym );
675648 rb_funcall (hash , rb_intern ("[]=" ), 2 , id_str , oid_sym );
676649 }
677650 }
@@ -686,13 +659,12 @@ static void write_doc(bson_buffer_t buffer, VALUE hash, VALUE check_keys, VALUE
686659
687660 // we have to check for an OrderedHash and handle that specially
688661 if (strcmp (rb_obj_classname (hash ), "BSON::OrderedHash" ) == 0 ) {
689- VALUE keys ;
690- keys = rb_funcall (hash , rb_intern ("keys" ), 0 );
691662 int i ;
692- VALUE key , value ;
693- for (i = 0 ; i < RARRAY_LEN (keys ); i ++ ) {
694- key = rb_ary_entry (keys , i );
695- value = rb_hash_aref (hash , key );
663+ VALUE keys = rb_funcall (hash , rb_intern ("keys" ), 0 );
664+
665+ for (i = 0 ; i < RARRAY_LEN (keys ); i ++ ) {
666+ VALUE key = rb_ary_entry (keys , i );
667+ VALUE value = rb_hash_aref (hash , key );
696668
697669 write_function (key , value , pack_extra (buffer , check_keys ));
698670 }
@@ -722,8 +694,7 @@ static VALUE method_serialize(VALUE self, VALUE doc, VALUE check_keys,
722694 VALUE move_id , VALUE max_size ) {
723695
724696 VALUE result ;
725- bson_buffer_t buffer ;
726- buffer = bson_buffer_new ();
697+ bson_buffer_t buffer = bson_buffer_new ();
727698 if (buffer == NULL ) {
728699 rb_raise (rb_eNoMemError , "failed to allocate memory in buffer.c" );
729700 }
@@ -770,11 +741,11 @@ static VALUE get_value(const char* buffer, int* position,
770741 int size ;
771742 memcpy (& size , buffer + * position , 4 );
772743 if (strcmp (buffer + * position + 5 , "$ref" ) == 0 ) { // DBRef
773- int offset , collection_length ;
774- offset = * position + 10 ;
775744 VALUE argv [2 ];
776- collection_length = * (int * )(buffer + offset ) - 1 ;
777745 unsigned char id_type ;
746+ int offset = * position + 10 ;
747+ int collection_length = * (int * )(buffer + offset ) - 1 ;
748+
778749 offset += 4 ;
779750
780751 argv [0 ] = STR_NEW (buffer + offset , collection_length );
@@ -798,11 +769,9 @@ static VALUE get_value(const char* buffer, int* position,
798769
799770 value = rb_ary_new ();
800771 while (* position < end ) {
801- unsigned char type ;
802- type = (unsigned char )buffer [(* position )++ ];
803- int key_size ;
804- key_size = (int )strlen (buffer + * position );
805772 VALUE to_append ;
773+ unsigned char type = (unsigned char )buffer [(* position )++ ];
774+ int key_size = (int )strlen (buffer + * position );
806775
807776 * position += key_size + 1 ; // just skip the key, they're in order.
808777 to_append = get_value (buffer , position , type , opts );
@@ -837,9 +806,8 @@ static VALUE get_value(const char* buffer, int* position,
837806 }
838807 case 7 :
839808 {
840- VALUE str , oid ;
841- str = rb_str_new (buffer + * position , 12 );
842- oid = rb_funcall (str , unpack_method , 1 , rb_str_new2 ("C*" ));
809+ VALUE str = rb_str_new (buffer + * position , 12 );
810+ VALUE oid = rb_funcall (str , unpack_method , 1 , rb_str_new2 ("C*" ));
843811 value = rb_class_new_instance (1 , & oid , ObjectId );
844812 * position += 12 ;
845813 break ;
@@ -879,11 +847,10 @@ static VALUE get_value(const char* buffer, int* position,
879847 }
880848 case 11 :
881849 {
882- int pattern_length ;
883- VALUE pattern , argv [ 3 ], flags_str ;
850+ int pattern_length = ( int ) strlen ( buffer + * position ) ;
851+ VALUE pattern = STR_NEW ( buffer + * position , pattern_length ) ;
884852 int flags_length ;
885- pattern_length = (int )strlen (buffer + * position );
886- pattern = STR_NEW (buffer + * position , pattern_length );
853+ VALUE argv [3 ], flags_str ;
887854 * position += pattern_length + 1 ;
888855
889856 flags_length = (int )strlen (buffer + * position );
@@ -987,16 +954,13 @@ static VALUE get_value(const char* buffer, int* position,
987954}
988955
989956static VALUE elements_to_hash (const char * buffer , int max , struct deserialize_opts * opts ) {
990- VALUE hash ;
991- hash = rb_class_new_instance (0 , NULL , OrderedHash );
992957 int position = 0 ;
958+ VALUE hash = rb_class_new_instance (0 , NULL , OrderedHash );
993959 while (position < max ) {
994- unsigned char type ;
995- int name_length ;
996- VALUE name , value ;
997- type = (unsigned char )buffer [position ++ ];
998- name_length = (int )strlen (buffer + position );
999- name = STR_NEW (buffer + position , name_length );
960+ VALUE value ;
961+ unsigned char type = (unsigned char )buffer [position ++ ];
962+ int name_length = (int )strlen (buffer + position );
963+ VALUE name = STR_NEW (buffer + position , name_length );
1000964 position += name_length + 1 ;
1001965 value = get_value (buffer , & position , type , opts );
1002966 rb_funcall (hash , element_assignment_method , 2 , name , value );
@@ -1005,11 +969,10 @@ static VALUE elements_to_hash(const char* buffer, int max, struct deserialize_op
1005969}
1006970
1007971static VALUE method_deserialize (VALUE self , VALUE bson , VALUE opts ) {
1008- const char * buffer ;
1009- int remaining ;
1010- buffer = RSTRING_PTR (bson );
1011- remaining = RSTRING_LENINT (bson );
972+ const char * buffer = RSTRING_PTR (bson );
973+ int remaining = RSTRING_LENINT (bson );
1012974 struct deserialize_opts deserialize_opts ;
975+
1013976 deserialize_opts .compile_regex = 1 ;
1014977 if (rb_funcall (opts , rb_intern ("has_key?" ), 1 , ID2SYM (rb_intern ("compile_regex" ))) == Qtrue &&
1015978 rb_hash_aref (opts , ID2SYM (rb_intern ("compile_regex" ))) == Qfalse ) {
0 commit comments