Implement tokenisation
This commit is contained in:
34
lib/memory_stream.c
Normal file
34
lib/memory_stream.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "memory_stream.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
static stream_status_t proc_byte(stream_t *s, uint8_t *out, bool consume)
|
||||
{
|
||||
memory_stream_t *ss = (memory_stream_t *)s;
|
||||
if (ss->src < ss->end) {
|
||||
*out = *ss->src;
|
||||
if (consume)
|
||||
++ss->src;
|
||||
return STREAM_STATUS_OK;
|
||||
} else {
|
||||
return STREAM_STATUS_END;
|
||||
}
|
||||
}
|
||||
|
||||
static stream_status_t get_byte(stream_t *s, uint8_t *out)
|
||||
{
|
||||
return proc_byte(s, out, true);
|
||||
}
|
||||
|
||||
static stream_status_t peek_byte(stream_t *s, uint8_t *out)
|
||||
{
|
||||
return proc_byte(s, out, false);
|
||||
}
|
||||
|
||||
void memory_stream_init(memory_stream_t *s, const uint8_t *mem, size_t size)
|
||||
{
|
||||
s->stream.get_byte = get_byte;
|
||||
s->stream.peek_byte = peek_byte;
|
||||
s->src = mem;
|
||||
s->end = mem + size;
|
||||
}
|
||||
Reference in New Issue
Block a user