module Unicode::DisplayWidth
Constants
- DATA_DIR
- DATA_FILE
- TABLE_FILE
- VERSION
Public Class Methods
build_table()
click to toggle source
# index
# File lib/unicode/display_width.rb, line 32 def build_table data = File.open DATA_FILE data.rewind table = {} dir = File.dirname TABLE_FILE Dir.mkdir(dir) unless Dir.exists?(dir) data.each_line{ |line| line =~ /^(\S+?);(\S+)\s+#.*$/ 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 22 def codepoint(n) n = n.to_s.unpack('U')[0] unless n.is_a? Integer table[n] or raise ArgumentError, 'codepoint not found' end
table()
click to toggle source
# lookup
# File lib/unicode/display_width.rb, line 14 def table if @table @table else @table = Marshal.load File.respond_to?(:binread) ? File.binread(TABLE_FILE) : File.read(TABLE_FILE) end end