Some work

This commit is contained in:
2026-01-22 21:08:02 -08:00
parent eca8ae3d3e
commit f67ed56d52
12 changed files with 488 additions and 38 deletions
+66
View File
@@ -36,3 +36,69 @@ void *lisp_aligned_alloc(size_t alignment, size_t size) {
}
return ptr;
}
#define STRING_STREAM_BLOCK_SIZE 32
static void ensure_string_stream_space(StringStream *restrict stream,
size_t space) {
size_t min_size = stream->nchars + space;
size_t new_size = stream->size;
while (new_size < min_size) {
new_size += STRING_STREAM_BLOCK_SIZE;
}
if (new_size != stream->size) {
stream->buffer = lisp_realloc(stream->buffer, new_size + 1);
}
}
int string_stream_printf(StringStream *restrict stream,
const char *restrict format, ...) {
va_list args;
va_start(args, format);
int rval = string_stream_vprintf(stream, format, args);
va_end(args);
return rval;
}
int string_stream_vprintf(StringStream *restrict stream,
const char *restrict format, va_list args) {
va_list args_copy;
va_copy(args_copy, args);
int space = vsnprintf(NULL, 0, format, args_copy);
if (space < 0) {
abort();
}
va_end(args_copy);
ensure_string_stream_space(stream, space);
int rval = vsnprintf(stream->buffer + stream->nchars,
stream->size + 1 - stream->nchars, format, args);
if (rval < 0) {
abort();
}
stream->nchars += rval;
return rval;
}
bool strgetline(const char *restrict buf, size_t buf_length,
const char **restrict start, size_t *restrict length) {
if (!*start) {
*start = buf;
if (!buf_length) {
*length = 0;
return true;
}
} else if (!buf_length) {
return false;
} else if (*start + *length >= buf + buf_length - 1) {
return false;
} else /* if (*start) */ {
*start += *length + 1;
}
size_t left = buf_length - (*start - buf);
char *found;
if ((found = memchr(*start, '\n', left))) {
*length = found - *start;
} else {
*length = left;
}
return true;
}