## ## UTF-5 library for Ruby ## (draft-jseng-utf5-01.txt) ## Nov 9, 2000 by yoshidam ## require 'uconv' module Uconv UTF5TRANS = "0123456789ABCDEFGHIJKLMNOPQRSTUV" def self._uarytoutf5(uary) ret = "" uary.each do |u| if u < 0x10 len = 1 elsif u < 0x100 len = 2 elsif u < 0x1000 len = 3 elsif u < 0x10000 len = 4 elsif u < 0x100000 len = 5 elsif u < 0x1000000 len = 6 elsif u < 0x10000000 len = 7 elsif u < 0x80000000 len = 8 else raise "invalid ucs-4" end for i in 1..len c = (u >> (4*(len - i))) & 0xf c |= 0x10 if i == 1 ret << UTF5TRANS[c] end end ret end def self._utf5touary(utf5) ret = [] start = 1 cc = nil utf5.each_byte do |c| v = UTF5TRANS.index(c.chr) raise "invalid utf-5" unless v if v >= 0x10 ret << cc if cc cc = v & 0xf else cc = (cc << 4) | v end end ret << cc if cc ret end def self.u8tou5(utf8) _uarytoutf5(utf8.unpack("U*")) end def self.u5tou8(utf5) _utf5touary(utf5).pack("U*") end end