Sha256: b33e262783164c4510d663dd6005e8f95865302b87d2a5d46a5c722f97dee195

Contents?: true

Size: 1.36 KB

Versions: 45

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module ActiveSupport
  module Multibyte
    module Unicode
      extend self

      # The Unicode version that is supported by the implementation
      UNICODE_VERSION = RbConfig::CONFIG["UNICODE_VERSION"]

      # Decompose composed characters to the decomposed form.
      def decompose(type, codepoints)
        if type == :compatibility
          codepoints.pack("U*").unicode_normalize(:nfkd).codepoints
        else
          codepoints.pack("U*").unicode_normalize(:nfd).codepoints
        end
      end

      # Compose decomposed characters to the composed form.
      def compose(codepoints)
        codepoints.pack("U*").unicode_normalize(:nfc).codepoints
      end

      # Replaces all ISO-8859-1 or CP1252 characters by their UTF-8 equivalent
      # resulting in a valid UTF-8 string.
      #
      # Passing +true+ will forcibly tidy all bytes, assuming that the string's
      # encoding is entirely CP1252 or ISO-8859-1.
      def tidy_bytes(string, force = false)
        return string if string.empty? || string.ascii_only?
        return recode_windows1252_chars(string) if force
        string.scrub { |bad| recode_windows1252_chars(bad) }
      end

      private
        def recode_windows1252_chars(string)
          string.encode(Encoding::UTF_8, Encoding::Windows_1252, invalid: :replace, undef: :replace)
        end
    end
  end
end

Version data entries

45 entries across 45 versions & 8 rubygems

Version Path
activesupport-8.0.2 lib/active_support/multibyte/unicode.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/activesupport-8.0.1/lib/active_support/multibyte/unicode.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/multibyte/unicode.rb
activesupport-8.0.1 lib/active_support/multibyte/unicode.rb
activesupport-8.0.0.1 lib/active_support/multibyte/unicode.rb
activesupport-7.2.2.1 lib/active_support/multibyte/unicode.rb
activesupport-7.1.5.1 lib/active_support/multibyte/unicode.rb
activesupport-8.0.0 lib/active_support/multibyte/unicode.rb
activesupport-7.2.2 lib/active_support/multibyte/unicode.rb
activesupport-7.1.5 lib/active_support/multibyte/unicode.rb
activesupport-8.0.0.rc2 lib/active_support/multibyte/unicode.rb
activesupport-7.2.1.2 lib/active_support/multibyte/unicode.rb
activesupport-7.1.4.2 lib/active_support/multibyte/unicode.rb
activesupport-8.0.0.rc1 lib/active_support/multibyte/unicode.rb
activesupport-7.2.1.1 lib/active_support/multibyte/unicode.rb
activesupport-7.1.4.1 lib/active_support/multibyte/unicode.rb
activesupport-8.0.0.beta1 lib/active_support/multibyte/unicode.rb
omg-activesupport-8.0.0.alpha9 lib/active_support/multibyte/unicode.rb
omg-activesupport-8.0.0.alpha8 lib/active_support/multibyte/unicode.rb
omg-activesupport-8.0.0.alpha7 lib/active_support/multibyte/unicode.rb