Skip to content

Commit

Permalink
Fix pending spec around string encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Apr 20, 2013
1 parent 48eaa15 commit 6e383dc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
10 changes: 4 additions & 6 deletions ext/bson/native.c
Expand Up @@ -499,17 +499,15 @@ static VALUE rb_string_set_int32(VALUE str, VALUE pos, VALUE an_int32)
* @example Convert the Ruby string to a BSON string.
* rb_string_to_bson_string(0, Qnil, "test");
*
* @param [ int ] argc The number or arguments.
* @param [ Array<Object> ] argv The arguments.
* @param [ String ] self The string value.
* @param [ String ] encoded The encoded string to append to.
*
* @return [ String ] The encoded string.
*
* @since 2.0.0
*/
static VALUE rb_string_to_bson_string(int argc, VALUE *argv, VALUE self)
static VALUE rb_string_to_bson_string(VALUE self, VALUE encoded)
{
VALUE encoded = rb_get_default_encoded(argc, argv);
VALUE binary = rb_bson_to_utf8_binary(self);
StringValue(binary);
rb_str_cat(encoded, RSTRING_PTR(binary), RSTRING_LEN(binary));
Expand Down Expand Up @@ -618,8 +616,8 @@ void Init_native()
// Redefine methods on the String class.
rb_undef_method(string, "set_int32");
rb_define_method(string, "set_int32", rb_string_set_int32, 2);
rb_undef_method(string, "to_bson_string");
rb_define_method(string, "to_bson_string", rb_string_to_bson_string, -1);
rb_undef_method(string, "to_utf8_binary");
rb_define_private_method(string, "to_utf8_binary", rb_string_to_bson_string, 1);

// Redefine the next method on the object id generator.
rb_undef_method(generator, "next");
Expand Down
8 changes: 7 additions & 1 deletion lib/bson/string.rb
Expand Up @@ -80,7 +80,7 @@ def to_bson_cstring(encoded = ''.force_encoding(BINARY))
# @since 2.0.0
def to_bson_string(encoded = ''.force_encoding(BINARY))
begin
encoded << encode(UTF8).force_encoding(BINARY)
to_utf8_binary(encoded)
rescue EncodingError
data = dup.force_encoding(UTF8)
raise unless data.valid_encoding?
Expand Down Expand Up @@ -129,6 +129,12 @@ def set_int32(pos, int32)
self[pos, 4] = [ int32 ].pack(Int32::PACK)
end

private

def to_utf8_binary(encoded)
encoded << encode(UTF8).force_encoding(BINARY)
end

module ClassMethods

# Deserialize a string from BSON.
Expand Down
2 changes: 1 addition & 1 deletion spec/bson/document_spec.rb
Expand Up @@ -717,7 +717,7 @@
end
end

pending "when binary strings with utf-8 values exist" do
context "when binary strings with utf-8 values exist" do

let(:string) { "europäischen" }
let(:document) do
Expand Down

0 comments on commit 6e383dc

Please sign in to comment.