Skip to content

Instantly share code, notes, and snippets.

@tompng
Created April 9, 2024 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tompng/6be795d487e1a0105ada41e24f9528c4 to your computer and use it in GitHub Desktop.
Save tompng/6be795d487e1a0105ada41e24f9528c4 to your computer and use it in GitHub Desktop.
# https://github.com/ruby/reline/pull/632
sizes=[]
CHUNK_LAST.zip(CHUNK_WIDTH).each{sizes << _2 while sizes.size <= [_1, 0x10ffff].min };
BITCHUNKS = sizes.each_slice(2048).map do |seg|
next seg.first if seg.uniq.size == 1
seg.each_slice(64).map do |sub|
next sub.first if sub.uniq.size == 1
s = sub.map { _1 == -1 ? 3 : _1 }.reverse.join
num = s.to_i(4)
num <= 2 ? ('1' + s).to_i(4) : num
end
end
def get_mbchar_width2(m)
ord = m.ord
a = BITCHUNKS[ord / 2048]
return a if a.is_a?(Integer)
ord %= 2048
b = a[ord / 64]
return b if b <= 2
c = b[ord % 64 * 2, 2]
c == 3 ? -1 : c
end
def get_mbchar_width(mbchar)
ord = mbchar.ord
chunk_index = CHUNK_LAST.bsearch_index { |o| ord <= o }
size = CHUNK_WIDTH[chunk_index]
size
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment