comparison mercurial/util.py @ 11759:05deba16c5d5

Merge with stable
author Matt Mackall <mpm@selenic.com>
date Fri, 06 Aug 2010 12:59:13 -0500
parents 1b3b843e1100 a79214972da2
children 851161f07068
comparison
equal deleted inserted replaced
11751:b8b4a2417fbd 11759:05deba16c5d5
923 yield chunk[pos:end] 923 yield chunk[pos:end]
924 pos = end 924 pos = end
925 else: 925 else:
926 yield chunk 926 yield chunk
927 self.iter = splitbig(in_iter) 927 self.iter = splitbig(in_iter)
928 self.buf = '' 928 self._queue = []
929 929
930 def read(self, l): 930 def read(self, l):
931 """Read L bytes of data from the iterator of chunks of data. 931 """Read L bytes of data from the iterator of chunks of data.
932 Returns less than L bytes if the iterator runs dry.""" 932 Returns less than L bytes if the iterator runs dry."""
933 if l > len(self.buf) and self.iter: 933 left = l
934 # Clamp to a multiple of 2**16 934 buf = ''
935 targetsize = max(l, 2**16) 935 queue = self._queue
936 collector = [str(self.buf)] 936 while left > 0:
937 collected = len(self.buf) 937 # refill the queue
938 for chunk in self.iter: 938 if not queue:
939 collector.append(chunk) 939 target = 2**18
940 collected += len(chunk) 940 for chunk in self.iter:
941 if collected >= targetsize: 941 queue.append(chunk)
942 target -= len(chunk)
943 if target <= 0:
944 break
945 if not queue:
942 break 946 break
947
948 chunk = queue.pop(0)
949 left -= len(chunk)
950 if left < 0:
951 queue.insert(0, chunk[left:])
952 buf += chunk[:left]
943 else: 953 else:
944 self.iter = False 954 buf += chunk
945 self.buf = ''.join(collector) 955
946 if len(self.buf) == l: 956 return buf
947 s, self.buf = str(self.buf), '' 957
948 else:
949 s, self.buf = self.buf[:l], buffer(self.buf, l)
950 return s
951
952 def filechunkiter(f, size=65536, limit=None): 958 def filechunkiter(f, size=65536, limit=None):
953 """Create a generator that produces the data in the file size 959 """Create a generator that produces the data in the file size
954 (default 65536) bytes at a time, up to optional limit (default is 960 (default 65536) bytes at a time, up to optional limit (default is
955 to read all data). Chunks may be less than size bytes if the 961 to read all data). Chunks may be less than size bytes if the
956 chunk is the last chunk in the file, or the file is a socket or 962 chunk is the last chunk in the file, or the file is a socket or