diff options
author | Matthew Wild <mwild1@gmail.com> | 2021-06-29 13:48:14 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2021-06-29 13:48:14 +0100 |
commit | 4e56658eb3564a41d1014e5ee89cd01c8004252e (patch) | |
tree | ca4eb45e809b1aeb0a1f646add1f2f1decc8a5d2 /util | |
parent | 08253e26710469574bb994bfe7670a008d787ba5 (diff) | |
download | prosody-4e56658eb3564a41d1014e5ee89cd01c8004252e.tar.gz prosody-4e56658eb3564a41d1014e5ee89cd01c8004252e.zip |
util.dbuffer: Add read_until() method
Diffstat (limited to 'util')
-rw-r--r-- | util/dbuffer.lua | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/util/dbuffer.lua b/util/dbuffer.lua index 54e12266..b148a4a6 100644 --- a/util/dbuffer.lua +++ b/util/dbuffer.lua @@ -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 + self.front_consumed; + local char_pos = chunk:find(char, start, true); + if char_pos then + return self:read(buffer_pos + (char_pos - start) + #char); + end + buffer_pos = buffer_pos + #chunk; + end + return nil; +end + function dbuffer_methods:discard(requested_bytes) if requested_bytes > self._length then return nil; |