Module: Twine::Encoding

Defined in:
lib/twine/encoding.rb

Class Method Summary collapse

Class Method Details

.encoding_for_path(path) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/twine/encoding.rb', line 3

def self.encoding_for_path path
  File.open(path, 'rb') do |f|
    begin
      a = f.readbyte
      b = f.readbyte
      if (a == 0xfe && b == 0xff)
        return 'UTF-16BE'
      elsif (a == 0xff && b == 0xfe)
        return 'UTF-16LE'
      end
    rescue EOFError
    end
  end

  'UTF-8'
end