stream: a bit of tidying up

This commit is contained in:
2026-02-09 08:09:25 +00:00
committed by oreodave
parent 2855536bdd
commit 6a54c54bfb

View File

@@ -126,22 +126,6 @@ u64 stream_size(stream_t *stream)
} }
} }
bool stream_eos(stream_t *stream)
{
assert(stream);
switch (stream->type)
{
case STREAM_TYPE_STRING:
return stream->position >= stream->string.size;
case STREAM_TYPE_PIPE:
case STREAM_TYPE_FILE:
return feof(stream->pipe.file);
default:
FAIL("Unreachable");
return 0;
}
}
bool stream_eoc(stream_t *stream) bool stream_eoc(stream_t *stream)
{ {
assert(stream); assert(stream);
@@ -185,8 +169,10 @@ bool stream_chunk(stream_t *stream)
return true; return true;
} }
else else
{
return false; return false;
} }
}
default: default:
FAIL("Unreachable"); FAIL("Unreachable");
return 0; return 0;
@@ -202,8 +188,7 @@ char stream_next(stream_t *stream)
char stream_peek(stream_t *stream) char stream_peek(stream_t *stream)
{ {
// If we've reached end of stream, and end of content, there's really nothing // End of the line? We're done.
// to check here.
if (stream_eoc(stream)) if (stream_eoc(stream))
return '\0'; return '\0';
@@ -216,9 +201,7 @@ char stream_peek(stream_t *stream)
{ {
// Cached already? We are done. // Cached already? We are done.
if (stream->position < stream->pipe.cache.size) if (stream->position < stream->pipe.cache.size)
{
return stream_sv(stream).data[0]; return stream_sv(stream).data[0];
}
// Try to read chunks in till we've reached it or we're at the end of the // Try to read chunks in till we've reached it or we're at the end of the
// file. // file.
@@ -245,9 +228,7 @@ u64 stream_seek(stream_t *stream, i64 offset)
else if (offset > 0) else if (offset > 0)
return stream_seek_forward(stream, offset); return stream_seek_forward(stream, offset);
else else
{
return 0; return 0;
}
} }
u64 stream_seek_forward(stream_t *stream, u64 offset) u64 stream_seek_forward(stream_t *stream, u64 offset)
@@ -300,11 +281,10 @@ u64 stream_seek_forward(stream_t *stream, u64 offset)
u64 stream_seek_backward(stream_t *stream, u64 offset) u64 stream_seek_backward(stream_t *stream, u64 offset)
{ {
assert(stream); if (!stream)
return 0;
if (stream->position < offset) if (stream->position < offset)
{
offset = stream->position; offset = stream->position;
}
stream->position -= offset; stream->position -= offset;
return offset; return offset;
@@ -312,7 +292,8 @@ u64 stream_seek_backward(stream_t *stream, u64 offset)
sv_t stream_sv(stream_t *stream) sv_t stream_sv(stream_t *stream)
{ {
return sv_chop_left(stream_sv_abs(stream), stream->position); sv_t sv = stream_sv_abs(stream);
return sv_chop_left(sv, stream->position);
} }
sv_t stream_sv_abs(stream_t *stream) sv_t stream_sv_abs(stream_t *stream)
@@ -404,25 +385,18 @@ void stream_line_col(stream_t *stream, u64 *line, u64 *col)
{ {
if (!stream || !line || !col) if (!stream || !line || !col)
return; return;
// Go through the cache, byte by byte.
char *cache = NULL; // Generate a string view from the stream of exactly the content /upto/
u64 size = 0; // stream.postion.
if (stream->type == STREAM_TYPE_STRING) sv_t sv = stream_sv_abs(stream);
{ sv = sv_truncate(sv, stream->position);
cache = stream->string.data;
size = stream->string.size;
}
else
{
cache = (char *)vec_data(&stream->pipe.cache);
size = stream->pipe.cache.size;
}
*line = 1; *line = 1;
*col = 0; *col = 0;
for (u64 i = 0; i < size; ++i) // TODO: Could this be faster? Does it matter?
for (u64 i = 0; i < sv.size; ++i)
{ {
char c = cache[i]; char c = sv.data[i];
if (c == '\n') if (c == '\n')
{ {
*line += 1; *line += 1;