module Git::EncodingUtils

Method that can be used to detect and normalize string encoding

Public Class Methods

best_guess_encoding() click to toggle source
# File lib/git/encoding_utils.rb, line 11
def self.best_guess_encoding
  # Encoding::ASCII_8BIT.name
  Encoding::UTF_8.name
end
default_encoding() click to toggle source
# File lib/git/encoding_utils.rb, line 7
def self.default_encoding
  __ENCODING__.name
end
detected_encoding(str) click to toggle source
# File lib/git/encoding_utils.rb, line 16
def self.detected_encoding(str)
  CharDet.detect(str)['encoding'] || best_guess_encoding
end
encoding_options() click to toggle source
# File lib/git/encoding_utils.rb, line 20
def self.encoding_options
  { invalid: :replace, undef: :replace }
end
normalize_encoding(str) click to toggle source
# File lib/git/encoding_utils.rb, line 24
def self.normalize_encoding(str)
  return str if str.valid_encoding? && str.encoding.name == default_encoding

  return str.encode(default_encoding, str.encoding, **encoding_options) if str.valid_encoding?

  str.encode(default_encoding, detected_encoding(str), **encoding_options)
end