Io module

read_line

procedureextensionexperimentalSource

The next line without its line ending (\n or \r\n), or the error that ended the reading: .END_OF_FILE once the source has ended and every line was handed out, .INVALID for a line longer than the limit, or whatever the source's read reported. After an error, stop reading and free the reader.

When the source ends in the middle of a line, that last piece is returned once, with reader.unterminated set (it is false after every line that had an ending). A protocol that acts on whole lines should drop it: the peer may have been cut off.

The line points into the reader's buffer and is valid until the next call. Lines already buffered are returned without reading, so a request pipelined behind another costs no wait.

Example↗
Io :: #import "Extensions/Io";

Io.io_run(() {
    near, far := Io.make_pipe();
    defer Io.close(near);
    defer Io.close(far);
    Io.write(Io.writer_of(far), "GET a\nGET b\nlast");
    Io.shutdown_write(far);

    lines := Io.make_line_reader(Io.reader_of(near));
    defer Io.free_line_reader(*lines);

    whole := 0;

    while true {
        line, error := Io.read_line(*lines);
        if error != .NONE break;
        if lines.unterminated break; // "last" was cut off: do not act on it
        whole += 1;
    }

    assert(whole == 2);
});
read_line :: (reader: *Line_Reader) -> (string, Io_Error)

Parameters

reader: *Line_Reader

Returns

string

See also