Sha256: 23f985941b9da9afd8d715bc8b0d0c7449b5f441ec44a0259f89eb40b220a51f

Contents?: true

Size: 629 Bytes

Versions: 7

Compression:

Stored size: 629 Bytes

Contents

require 'charlock_holmes'

module PrettyDiff
  module Encoding
    extend self

    def enforce(encoding, text)
      result = text
      if (detected = detect(result)) && detected.downcase != encoding.downcase
        result = convert(result, detected, encoding)
      end
      if RUBY_VERSION >= "2.0.0"
        result.force_encoding(encoding)
      else
        result
      end
    end

    def detect(str)
      if detected = CharlockHolmes::EncodingDetector.detect(str)
        detected[:encoding]
      end
    end

    def convert(str, from, to)
      CharlockHolmes::Converter.convert(str, from, to)
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pretty_diff-2.1.4 lib/pretty_diff/encoding.rb
pretty_diff-2.1.3 lib/pretty_diff/encoding.rb
pretty_diff-2.1.2 lib/pretty_diff/encoding.rb
pretty_diff-2.1.1 lib/pretty_diff/encoding.rb
pretty_diff-2.1.0 lib/pretty_diff/encoding.rb
pretty_diff-2.0.1 lib/pretty_diff/encoding.rb
pretty_diff-2.0.0 lib/pretty_diff/encoding.rb