Module: RubyGit::CommandLine::EncodingNormalizer
- Defined in:
- lib/ruby_git/command_line/encoding_normalizer.rb
Overview
Utility to normalize string encoding
Class Method Summary collapse
-
.detect_encoding(str) ⇒ String
Detects the character encoding used to create a string or binary data.
-
.normalize(str, normalize_to: Encoding::UTF_8.name) ⇒ String
Normalizes the encoding to normalize_to.
Class Method Details
.detect_encoding(str) ⇒ String
Detects the character encoding used to create a string or binary data
Detects the encoding of a string or return binary if it cannot be detected
22 23 24 |
# File 'lib/ruby_git/command_line/encoding_normalizer.rb', line 22 def self.detect_encoding(str) CharDet.detect(str)&.dig('encoding') || Encoding::BINARY.name end |
.normalize(str, normalize_to: Encoding::UTF_8.name) ⇒ String
Normalizes the encoding to normalize_to
40 41 42 43 44 45 46 47 48 |
# File 'lib/ruby_git/command_line/encoding_normalizer.rb', line 40 def self.normalize(str, normalize_to: Encoding::UTF_8.name) = { invalid: :replace, undef: :replace } detected_encoding = detect_encoding(str) return str if str.valid_encoding? && detected_encoding == normalize_to str.encode(normalize_to, detected_encoding, **) end |