const MODE_LINES
pub const MODE_LINES = 0Purpose
Constant MODE_LINES, defining protocol or algorithm parameters.
#bufioUse bufio to read buffered lines from an owned in-memory io.Buffer.
The buffer moves into the reader. read_line removes LF or CRLF, returns the final unterminated line, then throws EOF when no data remains; other errors exit with status 1.
Save the complete program as main.xu and run xu run main.xu locally.
@main
#bufio
#io
fn main() int {
$reader := bufio.new_reader(io.buffer_from("first\nlast"))
while true {
line := reader.read_line() >>> (err) {
if err.msg() == "EOF" { << 0 }
println(err.msg())
<< 1
}
println(line)
}
<< 0
}Expected output: first, then last.
const MODE_LINESpub const MODE_LINES = 0Constant MODE_LINES, defining protocol or algorithm parameters.
const MODE_WORDSpub const MODE_WORDS = 1Constant MODE_WORDS, defining protocol or algorithm parameters.
const MODE_BYTESpub const MODE_BYTES = 2Constant MODE_BYTES, defining protocol or algorithm parameters.
const MODE_FUNCpub const MODE_FUNC = 3Constant MODE_FUNC, defining protocol or algorithm parameters.
struct Readerpub struct Reader {
pub src io.Buffer
pub buf string
pub r int
pub err string
pub size int
pub last_byte int
pub last_rune int
}Bufio Streaming reader and decoder.
src io.Bufferbuf stringr interr stringsize intlast_byte intlast_rune intstruct Writerpub struct Writer {
pub dst io.Buffer
pub buf string
pub size int
pub err string
}Bufio Streaming writer and encoder.
dst io.Bufferbuf stringsize interr stringnew_readerpub fn new_reader(src io.Buffer) ReaderConstruct reader.
src io.BufferReader — a value of this type
new_reader_sizepub fn new_reader_size(src io.Buffer, size int) ReaderConstruct reader size.
src io.Buffersize intReader — a value of this type
new_writerpub fn new_writer(dst io.Buffer) WriterConstruct writer.
dst io.BufferWriter — a value of this type
new_writer_sizepub fn new_writer_size(dst io.Buffer, size int) WriterConstruct writer size.
dst io.Buffersize intWriter — a value of this type
struct RuneReadpub struct RuneRead {
pub ch int
pub size int
}read_byte returns the next byte, or throws at end of input.
ch intsize int($&Reader) read_runepub fn (r $&Reader) read_rune() RuneReadOperation on $&Reader: read one UTF-8 rune. Throws on failure; handle the error or propagate it to the caller.
r $&ReaderReader instance used by this call (exclusive borrow).RuneRead — a value of this type
($&Reader) read_bytepub fn (r $&Reader) read_byte() intOperation on $&Reader: read one byte; throws on failure. Throws on failure; handle the error or propagate it to the caller.
r $&ReaderReader instance used by this call (exclusive borrow).int — a value of this type
($&Reader) read_stringpub fn (r $&Reader) read_string(delim int) stringOperation on $&Reader: read string.
r $&ReaderReader instance used by this call (exclusive borrow).delim intstring — result string
($&Reader) read_linepub fn (r $&Reader) read_line() stringread_line reads until '\n' or eof and strips the trailing '\n' (and a preceding '\r', i.e. CRLF) — a simplified ReadLine.
r $&ReaderReader instance used by this call (exclusive borrow).string — result string
($&Reader) peekpub fn (r $&Reader) peek(n int) stringpeek returns the next n bytes without consuming them. n must be in 0..size; n > size throws ( bufio.ErrBufferFull). At eof with fewer than n bytes buffered it returns what is available (or throws). Throws on failure; handle the error or propagate it to the caller.
r $&ReaderReader instance used by this call (exclusive borrow).n intstring — result string
($&Writer) flushpub fn (w $&Writer) flush() intOperation on $&Writer: flush buffered data. Throws on failure; handle the error or propagate it to the caller.
w $&WriterWriter instance used by this call (exclusive borrow).int — a value of this type
($&Writer) write_stringpub fn (w $&Writer) write_string(s string) intRetain at most size bytes. Preserve the existing auto-flush-on-full contract. Errors are sticky until reset; never silently claim an unsuccessful write. Throws on failure; handle the error or propagate it to the caller.
w $&WriterWriter instance used by this call (exclusive borrow).s stringint — bytes written
($&Writer) bytespub fn (w $&Writer) bytes() stringbytes returns the sink's written bytes (for round-trip tests); it does not consume the sink's read position.
w $&WriterWriter instance used by this call (exclusive borrow).string — result string
($&Reader) bufferedpub fn (r $&Reader) buffered() intExecute buffered on $&Reader.
r $&ReaderReader instance used by this call (exclusive borrow).int — a value of this type
($&Reader) discardpub fn (r $&Reader) discard(n int) intExecute discard on $&Reader. Throws on failure; handle the error or propagate it to the caller.
r $&ReaderReader instance used by this call (exclusive borrow).n intint — a value of this type
($&Reader) read_bytespub fn (r $&Reader) read_bytes(delim int) stringOperation on $&Reader: read through delim (inclusive).
r $&ReaderReader instance used by this call (exclusive borrow).delim intstring — result string
($&Reader) unread_bytepub fn (r $&Reader) unread_byte() intunread_byte puts back the last byte from a successful non-peek read ( UnreadByte). Peek/discard/error invalidate it; twice in a row throws. Throws on failure; handle the error or propagate it to the caller.
r $&ReaderReader instance used by this call (exclusive borrow).int — a value of this type
($&Reader) unread_runepub fn (r $&Reader) unread_rune() intOperation on $&Reader: unread the last rune. Throws on failure; handle the error or propagate it to the caller.
r $&ReaderReader instance used by this call (exclusive borrow).int — a value of this type
($&Reader) read_slicepub fn (r $&Reader) read_slice(delim int) stringread_slice reads until delim (inclusive) in the current buffer ( ReadSlice). It does not accumulate across a full buffer: if the buffer fills with no delim, it returns that chunk ( also returns ErrBufferFull Xu returns the bytes and does not throw — caller sees no delim). eof with leftover returns the leftover; empty eof throws. Throws on failure; handle the error or propagate it to the caller.
r $&ReaderReader instance used by this call (exclusive borrow).delim intstring — result string
($&Reader) resetpub fn (r $&Reader) reset(src io.Buffer)Operation on $&Reader: clear internal state so the value can be reused.
r $&ReaderReader instance used by this call (exclusive borrow).src io.BufferThis call does not return a value.
($&Writer) write_bytepub fn (w $&Writer) write_byte(c int) intOperation on $&Writer: write one byte. Throws on failure; handle the error or propagate it to the caller.
w $&WriterWriter instance used by this call (exclusive borrow).c intint — bytes written
($&Writer) write_runepub fn (w $&Writer) write_rune(r int) intOperation on $&Writer: write a rune as UTF-8. Throws on failure; handle the error or propagate it to the caller.
w $&WriterWriter instance used by this call (exclusive borrow).r intint — bytes written
($&Writer) bufferedpub fn (w $&Writer) buffered() intExecute buffered on $&Writer.
w $&WriterWriter instance used by this call (exclusive borrow).int — a value of this type
(&Reader) sizepub fn (r &Reader) size() intOperation on &Reader: return the size.
r &ReaderReader instance used by this call (borrow).int — a value of this type
(&Writer) sizepub fn (w &Writer) size() intOperation on &Writer: return the size.
w &WriterWriter instance used by this call (borrow).int — a value of this type
(&Writer) availablepub fn (w &Writer) available() intExecute available on &Writer.
w &WriterWriter instance used by this call (borrow).int — a value of this type
($&Writer) resetpub fn (w $&Writer) reset(dst io.Buffer)reset discards unflushed bytes and points at a new sink ( Writer.Reset).
w $&WriterWriter instance used by this call (exclusive borrow).dst io.BufferThis call does not return a value.
struct SplitResultpub struct SplitResult {
pub advance int
pub token string
pub err string
}SplitResult mirrors SplitFunc's (advance, token, err).
advance inttoken stringerr stringsplit_okpub fn split_ok(advance int, token string) SplitResultSplit ok.
advance inttoken stringSplitResult — a value of this type
split_failpub fn split_fail(msg string) SplitResultSplit fail.
msg stringSplitResult — a value of this type
struct Scannerpub struct Scanner {
pub data string
pub pos int
pub token string
pub err string
pub mode int
pub has_split int
pub split_f fn(string, int) SplitResult
}Structure encapsulating data, pos, token, err, mode.
data stringpos inttoken stringerr stringmode inthas_split intsplit_f fn(string, int) SplitResultnew_scannerpub fn new_scanner(src io.Buffer) ScannerConstruct scanner.
src io.BufferScanner — a value of this type
($&Scanner) splitpub fn (s $&Scanner) split(mode int)Operation on $&Scanner: split on the separator.
s $&ScannerScanner instance used by this call (exclusive borrow).mode intThis call does not return a value.
($&Scanner) split_funcpub fn (s $&Scanner) split_func(f fn(string, int) SplitResult)Operation on $&Scanner: split callback.
s $&ScannerScanner instance used by this call (exclusive borrow).f fn(string, int) SplitResultfn(string, int) SplitResult; pass the function value for this operation to invoke.This call does not return a value.
scan_linespub fn scan_lines(data string, at_eof int) SplitResultPublic SplitFunc-shaped helpers ( ScanLines / ScanWords / ScanBytes).
data stringat_eof intSplitResult — a value of this type
scan_wordspub fn scan_words(data string, at_eof int) SplitResultSplit function for words, splitting at whitespace runs.
data stringat_eof intSplitResult — a value of this type
scan_bytespub fn scan_bytes(data string, at_eof int) SplitResultSplit function for bytes, advancing one byte per token.
data stringat_eof intSplitResult — a value of this type
($&Scanner) scanpub fn (s $&Scanner) scan() boolExecute scan on $&Scanner.
s $&ScannerScanner instance used by this call (exclusive borrow).bool — true or false
($&Scanner) textpub fn (s $&Scanner) text() stringOperation on $&Scanner: return the current text.
s $&ScannerScanner instance used by this call (exclusive borrow).string — result string
($&Scanner) err_textpub fn (s $&Scanner) err_text() stringOperation on $&Scanner: err text.
s $&ScannerScanner instance used by this call (exclusive borrow).string — result string
struct ResultReader#R:ProgressReaderpub struct ResultReader#R:ProgressReader {
pub src R
}Pending owns the terminal error until its accompanying bytes are drained. map.take transfers the enum without partially moving an owner field.
src Rstruct ResultWriter#W:ProgressWriterpub struct ResultWriter#W:ProgressWriter {
pub dst W
}Bufio Streaming writer and encoder.
dst Wnew_result_readerpub fn new_result_reader#R:ProgressReader(src R) ResultReader#RConstruct result reader.
src RResultReader#R — a value of this type
new_result_reader_sizepub fn new_result_reader_size#R:ProgressReader(src R, size int) ResultReader#RConstruct result reader size.
src Rsize intResultReader#R — a value of this type
new_result_writerpub fn new_result_writer#W:ProgressWriter(dst W) ResultWriter#WConstruct result writer.
dst WResultWriter#W — a value of this type
new_result_writer_sizepub fn new_result_writer_size#W:ProgressWriter(dst W, size int) ResultWriter#WConstruct result writer size.
dst Wsize intResultWriter#W — a value of this type
($&ResultReader#R) read_resultpub fn (r $&ResultReader#R) read_result(n int) io.ReadResultOperation on $&ResultReader#R: read result.
r $&ResultReader#RResultReader#R instance used by this call (exclusive borrow).n intio.ReadResult — a value of this type
(&ResultReader#R) bufferedpub fn (r &ResultReader#R) buffered() intExecute buffered on &ResultReader#R.
r &ResultReader#RResultReader#R instance used by this call (borrow).int — a value of this type
($&ResultReader#R) resetpub fn (r $&ResultReader#R) reset(src R)Operation on $&ResultReader#R: clear internal state so the value can be reused.
r $&ResultReader#RResultReader#R instance used by this call (exclusive borrow).src RThis call does not return a value.
($&ResultReader#R) read_npub fn (r $&ResultReader#R) read_n(n int) stringA legacy read defers terminal errors until the returned bytes are consumed. The first structured failure transfers its typed error to the caller subsequent sticky failures recreate its message, not its dynamic type. Throws on failure; handle the error or propagate it to the caller.
r $&ResultReader#RResultReader#R instance used by this call (exclusive borrow).n intstring — result string
($&ResultWriter#W) flush_resultpub fn (w $&ResultWriter#W) flush_result() io.WriteResultOperation on $&ResultWriter#W: flush result.
w $&ResultWriter#WResultWriter#W instance used by this call (exclusive borrow).io.WriteResult — a value of this type
($&ResultWriter#W) write_resultpub fn (w $&ResultWriter#W) write_result(data string) io.WriteResultProgress means bytes accepted by this buffer, including queued bytes. flush_result instead reports the underlying destination's progress.
w $&ResultWriter#WResultWriter#W instance used by this call (exclusive borrow).data stringio.WriteResult — a value of this type
(&ResultWriter#W) bufferedpub fn (w &ResultWriter#W) buffered() intExecute buffered on &ResultWriter#W.
w &ResultWriter#WResultWriter#W instance used by this call (borrow).int — a value of this type
(&ResultWriter#W) pendingpub fn (w &ResultWriter#W) pending() stringExecute pending on &ResultWriter#W.
w &ResultWriter#WResultWriter#W instance used by this call (borrow).string — result string
($&ResultWriter#W) resetpub fn (w $&ResultWriter#W) reset(dst W)Operation on $&ResultWriter#W: clear internal state so the value can be reused.
w $&ResultWriter#WResultWriter#W instance used by this call (exclusive borrow).dst WThis call does not return a value.
($&ResultWriter#W) flushpub fn (w $&ResultWriter#W) flush() intOperation on $&ResultWriter#W: flush buffered data. Throws on failure; handle the error or propagate it to the caller.
w $&ResultWriter#WResultWriter#W instance used by this call (exclusive borrow).int — a value of this type
($&ResultWriter#W) write_strpub fn (w $&ResultWriter#W) write_str(data string) intOperation on $&ResultWriter#W: write a string. Throws on failure; handle the error or propagate it to the caller.
w $&ResultWriter#WResultWriter#W instance used by this call (exclusive borrow).data stringint — a value of this type
struct BufferedReader#R:StreamReaderpub struct BufferedReader#R:StreamReader {
pub src R
pub buf string
pub r int
pub err string
pub size int
pub last_byte int
pub last_rune int
}Owning, concrete endpoint wrappers. Methods borrow the endpoint field every read/write preserves its position and propagates its errors. This is chunked string I/O, not borrowed/shared byte-buffer I/O.
src Rbuf stringr interr stringsize intlast_byte intlast_rune intstruct BufferedWriter#W:StreamWriterpub struct BufferedWriter#W:StreamWriter {
pub dst W
pub buf string
pub size int
pub err string
}Bufio Streaming writer and encoder.
dst Wbuf stringsize interr stringnew_stream_readerpub fn new_stream_reader#R:StreamReader(src R) BufferedReader#RConstruct stream reader.
src RBufferedReader#R — a value of this type
new_stream_reader_sizepub fn new_stream_reader_size#R:StreamReader(src R, size int) BufferedReader#RConstruct stream reader size.
src Rsize intBufferedReader#R — a value of this type
new_stream_writerpub fn new_stream_writer#W:StreamWriter(dst W) BufferedWriter#WConstruct stream writer.
dst WBufferedWriter#W — a value of this type
new_stream_writer_sizepub fn new_stream_writer_size#W:StreamWriter(dst W, size int) BufferedWriter#WConstruct stream writer size.
dst Wsize intBufferedWriter#W — a value of this type
($&BufferedReader#R) read_runepub fn (r $&BufferedReader#R) read_rune() RuneReadOperation on $&BufferedReader#R: read one UTF-8 rune. Throws on failure; handle the error or propagate it to the caller.
r $&BufferedReader#RBufferedReader#R instance used by this call (exclusive borrow).RuneRead — a value of this type
($&BufferedReader#R) read_bytepub fn (r $&BufferedReader#R) read_byte() intOperation on $&BufferedReader#R: read one byte; throws on failure. Throws on failure; handle the error or propagate it to the caller.
r $&BufferedReader#RBufferedReader#R instance used by this call (exclusive borrow).int — a value of this type
($&BufferedReader#R) read_stringpub fn (r $&BufferedReader#R) read_string(delim int) stringOperation on $&BufferedReader#R: read string.
r $&BufferedReader#RBufferedReader#R instance used by this call (exclusive borrow).delim intstring — result string
($&BufferedReader#R) read_linepub fn (r $&BufferedReader#R) read_line() stringOperation on $&BufferedReader#R: read line.
r $&BufferedReader#RBufferedReader#R instance used by this call (exclusive borrow).string — result string
($&BufferedReader#R) peekpub fn (r $&BufferedReader#R) peek(n int) stringExecute peek on $&BufferedReader#R. Throws on failure; handle the error or propagate it to the caller.
r $&BufferedReader#RBufferedReader#R instance used by this call (exclusive borrow).n intstring — result string
($&BufferedWriter#W) flushpub fn (w $&BufferedWriter#W) flush() intOperation on $&BufferedWriter#W: flush buffered data. Throws on failure; handle the error or propagate it to the caller.
w $&BufferedWriter#WBufferedWriter#W instance used by this call (exclusive borrow).int — a value of this type
($&BufferedWriter#W) write_stringpub fn (w $&BufferedWriter#W) write_string(s string) intOperation on $&BufferedWriter#W: write a string and return the byte count. Throws on failure; handle the error or propagate it to the caller.
w $&BufferedWriter#WBufferedWriter#W instance used by this call (exclusive borrow).s stringint — bytes written
($&BufferedReader#R) bufferedpub fn (r $&BufferedReader#R) buffered() intExecute buffered on $&BufferedReader#R.
r $&BufferedReader#RBufferedReader#R instance used by this call (exclusive borrow).int — a value of this type
($&BufferedReader#R) discardpub fn (r $&BufferedReader#R) discard(n int) intExecute discard on $&BufferedReader#R. Throws on failure; handle the error or propagate it to the caller.
r $&BufferedReader#RBufferedReader#R instance used by this call (exclusive borrow).n intint — a value of this type
($&BufferedReader#R) read_bytespub fn (r $&BufferedReader#R) read_bytes(delim int) stringOperation on $&BufferedReader#R: read through delim (inclusive).
r $&BufferedReader#RBufferedReader#R instance used by this call (exclusive borrow).delim intstring — result string
($&BufferedReader#R) unread_bytepub fn (r $&BufferedReader#R) unread_byte() intOperation on $&BufferedReader#R: unread the last byte. Throws on failure; handle the error or propagate it to the caller.
r $&BufferedReader#RBufferedReader#R instance used by this call (exclusive borrow).int — a value of this type
($&BufferedReader#R) unread_runepub fn (r $&BufferedReader#R) unread_rune() intOperation on $&BufferedReader#R: unread the last rune. Throws on failure; handle the error or propagate it to the caller.
r $&BufferedReader#RBufferedReader#R instance used by this call (exclusive borrow).int — a value of this type
($&BufferedReader#R) read_slicepub fn (r $&BufferedReader#R) read_slice(delim int) stringOperation on $&BufferedReader#R: read slice. Throws on failure; handle the error or propagate it to the caller.
r $&BufferedReader#RBufferedReader#R instance used by this call (exclusive borrow).delim intstring — result string
($&BufferedReader#R) resetpub fn (r $&BufferedReader#R) reset(src R)Operation on $&BufferedReader#R: clear internal state so the value can be reused.
r $&BufferedReader#RBufferedReader#R instance used by this call (exclusive borrow).src RThis call does not return a value.
($&BufferedWriter#W) write_bytepub fn (w $&BufferedWriter#W) write_byte(c int) intOperation on $&BufferedWriter#W: write one byte. Throws on failure; handle the error or propagate it to the caller.
w $&BufferedWriter#WBufferedWriter#W instance used by this call (exclusive borrow).c intint — bytes written
($&BufferedWriter#W) write_runepub fn (w $&BufferedWriter#W) write_rune(r int) intOperation on $&BufferedWriter#W: write a rune as UTF-8. Throws on failure; handle the error or propagate it to the caller.
w $&BufferedWriter#WBufferedWriter#W instance used by this call (exclusive borrow).r intint — bytes written
($&BufferedWriter#W) bufferedpub fn (w $&BufferedWriter#W) buffered() intExecute buffered on $&BufferedWriter#W.
w $&BufferedWriter#WBufferedWriter#W instance used by this call (exclusive borrow).int — a value of this type
(&BufferedReader#R) sizepub fn (r &BufferedReader#R) size() intOperation on &BufferedReader#R: return the size.
r &BufferedReader#RBufferedReader#R instance used by this call (borrow).int — a value of this type
(&BufferedWriter#W) sizepub fn (w &BufferedWriter#W) size() intOperation on &BufferedWriter#W: return the size.
w &BufferedWriter#WBufferedWriter#W instance used by this call (borrow).int — a value of this type
(&BufferedWriter#W) availablepub fn (w &BufferedWriter#W) available() intExecute available on &BufferedWriter#W.
w &BufferedWriter#WBufferedWriter#W instance used by this call (borrow).int — a value of this type
($&BufferedWriter#W) resetpub fn (w $&BufferedWriter#W) reset(dst W)Operation on $&BufferedWriter#W: clear internal state so the value can be reused.
w $&BufferedWriter#WBufferedWriter#W instance used by this call (exclusive borrow).dst WThis call does not return a value.
($&BufferedReader#R) read_npub fn (r $&BufferedReader#R) read_n(n int) stringA zero-size read does not access the source and invalidates unread state. Return buffered bytes first; a deferred source error is reported once those bytes are consumed. A negative size is invalid. Returned chunks are owned. Throws on failure; handle the error or propagate it to the caller.
r $&BufferedReader#RBufferedReader#R instance used by this call (exclusive borrow).n intstring — result string
($&BufferedWriter#W) write_strpub fn (w $&BufferedWriter#W) write_str(data string) intOperation on $&BufferedWriter#W: write a string. Throws on failure; handle the error or propagate it to the caller.
w $&BufferedWriter#WBufferedWriter#W instance used by this call (exclusive borrow).data stringint — a value of this type