mirror of
https://github.com/matrix-construct/construct
synced 2024-12-25 23:14:13 +01:00
ircd::parse::buffer: Add basic convenience functions.
This commit is contained in:
parent
bfd61f84f1
commit
03528ebf67
1 changed files with 45 additions and 7 deletions
|
@ -33,6 +33,9 @@ struct parse
|
|||
IRCD_EXCEPTION(error, grammar_error)
|
||||
IRCD_EXCEPTION(error, syntax_error)
|
||||
|
||||
using read_closure = std::function<void (char *&, char *)>;
|
||||
using parse_closure = std::function<bool (const char *&, const char *)>;
|
||||
|
||||
struct grammar;
|
||||
struct context;
|
||||
struct buffer;
|
||||
|
@ -54,10 +57,27 @@ struct parse::grammar
|
|||
|
||||
struct parse::buffer
|
||||
{
|
||||
char *base;
|
||||
const char *parsed;
|
||||
char *read;
|
||||
char *stop;
|
||||
char *base; // Lowest address of the buffer (const)
|
||||
const char *parsed; // Data between [base, parsed] is completed
|
||||
char *read; // Data between [parsed, read] is unparsed
|
||||
char *stop; // Data between [read, stop] is unreceived (const)
|
||||
|
||||
size_t size() const { return stop - base; }
|
||||
size_t completed() const { return parsed - base; }
|
||||
size_t unparsed() const { return read - parsed; }
|
||||
size_t remaining() const { return stop - read; }
|
||||
|
||||
void discard();
|
||||
void remove();
|
||||
|
||||
buffer(const buffer &old, char *const &start, char *const &stop)
|
||||
:base{start}
|
||||
,parsed{start}
|
||||
,read{start + old.unparsed()}
|
||||
,stop{stop}
|
||||
{
|
||||
memmove(base, old.base, old.unparsed());
|
||||
}
|
||||
|
||||
buffer(char *const &start, char *const &stop)
|
||||
:base{start}
|
||||
|
@ -66,6 +86,10 @@ struct parse::buffer
|
|||
,stop{stop}
|
||||
{}
|
||||
|
||||
template<size_t N> buffer(const buffer &old, char (&buf)[N])
|
||||
:buffer{old, buf, buf + N}
|
||||
{}
|
||||
|
||||
template<size_t N> buffer(char (&buf)[N])
|
||||
:buffer{buf, buf + N}
|
||||
{}
|
||||
|
@ -73,13 +97,13 @@ struct parse::buffer
|
|||
|
||||
struct parse::context
|
||||
{
|
||||
using read_closure = std::function<void (char *&, char *)>;
|
||||
using parse_closure = std::function<bool (const char *&, const char *)>;
|
||||
|
||||
const char *&parsed;
|
||||
char *&read;
|
||||
char *stop;
|
||||
|
||||
size_t unparsed() const { return read - parsed; }
|
||||
size_t remaining() const { return stop - read; }
|
||||
|
||||
read_closure reader;
|
||||
void operator()(const parse_closure &);
|
||||
|
||||
|
@ -117,3 +141,17 @@ ircd::parse::context::operator()(const parse_closure &pc)
|
|||
while(!pc(parsed, const_cast<const char *>(read)))
|
||||
reader(read, stop);
|
||||
}
|
||||
|
||||
inline void
|
||||
ircd::parse::buffer::remove()
|
||||
{
|
||||
memmove(base, parsed, unparsed());
|
||||
read = base + unparsed();
|
||||
parsed = base;
|
||||
}
|
||||
|
||||
inline void
|
||||
ircd::parse::buffer::discard()
|
||||
{
|
||||
read -= unparsed();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue