Sha256: e327c37fadb7d5cad77269ed4c2a4cb41e4c13185f6945fecf5b03a0cb4a9268

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 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 = {})
      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 < 0 ? 0 : res
    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

4 entries across 4 versions & 2 rubygems

Version Path
brakeman-3.6.1 bundle/ruby/2.3.0/gems/unicode-display_width-1.1.3/lib/unicode/display_width.rb
brakeman-3.6.0 bundle/ruby/2.3.0/gems/unicode-display_width-1.1.3/lib/unicode/display_width.rb
brakeman-3.5.0 bundle/ruby/2.3.0/gems/unicode-display_width-1.1.3/lib/unicode/display_width.rb
unicode-display_width-1.1.3 lib/unicode/display_width.rb