codec/codecutil/byte-scanner.go: renamed Scanner type to ByteScanner

This commit is contained in:
Saxon 2019-05-09 13:51:59 +09:30
parent 0effbae4f6
commit cbbe74b34a
2 changed files with 12 additions and 12 deletions

View File

@ -1,6 +1,6 @@
/* /*
NAME NAME
scanner.go byte-scanner.go
AUTHOR AUTHOR
Dan Kortschak <dan@ausocean.org> Dan Kortschak <dan@ausocean.org>
@ -26,8 +26,8 @@ package bytescan
import "io" import "io"
// Scanner is a byte scanner. // ByteScanner is a byte scanner.
type Scanner struct { type ByteScanner struct {
buf []byte buf []byte
off int off int
@ -35,15 +35,15 @@ type Scanner struct {
r io.Reader r io.Reader
} }
// NewScanner returns a scanner initialised with an io.Reader and a read buffer. // NewByteScanner returns a scanner initialised with an io.Reader and a read buffer.
func NewScanner(r io.Reader, buf []byte) *Scanner { func NewByteScanner(r io.Reader, buf []byte) *ByteScanner {
return &Scanner{r: r, buf: buf[:0]} return &ByteScanner{r: r, buf: buf[:0]}
} }
// ScanUntil scans the scanner's underlying io.Reader until a delim byte // ScanUntil scans the scanner's underlying io.Reader until a delim byte
// has been read, appending all read bytes to dst. The resulting appended data, // has been read, appending all read bytes to dst. The resulting appended data,
// the last read byte and whether the last read byte was the delimiter. // the last read byte and whether the last read byte was the delimiter.
func (c *Scanner) ScanUntil(dst []byte, delim byte) (res []byte, b byte, err error) { func (c *ByteScanner) ScanUntil(dst []byte, delim byte) (res []byte, b byte, err error) {
outer: outer:
for { for {
var i int var i int
@ -65,7 +65,7 @@ outer:
} }
// ReadByte is an unexported ReadByte. // ReadByte is an unexported ReadByte.
func (c *Scanner) ReadByte() (byte, error) { func (c *ByteScanner) ReadByte() (byte, error) {
if c.off >= len(c.buf) { if c.off >= len(c.buf) {
err := c.reload() err := c.reload()
if err != nil { if err != nil {
@ -78,7 +78,7 @@ func (c *Scanner) ReadByte() (byte, error) {
} }
// reload re-fills the scanner's buffer. // reload re-fills the scanner's buffer.
func (c *Scanner) reload() error { func (c *ByteScanner) reload() error {
n, err := c.r.Read(c.buf[:cap(c.buf)]) n, err := c.r.Read(c.buf[:cap(c.buf)])
c.buf = c.buf[:n] c.buf = c.buf[:n]
if err != nil { if err != nil {

View File

@ -1,6 +1,6 @@
/* /*
NAME NAME
scanner_test.go byte-scanner_test.go
DESCRIPTION DESCRIPTION
See Readme.md See Readme.md
@ -46,7 +46,7 @@ func TestScannerReadByte(t *testing.T) {
data := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.") data := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
for _, size := range []int{1, 2, 8, 1 << 10} { for _, size := range []int{1, 2, 8, 1 << 10} {
r := NewScanner(bytes.NewReader(data), make([]byte, size)) r := NewByteScanner(bytes.NewReader(data), make([]byte, size))
var got []byte var got []byte
for { for {
b, err := r.ReadByte() b, err := r.ReadByte()
@ -65,7 +65,7 @@ func TestScannerScanUntilZero(t *testing.T) {
data := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit,\x00 sed do eiusmod tempor incididunt ut \x00labore et dolore magna aliqua.") data := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit,\x00 sed do eiusmod tempor incididunt ut \x00labore et dolore magna aliqua.")
for _, size := range []int{1, 2, 8, 1 << 10} { for _, size := range []int{1, 2, 8, 1 << 10} {
r := NewScanner(bytes.NewReader(data), make([]byte, size)) r := NewByteScanner(bytes.NewReader(data), make([]byte, size))
var got [][]byte var got [][]byte
for { for {
buf, _, err := r.ScanUntil(nil, 0x0) buf, _, err := r.ScanUntil(nil, 0x0)