From: Ben Darnell Date: Tue, 15 Feb 2011 22:14:37 +0000 (-0800) Subject: Add utility method for dealing with deques of strings, in preparation X-Git-Tag: v1.2.0~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=025990947316ff9de67796dce185af91d11e5d02;p=thirdparty%2Ftornado.git Add utility method for dealing with deques of strings, in preparation for moving from cStringIO to deques for IOStream buffers. --- diff --git a/tornado/iostream.py b/tornado/iostream.py index 3cbd9b65e..ff4f90fd6 100644 --- a/tornado/iostream.py +++ b/tornado/iostream.py @@ -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() diff --git a/tornado/test/runtests.py b/tornado/test/runtests.py index b19152ba7..64ad78c1d 100755 --- a/tornado/test/runtests.py +++ b/tornado/test/runtests.py @@ -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',