Sha256: 5b4002e022ee442a53a4ad515cf8d179eefd680ae222b4b8476c445503b840c7

Contents?: true

Size: 1.12 KB

Versions: 12

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require 'active_support'

module LHC
  module FixInvalidEncodingConcern
    extend ActiveSupport::Concern

    module ClassMethods
      # fix strings that contain non-UTF8 encoding in a forceful way
      # should none of the fix-attempts be successful,
      # an empty string is returned instead
      def fix_invalid_encoding(string)
        return string unless string.is_a?(String)

        result = string.dup

        # we assume it's ISO-8859-1 first
        if !result.valid_encoding? || !utf8?(result)
          result.encode!('UTF-8', 'ISO-8859-1', invalid: :replace, undef: :replace, replace: '')
        end

        # if it's still an issue, try with BINARY
        if !result.valid_encoding? || !utf8?(result)
          result.encode!('UTF-8', 'BINARY', invalid: :replace, undef: :replace, replace: '')
        end

        # if its STILL an issue, return an empty string :(
        if !result.valid_encoding? || !utf8?(result)
          result = ""
        end

        result
      end

      private

      def utf8?(string)
        string.encoding == Encoding::UTF_8
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
lhc-15.2.1 lib/lhc/concerns/lhc/fix_invalid_encoding_concern.rb
lhc-15.2.0 lib/lhc/concerns/lhc/fix_invalid_encoding_concern.rb
lhc-15.1.3 lib/lhc/concerns/lhc/fix_invalid_encoding_concern.rb
lhc-15.1.2 lib/lhc/concerns/lhc/fix_invalid_encoding_concern.rb
lhc-15.1.1 lib/lhc/concerns/lhc/fix_invalid_encoding_concern.rb
lhc-15.1.0 lib/lhc/concerns/lhc/fix_invalid_encoding_concern.rb
lhc-16.0.0.pre.pro2162.2 lib/lhc/concerns/lhc/fix_invalid_encoding_concern.rb
lhc-16.0.0.pre.pro2162 lib/lhc/concerns/lhc/fix_invalid_encoding_concern.rb
lhc-15.0.1 lib/lhc/concerns/lhc/fix_invalid_encoding_concern.rb
lhc-15.0.0 lib/lhc/concerns/lhc/fix_invalid_encoding_concern.rb
lhc-14.0.0 lib/lhc/concerns/lhc/fix_invalid_encoding_concern.rb
lhc-13.4.0.pre.pro1766.1 lib/lhc/concerns/lhc/fix_invalid_encoding_concern.rb