From 0109ea57045b7d85367cd201d288a7683e20319c Mon Sep 17 00:00:00 2001 From: Steve Slaven Date: Mon, 2 Nov 2009 16:06:08 -0800 Subject: Keep track of length as long as possible so we don't have to loop over chunk lengths or rejoin our string all the time diff --git a/FuseArchive/ChunkBuffer.py b/FuseArchive/ChunkBuffer.py index 89d468e..575f5aa 100644 --- a/FuseArchive/ChunkBuffer.py +++ b/FuseArchive/ChunkBuffer.py @@ -5,11 +5,14 @@ import logging class ChunkBuffer: def __init__( self, data = '' ): logging.debug( "Creating chunkbuffer: %s" % data ) + self.l = len( data ) self.chunk = [ data ] def append( self, s ): + self.l += len( s ) self.chunk.append( s ) + # Note: replace doesn't effect length so we don't update it def replace( self, buf, start, end ): # We make a string of our chunk, then create a new list of the 3 # strings, the start, new chunk, and remaining chunk @@ -18,17 +21,11 @@ class ChunkBuffer: self.chunk = [ s[ :start ], buf[ :l ], s[ end: ] ] def length( self ): - if len( self.chunk ) > 5: - self._simplify() - - l = 0; - for c in self.chunk: - l += len( c ) - - return l + return self.l def truncate( self, l ): s = ''.join( self.chunk ) + self.l = l self.chunk = [ s[ :l ] ] def _simplify( self ): -- cgit v0.10.2