module Asciidoctor::Helpers

def prepare_source_array data, trim_end = true

returns a String Array of prepared lines

(true cleans all whitespace; false only removes trailing newline) (default: true)
trim_end - whether to trim whitespace from the end of each line;
data - the source data Array to prepare (no nil entries allowed)

encode it to UTF-8 from the specified source encoding.
If a BOM is found at the beginning of the data, a best attempt is made to

whitespace from every line.
Encodes the data to UTF-8, if necessary, and removes any trailing

Internal: Prepare the source data Array for parsing.
def prepare_source_array data, trim_end = true
  return [] if data.empty?
  if (leading_2_bytes = (leading_bytes = (first = data[0]).unpack 'C3').slice 0, 2) == BOM_BYTES_UTF_16LE
    data[0] = first.byteslice 2, first.bytesize
    # NOTE you can't split a UTF-16LE string using .lines when encoding is UTF-8; doing so will cause this line to fail
    return trim_end ? data.map {|line| (line.encode UTF_8, ::Encoding::UTF_16LE).rstrip } : data.map {|line| (line.encode UTF_8, ::Encoding::UTF_16LE).chomp }
  elsif leading_2_bytes == BOM_BYTES_UTF_16BE
    data[0] = first.byteslice 2, first.bytesize
    return trim_end ? data.map {|line| (line.encode UTF_8, ::Encoding::UTF_16BE).rstrip } : data.map {|line| (line.encode UTF_8, ::Encoding::UTF_16BE).chomp }
  elsif leading_bytes == BOM_BYTES_UTF_8
    data[0] = first.byteslice 3, first.bytesize
  end
  if first.encoding == UTF_8
    trim_end ? data.map {|line| line.rstrip } : data.map {|line| line.chomp }
  else
    trim_end ? data.map {|line| (line.encode UTF_8).rstrip } : data.map {|line| (line.encode UTF_8).chomp }
  end
end