Skip to content

Commit

Permalink
Breaking changes to make govet pass
Browse files Browse the repository at this point in the history
* Peek -> PeekRunes
* ReadRune(int) rune, error -> ReadRune(int) rune, int, error
  • Loading branch information
pelletier committed Feb 27, 2017
1 parent df1e16f commit c37440a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
25 changes: 16 additions & 9 deletions buffruneio.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ func NewReader(rd io.Reader) *Reader {
}
}

type runeWithSize struct {
r rune
size int
}

func (rd *Reader) feedBuffer() error {
r, _, err := rd.input.ReadRune()
r, size, err := rd.input.ReadRune()

if err != nil {
if err != io.EOF {
Expand All @@ -41,25 +46,27 @@ func (rd *Reader) feedBuffer() error {
r = EOF
}

rd.buffer.PushBack(r)
newRuneWithSize := runeWithSize{r, size}

rd.buffer.PushBack(newRuneWithSize)
if rd.current == nil {
rd.current = rd.buffer.Back()
}
return nil
}

// ReadRune reads the next rune from buffer, or from the underlying reader if needed.
func (rd *Reader) ReadRune() (rune, error) {
func (rd *Reader) ReadRune() (rune, int, error) {
if rd.current == rd.buffer.Back() || rd.current == nil {
err := rd.feedBuffer()
if err != nil {
return EOF, err
return EOF, 0, err
}
}

r := rd.current.Value
runeWithSize := rd.current.Value.(runeWithSize)
rd.current = rd.current.Next()
return r.(rune), nil
return runeWithSize.r, runeWithSize.size, nil
}

// UnreadRune pushes back the previously read rune in the buffer, extending it if needed.
Expand All @@ -84,9 +91,9 @@ func (rd *Reader) Forget() {
}
}

// Peek returns at most the next n runes, reading from the uderlying source if
// PeekRune returns at most the next n runes, reading from the uderlying source if
// needed. Does not move the current index. It includes EOF if reached.
func (rd *Reader) Peek(n int) []rune {
func (rd *Reader) PeekRunes(n int) []rune {
res := make([]rune, 0, n)
cursor := rd.current
for i := 0; i < n; i++ {
Expand All @@ -98,7 +105,7 @@ func (rd *Reader) Peek(n int) []rune {
cursor = rd.buffer.Back()
}
if cursor != nil {
r := cursor.Value.(rune)
r := cursor.Value.(runeWithSize).r
res = append(res, r)
if r == EOF {
return res
Expand Down
33 changes: 21 additions & 12 deletions buffruneio_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package buffruneio

import (
"runtime/debug"
"strings"
"testing"
)

func assertNoError(t *testing.T, err error) {
if err != nil {
t.Log("unexpected error", err)
debug.PrintStack()
t.FailNow()
}
}

func assumeRunesArray(t *testing.T, expected []rune, got []rune) {
if len(expected) != len(got) {
t.Fatal("expected", len(expected), "runes, but got", len(got))
Expand All @@ -17,15 +26,15 @@ func assumeRunesArray(t *testing.T, expected []rune, got []rune) {
}

func assumeRune(t *testing.T, rd *Reader, r rune) {
gotRune, err := rd.ReadRune()
if err != nil {
t.Fatal("unexpected error", err)
}
gotRune, size, err := rd.ReadRune()
assertNoError(t, err)
if gotRune != r {
t.Fatal("got", string(gotRune),
"(", []byte(string(gotRune)), ")",
"expected", string(r),
"(", []byte(string(r)), ")")
t.Fatal("got size", size,
"expected", len([]byte(string(r))))
}
}

Expand Down Expand Up @@ -55,7 +64,7 @@ func TestUnread(t *testing.T) {

assumeRune(t, rd, 'a')
assumeRune(t, rd, 'b')
rd.UnreadRune()
assertNoError(t, rd.UnreadRune())
assumeRune(t, rd, 'b')
assumeRune(t, rd, EOF)
}
Expand All @@ -64,10 +73,10 @@ func TestUnreadEOF(t *testing.T) {
s := ""
rd := NewReader(strings.NewReader(s))

rd.UnreadRune()
_ = rd.UnreadRune()
assumeRune(t, rd, EOF)
assumeRune(t, rd, EOF)
rd.UnreadRune()
assertNoError(t, rd.UnreadRune())
assumeRune(t, rd, EOF)
}

Expand Down Expand Up @@ -98,7 +107,7 @@ func TestPeekEmpty(t *testing.T) {
s := ""
rd := NewReader(strings.NewReader(s))

runes := rd.Peek(1)
runes := rd.PeekRunes(1)
if len(runes) != 1 {
t.Fatal("incorrect number of runes", len(runes))
}
Expand All @@ -111,14 +120,14 @@ func TestPeek(t *testing.T) {
s := "a"
rd := NewReader(strings.NewReader(s))

runes := rd.Peek(1)
runes := rd.PeekRunes(1)
assumeRunesArray(t, []rune{'a'}, runes)

runes = rd.Peek(1)
runes = rd.PeekRunes(1)
assumeRunesArray(t, []rune{'a'}, runes)

assumeRune(t, rd, 'a')
runes = rd.Peek(1)
runes = rd.PeekRunes(1)
assumeRunesArray(t, []rune{EOF}, runes)

assumeRune(t, rd, EOF)
Expand All @@ -128,7 +137,7 @@ func TestPeekLarge(t *testing.T) {
s := "abcdefg"
rd := NewReader(strings.NewReader(s))

runes := rd.Peek(100)
runes := rd.PeekRunes(100)
if len(runes) != len(s)+1 {
t.Fatal("incorrect number of runes", len(runes))
}
Expand Down

0 comments on commit c37440a

Please sign in to comment.