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
-
.best_guess_encoding ⇒ String
private
Returns the fallback encoding name used when detection fails.
-
.default_encoding ⇒ String
private
Returns the default encoding name used by this source file.
-
.detected_encoding(str) ⇒ String
private
Returns the detected encoding name for the given string.
-
.encoding_options ⇒ Hash<Symbol, Symbol>
private
Returns replacement options used when transcoding invalid byte sequences.
-
.normalize_encoding(str) ⇒ String
private
Returns the given string converted to EncodingUtils.default_encoding.
Class Method Details
.best_guess_encoding ⇒ 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 fallback encoding name used when detection fails
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_encoding ⇒ 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 default encoding name used by this source file
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
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_options ⇒ Hash<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
45 46 47 |
# File 'lib/git/encoding_utils.rb', line 45 def self. { 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
When the string is not valid in its own encoding, the source encoding is
taken from detected_encoding. rchardet can name an
encoding Ruby has no converter for (UTF-7 and the two unusual-octet-order
UCS-4 forms, each assigned from a byte-order mark). In that case the
invalid bytes are replaced with String#scrub instead, which keeps any
valid multibyte sequences on the same line.
63 64 65 66 67 68 69 70 71 |
# File 'lib/git/encoding_utils.rb', line 63 def self.normalize_encoding(str) return str if str.valid_encoding? && str.encoding.name == default_encoding return str.encode(default_encoding, str.encoding, **) if str.valid_encoding? str.encode(default_encoding, detected_encoding(str), **) rescue Encoding::ConverterNotFoundError scrub_to_default_encoding(str) end |