def utf_7_imap_decode(str): """TODO""" shiftEnd = 0 shiftStart = find(str, "&") if shiftStart == -1: plusPos = find(str, "+", shiftEnd, shiftStart) while plusPos != -1: str = str[:plusPos + 1] + "-" + str[plusPos + 1:] plusPos = find(str, "+", plusPos + 2, shiftStart) while shiftStart != -1: plusPos = find(str, "+", shiftEnd, shiftStart) while plusPos != -1: str = str[:plusPos + 1] + "-" + str[plusPos + 1:] plusPos = find(str, "+", plusPos + 2, shiftStart) shiftEnd = find(str, "-", shiftStart) if shiftEnd != -1: if (shiftEnd - shiftStart) == 1: str = str[:shiftStart + 1] + str[shiftStart + 2:] else: str = str[:shiftStart] + "+" + str[shiftStart + 1:] commaPos = find(str, ",", shiftStart, shiftEnd) while commaPos != -1: str = str[:commaPos] + "/" + str[commaPos + 1:] commaPos = find(str, "+", commaPos + 2, shiftEnd) shiftStart = find(str, "&", shiftEnd) return utf_7_decode(str)
def utf7_decode(self, string): """ utf-7解码 """ try: result = codecs.utf_7_decode(string)[0] return result except TypeError: raise TypeError("Data Type Error!")
def test_utf7_decode_incremental(self): b = "abc\u20acxyz".encode('utf-7') b += "abc\u20ad\u20aexyz".encode('utf-7') b += "abc\u20af\u20b0\u20b1xyz".encode('utf-7') # expected results generated by CPython 3.4 expected = [ ('', 0), ('a', 1), ('ab', 2), ('abc', 3), ('abc', 3), ('abc', 3), ('abc', 3), ('abc', 3), ('abc€', 8), ('abc€x', 9), ('abc€xy', 10), ('abc€xyz', 11), ('abc€xyza', 12), ('abc€xyzab', 13), ('abc€xyzabc', 14), ('abc€xyzabc', 14), ('abc€xyzabc', 14), ('abc€xyzabc', 14), ('abc€xyzabc', 14), ('abc€xyzabc', 14), ('abc€xyzabc', 14), ('abc€xyzabc', 14), ('abc€xyzabc₭₮', 22), ('abc€xyzabc₭₮x', 23), ('abc€xyzabc₭₮xy', 24), ('abc€xyzabc₭₮xyz', 25), ('abc€xyzabc₭₮xyza', 26), ('abc€xyzabc₭₮xyzab', 27), ('abc€xyzabc₭₮xyzabc', 28), ('abc€xyzabc₭₮xyzabc', 28), ('abc€xyzabc₭₮xyzabc', 28), ('abc€xyzabc₭₮xyzabc', 28), ('abc€xyzabc₭₮xyzabc', 28), ('abc€xyzabc₭₮xyzabc', 28), ('abc€xyzabc₭₮xyzabc', 28), ('abc€xyzabc₭₮xyzabc', 28), ('abc€xyzabc₭₮xyzabc', 28), ('abc€xyzabc₭₮xyzabc', 28), ('abc€xyzabc₭₮xyzabc₯₰₱', 38), ('abc€xyzabc₭₮xyzabc₯₰₱x', 39), ('abc€xyzabc₭₮xyzabc₯₰₱xy', 40), ('abc€xyzabc₭₮xyzabc₯₰₱xyz', 41) ] for i in range(len(b) + 1): res = codecs.utf_7_decode(b[:i]) self.assertEqual(res, expected[i])
def test_codecs_builtins(self): s = "abc" encoded = codecs.utf_8_encode(s) self.assertEqual(s, codecs.utf_8_decode(encoded[0])[0]) encoded = codecs.utf_7_encode(s) self.assertEqual(s, codecs.utf_7_decode(encoded[0])[0]) encoded = codecs.utf_16_encode(s) self.assertEqual(s, codecs.utf_16_decode(encoded[0])[0]) encoded = codecs.utf_16_le_encode(s) self.assertEqual(s, codecs.utf_16_le_decode(encoded[0])[0]) encoded = codecs.utf_16_be_encode(s) self.assertEqual(s, codecs.utf_16_be_decode(encoded[0])[0]) encoded = codecs.utf_32_encode(s) self.assertEqual(s, codecs.utf_32_decode(encoded[0])[0]) encoded = codecs.utf_32_le_encode(s) self.assertEqual(s, codecs.utf_32_le_decode(encoded[0])[0]) encoded = codecs.utf_32_be_encode(s) self.assertEqual(s, codecs.utf_32_be_decode(encoded[0])[0]) encoded = codecs.utf_32_be_encode(s) self.assertEqual(s, codecs.utf_32_be_decode(encoded[0])[0]) encoded = codecs.raw_unicode_escape_encode(s) self.assertEqual(s, codecs.raw_unicode_escape_decode(encoded[0])[0]) encoded = codecs.unicode_escape_encode(s) self.assertEqual(s, codecs.unicode_escape_decode(encoded[0])[0]) encoded = codecs.latin_1_encode(s) self.assertEqual(s, codecs.latin_1_decode(encoded[0])[0]) encoded = codecs.ascii_encode(s) self.assertEqual(s, codecs.ascii_decode(encoded[0])[0])
def test_utf_7_decode(self): #sanity new_str, size = codecs.utf_7_decode("abc") self.assertEqual(new_str, u'abc') self.assertEqual(size, 3)
def decode(input, errors='strict'): return codecs.utf_7_decode(input, errors, True)
def decode(input, errors = 'strict'): return codecs.utf_7_decode(input, errors, True)
def test_utf_7_decode(self): #sanity new_str, num_processed = codecs.utf_7_decode(b"abc") self.assertEqual(new_str, 'abc') self.assertEqual(num_processed, 3)
def _buffer_decode(self, input, errors, final): return codecs.utf_7_decode(input, self.errors)
def update_event(self, inp=-1): self.set_output_val( 0, codecs.utf_7_decode(self.input(0), self.input(1), self.input(2)))