Sha256: 2244de6e87796a474815cf6b19aed6d30480166bf31f847d638c891e88cd313a

Contents?: true

Size: 1.74 KB

Versions: 6839

Compression:

Stored size: 1.74 KB

Contents

require_relative 'display_width/constants'
require_relative 'display_width/index'

module Unicode
  module DisplayWidth
    DEPTHS = [0x10000, 0x1000, 0x100, 0x10].freeze

    def self.of(string, ambiguous = 1, overwrite = {}, options = {})
      res = string.codepoints.inject(0){ |total_width, codepoint|
        index_or_value = INDEX
        codepoint_depth_offset = codepoint
        DEPTHS.each{ |depth|
          index_or_value         = index_or_value[codepoint_depth_offset / depth]
          codepoint_depth_offset = codepoint_depth_offset % depth
          break unless index_or_value.is_a? Array
        }
        width = index_or_value.is_a?(Array) ? index_or_value[codepoint_depth_offset] : index_or_value
        width = ambiguous if width == :A
        total_width + (overwrite[codepoint] || width || 1)
      }

      res -= emoji_extra_width_of(string) if options[:emoji]
      res < 0 ? 0 : res
    end

    def self.emoji_extra_width_of(string, ambiguous = 1, overwrite = {}, _ = {})
      require "unicode/emoji"

      extra_width = 0
      modifier_regex = /[#{ Unicode::Emoji::EMOJI_MODIFIERS.pack("U*") }]/
      zwj_regex = /(?<=#{ [Unicode::Emoji::ZWJ].pack("U") })./

      string.scan(Unicode::Emoji::REGEX){ |emoji|
        extra_width += 2 * emoji.scan(modifier_regex).size

        emoji.scan(zwj_regex){ |zwj_succ|
          extra_width += self.of(zwj_succ, ambiguous, overwrite)
        }
      }

      extra_width
    end
  end
end

# Allows you to opt-out of the default string extension. Will eventually be removed,
# so you must opt-in for the core extension by requiring 'display_width/string_ext'
unless defined?(Unicode::DisplayWidth::NO_STRING_EXT) && Unicode::DisplayWidth::NO_STRING_EXT
  require_relative 'display_width/string_ext'
end

Version data entries

6,839 entries across 6,807 versions & 30 rubygems

Version Path
cybrid_api_organization_ruby-0.123.124 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_id_ruby-0.123.124 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_bank_ruby-0.123.123 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_id_ruby-0.123.123 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_organization_ruby-0.123.123 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_bank_ruby-0.123.122 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_organization_ruby-0.123.122 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_id_ruby-0.123.122 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_bank_ruby-0.123.121 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_id_ruby-0.123.121 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_organization_ruby-0.123.121 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_bank_ruby-0.123.120 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_organization_ruby-0.123.120 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_id_ruby-0.123.120 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
avalara_sdk-24.12.0 vendor/bundle/ruby/2.7.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_bank_ruby-0.123.119 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_organization_ruby-0.123.119 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_id_ruby-0.123.119 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_bank_ruby-0.123.118 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb
cybrid_api_id_ruby-0.123.118 vendor/bundle/ruby/3.3.0/gems/unicode-display_width-1.5.0/lib/unicode/display_width.rb