Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove utf-8 encoding for keys and regex

These are serialized as cstring, and therefore shouldn't be UTF-8.
  • Loading branch information...
commit 9ab692218284a1cd58d5eabd14adbc8b37218af0 1 parent 3949853
@bernerdschaefer bernerdschaefer authored
View
4 lib/moped/bson/extensions/hash.rb
@@ -10,7 +10,7 @@ def __bson_load__(io, doc = new)
io.read 4
while (buf = io.readbyte) != 0
- key = io.gets(NULL_BYTE).chop!.force_encoding('utf-8')
+ key = io.gets(NULL_BYTE).chop!
if native_class = Types::MAP[buf]
doc[key] = native_class.__bson_load__(io)
@@ -34,7 +34,7 @@ def __bson_dump__(io = "", key = nil)
io << START_LENGTH
each do |k, v|
- v.__bson_dump__(io, k.to_s.encode('utf-8').force_encoding('binary'))
+ v.__bson_dump__(io, k.to_s)
end
io << EOD
View
4 lib/moped/bson/extensions/regexp.rb
@@ -5,7 +5,7 @@ module Extensions
module Regexp
module ClassMethods
def __bson_load__(io)
- source = io.gets(NULL_BYTE).chop!.force_encoding('utf-8')
+ source = io.gets(NULL_BYTE).chop!
options = 0
while (option = io.getbyte) != 0
case option
@@ -26,7 +26,7 @@ def __bson_dump__(io, key)
io << Types::REGEX
io << key
io << NULL_BYTE
- io << source.force_encoding('binary')
+ io << source
io << NULL_BYTE
io << 'i' if (options & ::Regexp::IGNORECASE) != 0
View
16 lib/moped/bson/extensions/symbol.rb
@@ -14,9 +14,19 @@ def __bson_dump__(io, key)
io << key
io << NULL_BYTE
- str = to_s.force_encoding('utf-8').force_encoding('binary')
- io << [str.bytesize+1].pack(INT32_PACK)
- io << str
+ begin
+ data = to_s.encode('utf-8')
+ rescue EncodingError
+ data = to_s.dup
+ data.force_encoding('utf-8')
+
+ raise unless data.valid_encoding?
+ end
+
+ data.force_encoding('binary')
+
+ io << [data.bytesize+1].pack(INT32_PACK)
+ io << data
io << NULL_BYTE
end
end
View
10 spec/moped/bson/document_spec.rb
@@ -28,11 +28,6 @@
end
context "utf8 data" do
- it "handles utf-8 keys" do
- doc = { "_id" => Moped::BSON::ObjectId.new, "gültig" => 1 }
- Moped::BSON::Document.deserialize(StringIO.new(Moped::BSON::Document.serialize(doc))).should eq doc
- end
-
it "handles utf-8 string values" do
doc = { "_id" => Moped::BSON::ObjectId.new, "type" => "gültig" }
Moped::BSON::Document.deserialize(StringIO.new(Moped::BSON::Document.serialize(doc))).should eq doc
@@ -64,11 +59,6 @@
Moped::BSON::Document.deserialize(StringIO.new(Moped::BSON::Document.serialize(doc))).should eq doc
end
- it "handles utf-8 regex values" do
- doc = { "_id" => Moped::BSON::ObjectId.new, "type" => /gültig/ }
- Moped::BSON::Document.deserialize(StringIO.new(Moped::BSON::Document.serialize(doc))).should eq doc
- end
-
it "handles utf-8 string values in an array" do
doc = { "_id" => Moped::BSON::ObjectId.new, "type" => ["gültig"] }
Moped::BSON::Document.deserialize(StringIO.new(Moped::BSON::Document.serialize(doc))).should eq doc
Please sign in to comment.
Something went wrong with that request. Please try again.