@@ -68,6 +68,16 @@ def __bytes__(self):
6868 __str__ = __bytes__
6969 __nonzero__ = __bool__
7070
71+ def _extract (self , count ):
72+ # extracting an initial slice of the data buffer and return it
73+ out = self ._data [:count ]
74+ del self ._data [:count ]
75+
76+ self ._next_line_search = 0
77+ self ._multiple_lines_search = 0
78+
79+ return out
80+
7181 def maybe_extract_at_most (self , count ):
7282 """
7383 Extract a fixed number of bytes from the buffer.
@@ -76,65 +86,51 @@ def maybe_extract_at_most(self, count):
7686 if not out :
7787 return None
7888
79- self ._data [:count ] = b""
80- self ._next_line_search = 0
81- self ._multiple_lines_search = 0
82- return out
89+ return self ._extract (count )
8390
8491 def maybe_extract_next_line (self ):
8592 """
8693 Extract the first line, if it is completed in the buffer.
8794 """
8895 # Only search in buffer space that we've not already looked at.
8996 search_start_index = max (0 , self ._next_line_search - 1 )
90- partial_buffer = self ._data [ search_start_index :]
91- partial_idx = partial_buffer . find ( b" \r \n " )
97+ partial_idx = self ._data . find ( b" \r \n " , search_start_index )
98+
9299 if partial_idx == - 1 :
93100 self ._next_line_search = len (self ._data )
94101 return None
95102
96- # Truncate the buffer and return it.
97103 # + 2 is to compensate len(b"\r\n")
98- idx = search_start_index + partial_idx + 2
99- out = self ._data [:idx ]
100- self ._data [:idx ] = b""
101- self ._next_line_search = 0
102- self ._multiple_lines_search = 0
103- return out
104+ idx = partial_idx + 2
105+
106+ return self ._extract (idx )
104107
105108 def maybe_extract_lines (self ):
106109 """
107110 Extract everything up to the first blank line, and return a list of lines.
108111 """
109112 # Handle the case where we have an immediate empty line.
110113 if self ._data [:1 ] == b"\n " :
111- self ._data [:1 ] = b""
112- self ._next_line_search = 0
113- self ._multiple_lines_search = 0
114+ self ._extract (1 )
114115 return []
115116
116117 if self ._data [:2 ] == b"\r \n " :
117- self ._data [:2 ] = b""
118- self ._next_line_search = 0
119- self ._multiple_lines_search = 0
118+ self ._extract (2 )
120119 return []
121120
122121 # Only search in buffer space that we've not already looked at.
123- partial_buffer = self ._data [self ._multiple_lines_search :]
124- match = blank_line_regex .search (partial_buffer )
122+ match = blank_line_regex .search (self ._data , self ._multiple_lines_search )
125123 if match is None :
126124 self ._multiple_lines_search = max (0 , len (self ._data ) - 2 )
127125 return None
128126
129127 # Truncate the buffer and return it.
130- idx = self . _multiple_lines_search + match .span (0 )[- 1 ]
131- out = self ._data [: idx ]
128+ idx = match .span (0 )[- 1 ]
129+ out = self ._extract ( idx )
132130 lines = [line .rstrip (b"\r " ) for line in out .split (b"\n " )]
133131
134- self ._data [:idx ] = b""
135- self ._next_line_search = 0
136- self ._multiple_lines_search = 0
137-
138132 assert lines [- 2 ] == lines [- 1 ] == b""
139133
140- return lines [:- 2 ]
134+ del lines [- 2 :]
135+
136+ return lines
0 commit comments