Add dstringwriter.

This commit is contained in:
Christoffer Lerno
2023-03-22 12:23:47 +01:00
parent 9850adfa56
commit 316af36723
4 changed files with 65 additions and 5 deletions

View File

@@ -360,7 +360,7 @@ fn StringData* DString.data(DString str) @inline @private
return (StringData*)str;
}
fn void DString.reserve(DString* str, usz addition) @private
fn void DString.reserve(DString* str, usz addition)
{
StringData* data = str.data();
if (!data)
@@ -375,6 +375,35 @@ fn void DString.reserve(DString* str, usz addition) @private
*str = (DString)realloc(data, StringData.sizeof + new_capacity, .using = data.allocator);
}
fn usz! DString.read_from_stream(DString* string, Stream* reader)
{
if (reader.supports_available())
{
usz total_read = 0;
while (usz available = reader.available()?)
{
string.reserve(available);
StringData* data = string.data();
usz len = reader.read(data.chars[data.len..(data.capacity - 1)])?;
total_read += len;
data.len += len;
}
return total_read;
}
usz total_read = 0;
while (true)
{
// Reserve at least 16 bytes
string.reserve(16);
StringData* data = string.data();
// Read into the rest of the buffer
usz read = reader.read(data.chars[data.len..(data.capacity - 1)])?;
data.len += read;
// Ok, we reached the end.
if (read < 16) return total_read;
// Otherwise go another round
}
}
struct StringData @private
{