Comparison

util/dbuffer.lua @ 11636:11e0a0a08da3

util.dbuffer: Add read_until() method
author Matthew Wild <mwild1@gmail.com>
date Tue, 29 Jun 2021 13:48:14 +0100
parent 11196:f243836c449a
child 11637:19cddf92fcc2
comparison
equal deleted inserted replaced
11635:1b17b967838e 11636:11e0a0a08da3
74 end 74 end
75 75
76 return table.concat(chunks); 76 return table.concat(chunks);
77 end 77 end
78 78
79 -- Read to, and including, the specified character sequence (return nil if not found)
80 function dbuffer_methods:read_until(char)
81 local buffer_pos = 0;
82 for i, chunk in self.items:items() do
83 local start = 1 + self.front_consumed;
84 local char_pos = chunk:find(char, start, true);
85 if char_pos then
86 return self:read(buffer_pos + (char_pos - start) + #char);
87 end
88 buffer_pos = buffer_pos + #chunk;
89 end
90 return nil;
91 end
92
79 function dbuffer_methods:discard(requested_bytes) 93 function dbuffer_methods:discard(requested_bytes)
80 if requested_bytes > self._length then 94 if requested_bytes > self._length then
81 return nil; 95 return nil;
82 end 96 end
83 97