readerutil

package
v0.0.0-...-2c87074 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: Unlicense Imports: 9 Imported by: 1

Documentation

Overview

Package readerutil implements io.Reader utility functions.

Index

Examples

Constants

View Source
const (
	// Whitespace characters include space, tab, carriage return and newline.
	Whitespace = " \t\r\n"
)

Variables

View Source
var (
	ErrUnknownEncoding = errors.New("readerutil.NewLineReader: unknown encoding.")
)

Error values.

Functions

func IsUTF16

func IsUTF16(r io.ReadSeeker, order binary.ByteOrder) (ok bool, err error)

IsUTF16 decodes a chunk of data as UTF-16 with the specified byte order and returns true if a valid BOM byte sequence was located or no decoding errors occured and at least 75% of the decoded runes are graphic or space and within the ASCII range.

func IsUTF8

func IsUTF8(r io.ReadSeeker) (ok bool, err error)

IsUTF8 decodes a chunk of data as UTF-8 and returns true if no decoding errors occured and at least 75% of the decoded runes are graphic or space and within the ASCII range.

func NewByteReader

func NewByteReader(r io.Reader) io.ByteReader

NewByteReader returns a new io.ByteReader based on the provided io.Reader.

func NewFilter

func NewFilter(r io.Reader, charset string) io.Reader

NewFilter returns an io.Reader which discards characters from charset when reading from r.

Example
r := strings.NewReader("foo,bar;baz")
f := NewFilter(r, ",;")
io.Copy(os.Stdout, f)
Output:

foobarbaz

func NewSpaceFilter

func NewSpaceFilter(r io.Reader) io.Reader

NewSpaceFilter returns an io.Reader which discards whitespace characters when reading from r.

Example
r := strings.NewReader("foo bar\nbaz")
f := NewSpaceFilter(r)
io.Copy(os.Stdout, f)
Output:

foobarbaz

func ReadByte

func ReadByte(r io.Reader) (c byte, err error)

ReadByte reads and returns the next byte from the provided io.Reader.

func Size

func Size(r io.Seeker) (n int64, err error)

Size returns the total size in bytes of the provided io.Seeker. The original position is preserved.

Types

type BinaryPeeker

type BinaryPeeker interface {
	io.ReadSeeker
	Peek(data interface{}) (err error)
}

A BinaryPeeker is an io.ReadSeeker that can also peek ahead.

Peek reads structured binary data without advancing the reader. Data must be a pointer to a fixed-size value or a slice of fixed-size values. Bytes read from r are decoded using the receiver's byte order and written to successive fields of the data. When reading into structs, the field data for fields with blank (_) field names is skipped; i.e., blank field names may be used for padding.

func NewBinaryPeeker

func NewBinaryPeeker(r io.ReadSeeker, order binary.ByteOrder) BinaryPeeker

NewBinaryPeeker returns a new BinaryPeeker based on the provided io.ReadSeeker.

type LineReader

type LineReader interface {
	ReadLine() (line string, err error)
}

LineReader implements the ReadLine method.

ReadLine returns a single line, not including the end-of-line bytes. ReadLine either returns a valid line (can be empty) or it returns an error, never both.

func NewLineReader

func NewLineReader(r io.ReadSeeker) (lr LineReader, err error)

NewLineReader returns a LineReader that reads from r. If able to determine the encoding it decodes each line to UTF-8.

type Peeker

type Peeker interface {
	io.ReadSeeker
	Peek(n int) (buf []byte, err error)
}

A Peeker is an io.ReadSeeker that can also peek ahead.

Peek returns the next n bytes without advancing the reader. The error is EOF only if no bytes were read. If an EOF happens after reading some but not all the bytes, ReadFull returns ErrUnexpectedEOF.

func NewPeeker

func NewPeeker(r io.ReadSeeker) Peeker

NewPeeker returns a new Peeker based on the provided io.ReadSeeker.

Jump to

Keyboard shortcuts

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