aboutsummaryrefslogtreecommitdiffstats
path: root/util/dbuffer.lua
diff options
context:
space:
mode:
Diffstat (limited to 'util/dbuffer.lua')
-rw-r--r--util/dbuffer.lua16
1 files changed, 15 insertions, 1 deletions
diff --git a/util/dbuffer.lua b/util/dbuffer.lua
index 640c1449..3ad5fdfe 100644
--- a/util/dbuffer.lua
+++ b/util/dbuffer.lua
@@ -2,7 +2,7 @@ local queue = require "util.queue";
local s_byte, s_sub = string.byte, string.sub;
local dbuffer_methods = {};
-local dynamic_buffer_mt = { __index = dbuffer_methods };
+local dynamic_buffer_mt = { __name = "dbuffer", __index = dbuffer_methods };
function dbuffer_methods:write(data)
if self.max_size and #data + self._length > self.max_size then
@@ -76,6 +76,20 @@ function dbuffer_methods:read(requested_bytes)
return table.concat(chunks);
end
+-- Read to, and including, the specified character sequence (return nil if not found)
+function dbuffer_methods:read_until(char)
+ local buffer_pos = 0;
+ for i, chunk in self.items:items() do
+ local start = 1 + ((i == 1) and self.front_consumed or 0);
+ local char_pos = chunk:find(char, start, true);
+ if char_pos then
+ return self:read(1 + buffer_pos + char_pos - start);
+ end
+ buffer_pos = buffer_pos + #chunk - (start - 1);
+ end
+ return nil;
+end
+
function dbuffer_methods:discard(requested_bytes)
if requested_bytes > self._length then
return nil;