module Asciidoctor::Helpers

def prepare_source_string 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 String to prepare

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

removes any trailing whitespace from every line.
Encodes the data to UTF-8, if necessary, splits it into an array, and

Internal: Prepare the source data String for parsing.
def prepare_source_string data, trim_end = true
  return [] if data.nil_or_empty?
  if (leading_2_bytes = (leading_bytes = data.unpack 'C3').slice 0, 2) == BOM_BYTES_UTF_16LE
    data = (data.byteslice 2, data.bytesize).encode UTF_8, ::Encoding::UTF_16LE
  elsif leading_2_bytes == BOM_BYTES_UTF_16BE
    data = (data.byteslice 2, data.bytesize).encode UTF_8, ::Encoding::UTF_16BE
  elsif leading_bytes == BOM_BYTES_UTF_8
    data = data.byteslice 3, data.bytesize
    data = data.encode UTF_8 unless data.encoding == UTF_8
  elsif data.encoding != UTF_8
    data = data.encode UTF_8
  end
  if trim_end
    [].tap {|lines| data.each_line {|line| lines << line.rstrip } }
  else
    [].tap {|lines| data.each_line {|line| lines << line.chomp } }
  end
end