diff mercurial/util.py @ 7414:040484030491

Merge with crew
author Matt Mackall <mpm@selenic.com>
date Tue, 25 Nov 2008 16:24:22 -0600
parents 5751631246de 526c40a74bd0
children f0a3e87c810d
line wrap: on
line diff
--- a/mercurial/util.py	Tue Nov 18 16:02:14 2008 -0600
+++ b/mercurial/util.py	Tue Nov 25 16:24:22 2008 -0600
@@ -290,6 +290,37 @@
     l.sort()
     return l
 
+def increasingchunks(source, min=1024, max=65536):
+    '''return no less than min bytes per chunk while data remains,
+    doubling min after each chunk until it reaches max'''
+    def log2(x):
+        if not x:
+            return 0
+        i = 0
+        while x:
+            x >>= 1
+            i += 1
+        return i - 1
+
+    buf = []
+    blen = 0
+    for chunk in source:
+        buf.append(chunk)
+        blen += len(chunk)
+        if blen >= min:
+            if min < max:
+                min = min << 1
+                nmin = 1 << log2(blen)
+                if nmin > min:
+                    min = nmin
+                if min > max:
+                    min = max
+            yield ''.join(buf)
+            blen = 0
+            buf = []
+    if buf:
+        yield ''.join(buf)
+
 class Abort(Exception):
     """Raised if a command needs to print an error and exit."""