def decode_article(self, size): res = "" while len(res) < size: sym_idx = self._ltArticles.decode() sym = self._article_symbols[sym_idx] if sym <= 0x80: if sym <= 0x3F: start_pref_idx = self.bstr.read_bits(tools.bit_length(len(self.prefix))) s = sym + 3 res += self.prefix[start_pref_idx:start_pref_idx + s] else: start_idx = self.bstr.read_bits(tools.bit_length(size)) s = sym - 0x3d res += res[start_idx:start_idx + s] else: res += int2unichr(sym - 0x80) return res
def decode_article(self, size): res = "" while len(res) < size: sym_idx = self._ltArticles.decode() sym = self._article_symbols[sym_idx] if sym <= 0x80: if sym <= 0x3F: start_pref_idx = self.bstr.read_bits( tools.bit_length(len(self.prefix))) s = sym + 3 res += self.prefix[start_pref_idx:start_pref_idx + s] else: start_idx = self.bstr.read_bits(tools.bit_length(size)) s = sym - 0x3d res += res[start_idx:start_idx + s] else: res += unichr(sym - 0x80) return res
def read_reference(self, huffman_number): reference = "" code = self.bstr.read_bits(2) if code == 3: self.bstr.read_bits(32) return reference size = tools.bit_length(huffman_number) assert(size >= 2) return (code << (size - 2)) | self.bstr.read_bits(size - 2)
def read_reference(self, huffman_number): reference = "" code = self.bstr.read_bits(2) if code == 3: self.bstr.read_bits(32) return reference size = tools.bit_length(huffman_number) assert (size >= 2) return (code << (size - 2)) | self.bstr.read_bits(size - 2)
def decode_article(self, size): """ decode User and Abrv dict """ res = "" while len(res) < size: sym_idx = self._ltArticles.decode() sym = self._article_symbols[sym_idx] if sym >= 0x10000: if sym >= 0x10040: start_idx = self.bstr.read_bits(tools.bit_length(size)) s = sym - 0x1003d res += res[start_idx:start_idx + s] else: prefix_idx = self.bstr.read_bits(tools.bit_length(len(self.prefix))) s = sym - 0xfffd res += self.prefix[prefix_idx:prefix_idx + s] else: res += int2unichr(sym) return res
def decode_article(self, size): """ decode User and Abrv dict """ res = "" while len(res) < size: sym_idx = self._ltArticles.decode() sym = self._article_symbols[sym_idx] if sym >= 0x10000: if sym >= 0x10040: start_idx = self.bstr.read_bits(tools.bit_length(size)) s = sym - 0x1003d res += res[start_idx:start_idx + s] else: prefix_idx = self.bstr.read_bits( tools.bit_length(len(self.prefix))) s = sym - 0xfffd res += self.prefix[prefix_idx:prefix_idx + s] else: res += unichr(sym) return res
def test_bit_length_0(self): self.assertEqual(tools.bit_length(0), 1)
def test_bit_length_4(self): self.assertEqual(tools.bit_length(4), 3)
def test_bit_length_2(self): self.assertEqual(tools.bit_length(2), 2)