1
0
mirror of https://github.com/pbnjay/grate.git synced 2024-12-13 13:58:27 +02:00

fix and export SliceReader. update test

This commit is contained in:
Jeremy Jay 2021-02-05 10:49:40 -05:00
parent 986d4d9116
commit a477a30993
3 changed files with 24 additions and 21 deletions

View File

@ -292,7 +292,7 @@ func (d *doc) getStreamReader(sid uint32, size uint64) io.Reader {
panic("incomplete read")
}
return &sliceReader{data: streamData, offset: 0}
return &SliceReader{Data: streamData}
}
func (d *doc) getMiniStreamReader(sid uint32, size uint64) io.Reader {
@ -341,5 +341,5 @@ func (d *doc) getMiniStreamReader(sid uint32, size uint64) io.Reader {
sid = d.minifat[sid]
}
return &sliceReader{data: streamData, offset: 0}
return &SliceReader{Data: streamData}
}

View File

@ -9,7 +9,7 @@ import (
func TestHeader(t *testing.T) {
d := &doc{}
f, _ := os.Open("test.xls")
f, _ := os.Open("../testdata/test.xls")
err := d.load(f)
if err != nil {
t.Fatal(err)
@ -18,7 +18,7 @@ func TestHeader(t *testing.T) {
func TestHeader2(t *testing.T) {
d := &doc{}
f, _ := os.Open("test2.xls")
f, _ := os.Open("../testdata/test2.xls")
err := d.load(f)
if err != nil {
t.Fatal(err)
@ -27,7 +27,7 @@ func TestHeader2(t *testing.T) {
func TestHeader3(t *testing.T) {
d := &doc{}
f, _ := os.Open("test3.xls")
f, _ := os.Open("../testdata/test3.xls")
err := d.load(f)
if err != nil {
t.Fatal(err)
@ -36,7 +36,7 @@ func TestHeader3(t *testing.T) {
func TestHeader4(t *testing.T) {
d := &doc{}
f, _ := os.Open("test4.xls")
f, _ := os.Open("../testdata/test4.xls")
err := d.load(f)
if err != nil {
t.Fatal(err)

View File

@ -1,25 +1,28 @@
package cfb
import "io"
import (
"io"
)
type sliceReader struct {
data [][]byte
offset uint
type SliceReader struct {
Data [][]byte
Index uint
Offset uint
}
func (s *sliceReader) Read(b []byte) (int, error) {
var err error
if s.offset >= uint(len(s.data)) {
func (s *SliceReader) Read(b []byte) (int, error) {
if s.Index >= uint(len(s.Data)) {
return 0, io.EOF
}
if len(b) < len(s.data[s.offset]) {
return 0, io.ErrShortBuffer
n := copy(b, s.Data[s.Index][s.Offset:])
if n > 0 {
s.Offset += uint(n)
if s.Offset == uint(len(s.Data[s.Index])) {
s.Offset = 0
s.Index++
}
return n, nil
}
n := copy(b, s.data[s.offset])
if n == 0 {
err = io.EOF
}
s.offset++
return n, err
return 0, io.EOF
}