stream: stream_sv and stream_sv_abs

Just straight string views into the current stream content - obviously
may not be stable.  Helpful when analysing a non-moving stream.
This commit is contained in:
2026-02-06 07:05:10 +00:00
committed by oreodave
parent a6d3d861b7
commit 477abc9aa1
2 changed files with 34 additions and 15 deletions

View File

@@ -70,6 +70,12 @@ u64 stream_seek(stream_t *, i64);
u64 stream_seek_forward(stream_t *, u64);
u64 stream_seek_backward(stream_t *, u64);
// Return a string view to stream data relative to the current position of the
// stream
sv_t stream_sv(stream_t *);
// Return a string view to data from the start of the stream
sv_t stream_sv_abs(stream_t *);
// Return a relative substring of a given size
sv_t stream_substr(stream_t *, u64);
// Return an absolute substring at given index and of given size.

View File

@@ -11,6 +11,8 @@
#include <alisp/base.h>
#include <alisp/stream.h>
#include <alisp/sv.h>
#include <alisp/vec.h>
const char *stream_err_to_cstr(stream_err_t err)
{
@@ -310,6 +312,32 @@ u64 stream_seek_backward(stream_t *stream, u64 offset)
return offset;
}
sv_t stream_sv(stream_t *stream)
{
return sv_chop_left(stream_sv_abs(stream), stream->position);
}
sv_t stream_sv_abs(stream_t *stream)
{
if (!stream)
return SV(NULL, 0);
sv_t sv = {0};
switch (stream->type)
{
case STREAM_TYPE_STRING:
sv = stream->string;
break;
case STREAM_TYPE_PIPE:
case STREAM_TYPE_FILE:
sv = SV((char *)vec_data(&stream->pipe.cache), stream_size(stream));
break;
default:
FAIL("Unreachable");
return SV(NULL, 0);
}
return sv;
}
sv_t stream_substr(stream_t *stream, u64 size)
{
if (stream_eoc(stream))
@@ -324,21 +352,6 @@ sv_t stream_substr(stream_t *stream, u64 size)
if (successful != size)
return SV(NULL, 0);
char *ptr = NULL;
switch (stream->type)
{
case STREAM_TYPE_STRING:
ptr = stream->string.data;
break;
case STREAM_TYPE_PIPE:
case STREAM_TYPE_FILE:
ptr = (char *)vec_data(&stream->pipe.cache);
break;
default:
FAIL("Unreachable");
return SV(NULL, 0);
}
return SV(ptr + stream->position, size);
}