runeio
is a Go package that provides basic interfaces to I/O primitives for
runes. Its primary job is to wrap and supplement existing implementations of
such primitives, such as those in package io
.
Because these interfaces and primitives wrap lower-level operations with various implementations, unless otherwise informed clients should not assume they are safe for parallel execution.
The package provides a buffered RuneReader
that implements the
io.RuneScanner
interface. Its primary
purpose is to buffer the reading of runes and allow users to Peek
ahead by a
number of runes rather than a number of bytes as with the bufio.Reader
.
A single rune can be read with ReadRune
.
r := runeio.NewReader(strings.NewReader("Hello World!"))
var runes []rune
for {
rn, _, err := r.ReadRune()
if err != nil {
break
}
runes = append(runes, rn)
}
fmt.Print(string(runes))
// Output: Hello World!
Read
can be used to read runes into a buffer.
r := runeio.NewReader(strings.NewReader("Hello World!"))
buf := make([]rune, 5)
_, _ = r.Read(buf)
fmt.Print(string(buf))
// Output: Hello
Peek
can be used to look ahead into the stream without consuming the runes.
r := runeio.NewReader(strings.NewReader("Hello World!"))
buf := make([]rune, 6)
_, _ = r.Read(buf)
peeked, _ := r.Peek(6)
fmt.Print(string(peeked))
// Output: World!
- pelletier/go-buffruneio: Another
buffering implementation of
io.RuneScanner
.- Uses a custom
badRune
value for encoding errors instead ofunicode.ReplacementChar
- Uses a custom
EOF
rune value instead of just returningio.EOF
. - The undo buffer grows unbounded until
Forget
is called. NewReader
also takes aio.Reader
which is wrapped in abufio.Reader
when it could just take anio.RuneReader
since it the implementation only requiresReadRune
from the underlying reader.
- Uses a custom
- SteelSeries/bufrr: Another buffering
implementation of
io.RuneScanner
.- Implements only a static single rune buffer. This means that only one rune could be peeked at.
See CONTRIBUTING.md