Module: Git::EncodingUtils Private

Defined in:
lib/git/encoding_utils.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Provides helpers for detecting and normalizing string encodings

default_encoding refers to EncodingUtils.default_encoding, which is derived from this source file's encoding declaration

Class Method Summary collapse

Class Method Details

.best_guess_encodingString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the fallback encoding name used when detection fails

Returns:

  • (String)

    the fallback encoding name



26
27
28
29
# File 'lib/git/encoding_utils.rb', line 26

def self.best_guess_encoding
  # Encoding::ASCII_8BIT.name
  Encoding::UTF_8.name
end

.default_encodingString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the default encoding name used by this source file

Returns:

  • (String)

    the source file encoding name



18
19
20
# File 'lib/git/encoding_utils.rb', line 18

def self.default_encoding
  __ENCODING__.name
end

.detected_encoding(str) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the detected encoding name for the given string

Parameters:

  • str (String)

    the string whose encoding should be detected

Returns:

  • (String)

    the detected encoding name or the fallback encoding name



37
38
39
# File 'lib/git/encoding_utils.rb', line 37

def self.detected_encoding(str)
  CharDet.detect(str)['encoding'] || best_guess_encoding
end

.encoding_optionsHash<Symbol, Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns replacement options used when transcoding invalid byte sequences

Returns:

  • (Hash<Symbol, Symbol>)

    options for replacing invalid and undefined bytes



45
46
47
# File 'lib/git/encoding_utils.rb', line 45

def self.encoding_options
  { invalid: :replace, undef: :replace }
end

.normalize_encoding(str) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the given string converted to default_encoding

Parameters:

  • str (String)

    the string to normalize

Returns:



56
57
58
59
60
61
62
# File 'lib/git/encoding_utils.rb', line 56

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