Sha256: afbfcb81b8101be8c7b28daef66b2c5e373a29e81f26b14e2fa3a9f85f2be0a2

Contents?: true

Size: 960 Bytes

Versions: 33

Compression:

Stored size: 960 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

33 entries across 33 versions & 1 rubygems

Version Path
gitlab_git-7.0.1 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.0.0 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.0.0.rc15 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.0.0.rc14 lib/gitlab_git/encoding_helper.rb
gitlab_git-6.2.2 lib/gitlab_git/encoding_herlper.rb
gitlab_git-7.0.0.rc13 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.0.0.rc12 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.0.0.rc11 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.0.0.rc10 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.0.0.rc9 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.0.0.rc8 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.0.0.rc7 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.0.0.rc6 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.0.0.rc5 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.0.0.rc4 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.0.0.rc3 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.0.0.rc2 lib/gitlab_git/encoding_helper.rb
gitlab_git-7.0.0.rc1 lib/gitlab_git/encoding_helper.rb
gitlab_git-6.3.0 lib/gitlab_git/encoding_helper.rb
gitlab_git-6.2.1 lib/gitlab_git/encoding_herlper.rb