Sha256: cb751bc6eee23fe4fdaffe0dfe650bd1df31d36354d4d08f4c411f82e16e40b6

Contents?: true

Size: 825 Bytes

Versions: 26

Compression:

Stored size: 825 Bytes

Contents

# frozen_string_literal: true

require 'rchardet'

module Git
  # Method that can be used to detect and normalize string encoding
  module EncodingUtils
    def self.default_encoding
      __ENCODING__.name
    end

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

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

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

    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
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
git-2.3.3 lib/git/encoding_utils.rb
git-2.3.2 lib/git/encoding_utils.rb
git-2.3.1 lib/git/encoding_utils.rb
git-2.3.0 lib/git/encoding_utils.rb
git-2.2.0 lib/git/encoding_utils.rb
git-2.1.1 lib/git/encoding_utils.rb
git-2.1.0 lib/git/encoding_utils.rb
git-2.0.0.pre3 lib/git/encoding_utils.rb
git-2.0.0.pre2 lib/git/encoding_utils.rb
git-2.0.0.pre1 lib/git/encoding_utils.rb
git-1.19.1 lib/git/encoding_utils.rb
git-1.19.0 lib/git/encoding_utils.rb
git-1.18.0 lib/git/encoding_utils.rb
git-1.17.2 lib/git/encoding_utils.rb
git-1.17.1 lib/git/encoding_utils.rb
git-1.17.0 lib/git/encoding_utils.rb
git-1.16.0 lib/git/encoding_utils.rb
git-1.15.0 lib/git/encoding_utils.rb
git-1.14.0 lib/git/encoding_utils.rb
git-1.13.2 lib/git/encoding_utils.rb