usv

package module
v0.0.0-...-4851b6b Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2017 License: BSD-3-Clause Imports: 2 Imported by: 0

README

NOTICE: do not use this

Recent versions of Go (>=1.9) now have (encoding/csv.Reader).ReuseRecord which causes far fewer allocations. This makes USV less of a win than it used to be (maybe 1-2x, rather than 5x), and I would advise using the vanilla reader if possible.

USV: A parser for Unit Separator separated values

An efficient (zero-alloc) parser for CSV-like data.

Why use USV? Well, ASCII has a unit separator, which is unlikely to appear in your data. If you can avoid having fields which need quoting, you can parse it much more efficiently (typically 5x or more).

This parser can also be used with TSV, so long as the tab does not appear in the data, for example:

r := usv.NewReader(os.Stdin)
r.RecordSeparator = '\n'
r.UnitSeparator = '\t'

for {
	row, err := r.Read()
	if err == io.EOF {
		return out, nil
	}
	if err != nil {
		return nil, err
	}
    // This prints bytes.
    log.Println(row)
}

License

BSD 3-clause. See LICENSE.

Documentation

Index

Constants

View Source
const (
	RecordSeparator byte = 0x1e
	UnitSeparator   byte = 0x1f
)

RecordSeparator as defined in ASCII

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	RecordSeparator, UnitSeparator byte
	// contains filtered or unexported fields
}

Reader implements a unit-separator separated values reader. It works for any data where the field separator and the record separator cannot appear in the data. It avoids allocations for performance. It's around 6x faster than the builtin CSV reader, at the expense of not being able to handle quoted fields.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader constructs a Reader

func (*Reader) CSV

func (r *Reader) CSV() *Reader

CSV makes Reader read simple (no spacing, no quoting) CSV files.

func (*Reader) Read

func (r *Reader) Read() ([][]byte, error)

Read reads one line of a CSV into bss.

func (*Reader) ReadInto

func (r *Reader) ReadInto(buf *[][]byte) ([][]byte, error)

ReadInto reads the next line into buf without allocating.

func (*Reader) ReadString

func (r *Reader) ReadString() ([]string, error)

ReadString reads one line of CSV, allocating for convenience. Use Read or ReadInto to avoid allocating.

func (*Reader) Skip

func (r *Reader) Skip(n int) *Reader

Skip n rows of input.

func (*Reader) TSV

func (r *Reader) TSV() *Reader

TSV makes Reader read TSV files.

Directories

Path Synopsis
cmd
readcsv command
readus command

Jump to

Keyboard shortcuts

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