Sha256: cf145b9150b80a922282bbb3eebd2b58ba88ee313c31b398204d3c14ab0582bb

Contents?: true

Size: 1.65 KB

Versions: 72

Compression:

Stored size: 1.65 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module Mail #:nodoc:
  module Multibyte #:nodoc:
    if RUBY_VERSION >= "1.9"
      # Returns a regular expression that matches valid characters in the current encoding
      def self.valid_character
        VALID_CHARACTER[Encoding.default_external.to_s]
      end
    else
      def self.valid_character
        case $KCODE
        when 'UTF8'
          VALID_CHARACTER['UTF-8']
        when 'SJIS'
          VALID_CHARACTER['Shift_JIS']
        end
      end
    end

    if 'string'.respond_to?(:valid_encoding?)
      # Verifies the encoding of a string
      def self.verify(string)
        string.valid_encoding?
      end
    else
      def self.verify(string)
        if expression = valid_character
          # Splits the string on character boundaries, which are determined based on $KCODE.
          string.split(//).all? { |c| expression =~ c }
        else
          true
        end
      end
    end

    # Verifies the encoding of the string and raises an exception when it's not valid
    def self.verify!(string)
      raise EncodingError.new("Found characters with invalid encoding") unless verify(string)
    end

    if 'string'.respond_to?(:force_encoding)
      # Removes all invalid characters from the string.
      #
      # Note: this method is a no-op in Ruby 1.9
      def self.clean(string)
        string
      end
    else
      def self.clean(string)
        if expression = valid_character
          # Splits the string on character boundaries, which are determined based on $KCODE.
          string.split(//).grep(expression).join
        else
          string
        end
      end
    end
  end
end

Version data entries

72 entries across 56 versions & 13 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
tdiary-5.1.6 vendor/bundle/ruby/3.0.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/2.7.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/mail-2.7.1/lib/mail/multibyte/utils.rb