@@ -68,6 +68,16 @@ def __bytes__(self):
68
68
__str__ = __bytes__
69
69
__nonzero__ = __bool__
70
70
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
+
71
81
def maybe_extract_at_most (self , count ):
72
82
"""
73
83
Extract a fixed number of bytes from the buffer.
@@ -76,65 +86,51 @@ def maybe_extract_at_most(self, count):
76
86
if not out :
77
87
return None
78
88
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 )
83
90
84
91
def maybe_extract_next_line (self ):
85
92
"""
86
93
Extract the first line, if it is completed in the buffer.
87
94
"""
88
95
# Only search in buffer space that we've not already looked at.
89
96
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
+
92
99
if partial_idx == - 1 :
93
100
self ._next_line_search = len (self ._data )
94
101
return None
95
102
96
- # Truncate the buffer and return it.
97
103
# + 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 )
104
107
105
108
def maybe_extract_lines (self ):
106
109
"""
107
110
Extract everything up to the first blank line, and return a list of lines.
108
111
"""
109
112
# Handle the case where we have an immediate empty line.
110
113
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 )
114
115
return []
115
116
116
117
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 )
120
119
return []
121
120
122
121
# 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 )
125
123
if match is None :
126
124
self ._multiple_lines_search = max (0 , len (self ._data ) - 2 )
127
125
return None
128
126
129
127
# 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 )
132
130
lines = [line .rstrip (b"\r " ) for line in out .split (b"\n " )]
133
131
134
- self ._data [:idx ] = b""
135
- self ._next_line_search = 0
136
- self ._multiple_lines_search = 0
137
-
138
132
assert lines [- 2 ] == lines [- 1 ] == b""
139
133
140
- return lines [:- 2 ]
134
+ del lines [- 2 :]
135
+
136
+ return lines
0 commit comments