Small updates to std::lib (#949)

* lib/std/io: add Stream.supports_{read,Write}_byte and Stream.write_all
* std/io/: use char.digit()
This commit is contained in:
Pierre Curto
2023-08-24 11:59:00 +02:00
committed by GitHub
parent 056ffa5876
commit c63b3d4209
2 changed files with 11 additions and 2 deletions

View File

@@ -128,7 +128,7 @@ fn uint simple_atoi(char* buf, usz maxlen, usz* len_ptr) @inline @private
while (len < maxlen)
{
char c = buf[len];
if (c < '0' || c > '9') break;
if (!c.is_digit()) break;
i = i * 10 + c - '0';
len++;
}
@@ -657,7 +657,7 @@ fn int! printf_parse_format_field(
char* format_ptr, usz format_len, usz* index_ptr) @inline @private
{
char c = format_ptr[*index_ptr];
if (c >= '0' && c <= '9') return simple_atoi(format_ptr, format_len, index_ptr);
if (c.is_digit()) return simple_atoi(format_ptr, format_len, index_ptr);
if (c != '*') return 0;
printf_advance_format(format_len, index_ptr)!;
any val = next_any(args_ptr, args_len, args_index_ptr)!;

View File

@@ -46,6 +46,8 @@ fn bool Stream.supports_read_from(s) @inline => (bool)s.fns.read_stream_fn;
fn bool Stream.supports_write_to(s) @inline => (bool)s.fns.write_stream_fn;
fn bool Stream.supports_pushback_byte(s) @inline => s.fns.pushback_byte_fn || s.fns.seek_fn;
fn bool Stream.supports_write(s) @inline => s.fns.write_fn || s.fns.write_byte_fn;
fn bool Stream.supports_read_byte(s) @inline => (bool)s.fns.read_byte_fn;
fn bool Stream.supports_write_byte(s) @inline => (bool)s.fns.write_byte_fn;
fn void! Stream.destroy(self) @inline @maydiscard
{
@@ -127,6 +129,13 @@ fn usz! Stream.read_all(self, char[] buffer) @inline
return n;
}
fn usz! Stream.write_all(self, char[] buffer) @inline
{
usz n = self.write(buffer)!;
if (n != buffer.len) return IoError.INCOMPLETE_WRITE?;
return n;
}
fn String! Stream.treadline(self) => self.readline(mem::temp()) @inline;
fn String! Stream.readline(self, Allocator* using = mem::heap())