Module: When::EncodingConversion

Included in:
Regexp, String
Defined in:
lib/when_exe/locales/encoding_conversion.rb

Overview

エンコーディングの変換

Instance Method Summary collapse

Instance Method Details

#+@Object

内部エンコーディング文字列化(単項)



123
124
125
# File 'lib/when_exe/locales/encoding_conversion.rb', line 123

def +@
  to_internal_encoding
end

#-@Object

外部エンコーディング文字列化(単項)



130
131
132
# File 'lib/when_exe/locales/encoding_conversion.rb', line 130

def -@
  to_external_encoding
end

#to_external_encodingObject

外部エンコーディング文字列化



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/when_exe/locales/encoding_conversion.rb', line 58

def to_external_encoding(source)
  default_external = Encoding.default_external
  case source
  when Locale ;  source.to_external_encoding
  when String ; case source.encoding
                when default_external     ; source
                when Encoding::ASCII_8BIT ; source.dup.force_encoding(default_external)
                else                      ; source.encode(default_external)
                end
  when Regexp ; case source.encoding
                when default_external     ; source
                when Encoding::ASCII_8BIT ; Regexp.new(source.dup.force_encoding(default_external))
                else                      ; Regexp.new(source.to_s.encode(default_external))
                end
  when Array  ;  source.map {|e| to_external_encoding(e)}
  when Hash
    hash = {}
    source.each_pair do |key, value|
      hash[to_external_encoding(key)] = to_external_encoding(value)
    end
    hash
  else        ;  source.respond_to?(:to_external_encoding) ? source.to_external_encoding : source
  end
end

#to_internal_encodingObject

内部エンコーディング文字列化



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/when_exe/locales/encoding_conversion.rb', line 25

def to_internal_encoding(source)
  default_internal = Encoding.default_internal||'UTF-8'
  case source
  when Locale ;  source.to_internal_encoding
  when String ; case source.encoding
                when default_internal     ; source
                when Encoding::ASCII_8BIT ; source.dup.force_encoding(default_internal)
                else                      ; source.encode(default_internal)
                end
  when Regexp ; case source.encoding
                when default_internal     ; source
                when Encoding::ASCII_8BIT ; Regexp.new(source.dup.force_encoding(default_internal))
                else                      ; Regexp.new(source.to_s.encode(default_internal))
                end
  when Array  ;  source.map {|e| to_internal_encoding(e)}
  when Hash
    hash = {}
    source.each_pair do |key, value|
      hash[to_internal_encoding(key)] = to_internal_encoding(value)
    end
    hash
  else        ;  source.respond_to?(:to_internal_encoding) ? source.to_internal_encoding : source
  end
end