Sha256: 2686848da7db0a3d5c3683fe6260b61bb3f09ddb2302fa76a3330a9c1984dc39

Contents?: true

Size: 951 Bytes

Versions: 28

Compression:

Stored size: 951 Bytes

Contents

module EncodingHelper
  extend self

  def encode!(message)
    return nil unless message.respond_to? :force_encoding

    # if message is utf-8 encoding, just return it
    message.force_encoding("UTF-8")
    return message if message.valid_encoding?

    # return message if message type is binary
    detect = CharlockHolmes::EncodingDetector.detect(message)
    return message.force_encoding("BINARY") if detect && detect[:type] == :binary

    # encoding message to detect encoding
    if detect && detect[:encoding]
      message.force_encoding(detect[:encoding])
    end

    # encode and clean the bad chars
    message.replace clean(message)
  rescue
    encoding = detect ? detect[:encoding] : "unknown"
    "--broken encoding: #{encoding}"
  end

  private

  def clean(message)
    message.encode("UTF-16BE", undef: :replace, invalid: :replace, replace: "")
           .encode("UTF-8")
           .gsub("\0".encode("UTF-8"), "")
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
gitlab_git-7.2.14 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.2.13 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.2.12 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.2.11 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.2.10 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.2.9 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.2.8 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.2.6 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.2.5 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.2.4 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.2.3 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.2.2 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.2.1 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.2.0 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.1.13 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.1.12 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.1.11 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.1.10 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.1.9 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.1.8 lib/gitlab_git/encoding_helper.rb