Module: FileExtensions

Defined in:
lib/cuporter/extensions/file.rb

Overview

Copyright 2011 ThoughtWorks, Inc. Licensed under the MIT License

Constant Summary collapse

BOM =

BOM may be included at first position by windows editors or due to other file encoding related reasons.

"\xEF\xBB\xBF"

Instance Method Summary collapse

Instance Method Details

#without_byte_order_mark(path) ⇒ Object

codepoint: U+FEFF hex: ef bb bf octal: 357273277



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cuporter/extensions/file.rb', line 10

def without_byte_order_mark(path)
  content = read(path)
  if RUBY_VERSION =~ /^1.8/
    content.slice!(0..2) if content[0..2] == BOM
  else
    BOM.force_encoding("UTF-8")
    unless Encoding.compatible?(content, BOM)
      raise "Encoding #{content.encoding} of file '#{path}' is incompatible for comparison with #{BOM.encoding}"
    end
    content.slice!(0) if content[0] == BOM
  end
  content
end