3 class StringBuffer(object):
4 'Buffer manager with great worst-case behavior'
6 def __init__(self, data=''):
7 self.buf = collections.deque([data])
8 self.buf_len = len(data)
12 return self.buf_len - self.pos
16 self.buf_len += len(data)
19 if self.buf_len - self.pos < wants:
20 raise IndexError('not enough data')
23 seg = self.buf[0][self.pos:self.pos+wants]
25 while self.buf and self.pos >= len(self.buf[0]):
26 x = self.buf.popleft()
27 self.buf_len -= len(x)
34 def _DataChunker(receiver):
35 wants = receiver.next()
40 wants = receiver.send(buf.get(wants))
43 def DataChunker(receiver):
45 Produces a function that accepts data that is input into a generator
46 (receiver) in response to the receiver yielding the size of data to wait on
48 x = _DataChunker(receiver)