\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef/embedded/lib/ruby/gems/2.7.0/gems/unicode_utils-1.4.0/lib/unicode_utils/

Viewing File: nfc.rb

# -*- encoding: utf-8 -*-

require "unicode_utils/read_cdata"
require "unicode_utils/canonical_decomposition"
require "unicode_utils/combining_class"

module UnicodeUtils
  
  module Impl # :nodoc:all
    
    COMPOSITION_EXCLUSION_SET =
      Impl.read_code_point_set("composition_exclusion_set")

    CANONICAL_COMPOSITION_MAP = Hash.new.tap do |m|
      CANONICAL_DECOMPOSITION_MAP.each_pair { |comp, decomp|
        if decomp.length == 2
          (m[decomp[0]] ||= {})[decomp[1]] = comp
        end
      }
    end

    module NFC

      # does b block c?
      def self.blocked?(b, c)
        # From the standard:
        # "If a combining character sequence is in canonical order,
        # then testing whether a character is blocked requires looking
        # at only the immediately preceding character."
        # cpary is in canonical order (since it comes out of
        # canonical_decomposition).
        COMBINING_CLASS_MAP[b] >= COMBINING_CLASS_MAP[c]
      end

      def self.primary_composite?(cp)
        unless CANONICAL_DECOMPOSITION_MAP[cp] ||
            # has hangul syllable decomposition?
            (cp >= 0xAC00 && cp <= 0xD7A3)
          return false
        end
        !COMPOSITION_EXCLUSION_SET.include?(cp)
      end

    end

    def self.composition(str)
      ### constants for hangul composition ###
      sbase = 0xAC00
      lbase = 0x1100
      vbase = 0x1161
      tbase = 0x11A7
      lcount = 19
      vcount = 21
      tcount = 28
      ncount = vcount * tcount
      scount = lcount * ncount
      ########################################

      String.new.force_encoding(str.encoding).tap do |res|
        last_starter = nil
        uncomposable_non_starters = []
        str.each_codepoint { |cp|
          if COMBINING_CLASS_MAP[cp] == 0 # starter?
            combined = false
            if last_starter && uncomposable_non_starters.empty?
              ### hangul ###
              lindex = last_starter - lbase
              if 0 <= lindex && lindex < lcount
                vindex = cp - vbase
                if 0 <= vindex && vindex <= vcount
                  last_starter =
                    sbase + (lindex * vcount + vindex) * tcount
                  combined = true
                end
              end
              unless combined
                sindex = last_starter - sbase
                if 0 <= sindex && sindex < scount && (sindex % tcount) == 0
                  tindex = cp - tbase
                  if 0 <= tindex && tindex < tcount
                    last_starter += tindex
                    combined = true
                  end
                end
              end
              ##############
              unless combined
                map = Impl::CANONICAL_COMPOSITION_MAP[last_starter]
                composition = map && map[cp]
                if composition && Impl::NFC.primary_composite?(composition)
                  last_starter = composition
                  combined = true
                end
              end
            end
            unless combined
              res << last_starter if last_starter
              uncomposable_non_starters.each { |nc| res << nc }
              uncomposable_non_starters.clear
              last_starter = cp
            end
          else
            last_non_starter = uncomposable_non_starters.last
            if last_non_starter && Impl::NFC.blocked?(last_non_starter, cp)
              uncomposable_non_starters << cp
            else
              map = Impl::CANONICAL_COMPOSITION_MAP[last_starter]
              composition = map && map[cp]
              if composition && Impl::NFC.primary_composite?(composition)
                last_starter = composition
              else
                uncomposable_non_starters << cp
              end
            end
          end
        }
        res << last_starter if last_starter
        uncomposable_non_starters.each { |nc| res << nc }
      end
    end

  end

  # Get +str+ in Normalization Form C.
  #
  # The Unicode standard has multiple representations for some
  # characters. One representation as a single code point and other
  # representation(s) as a combination of multiple code points. This
  # function "composes" these characters into the former
  # representation.
  #
  # Example:
  #
  #   require "unicode_utils/nfc"
  #   UnicodeUtils.nfc("La\u{308}mpchen") => "Lämpchen"
  def nfc(str)
    str = UnicodeUtils.canonical_decomposition(str)
    Impl.composition(str)
  end
  module_function :nfc

end