Class: Mp3Info::EncodingHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/mp3info/extension_modules.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.convert_from_iso_8859_1(value) ⇒ Object


58
59
60
61
62
63
64
# File 'lib/mp3info/extension_modules.rb', line 58

def self.convert_from_iso_8859_1(value)
  if RUBY_1_8
    ruby_18_encode("utf-8", "iso-8859-1", value)
  else
    value.force_encoding("iso-8859-1").encode("utf-8")
  end
end

.convert_to(value, from, to) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mp3info/extension_modules.rb', line 43

def self.convert_to(value, from, to)
  if RUBY_1_8
    if to == "iso-8859-1"
      to = to + "//TRANSLIT"
    end
    ruby_18_encode(from, to, value)
  else
    if to == "utf-16"
      ("\uFEFF" +  value).encode("UTF-16LE") # Chab 01.apr.2012 : moved from big to little endian for more compatibility (Windows Media Player, older Quicktime..)
    else
      value.encode(to)
    end
  end
end

.decode_utf16(out) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mp3info/extension_modules.rb', line 70

def self.decode_utf16(out)
  if RUBY_1_8
    convert_to(out, "UTF-8", "UTF-16")
  else
    # String#bytes is not an array in Ruby 1.9
    bytes = out.bytes.to_a
    if out.length >= 2 and bytes[0] == 0xff and bytes[1] == 0xfe
      tag_encoding = "UTF-16LE"
      first_valid = 1
    elsif out.length >= 2 and bytes[0] == 0xfe and bytes[1] == 0xff
      tag_encoding = "UTF-16BE"
      first_valid = 1
    else
      # ID3v2.3.0 section 3.3 mandates a BOM but some software
      # erroneously omits it so we have to guess. Since most of
      # the world is little endian we might as well go with that.
      tag_encoding = "UTF-16LE"
      first_valid = 0
    end
    out = out.dup.force_encoding(tag_encoding)[first_valid..-1]
  end
end

.ruby_18_encode(from, to, value) ⇒ Object


66
67
68
# File 'lib/mp3info/extension_modules.rb', line 66

def self.ruby_18_encode(from, to, value)
  Iconv.iconv(to, from, value).first
end