Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Mostly fixed String#encode, #encode!. Fixes #2047.

More fixes coming once I replace the :xml => stuff hacks.
  • Loading branch information...
commit 55fad501245715c17f62416c720c457dbc3ee174 1 parent ea748ae
@brixen brixen authored
View
84 kernel/common/string19.rb
@@ -17,49 +17,73 @@ def codepoints
def encode!(to=undefined, from=undefined, options=undefined)
Rubinius.check_frozen
- replace encode(to, from, options)
- end
-
- def encode(to=undefined, from=undefined, options=undefined)
- # TODO
- if to.equal? undefined
- to = Encoding.default_internal
- return self.dup unless to
+ case to
+ when Encoding
+ to_enc = to
+ when Hash
+ options = to
+ to_enc = Encoding.default_internal
+ when undefined
+ to_enc = Encoding.default_internal
else
- to = Rubinius::Type.coerce_to_encoding to
- end
+ opts = Rubinius::Type::check_convert_type to, Hash, :to_hash
- if from.equal? undefined
- from = encoding
- options = 0
+ if opts
+ options = opts
+ to_enc = Encoding.default_internal
+ else
+ to_enc = Rubinius::Type.try_convert_to_encoding to
+ end
end
- if options.equal? undefined
- if from.kind_of? Hash
- options = from
- from = encoding
+ case from
+ when undefined
+ from_enc = encoding
+ when Encoding
+ from_enc = from
+ when Hash
+ options = from
+ from_enc = encoding
+ else
+ opts = Rubinius::Type::check_convert_type from, Hash, :to_hash
+
+ if opts
+ options = opts
+ from_enc = encoding
else
- from = Rubinius::Type.coerce_to_encoding from
- options = 0
+ from_enc = Rubinius::Type.coerce_to_encoding from
end
end
- if from == to
- str = self.dup
+ if from_enc.equal? undefined or to_enc.equal? undefined
+ raise Encoding::ConverterNotFoundError, "undefined code converter (#{from} to #{to})"
+ end
+
+ case options
+ when undefined
+ options = 0
+ when Hash
+ # do nothing
else
- ec = Encoding::Converter.new from, to, options
- str = ec.convert self
+ options = Rubinius::Type.coerce_to options, Hash, :to_hash
+ end
+
+ if ascii_only? and to_enc.ascii_compatible?
+ force_encoding to_enc
+ elsif to_enc and from_enc != to_enc
+ ec = Encoding::Converter.new from_enc, to_enc, options
+ replace ec.convert(self)
end
# TODO: replace this hack with transcoders
if options.kind_of? Hash
case xml = options[:xml]
when :text
- str.gsub!(/[&><]/, '&' => '&amp;', '>' => '&gt;', '<' => '&lt;')
+ gsub!(/[&><]/, '&' => '&amp;', '>' => '&gt;', '<' => '&lt;')
when :attr
- str.gsub!(/[&><"]/, '&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;')
- str.insert(0, '"')
- str.insert(-1, '"')
+ gsub!(/[&><"]/, '&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;')
+ insert(0, '"')
+ insert(-1, '"')
when nil
# nothing
else
@@ -67,7 +91,11 @@ def encode(to=undefined, from=undefined, options=undefined)
end
end
- str
+ self
+ end
+
+ def encode(to=undefined, from=undefined, options=undefined)
+ dup.encode! to, from, options
end
def force_encoding(enc)
View
4 spec/tags/19/ruby/core/string/encode_tags.txt
@@ -1,8 +1,4 @@
-fails:String#encode raises an Encoding::InvalidByteSequenceError for invalid byte sequences
-fails:String#encode raises Encoding::ConverterNotFoundError for invalid target encodings
fails:String#encode given the :xml => :text option replaces undefined characters with their upper-case hexadecimal numeric character references
fails:String#encode given the :xml => :attr option replaces undefined characters with their upper-case hexadecimal numeric character references
-fails:String#encode! raises an Encoding::InvalidByteSequenceError for invalid byte sequences
-fails:String#encode! raises Encoding::ConverterNotFoundError for invalid target encodings
fails:String#encode! given the :xml => :text option replaces undefined characters with their upper-case hexadecimal numeric character references
fails:String#encode! given the :xml => :attr option replaces undefined characters with their upper-case hexadecimal numeric character references
View
4 spec/tags/20/ruby/core/string/encode_tags.txt
@@ -1,7 +1,3 @@
-fails:String#encode raises an Encoding::InvalidByteSequenceError for invalid byte sequences
-fails:String#encode raises Encoding::ConverterNotFoundError for invalid target encodings
-fails:String#encode! raises an Encoding::InvalidByteSequenceError for invalid byte sequences
-fails:String#encode! raises Encoding::ConverterNotFoundError for invalid target encodings
fails:String#encode given the :xml => :text option replaces undefined characters with their upper-case hexadecimal numeric character references
fails:String#encode given the :xml => :attr option replaces undefined characters with their upper-case hexadecimal numeric character references
fails:String#encode! given the :xml => :text option replaces undefined characters with their upper-case hexadecimal numeric character references
Please sign in to comment.
Something went wrong with that request. Please try again.