def _CheckRanges(self): """Verify that in each range tuple the source and target ranges have the same length.""" if self._uni_to_number_ranges: for range in self._uni_to_number_ranges: assert (range[1] - range[0]) == (range[3] - range[2]) if self._uni_to_old_number_ranges: for range in self._uni_to_old_number_ranges: assert (range[1] - range[0]) == (range[3] - range[2]) if self._uni_to_shift_jis_ranges: for range in self._uni_to_shift_jis_ranges: # Shift the Shift-JIS codes down to JIS X 0208 and compute the # linear differences. shift_jis_start = row_cell.FromShiftJis((range[2] >> 8) - 0x10, range[2] & 0xff) shift_jis_end = row_cell.FromShiftJis((range[3] >> 8) - 0x10, range[3] & 0xff) assert (range[1] - range[0]) == (shift_jis_end - shift_jis_start) if self._uni_to_jis_ranges: for range in self._uni_to_jis_ranges: jis_start = row_cell.From2022((range[2] >> 8), range[2] & 0xff) jis_end = row_cell.From2022((range[3] >> 8), range[3] & 0xff) assert (range[1] - range[0]) == (jis_end - jis_start)
def _JisFromUnicode(ranges, uni): """Map a Unicode code point to a JIS X 0208 (ISO-2022-JP) code. In a range of JIS codes, only valid codes according to the encoding scheme are counted. For example, after 757E follows 7621. Args: ranges: A list of ranges. See _RangeFromUnicode(). uni: A Unicode code point (a hex digit string). Returns: The JIS code (integer) corresponding to the Unicode code point, according to the ranges; or None if none of the ranges contains the code point. """ uni = int(uni, 16) range = _RangeFromUnicode(ranges, uni) offset = uni - range[0] rc = row_cell.From2022(range[2] >> 8, range[2] & 0xff) + offset (b1, b2) = rc.To2022() return (b1 << 8) | b2
def testFrom2022(self): rc = row_cell.From2022(0x21, 0x7e) self.assertEqual(rc, row_cell.RowCell(1, 94)) self.assertRaises(ValueError, row_cell.From2022, 0, 94) self.assertRaises(ValueError, row_cell.From2022, 1, 95)