From dae6382f4bfadf94b1a86e585268494122a1206f Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Wed, 21 Jan 2026 09:27:12 +0000 Subject: [PATCH] Added stream_till and stream_while helpers Allows more abstract movement around the stream. --- alisp.h | 9 ++++++++- stream.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/alisp.h b/alisp.h index 57ab9c7..a9d254c 100644 --- a/alisp.h +++ b/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; diff --git a/stream.c b/stream.c index 94fb756..19fa726 100644 --- a/stream.c +++ b/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); +}