Added stream_till and stream_while helpers
Allows more abstract movement around the stream.
This commit is contained in:
9
alisp.h
9
alisp.h
@@ -158,7 +158,14 @@ sv_t stream_substr(stream_t *, u64);
|
||||
// given size.
|
||||
sv_t stream_substr_abs(stream_t *, u64, u64);
|
||||
|
||||
/// Basic defintions for a Lisp
|
||||
// Skip forward in stream till one of the characters in the given C string is
|
||||
// encountered.
|
||||
sv_t stream_till(stream_t *, const char *);
|
||||
// Skip forward in stream while one of the characters in the given C string is
|
||||
// present.
|
||||
sv_t stream_while(stream_t *, const char *);
|
||||
|
||||
/// Lisp type definitions
|
||||
#define NIL 0
|
||||
|
||||
typedef struct Obj lisp_t;
|
||||
|
||||
28
stream.c
28
stream.c
@@ -339,3 +339,31 @@ sv_t stream_substr_abs(stream_t *stream, u64 index, u64 size)
|
||||
return SV(NULL, 0);
|
||||
}
|
||||
}
|
||||
|
||||
sv_t stream_till(stream_t *stream, const char *str)
|
||||
{
|
||||
if (stream_eoc(stream))
|
||||
return SV(NULL, 0);
|
||||
u64 current_position = stream->position;
|
||||
for (char c = stream_peek(stream); c != '\0' && strchr(str, c) == NULL;
|
||||
c = stream_next(stream))
|
||||
continue;
|
||||
u64 size = stream->position - current_position;
|
||||
if (size == 0)
|
||||
return SV(NULL, 0);
|
||||
return stream_substr_abs(stream, current_position, size - 1);
|
||||
}
|
||||
|
||||
sv_t stream_while(stream_t *stream, const char *str)
|
||||
{
|
||||
if (stream_eoc(stream))
|
||||
return SV(NULL, 0);
|
||||
u64 current_position = stream->position;
|
||||
for (char c = stream_peek(stream); c != '\0' && strchr(str, c);
|
||||
c = stream_next(stream))
|
||||
continue;
|
||||
u64 size = stream->position - current_position;
|
||||
if (size == 0)
|
||||
return SV(NULL, 0);
|
||||
return stream_substr_abs(stream, current_position, size - 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user