MCPcopy Index your code
hub / github.com/RustPython/RustPython / encode_long

Function encode_long

Lib/pickle.py:356–384  ·  view source on GitHub ↗

r"""Encode a long to a two's complement little-endian binary string. Note that 0 is a special case, returning an empty string, to save a byte in the LONG1 pickling context. >>> encode_long(0) b'' >>> encode_long(255) b'\xff\x00' >>> encode_long(32767) b'\xff\x7f'

(x)

Source from the content-addressed store, hash-verified

354 f"Can't pickle {obj!r}: it's not the same object as {module_name}.{name}")
355
356def encode_long(x):
357 r"""Encode a long to a two's complement little-endian binary string.
358 Note that 0 is a special case, returning an empty string, to save a
359 byte in the LONG1 pickling context.
360
361 >>> encode_long(0)
362 b''
363 >>> encode_long(255)
364 b'\xff\x00'
365 >>> encode_long(32767)
366 b'\xff\x7f'
367 >>> encode_long(-256)
368 b'\x00\xff'
369 >>> encode_long(-32768)
370 b'\x00\x80'
371 >>> encode_long(-128)
372 b'\x80'
373 >>> encode_long(127)
374 b'\x7f'
375 >>>
376 """
377 if x == 0:
378 return b''
379 nbytes = (x.bit_length() >> 3) + 1
380 result = x.to_bytes(nbytes, byteorder='little', signed=True)
381 if x < 0 and nbytes > 1:
382 if result[-1] == 0xff and (result[-2] & 0x80) != 0:
383 result = result[:-1]
384 return result
385
386def decode_long(data):
387 r"""Decode a long from a two&#x27;s complement little-endian binary string.

Callers 1

save_longMethod · 0.85

Calls 2

to_bytesMethod · 0.80
bit_lengthMethod · 0.45

Tested by

no test coverage detected