[Python-checkins] python/dist/src/Lib pickle.py,1.128,1.129

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Thu, 30 Jan 2003 19:44:00 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv2828/Lib

Modified Files:
	pickle.py 
Log Message:
Linear-time implementations of {encode,decode}_long.


Index: pickle.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/pickle.py,v
retrieving revision 1.128
retrieving revision 1.129
diff -C2 -d -r1.128 -r1.129
*** pickle.py	30 Jan 2003 15:41:46 -0000	1.128
--- pickle.py	31 Jan 2003 03:43:58 -0000	1.129
***************
*** 1286,1293 ****
      pass
  
! # Encode/decode longs.
  
  def encode_long(x):
!     r"""Encode a long to a two's complement little-ending binary string.
      >>> encode_long(255L)
      '\xff\x00'
--- 1286,1295 ----
      pass
  
! # Encode/decode longs in linear time.
! 
! import binascii as _binascii
  
  def encode_long(x):
!     r"""Encode a long to a two's complement little-endian binary string.
      >>> encode_long(255L)
      '\xff\x00'
***************
*** 1304,1315 ****
      >>>
      """
!     # XXX This is still a quadratic algorithm.
!     # Should use hex() to get started.
!     digits = []
!     while not -128 <= x < 128:
!         digits.append(x & 0xff)
!         x >>= 8
!     digits.append(x & 0xff)
!     return "".join(map(chr, digits))
  
  def decode_long(data):
--- 1306,1349 ----
      >>>
      """
! 
!     if x == 0:
!         return '\x00'
!     if x > 0:
!         ashex = hex(x)
!         assert ashex.startswith("0x")
!         njunkchars = 2 + ashex.endswith('L')
!         nibbles = len(ashex) - njunkchars
!         if nibbles & 1:
!             # need an even # of nibbles for unhexlify
!             ashex = "0x0" + ashex[2:]
!         elif ashex[2] >= '8':
!             # "looks negative", so need a byte of sign bits
!             ashex = "0x00" + ashex[2:]
!     else:
!         # Build the 256's-complement:  (1L << nbytes) + x.  The trick is
!         # to find the number of bytes in linear time (although that should
!         # really be a constant-time task).
!         ashex = hex(-x)
!         assert ashex.startswith("0x")
!         njunkchars = 2 + ashex.endswith('L')
!         nibbles = len(ashex) - njunkchars
!         if nibbles & 1:
!             # need an even # of nibbles for unhexlify
!             nibbles += 1
!         nbytes = nibbles >> 1
!         x += 1L << (nbytes * 8)
!         assert x > 0
!         ashex = hex(x)
!         if x >> (nbytes * 8 - 1) == 0:
!             # "looks positive", so need a byte of sign bits
!             ashex = "0xff" + x[2:]
! 
!     if ashex.endswith('L'):
!         ashex = ashex[2:-1]
!     else:
!         ashex = ashex[2:]
!     assert len(ashex) & 1 == 0
!     binary = _binascii.unhexlify(ashex)
!     return binary[::-1]
  
  def decode_long(data):
***************
*** 1328,1340 ****
      127L
      """
!     # XXX This is quadratic too.
!     x = 0L
!     i = 0L
!     for c in data:
!         x |= long(ord(c)) << i
!         i += 8L
!     if data and ord(c) >= 0x80:
!         x -= 1L << i
!     return x
  
  # Shorthands
--- 1362,1371 ----
      127L
      """
! 
!     ashex = _binascii.hexlify(data[::-1])
!     n = long(ashex, 16)
!     if data[-1] >= '\x80':
!         n -= 1L << (len(data) * 8)
!     return n
  
  # Shorthands