Documentation
¶
Overview ¶
Package bufioreader provides a wrapper around bufio.Reader with additional iteration capabilities.
Example ¶
package main
import (
"errors"
"fmt"
"io"
"strings"
"testing/iotest"
"github.com/goaux/iter/bufioreader"
)
func main() {
r := bufioreader.NewReader(io.MultiReader(
strings.NewReader("hello\nworld\nexample"),
iotest.ErrReader(errors.New("io error?")), // simulate some error
))
for i, s := range r.ReadString('\n') { // ReadString returns an iterator
fmt.Printf("[%d] %q\n", i, s)
}
if err := r.Err(); err != nil {
fmt.Printf("error: %v (remain:%q)\n", err, bufioreader.GetErrorBufferString(err))
}
}
Output: [0] "hello\n" [1] "world\n" error: io error? (remain:"example")
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetErrorBuffer ¶
GetErrorBuffer attempts to extract the byte buffer from an error. If the error is of type *Error, it returns the buffer calling Error.Buffer. Otherwise, it returns nil.
see Reader.Err.
func GetErrorBufferString ¶
GetErrorBufferString attempts to extract the byte buffer from an error and return it as a string. If the error is of type *Error, it returns the buffer as a string calling Error.BufferString. Otherwise, it returns an empty string.
see Reader.Err.
Types ¶
type Error ¶
type Error struct {
// Err holds the underlying error.
Err error
// Buf holds the buffer.
Buf []byte
}
Error is a custom error type that includes a buffer of bytes along with the error.
func (*Error) BufferString ¶
BufferString returns the byte buffer as a string.
type Reader ¶
Reader wraps a bufio.Reader and provides methods to iterate over its content.
func NewReaderSize ¶
NewReaderSize creates a new Reader with a buffer size.
func (*Reader) Err ¶
Err returns the last error. It returns nil if the last error is io.EOF.
bufio.Reader.ReadBytes, bufio.Reader.ReadSlice and bufio.Reader.ReadString returns the data read before the error. This data would not be passed to loop body. Instead, it can be retrieved by GetErrorBuffer or GetErrorBufferString.
In normal, error-free situations, bufio.Reader.ReadBytes, bufio.Reader.ReadSlice, and bufio.Reader.ReadString will always return io.EOF on the final call. Since this is not an error that indicates an abnormality, Err() will return nil.
In the loop body, one way to know that io.EOF has been returned is to check whether the delimiter is included in the result.
func (*Reader) ReadBytes ¶
ReadBytes returns an iterator that yields byte slices delimited by the given byte using bufio.Reader.ReadBytes.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/goaux/iter/bufioreader"
)
func main() {
r := bufioreader.NewReader(strings.NewReader("hello\nworld"))
for i, b := range r.ReadBytes('\n') {
fmt.Printf("[%d] % 02x\n", i, b)
}
if err := r.Err(); err != nil { // r.Err never returns io.EOF
fmt.Printf("error: %v (remain:%q)\n", err, bufioreader.GetErrorBufferString(err))
}
}
Output: [0] 68 65 6c 6c 6f 0a [1] 77 6f 72 6c 64
func (*Reader) ReadSlice ¶
ReadSlice returns an iterator that yields byte slices delimited by the given byte using bufio.Reader.ReadSlice.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/goaux/iter/bufioreader"
)
func main() {
r := bufioreader.NewReader(strings.NewReader("hello\nworld"))
for i, b := range r.ReadSlice('\n') {
fmt.Printf("[%d] % 02x\n", i, b)
}
if err := r.Err(); err != nil { // r.Err never returns io.EOF
fmt.Printf("error: %v (remain:%q)\n", err, bufioreader.GetErrorBufferString(err))
}
}
Output: [0] 68 65 6c 6c 6f 0a [1] 77 6f 72 6c 64
func (*Reader) ReadString ¶
ReadString returns an iterator that yields strings delimited by the given byte using bufio.Reader.ReadString.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/goaux/iter/bufioreader"
)
func main() {
r := bufioreader.NewReaderSize(strings.NewReader("hello\nworld"), 32)
for i, s := range r.ReadString('\n') {
fmt.Printf("[%d] %q\n", i, s)
}
if err := r.Err(); err != nil { // r.Err never returns io.EOF
fmt.Printf("error: %v (remain:%q)\n", err, bufioreader.GetErrorBufferString(err))
}
}
Output: [0] "hello\n" [1] "world"