Skip to content

Commit

Permalink
Added Encoding.asciicompat_encoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Aug 13, 2012
1 parent 99019d7 commit 1a2d5d1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
28 changes: 18 additions & 10 deletions kernel/common/encoding.rb
Expand Up @@ -10,9 +10,10 @@ class UndefinedConversionError < EncodingError
class CompatibilityError < EncodingError
end

EncodingMap = Rubinius::EncodingClass::EncodingMap
EncodingList = Rubinius::EncodingClass::EncodingList
LocaleCharmap = Rubinius::EncodingClass::LocaleCharmap
EncodingMap = Rubinius::EncodingClass::EncodingMap
EncodingList = Rubinius::EncodingClass::EncodingList
LocaleCharmap = Rubinius::EncodingClass::LocaleCharmap
TranscodingMap = Rubinius::EncodingClass::TranscodingMap

class Transcoder
attr_accessor :source
Expand All @@ -29,6 +30,18 @@ class Converter
attr_accessor :replacement
attr_accessor :convpath

def self.asciicompat_encoding(string_or_encoding)
encoding = Rubinius::Type.try_convert_to_encoding string_or_encoding

return if not encoding or encoding.equal? undefined
return if encoding.ascii_compatible?

transcoding = TranscodingMap[encoding.name]
return unless transcoding and transcoding.size == 1

Encoding.find transcoding.keys.first.to_s
end

def initialize(from, to, options=undefined)
@source_encoding = Rubinius::Type.coerce_to_encoding from
@destination_encoding = Rubinius::Type.coerce_to_encoding to
Expand Down Expand Up @@ -121,13 +134,8 @@ def self.default_internal=(enc)
end

def self.find(name)
key = StringValue(name).upcase.to_sym

pair = EncodingMap[key]
if pair
index = pair.last
return index && EncodingList[index]
end
enc = Rubinius::Type.try_convert_to_encoding name
return enc unless enc.equal? undefined

raise ArgumentError, "unknown encoding name - #{name}"
end
Expand Down
23 changes: 21 additions & 2 deletions kernel/common/type19.rb
Expand Up @@ -8,13 +8,32 @@ def self.coerce_to_encoding(obj)
return obj
when String
return Encoding.find obj
when nil
# TODO: temporary until __ENCODING__ is fixed
else
return Encoding.find StringValue(obj)
end
end

def self.try_convert_to_encoding(obj)
case obj
when Encoding
return obj
when String
str = obj
else
str = StringValue obj
end

key = str.upcase.to_sym

pair = Encoding::EncodingMap[key]
if pair
index = pair.last
return index && Encoding::EncodingList[index]
end

return undefined
end

def self.compatible_encoding(a, b)
enc = Encoding.compatible? a, b

Expand Down

This file was deleted.

0 comments on commit 1a2d5d1

Please sign in to comment.