]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add utility method for dealing with deques of strings, in preparation
authorBen Darnell <ben@bendarnell.com>
Tue, 15 Feb 2011 22:14:37 +0000 (14:14 -0800)
committerBen Darnell <ben@bendarnell.com>
Tue, 15 Feb 2011 22:14:37 +0000 (14:14 -0800)
for moving from cStringIO to deques for IOStream buffers.

tornado/iostream.py
tornado/test/runtests.py

index 3cbd9b65e0079be48e57242316a19dcd08811c0b..ff4f90fd6e1d38f52265f4ce8340bbb28dfc8453 100644 (file)
@@ -18,6 +18,7 @@
 
 from __future__ import with_statement
 
+import collections
 import errno
 import logging
 import socket
@@ -462,3 +463,36 @@ class SSLIOStream(IOStream):
             self.close()
             return None
         return chunk
+
+def _merge_prefix(deque, size):
+    """Replace the first entries in a deque of strings with a single
+    string of up to size bytes.
+
+    >>> d = collections.deque(['abc', 'de', 'fghi', 'j'])
+    >>> _merge_prefix(d, 5); print d
+    deque(['abcde', 'fghi', 'j'])
+
+    Strings will be split as necessary to reach the desired size.
+    >>> _merge_prefix(d, 7); print d
+    deque(['abcdefg', 'hi', 'j'])
+
+    >>> _merge_prefix(d, 3); print d
+    deque(['abc', 'defg', 'hi', 'j'])
+
+    >>> _merge_prefix(d, 100); print d
+    deque(['abcdefghij'])
+    """
+    prefix = []
+    remaining = size
+    while deque and remaining > 0:
+        chunk = deque.popleft()
+        if len(chunk) > remaining:
+            deque.appendleft(chunk[remaining:])
+            chunk = chunk[:remaining]
+        prefix.append(chunk)
+        remaining -= len(chunk)
+    deque.appendleft(''.join(prefix))
+
+def doctests():
+    import doctest
+    return doctest.DocTestSuite()
index b19152ba7f2ad5a8ec5e56521bad283cc978e42e..64ad78c1de3ae8c4d995a3d1c1223048b5fbb8a1 100755 (executable)
@@ -3,6 +3,7 @@ import unittest
 
 TEST_MODULES = [
     'tornado.httputil.doctests',
+    'tornado.iostream.doctests',
     'tornado.test.escape_test',
     'tornado.test.httpserver_test',
     'tornado.test.ioloop_test',