ruby-changes:56539
From: Hiroshi <ko1@a...>
Date: Mon, 15 Jul 2019 11:16:47 +0900 (JST)
Subject: [ruby-changes:56539] Hiroshi SHIBATA: 41c5f9a166 (master): Put jisx0208.rb to under the library directory.
https://git.ruby-lang.org/ruby.git/commit/?id=41c5f9a166 From 41c5f9a166637bf9e49af19b3fa94163b51db64d Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA <hsbt@r...> Date: Mon, 15 Jul 2019 10:43:12 +0900 Subject: Put jisx0208.rb to under the library directory. diff --git a/tool/enc-emoji-citrus-gen.rb b/tool/enc-emoji-citrus-gen.rb index 94864e5..da9c8a6 100644 --- a/tool/enc-emoji-citrus-gen.rb +++ b/tool/enc-emoji-citrus-gen.rb @@ -1,4 +1,4 @@ https://github.com/ruby/ruby/blob/trunk/tool/enc-emoji-citrus-gen.rb#L1 -require File.expand_path('../jisx0208', __FILE__) +require File.expand_path('../lib/jisx0208', __FILE__) ENCODES = [ { diff --git a/tool/jisx0208.rb b/tool/jisx0208.rb deleted file mode 100644 index 30185fb..0000000 --- a/tool/jisx0208.rb +++ /dev/null @@ -1,86 +0,0 @@ https://github.com/ruby/ruby/blob/trunk/tool/enc-emoji-citrus-gen.rb#L0 -# Library used by tools/enc-emoji-citrus-gen.rb - -module JISX0208 - class Char - class << self - def from_sjis(sjis) - unless 0x8140 <= sjis && sjis <= 0xFCFC - raise ArgumentError, "out of the range of JIS X 0208: 0x#{sjis.to_s(16)}" - end - sjis_hi, sjis_lo = sjis >> 8, sjis & 0xFF - sjis_hi = (sjis_hi - ((sjis_hi <= 0x9F) ? 0x80 : 0xC0)) << 1 - if sjis_lo <= 0x9E - sjis_hi -= 1 - sjis_lo -= (sjis_lo <= 0x7E) ? 0x3F : 0x40 - else - sjis_lo -= 0x9E - end - return self.new(sjis_hi, sjis_lo) - end - end - - def initialize(row, cell=nil) - if cell - @code = row_cell_to_code(row, cell) - else - @code = row.to_int - end - end - - def ==(other) - if self.class === other - return Integer(self) == Integer(other) - end - return super(other) - end - - def to_int - return @code - end - - def hi - Integer(self) >> 8 - end - - def lo - Integer(self) & 0xFF - end - - def row - self.hi - 0x20 - end - - def cell - self.lo - 0x20 - end - - def succ - succ_hi, succ_lo = self.hi, self.lo + 1 - if succ_lo > 0x7E - succ_lo = 0x21 - succ_hi += 1 - end - return self.class.new(succ_hi << 8 | succ_lo) - end - - def to_sjis - h, l = self.hi, self.lo - h = (h + 1) / 2 + ((0x21..0x5E).include?(h) ? 0x70 : 0xB0) - l += self.hi.odd? ? 0x1F + ((l >= 0x60) ? 1 : 0) : 0x7E - return h << 8 | l - end - - def inspect - "#<JISX0208::Char:#{self.object_id.to_s(16)} sjis=#{self.to_sjis.to_s(16)} jis=#{self.to_int.to_s(16)}>" - end - - private - - def row_cell_to_code(row, cell) - unless 0 < row && (1..94).include?(cell) - raise ArgumentError, "out of row-cell range: #{row}-#{cell}" - end - return (row + 0x20) << 8 | (cell + 0x20) - end - end -end diff --git a/tool/lib/jisx0208.rb b/tool/lib/jisx0208.rb new file mode 100644 index 0000000..30185fb --- /dev/null +++ b/tool/lib/jisx0208.rb @@ -0,0 +1,86 @@ https://github.com/ruby/ruby/blob/trunk/tool/lib/jisx0208.rb#L1 +# Library used by tools/enc-emoji-citrus-gen.rb + +module JISX0208 + class Char + class << self + def from_sjis(sjis) + unless 0x8140 <= sjis && sjis <= 0xFCFC + raise ArgumentError, "out of the range of JIS X 0208: 0x#{sjis.to_s(16)}" + end + sjis_hi, sjis_lo = sjis >> 8, sjis & 0xFF + sjis_hi = (sjis_hi - ((sjis_hi <= 0x9F) ? 0x80 : 0xC0)) << 1 + if sjis_lo <= 0x9E + sjis_hi -= 1 + sjis_lo -= (sjis_lo <= 0x7E) ? 0x3F : 0x40 + else + sjis_lo -= 0x9E + end + return self.new(sjis_hi, sjis_lo) + end + end + + def initialize(row, cell=nil) + if cell + @code = row_cell_to_code(row, cell) + else + @code = row.to_int + end + end + + def ==(other) + if self.class === other + return Integer(self) == Integer(other) + end + return super(other) + end + + def to_int + return @code + end + + def hi + Integer(self) >> 8 + end + + def lo + Integer(self) & 0xFF + end + + def row + self.hi - 0x20 + end + + def cell + self.lo - 0x20 + end + + def succ + succ_hi, succ_lo = self.hi, self.lo + 1 + if succ_lo > 0x7E + succ_lo = 0x21 + succ_hi += 1 + end + return self.class.new(succ_hi << 8 | succ_lo) + end + + def to_sjis + h, l = self.hi, self.lo + h = (h + 1) / 2 + ((0x21..0x5E).include?(h) ? 0x70 : 0xB0) + l += self.hi.odd? ? 0x1F + ((l >= 0x60) ? 1 : 0) : 0x7E + return h << 8 | l + end + + def inspect + "#<JISX0208::Char:#{self.object_id.to_s(16)} sjis=#{self.to_sjis.to_s(16)} jis=#{self.to_int.to_s(16)}>" + end + + private + + def row_cell_to_code(row, cell) + unless 0 < row && (1..94).include?(cell) + raise ArgumentError, "out of row-cell range: #{row}-#{cell}" + end + return (row + 0x20) << 8 | (cell + 0x20) + end + end +end diff --git a/tool/test/test_jisx0208.rb b/tool/test/test_jisx0208.rb index 703ee36..98f216f 100644 --- a/tool/test/test_jisx0208.rb +++ b/tool/test/test_jisx0208.rb @@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/tool/test/test_jisx0208.rb#L1 require 'test/unit' -require_relative '../jisx0208' +require_relative '../lib/jisx0208' class Test_JISX0208_Char < Test::Unit::TestCase def test_create_with_row_cell -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/