type FixedReader { bytes: Span, cursor: usize, } type FixedWriter { buffer: MutSpan, cursor: usize, } fn __zero_std_io_write_byte(buffer: MutSpan, offset: usize, byte: u8) -> Maybe { if offset >= std.mem.len(buffer) { return null } buffer[offset] = byte return offset + 1 } fn __zero_std_io_write_span(buffer: MutSpan, offset: usize, bytes: Span) -> Maybe { if offset > std.mem.len(buffer) { return null } if std.mem.len(bytes) > std.mem.len(buffer) - offset { return null } var index: usize = 0 while index < std.mem.len(bytes) { buffer[offset + index] = bytes[index] index = index + 1 } return offset + std.mem.len(bytes) } fn __zero_std_io_write_all(buffer: MutSpan, offset: usize, bytes: Span) -> Maybe { return __zero_std_io_write_span(buffer, offset, bytes) } fn __zero_std_io_write_at(buffer: MutSpan, offset: usize, bytes: Span) -> Maybe { return __zero_std_io_write_span(buffer, offset, bytes) } fn __zero_std_io_read(bytes: Span, offset: usize, buffer: MutSpan) -> Maybe { if offset > std.mem.len(bytes) { return null } let available: usize = std.mem.len(bytes) - offset var count: usize = std.mem.len(buffer) if available < count { count = available } var index: usize = 0 while index < count { buffer[index] = bytes[offset + index] index = index + 1 } return offset + count } fn __zero_std_io_read_exact(bytes: Span, offset: usize, buffer: MutSpan) -> Maybe { if offset > std.mem.len(bytes) { return null } if std.mem.len(buffer) > std.mem.len(bytes) - offset { return null } var index: usize = 0 while index < std.mem.len(buffer) { buffer[index] = bytes[offset + index] index = index + 1 } return offset + std.mem.len(buffer) } fn __zero_std_io_read_at(bytes: Span, offset: usize, buffer: MutSpan) -> Maybe { return __zero_std_io_read(bytes, offset, buffer) } fn __zero_std_io_read_all(bytes: Span, buffer: MutSpan) -> Maybe> { let count: usize = std.mem.len(bytes) let copied: Maybe = __zero_std_io_copy_n(buffer, bytes, count) if copied.has { return __zero_std_io_written(buffer, copied.value) } return null } fn __zero_std_io_copy(buffer: MutSpan, bytes: Span) -> usize { var count: usize = std.mem.len(bytes) if std.mem.len(buffer) < count { count = std.mem.len(buffer) } var index: usize = 0 while index < count { buffer[index] = bytes[index] index = index + 1 } return count } fn __zero_std_io_read_byte(bytes: Span, offset: usize) -> Maybe { if offset >= std.mem.len(bytes) { return null } return bytes[offset] } fn __zero_std_io_copy_n(buffer: MutSpan, bytes: Span, count: usize) -> Maybe { if count > std.mem.len(buffer) { return null } if count > std.mem.len(bytes) { return null } var index: usize = 0 while index < count { buffer[index] = bytes[index] index = index + 1 } return count } fn __zero_std_io_written(buffer: Span, len: usize) -> Span { if len > std.mem.len(buffer) { return buffer } return buffer[..len] } fn __zero_std_io_remaining(buffer: Span, offset: usize) -> usize { if offset >= std.mem.len(buffer) { return 0 } return std.mem.len(buffer) - offset } fn __zero_std_io_next_line(bytes: Span, start: usize) -> Maybe> { if start >= std.mem.len(bytes) { return null } var end: usize = start while end < std.mem.len(bytes) && bytes[end] != 10_u8 { end = end + 1 } if end > start && bytes[end - 1] == 13_u8 { return bytes[start..end - 1] } return bytes[start..end] } fn __zero_std_io_read_line(bytes: Span, start: usize) -> Maybe> { return __zero_std_io_next_line(bytes, start) } fn __zero_std_io_next_line_start(bytes: Span, start: usize) -> usize { if start >= std.mem.len(bytes) { return std.mem.len(bytes) } var end: usize = start while end < std.mem.len(bytes) && bytes[end] != 10_u8 { end = end + 1 } if end < std.mem.len(bytes) { return end + 1 } return std.mem.len(bytes) } fn __zero_std_io_read_line_start(bytes: Span, start: usize) -> usize { return __zero_std_io_next_line_start(bytes, start) } fn __zero_std_io_read_until_delimiter(bytes: Span, start: usize, delimiter: u8) -> Maybe> { if start >= std.mem.len(bytes) { return null } var end: usize = start while end < std.mem.len(bytes) && bytes[end] != delimiter { end = end + 1 } return bytes[start..end] } fn __zero_std_io_read_until_delimiter_start(bytes: Span, start: usize, delimiter: u8) -> usize { if start >= std.mem.len(bytes) { return std.mem.len(bytes) } var end: usize = start while end < std.mem.len(bytes) && bytes[end] != delimiter { end = end + 1 } if end < std.mem.len(bytes) { return end + 1 } return std.mem.len(bytes) } fn __zero_std_io_fixed_reader(bytes: Span, cursor: usize) -> FixedReader { var start: usize = cursor if start > std.mem.len(bytes) { start = std.mem.len(bytes) } return FixedReader { bytes: bytes, cursor: start } } fn __zero_std_io_fixed_reader_cursor(reader: ref) -> usize { return reader.cursor } fn __zero_std_io_fixed_reader_len(reader: ref) -> usize { return std.mem.len(reader.bytes) } fn __zero_std_io_fixed_reader_remaining(reader: ref) -> usize { return __zero_std_io_remaining(reader.bytes, reader.cursor) } fn __zero_std_io_fixed_reader_done(reader: ref) -> Bool { return reader.cursor >= std.mem.len(reader.bytes) } fn __zero_std_io_fixed_reader_seek(reader: mutref, cursor: usize) -> Bool { if cursor > std.mem.len(reader.bytes) { return false } reader.cursor = cursor return true } fn __zero_std_io_fixed_reader_limit(reader: ref, count: usize) -> FixedReader { let bytes: Span = std.mem.cursorChunk(reader.bytes, reader.cursor, count) return FixedReader { bytes: bytes, cursor: 0 } } fn __zero_std_io_fixed_reader_read(reader: mutref, buffer: MutSpan) -> usize { let before: usize = reader.cursor let next: Maybe = __zero_std_io_read(reader.bytes, before, buffer) if next.has { reader.cursor = next.value return next.value - before } return 0 } fn __zero_std_io_fixed_reader_read_exact(reader: mutref, buffer: MutSpan) -> Bool { let next: Maybe = __zero_std_io_read_exact(reader.bytes, reader.cursor, buffer) if next.has { reader.cursor = next.value return true } return false } fn __zero_std_io_fixed_reader_read_at(reader: ref, offset: usize, buffer: MutSpan) -> Maybe { return __zero_std_io_read(reader.bytes, offset, buffer) } fn __zero_std_io_fixed_reader_read_byte(reader: mutref) -> Maybe { let byte: Maybe = __zero_std_io_read_byte(reader.bytes, reader.cursor) if byte.has { reader.cursor = reader.cursor + 1 } return byte } fn __zero_std_io_fixed_reader_read_line(reader: mutref) -> Maybe> { let line: Maybe> = __zero_std_io_next_line(reader.bytes, reader.cursor) if line.has { reader.cursor = __zero_std_io_next_line_start(reader.bytes, reader.cursor) } return line } fn __zero_std_io_fixed_reader_read_until_delimiter(reader: mutref, delimiter: u8) -> Maybe> { let chunk: Maybe> = __zero_std_io_read_until_delimiter(reader.bytes, reader.cursor, delimiter) if chunk.has { reader.cursor = __zero_std_io_read_until_delimiter_start(reader.bytes, reader.cursor, delimiter) } return chunk } fn __zero_std_io_fixed_reader_read_all(reader: mutref, buffer: MutSpan) -> Maybe> { let remaining: usize = __zero_std_io_fixed_reader_remaining(reader) if remaining > std.mem.len(buffer) { return null } let copied: Maybe = __zero_std_io_copy_n(buffer, reader.bytes[reader.cursor..], remaining) if copied.has { reader.cursor = std.mem.len(reader.bytes) return __zero_std_io_written(buffer, copied.value) } return null } fn __zero_std_io_fixed_writer(buffer: MutSpan, cursor: usize) -> FixedWriter { var start: usize = cursor if start > std.mem.len(buffer) { start = std.mem.len(buffer) } return FixedWriter { buffer: buffer, cursor: start } } fn __zero_std_io_fixed_writer_cursor(writer: ref) -> usize { return writer.cursor } fn __zero_std_io_fixed_writer_capacity(writer: ref) -> usize { return std.mem.len(writer.buffer) } fn __zero_std_io_fixed_writer_remaining(writer: ref) -> usize { return __zero_std_io_remaining(writer.buffer, writer.cursor) } fn __zero_std_io_fixed_writer_view(writer: ref) -> Span { return __zero_std_io_written(writer.buffer, writer.cursor) } fn __zero_std_io_fixed_writer_clear(writer: mutref) -> usize { writer.cursor = 0 return writer.cursor } fn __zero_std_io_fixed_writer_truncate(writer: mutref, len: usize) -> usize { if len < writer.cursor { writer.cursor = len } return writer.cursor } fn __zero_std_io_fixed_writer_seek(writer: mutref, cursor: usize) -> Bool { if cursor > std.mem.len(writer.buffer) { return false } writer.cursor = cursor return true } fn __zero_std_io_fixed_writer_write_byte(writer: mutref, byte: u8) -> Bool { let next: Maybe = __zero_std_io_write_byte(writer.buffer, writer.cursor, byte) if next.has { writer.cursor = next.value return true } return false } fn __zero_std_io_fixed_writer_write(writer: mutref, bytes: Span) -> Bool { let next: Maybe = __zero_std_io_write_all(writer.buffer, writer.cursor, bytes) if next.has { writer.cursor = next.value return true } return false } fn __zero_std_io_fixed_writer_write_all(writer: mutref, bytes: Span) -> Bool { return __zero_std_io_fixed_writer_write(writer, bytes) } fn __zero_std_io_fixed_writer_write_at(writer: mutref, offset: usize, bytes: Span) -> Bool { let next: Maybe = __zero_std_io_write_at(writer.buffer, offset, bytes) return next.has } fn __zero_std_io_copy_buffer(reader: mutref, writer: mutref, scratch: MutSpan) -> usize { if std.mem.len(scratch) == 0 { return 0 } var total: usize = 0 while true { let writable: usize = __zero_std_io_fixed_writer_remaining(writer) if writable == 0 { return total } var chunk_len: usize = std.mem.len(scratch) if writable < chunk_len { chunk_len = writable } let chunk_buffer: MutSpan = scratch[..chunk_len] let before: usize = reader.cursor let count: usize = __zero_std_io_fixed_reader_read(reader, chunk_buffer) if count == 0 { return total } let chunk: Span = __zero_std_io_written(chunk_buffer, count) if !__zero_std_io_fixed_writer_write_all(writer, chunk) { reader.cursor = before return total } total = total + count } return total } fn __zero_std_io_copy_reader_n(reader: mutref, writer: mutref, count: usize, scratch: MutSpan) -> Maybe { if count == 0 { return 0_usize } if std.mem.len(scratch) == 0 { return null } let reader_start: usize = reader.cursor let writer_start: usize = writer.cursor if count > __zero_std_io_fixed_reader_remaining(reader) { return null } if count > __zero_std_io_fixed_writer_remaining(writer) { return null } var copied: usize = 0 while copied < count { var chunk_len: usize = count - copied if chunk_len > std.mem.len(scratch) { chunk_len = std.mem.len(scratch) } let chunk_buffer: MutSpan = scratch[..chunk_len] if !__zero_std_io_fixed_reader_read_exact(reader, chunk_buffer) { reader.cursor = reader_start writer.cursor = writer_start return null } let chunk: Span = __zero_std_io_written(chunk_buffer, chunk_len) if !__zero_std_io_fixed_writer_write_all(writer, chunk) { reader.cursor = reader_start writer.cursor = writer_start return null } copied = copied + chunk_len } return copied } fn __zero_std_io_discard(reader: mutref, scratch: MutSpan) -> usize { if std.mem.len(scratch) == 0 { return 0 } var total: usize = 0 while true { let count: usize = __zero_std_io_fixed_reader_read(reader, scratch) if count == 0 { return total } total = total + count } return total } fn __zero_std_io_tee_read(reader: mutref, writer: mutref, buffer: MutSpan) -> Maybe { let before: usize = reader.cursor let count: usize = __zero_std_io_fixed_reader_read(reader, buffer) if count == 0 { return 0_usize } let chunk: Span = __zero_std_io_written(buffer, count) if __zero_std_io_fixed_writer_write_all(writer, chunk) { return count } reader.cursor = before return null } fn __zero_std_io_multi_read(first: mutref, second: mutref, buffer: MutSpan) -> usize { let first_count: usize = __zero_std_io_fixed_reader_read(first, buffer) if first_count >= std.mem.len(buffer) { return first_count } let second_count: usize = __zero_std_io_fixed_reader_read(second, buffer[first_count..]) return first_count + second_count } fn __zero_std_io_error_none() -> u32 { return 0_u32 } fn __zero_std_io_error_eof() -> u32 { return 1_u32 } fn __zero_std_io_error_short_read() -> u32 { return 2_u32 } fn __zero_std_io_error_short_write() -> u32 { return 3_u32 } fn __zero_std_io_error_capacity() -> u32 { return 4_u32 } fn __zero_std_io_error_permission() -> u32 { return 5_u32 } fn __zero_std_io_error_timeout() -> u32 { return 6_u32 } fn __zero_std_io_error_io() -> u32 { return 7_u32 } fn __zero_std_io_error_name(code: u32) -> String { if code == 0_u32 { return "none" } if code == 1_u32 { return "eof" } if code == 2_u32 { return "short_read" } if code == 3_u32 { return "short_write" } if code == 4_u32 { return "capacity" } if code == 5_u32 { return "permission" } if code == 6_u32 { return "timeout" } if code == 7_u32 { return "io" } return "unknown" } fn __zero_std_io_count_lines(bytes: Span) -> usize { var start: usize = 0 var count: usize = 0 while start < std.mem.len(bytes) { count = count + 1 start = __zero_std_io_next_line_start(bytes, start) } return count }