Exemplo n.º 1
0
    def __store(self, s, bits):
        """Add string s to known data.

        Seeks to avoid duplication, where possible.
        For example, short-forms may be prefixes of long-forms.
        """
        if not s:
            return StringDataToken(0, 0, bits)
        ucs2 = unicode2hex(s)
        try:
            index = self.text.index(s) - 1
            matched = 0
            while matched < len(ucs2):
                index, matched = self.data.index(ucs2[0], index + 1), 1
                if index + len(ucs2) >= len(self.data):
                    raise ValueError  # not found after all !
                while matched < len(ucs2) and self.data[
                        index + matched] == ucs2[matched]:
                    matched += 1
        except ValueError:
            index = len(self.data)
            self.data += ucs2
            self.text += s

        assert index >= 0
        try:
            return StringDataToken(index, len(ucs2), bits)
        except ValueError as e:
            e.args += (self.name, s)
            raise
Exemplo n.º 2
0
    def append(self, s):
        s = s + '\0'
        if s in self.hash:
            return self.hash[s]

        lst = unicode2hex(s)
        index = len(self.data)
        if index > 0xffff:
            raise Error('Index ({}) outside the uint16 range !'.format(index))
        self.hash[s] = index
        self.data += lst
        return index
Exemplo n.º 3
0
    def append(self, s):
        if s in self.hash:
            return self.hash[s]

        lst = unicode2hex(s)
        index = len(self.data)
        if index > 0xffff:
            raise Error('Data index {} is too big for uint16!'.format(index))
        size = len(lst)
        if size >= 0xffff:
            raise Error('Data is too big ({}) for uint16 size!'.format(size))
        token = None
        try:
            token = StringDataToken(index, size)
        except Error as e:
            e.message += '(on data "{}")'.format(s)
            raise
        self.hash[s] = token
        self.data += lst
        return token