Method: Sprockets::EncodingUtils#detect_unicode_bom

Defined in:
lib/sprockets/encoding_utils.rb

#detect_unicode_bom(str) ⇒ Object

Public: Detect and strip BOM from possible unicode string.

str - ASCII-8BIT encoded String

Returns UTF 8/16/32 encoded String without BOM or the original String if no BOM was present.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/sprockets/encoding_utils.rb', line 156

def detect_unicode_bom(str)
  bom_bytes = str.byteslice(0, 4).bytes.to_a

  BOM.each do |encoding, bytes|
    if bom_bytes[0, bytes.size] == bytes
      str = str.dup
      str.force_encoding(Encoding::BINARY)
      str.slice!(0, bytes.size)
      str.force_encoding(encoding)
      return str
    end
  end

  return str
end