module Unicode::DisplayWidth

Constants

DATA_DIR
DATA_FILE
TABLE_FILE
VERSION

Public Class Methods

build_table() click to toggle source
# File lib/unicode/display_width.rb, line 32
def build_table
  data.rewind
  table = {}
  dir = File.dirname TABLE_FILE
  Dir.mkdir(dir) unless Dir.exists?(dir)
  data.lines.each{ |line|
    line =~ %r^(.*);(.*) # .*$/
    if $1 && $2
      cps, width = $1, $2
      if cps['..']
        range = Range.new *cps.split('..').map{ |cp| cp.to_i(16) }
        range.each{ |cp| table[ cp ] = width.to_sym }
      else
        table[ cps.to_i(16) ] = width.to_sym
      end
    end

  }
  File.open(TABLE_FILE, 'wb') { |f| Marshal.dump(table, f) }
end
codepoint(n) click to toggle source
# File lib/unicode/display_width.rb, line 25
def codepoint(n)
  n = n.to_s.unpack('U')[0] unless n.is_a? Integer
  table[n] or raise ArgumentError
end
Also aliased as: width, of
data() click to toggle source

only needed for building the index

# File lib/unicode/display_width.rb, line 12
def data
  @data ||= File.open DATA_FILE
end
of(n) click to toggle source
Alias for: codepoint
table() click to toggle source
# File lib/unicode/display_width.rb, line 16
def table
  if @table
    @table
  else
    build_table unless File.file?(TABLE_FILE)
    @table = Marshal.load File.respond_to?(:binread) ? File.binread(TABLE_FILE) : File.read(TABLE_FILE)
  end
end
width(n) click to toggle source
Alias for: codepoint