def iter_slices_future(r, slice_length, decode_unicode=False):
"""Iterate over slices of a string."""
pos, string = 0, r._content
if r.encoding is None:
decode_unicode = False
if decode_unicode:
decoder = codecs.getincrementaldecoder(r.encoding)(errors='replace')
if slice_length is None or slice_length <= 0:
slice_length = len(string)
while pos < len(string):
future = Future()
chunk = string[pos:pos + slice_length]
if decode_unicode:
chunk = decoder.decode(chunk)
future.set_result(chunk)
yield future
pos += slice_length
chunk = decoder.decode(b'', final=True)
if chunk:
future = Future()
future.set_result(chunk)
yield future
评论列表
文章目录