deserialize to RegexpOfHolding only when necessary

This commit is contained in:
Kyle Banker 2010-02-15 09:47:53 -05:00
parent 32606db5e9
commit fc2ddf3bbd
3 changed files with 17 additions and 6 deletions

View File

@ -62,6 +62,7 @@ static VALUE DBRef;
static VALUE Code;
static VALUE MinKey;
static VALUE MaxKey;
static VALUE Regexp;
static VALUE RegexpOfHolding;
static VALUE OrderedHash;
static VALUE InvalidName;
@ -693,8 +694,13 @@ static VALUE get_value(const char* buffer, int* position, int type) {
}
argv[0] = pattern;
argv[1] = INT2FIX(flags);
argv[2] = rb_str_new2(extra);
value = rb_class_new_instance(3, argv, RegexpOfHolding);
if(extra[0] == 0) {
value = rb_class_new_instance(2, argv, Regexp);
}
else { // Deserializing a RegexpOfHolding
argv[2] = rb_str_new2(extra);
value = rb_class_new_instance(3, argv, RegexpOfHolding);
}
*position += flags_length + 1;
break;
}
@ -877,6 +883,7 @@ void Init_cbson() {
MinKey = rb_const_get(mongo, rb_intern("MinKey"));
MaxKey = rb_const_get(mongo, rb_intern("MaxKey"));
rb_require("mongo/types/regexp_of_holding");
Regexp = rb_const_get(rb_cObject, rb_intern("Regexp"));
RegexpOfHolding = rb_const_get(mongo, rb_intern("RegexpOfHolding"));
rb_require("mongo/exceptions");
InvalidName = rb_const_get(mongo, rb_intern("InvalidName"));

View File

@ -329,7 +329,12 @@ class BSON_RUBY
options |= Regexp::MULTILINE if options_str.include?('m')
options |= Regexp::EXTENDED if options_str.include?('x')
options_str.gsub!(/[imx]/, '') # Now remove the three we understand
RegexpOfHolding.new(str, options, options_str)
if options_str == ''
Regexp.new(str, options)
else
warn("Use deprecated Regexp options #{options_str}; future versions of this MongoDB driver will support only i, m, and x. See deprecated class RegexpOfHolding for more info.")
RegexpOfHolding.new(str, options, options_str)
end
end
def deserialize_string_data(buf)

View File

@ -138,10 +138,9 @@ class BSONTest < Test::Unit::TestCase
assert_equal doc, doc2
r = doc2['doc']
assert_kind_of RegexpOfHolding, r
assert_equal '', r.extra_options_str
assert_kind_of Regexp, r
r.extra_options_str << 'zywcab'
r = RegexpOfHolding.new('st', 0, 'zywcab')
assert_equal 'zywcab', r.extra_options_str
doc = {'doc' => r}