Ejemplo n.º 1
0
 def testFromShiftJis(self):
     rc = row_cell.FromShiftJis(0x81, 0x40)
     self.assertEqual(rc, row_cell.RowCell(1, 1))
     rc = row_cell.FromShiftJis(0xef, 0xfc)
     self.assertEqual(rc, row_cell.RowCell(94, 94))
     self.assertRaises(ValueError, row_cell.FromShiftJis, 0x80, 0x40)
     self.assertRaises(ValueError, row_cell.FromShiftJis, 0xa0, 0x40)
     self.assertRaises(ValueError, row_cell.FromShiftJis, 0xf0, 0x40)
     self.assertRaises(ValueError, row_cell.FromShiftJis, 0x81, 0x3f)
     self.assertRaises(ValueError, row_cell.FromShiftJis, 0x81, 0x7f)
     self.assertRaises(ValueError, row_cell.FromShiftJis, 0x81, 0xfd)
def _ShiftJisFromUnicode(ranges, uni):
    """Map a Unicode code point to a Shift-JIS code.

  In a range of Shift-JIS codes, only valid codes according to the encoding
  scheme are counted. For example, after F27E follows F280 because 7F is not
  a valid trail byte.

  Args:
    ranges: A list of ranges. See _RangeFromUnicode().
    uni: A Unicode code point (a hex digit string).

  Returns:
    The Shift-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]
    # Shift the Shift-JIS codes down to JIS X 0208 and back up
    # so that we get standard row-cell byte values (1..94) and can use RowCell.
    rc = row_cell.FromShiftJis(
        (range[2] >> 8) - 0x10, range[2] & 0xff) + offset
    (b1, b2) = rc.ToShiftJis()
    return ((b1 + 0x10) << 8) | b2
Ejemplo n.º 3
0
 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)