Ejemplo n.º 1
0
    def write(self, io: ByteIo, value: int) -> None:
        if value is None:
            io.write(NoneCodec.NONE_VALUE)
            return

        b: bytes = struct.pack(">q", value)
        buffer_len: int = len(b)
        found: bool = False

        for i in range(0, len(b)):
            if not found and b[i] == 0:
                buffer_len = buffer_len - 1
            else:
                found = True

        if buffer_len == 8:
            io.write(self.size_8)
            io.write8(value)
        elif buffer_len == 7:
            io.write(self.size_7)
            io.write7(value)
        elif buffer_len == 6:
            io.write(self.size_6)
            io.write6(value)
        elif buffer_len == 5:
            io.write(self.size_5)
            io.write5(value)
        elif buffer_len == 4:
            io.write(self.size_4)
            io.write4(value)
        elif buffer_len == 3:
            io.write(self.size_3)
            io.write3(value)
        elif buffer_len == 2:
            io.write(self.size_2)
            io.write2(value)
        elif buffer_len <= 1:
            io.write(self.size_1)
            io.write1(value)
        else:
            raise TypeError(f"Could not serialize long {value}")
Ejemplo n.º 2
0
    def write(self, io: ByteIo, value: float) -> None:
        if value is None:
            io.write(NoneCodec.NONE_VALUE)
            return

        packed: bytes = struct.pack(">d", value)
        long: int = int_from_byte(packed)
        if long < -2147483648:
            if long < -549755813888:
                if long < -140737488355328:
                    if long < -36028797018963968:
                        io.write(self.size_8)
                        io.write8(long)
                    else:
                        io.write(self.size_7)
                        io.write7(long)
                else:
                    io.write(self.size_6)
                    io.write6(long)
            else:
                io.write(self.size_5)
                io.write5(long)
        elif long < 128:
            if long < -128:
                if long < -32768:
                    if long < -8388608:
                        io.write(self.size_4)
                        io.write4(long)
                    else:
                        io.write(self.size_3)
                        io.write3(long)
                else:
                    io.write(self.size_2)
                    io.write2(long)
            else:
                io.write(self.size_1)
                io.write1(long)
        elif long < 2147483648:
            if long < 8388608:
                if long < 32768:
                    io.write(self.size_2)
                    io.write2(long)
                else:
                    io.write(self.size_3)
                    io.write3(long)
            else:
                io.write(self.size_4)
                io.write4(long)
        elif long < 140737488355328:
            if long < 549755813888:
                io.write(self.size_5)
                io.write5(long)
            else:
                io.write(self.size_6)
                io.write6(long)
        elif long < 36028797018963968:
            io.write(self.size_7)
            io.write7(long)
        else:
            io.write(self.size_8)
            io.write8(long)