Method: Parser::Source::Buffer.reencode_string

Defined in:
lib/parser/source/buffer.rb

.reencode_string(input) ⇒ String

Recognize encoding of input and process it so it could be lexed.

  • If input does not contain BOM or magic encoding comment, it is kept in the original encoding.
  • If the detected encoding is binary, input is kept in binary.
  • Otherwise, input is re-encoded into UTF-8 and returned as a new string.

This method mutates the encoding of input, but not its content.

Parameters:

  • input (String)

Returns:

  • (String)

Raises:

  • (EncodingError)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/parser/source/buffer.rb', line 95

def self.reencode_string(input)
  original_encoding = input.encoding
  detected_encoding = recognize_encoding(input.force_encoding(Encoding::BINARY))

  if detected_encoding.nil?
    input.force_encoding(original_encoding)
  elsif detected_encoding == Encoding::BINARY
    input
  else
    input.
      force_encoding(detected_encoding).
      encode(Encoding::UTF_8)
  end
end