bufioreader

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

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

func GetErrorBuffer(err error) []byte

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

func GetErrorBufferString(err error) string

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.

func NewError

func NewError(err error, buf []byte) error

NewError creates a new Error instance. If the buffer is empty, it returns the original error. Otherwise, it returns a new Error with the provided error and buffer.

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) Buffer

func (err *Error) Buffer() []byte

Buffer returns the byte buffer associated with the error.

func (*Error) BufferString

func (err *Error) BufferString() string

BufferString returns the byte buffer as a string.

func (*Error) Error

func (err *Error) Error() string

Error returns the error message of the underlying error.

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap returns the underlying error.

type Reader

type Reader struct {
	*bufio.Reader
	// contains filtered or unexported fields
}

Reader wraps a bufio.Reader and provides methods to iterate over its content.

func New

func New(b *bufio.Reader) *Reader

NewReader creates a new Reader wraps a bufio.Reader.

func NewReader

func NewReader(rd io.Reader) *Reader

NewReader creates a new Reader with a default buffer size.

func NewReaderSize

func NewReaderSize(rd io.Reader, size int) *Reader

NewReaderSize creates a new Reader with a buffer size.

func (*Reader) Err

func (r *Reader) Err() error

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

func (r *Reader) ReadBytes(delim byte) iter.Seq2[int, []byte]

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

func (r *Reader) ReadSlice(delim byte) iter.Seq2[int, []byte]

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

func (r *Reader) ReadString(delim byte) iter.Seq2[int, string]

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"

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL