Module: Twine::Encoding

Defined in:
lib/twine/encoding.rb

Class Method Summary collapse

Class Method Details

.bom(path) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/twine/encoding.rb', line 4

def self.bom(path)
  first_bytes = IO.binread(path, 2)
  return nil unless first_bytes
  first_bytes = first_bytes.codepoints.map.to_a
  return 'UTF-16BE' if first_bytes == [0xFE, 0xFF]
  return 'UTF-16LE' if first_bytes == [0xFF, 0xFE]
rescue EOFError
  return nil
end

.encoding_for_path(path) ⇒ Object



18
19
20
# File 'lib/twine/encoding.rb', line 18

def self.encoding_for_path(path)
  bom(path) || 'UTF-8'      
end

.has_bom?(path) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/twine/encoding.rb', line 14

def self.has_bom?(path)
  !bom(path).nil?
end